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 {...@@ -687,9 +687,6 @@ pub const DeclGen = struct {
687 const target = dg.module.getTarget();687 const target = dg.module.getTarget();
688 const sret = firstParamSRet(fn_info, target);688 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
693 const fn_type = try dg.llvmType(zig_fn_type);690 const fn_type = try dg.llvmType(zig_fn_type);
694691
695 const fqn = try decl.getFullyQualifiedName(dg.gpa);692 const fqn = try decl.getFullyQualifiedName(dg.gpa);
...@@ -708,6 +705,8 @@ pub const DeclGen = struct {...@@ -708,6 +705,8 @@ pub const DeclGen = struct {
708 if (sret) {705 if (sret) {
709 dg.addArgAttr(llvm_fn, 0, "nonnull"); // Sret pointers must not be address 0706 dg.addArgAttr(llvm_fn, 0, "nonnull"); // Sret pointers must not be address 0
710 dg.addArgAttr(llvm_fn, 0, "noalias");707 dg.addArgAttr(llvm_fn, 0, "noalias");
708
709 const raw_llvm_ret_ty = try dg.llvmType(fn_info.return_type);
711 llvm_fn.addSretAttr(0, raw_llvm_ret_ty);710 llvm_fn.addSretAttr(0, raw_llvm_ret_ty);
712 }711 }
713712
...@@ -737,7 +736,7 @@ pub const DeclGen = struct {...@@ -737,7 +736,7 @@ pub const DeclGen = struct {
737 // Function attributes that are independent of analysis results of the function body.736 // Function attributes that are independent of analysis results of the function body.
738 dg.addCommonFnAttributes(llvm_fn);737 dg.addCommonFnAttributes(llvm_fn);
739738
740 if (return_type.isNoReturn()) {739 if (fn_info.return_type.isNoReturn()) {
741 dg.addFnAttr(llvm_fn, "noreturn");740 dg.addFnAttr(llvm_fn, "noreturn");
742 }741 }
743742
...@@ -829,6 +828,7 @@ pub const DeclGen = struct {...@@ -829,6 +828,7 @@ pub const DeclGen = struct {
829 .Void, .NoReturn => return dg.context.voidType(),828 .Void, .NoReturn => return dg.context.voidType(),
830 .Int => {829 .Int => {
831 const info = t.intInfo(target);830 const info = t.intInfo(target);
831 assert(info.bits != 0);
832 return dg.context.intType(info.bits);832 return dg.context.intType(info.bits);
833 },833 },
834 .Enum => {834 .Enum => {
...@@ -1147,17 +1147,17 @@ pub const DeclGen = struct {...@@ -1147,17 +1147,17 @@ pub const DeclGen = struct {
1147 const fn_info = t.fnInfo();1147 const fn_info = t.fnInfo();
1148 const sret = firstParamSRet(fn_info, target);1148 const sret = firstParamSRet(fn_info, target);
1149 const return_type = fn_info.return_type;1149 const return_type = fn_info.return_type;
1150 const raw_llvm_ret_ty = try dg.llvmType(return_type);1150 const llvm_sret_ty = if (return_type.hasRuntimeBits())
1151 const llvm_ret_ty = if (!return_type.hasRuntimeBits() or sret)1151 try dg.llvmType(return_type)
1152 dg.context.voidType()
1153 else1152 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
1156 var llvm_params = std.ArrayList(*const llvm.Type).init(dg.gpa);1156 var llvm_params = std.ArrayList(*const llvm.Type).init(dg.gpa);
1157 defer llvm_params.deinit();1157 defer llvm_params.deinit();
11581158
1159 if (sret) {1159 if (sret) {
1160 try llvm_params.append(raw_llvm_ret_ty.pointerType(0));1160 try llvm_params.append(llvm_sret_ty.pointerType(0));
1161 }1161 }
11621162
1163 for (fn_info.param_types) |param_ty| {1163 for (fn_info.param_types) |param_ty| {
...@@ -1210,6 +1210,7 @@ pub const DeclGen = struct {...@@ -1210,6 +1210,7 @@ pub const DeclGen = struct {
1210 const bigint = tv.val.toBigInt(&bigint_space);1210 const bigint = tv.val.toBigInt(&bigint_space);
1211 const target = dg.module.getTarget();1211 const target = dg.module.getTarget();
1212 const int_info = tv.ty.intInfo(target);1212 const int_info = tv.ty.intInfo(target);
1213 assert(int_info.bits != 0);
1213 const llvm_type = dg.context.intType(int_info.bits);1214 const llvm_type = dg.context.intType(int_info.bits);
12141215
1215 const unsigned_val = v: {1216 const unsigned_val = v: {
...@@ -2241,7 +2242,6 @@ pub const FuncGen = struct {...@@ -2241,7 +2242,6 @@ pub const FuncGen = struct {
2241 };2242 };
2242 const fn_info = zig_fn_ty.fnInfo();2243 const fn_info = zig_fn_ty.fnInfo();
2243 const return_type = fn_info.return_type;2244 const return_type = fn_info.return_type;
2244 const llvm_ret_ty = try self.dg.llvmType(return_type);
2245 const llvm_fn = try self.resolveInst(pl_op.operand);2245 const llvm_fn = try self.resolveInst(pl_op.operand);
2246 const target = self.dg.module.getTarget();2246 const target = self.dg.module.getTarget();
2247 const sret = firstParamSRet(fn_info, target);2247 const sret = firstParamSRet(fn_info, target);
...@@ -2250,6 +2250,7 @@ pub const FuncGen = struct {...@@ -2250,6 +2250,7 @@ pub const FuncGen = struct {
2250 defer llvm_args.deinit();2250 defer llvm_args.deinit();
22512251
2252 const ret_ptr = if (!sret) null else blk: {2252 const ret_ptr = if (!sret) null else blk: {
2253 const llvm_ret_ty = try self.dg.llvmType(return_type);
2253 const ret_ptr = self.buildAlloca(llvm_ret_ty);2254 const ret_ptr = self.buildAlloca(llvm_ret_ty);
2254 ret_ptr.setAlignment(return_type.abiAlignment(target));2255 ret_ptr.setAlignment(return_type.abiAlignment(target));
2255 try llvm_args.append(ret_ptr);2256 try llvm_args.append(ret_ptr);
...@@ -2284,6 +2285,7 @@ pub const FuncGen = struct {...@@ -2284,6 +2285,7 @@ pub const FuncGen = struct {
2284 } else if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBits()) {2285 } else if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBits()) {
2285 return null;2286 return null;
2286 } else if (sret) {2287 } else if (sret) {
2288 const llvm_ret_ty = try self.dg.llvmType(return_type);
2287 call.setCallSret(llvm_ret_ty);2289 call.setCallSret(llvm_ret_ty);
2288 return ret_ptr;2290 return ret_ptr;
2289 } else {2291 } else {