authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-11 00:03:53-07:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-12 11:18:23+01:00
log38236533f1d031d31c6d10cbe7d0f8c59a9e3520
tree33a3d20341ff288678b03a96c380f9c7707518cc
parentba31a9469f48065c9ebf935160faffd0f6c9bfdc

LLVM backend: avoid creating invalid LLVM types

Fixes assertions from creating i0 types which are not allowed in LLVM.

1 files changed, 12 insertions(+), 10 deletions(-)

src/codegen/llvm.zig+12-10
......@@ -687,9 +687,6 @@ pub const DeclGen = struct {
687687 const target = dg.module.getTarget();
688688 const sret = firstParamSRet(fn_info, target);
689689
690 const return_type = fn_info.return_type;
691 const raw_llvm_ret_ty = try dg.llvmType(return_type);
692
693690 const fn_type = try dg.llvmType(zig_fn_type);
694691
695692 const fqn = try decl.getFullyQualifiedName(dg.gpa);
......@@ -708,6 +705,8 @@ pub const DeclGen = struct {
708705 if (sret) {
709706 dg.addArgAttr(llvm_fn, 0, "nonnull"); // Sret pointers must not be address 0
710707 dg.addArgAttr(llvm_fn, 0, "noalias");
708
709 const raw_llvm_ret_ty = try dg.llvmType(fn_info.return_type);
711710 llvm_fn.addSretAttr(0, raw_llvm_ret_ty);
712711 }
713712
......@@ -737,7 +736,7 @@ pub const DeclGen = struct {
737736 // Function attributes that are independent of analysis results of the function body.
738737 dg.addCommonFnAttributes(llvm_fn);
739738
740 if (return_type.isNoReturn()) {
739 if (fn_info.return_type.isNoReturn()) {
741740 dg.addFnAttr(llvm_fn, "noreturn");
742741 }
743742
......@@ -829,6 +828,7 @@ pub const DeclGen = struct {
829828 .Void, .NoReturn => return dg.context.voidType(),
830829 .Int => {
831830 const info = t.intInfo(target);
831 assert(info.bits != 0);
832832 return dg.context.intType(info.bits);
833833 },
834834 .Enum => {
......@@ -1147,17 +1147,17 @@ pub const DeclGen = struct {
11471147 const fn_info = t.fnInfo();
11481148 const sret = firstParamSRet(fn_info, target);
11491149 const return_type = fn_info.return_type;
1150 const raw_llvm_ret_ty = try dg.llvmType(return_type);
1151 const llvm_ret_ty = if (!return_type.hasRuntimeBits() or sret)
1152 dg.context.voidType()
1150 const llvm_sret_ty = if (return_type.hasRuntimeBits())
1151 try dg.llvmType(return_type)
11531152 else
1154 raw_llvm_ret_ty;
1153 dg.context.voidType();
1154 const llvm_ret_ty = if (sret) dg.context.voidType() else llvm_sret_ty;
11551155
11561156 var llvm_params = std.ArrayList(*const llvm.Type).init(dg.gpa);
11571157 defer llvm_params.deinit();
11581158
11591159 if (sret) {
1160 try llvm_params.append(raw_llvm_ret_ty.pointerType(0));
1160 try llvm_params.append(llvm_sret_ty.pointerType(0));
11611161 }
11621162
11631163 for (fn_info.param_types) |param_ty| {
......@@ -1210,6 +1210,7 @@ pub const DeclGen = struct {
12101210 const bigint = tv.val.toBigInt(&bigint_space);
12111211 const target = dg.module.getTarget();
12121212 const int_info = tv.ty.intInfo(target);
1213 assert(int_info.bits != 0);
12131214 const llvm_type = dg.context.intType(int_info.bits);
12141215
12151216 const unsigned_val = v: {
......@@ -2241,7 +2242,6 @@ pub const FuncGen = struct {
22412242 };
22422243 const fn_info = zig_fn_ty.fnInfo();
22432244 const return_type = fn_info.return_type;
2244 const llvm_ret_ty = try self.dg.llvmType(return_type);
22452245 const llvm_fn = try self.resolveInst(pl_op.operand);
22462246 const target = self.dg.module.getTarget();
22472247 const sret = firstParamSRet(fn_info, target);
......@@ -2250,6 +2250,7 @@ pub const FuncGen = struct {
22502250 defer llvm_args.deinit();
22512251
22522252 const ret_ptr = if (!sret) null else blk: {
2253 const llvm_ret_ty = try self.dg.llvmType(return_type);
22532254 const ret_ptr = self.buildAlloca(llvm_ret_ty);
22542255 ret_ptr.setAlignment(return_type.abiAlignment(target));
22552256 try llvm_args.append(ret_ptr);
......@@ -2284,6 +2285,7 @@ pub const FuncGen = struct {
22842285 } else if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBits()) {
22852286 return null;
22862287 } else if (sret) {
2288 const llvm_ret_ty = try self.dg.llvmType(return_type);
22872289 call.setCallSret(llvm_ret_ty);
22882290 return ret_ptr;
22892291 } else {