LLVM: fix invalid IR on @returnAddress of wasm/bpf

see #11946
This commit is contained in:
Andrew Kelley 2022-06-27 17:12:45 -07:00
parent 8d8a5f9733
commit 3c1daf951c
4 changed files with 20 additions and 5 deletions

View File

@ -433,7 +433,7 @@ pub const IPv6 = extern struct {
'0'...'9' => fmt.parseInt(u32, scope_id_slice, 10),
else => resolveScopeId(scope_id_slice) catch |err| switch (err) {
error.InterfaceNotFound => return error.InterfaceNotFound,
else => err,
else => |e| return e,
},
} catch return error.UnknownScopeId;

View File

@ -7212,11 +7212,17 @@ pub const FuncGen = struct {
fn airRetAddr(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
if (self.liveness.isUnused(inst)) return null;
const llvm_usize = try self.dg.lowerType(Type.usize);
const target = self.dg.module.getTarget();
if (!target_util.supportsReturnAddress(target)) {
// https://github.com/ziglang/zig/issues/11946
return llvm_usize.constNull();
}
const llvm_i32 = self.context.intType(32);
const llvm_fn = self.getIntrinsic("llvm.returnaddress", &.{});
const params = [_]*const llvm.Value{llvm_i32.constNull()};
const ptr_val = self.builder.buildCall(llvm_fn, &params, params.len, .Fast, .Auto, "");
const llvm_usize = try self.dg.lowerType(Type.usize);
return self.builder.buildPtrToInt(ptr_val, llvm_usize, "");
}
@ -8021,7 +8027,6 @@ pub const FuncGen = struct {
assert(union_obj.haveFieldTypes());
const field = union_obj.fields.values()[extra.field_index];
const field_llvm_ty = try self.dg.lowerType(field.ty);
const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty);
const field_size = field.ty.abiSize(target);
const field_align = field.normalAlignment(target);
@ -8044,6 +8049,7 @@ pub const FuncGen = struct {
const fields: [1]*const llvm.Type = .{payload};
break :t self.context.structType(&fields, fields.len, .False);
}
const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty);
var fields: [3]*const llvm.Type = undefined;
var fields_len: c_uint = 2;
if (layout.tag_align >= layout.payload_align) {
@ -8100,6 +8106,7 @@ pub const FuncGen = struct {
index_type.constInt(@boolToInt(layout.tag_align < layout.payload_align), .False),
};
const field_ptr = self.builder.buildInBoundsGEP(casted_ptr, &indices, indices.len, "");
const tag_llvm_ty = try self.dg.lowerType(union_obj.tag_ty);
const llvm_tag = tag_llvm_ty.constInt(extra.field_index, .False);
const store_inst = self.builder.buildStore(llvm_tag, field_ptr);
store_inst.setAlignment(union_obj.tag_ty.abiAlignment(target));

View File

@ -6764,8 +6764,8 @@ static LLVMValueRef ir_render_return_address(CodeGen *g, Stage1Air *executable,
Stage1AirInstReturnAddress *instruction)
{
if ((target_is_wasm(g->zig_target) && g->zig_target->os != OsEmscripten) || target_is_bpf(g->zig_target)) {
// I got this error from LLVM 10:
// "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address"
// LLVM 13 reports "Non-Emscripten WebAssembly hasn't implemented __builtin_return_address"
// https://github.com/ziglang/zig/issues/11946
return LLVMConstNull(get_llvm_type(g, instruction->base.value->type));
}

View File

@ -283,6 +283,14 @@ pub fn supportsStackProbing(target: std.Target) bool {
(target.cpu.arch == .i386 or target.cpu.arch == .x86_64);
}
pub fn supportsReturnAddress(target: std.Target) bool {
return switch (target.cpu.arch) {
.wasm32, .wasm64 => target.os.tag == .emscripten,
.bpfel, .bpfeb => false,
else => true,
};
}
pub fn osToLLVM(os_tag: std.Target.Os.Tag) llvm.OSType {
return switch (os_tag) {
.freestanding, .other, .opencl, .glsl450, .vulkan, .plan9 => .UnknownOS,