commit 8a43d67c3bf878fa32ef4876897a32969a4b5d24 (tree)
parent c3663f2617fd317f458bfa013ea94efb7dbcfee5
Author: Andrew Kelley <andrew@ziglang.org>
Date: Wed, 16 Mar 2022 00:08:44 -0700
LLVM: fix LLVM assertion when slicing
Diffstat:
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
@@ -4761,7 +4761,15 @@ pub const FuncGen = struct {
const inst_ty = self.air.typeOfIndex(inst);
const llvm_slice_ty = try self.dg.llvmType(inst_ty);
- const partial = self.builder.buildInsertValue(llvm_slice_ty.getUndef(), ptr, 0, "");
+ // In case of slicing a global, the result type looks something like `{ i8*, i64 }`
+ // but `ptr` is pointing to the global directly. If it's an array, we would want to
+ // do GEP(0,0), or we can just bitcast it to be correct, like we do here.
+ // This prevents an assertion failure.
+ var buf: Type.SlicePtrFieldTypeBuffer = undefined;
+ const ptr_ty = inst_ty.slicePtrFieldType(&buf);
+ const ptr_llvm_ty = try self.dg.llvmType(ptr_ty);
+ const casted_ptr = self.builder.buildBitCast(ptr, ptr_llvm_ty, "");
+ const partial = self.builder.buildInsertValue(llvm_slice_ty.getUndef(), casted_ptr, 0, "");
return self.builder.buildInsertValue(partial, len, 1, "");
}