Commit 1046ead0cc

Andrew Kelley <andrew@ziglang.org>
2022-03-08 21:01:58
LLVM: no longer store args into alloca instructions
Previously, we did this so that we could insert a debug variable declaration intrinsic on the alloca. But there is a dbg.value intrinsic for declaring variables that are values.
1 parent 7cea8f0
Changed files (4)
src/codegen/llvm/bindings.zig
@@ -1700,6 +1700,15 @@ pub const DIBuilder = opaque {
         debug_loc: *DILocation,
         insert_before_instr: *const Value,
     ) *const Value;
+
+    pub const insertDbgValueIntrinsicAtEnd = ZigLLVMInsertDbgValueIntrinsicAtEnd;
+    extern fn ZigLLVMInsertDbgValueIntrinsicAtEnd(
+        dib: *DIBuilder,
+        val: *const Value,
+        var_info: *DILocalVariable,
+        debug_loc: *DILocation,
+        basic_block_ref: *const BasicBlock,
+    ) *const Value;
 };
 
 pub const DIFlags = opaque {
src/codegen/llvm.zig
@@ -5117,16 +5117,6 @@ pub const FuncGen = struct {
         self.arg_index += 1;
 
         const inst_ty = self.air.typeOfIndex(inst);
-        const result: struct { ptr: *const llvm.Value, operand: *const llvm.Value } = r: {
-            if (isByRef(inst_ty)) {
-                break :r .{ .ptr = arg_val, .operand = arg_val };
-            } else {
-                const ptr_val = self.buildAlloca(try self.dg.llvmType(inst_ty));
-                _ = self.builder.buildStore(arg_val, ptr_val);
-                break :r .{ .ptr = ptr_val, .operand = arg_val };
-            }
-        };
-
         if (self.dg.object.di_builder) |dib| {
             const src_index = self.getSrcArgIndex(self.arg_index - 1);
             const func = self.dg.decl.getFunction().?;
@@ -5145,10 +5135,14 @@ pub const FuncGen = struct {
 
             const debug_loc = llvm.getDebugLoc(lbrace_line, lbrace_col, self.di_scope.?);
             const insert_block = self.builder.getInsertBlock();
-            _ = dib.insertDeclareAtEnd(result.ptr, di_local_var, debug_loc, insert_block);
+            if (isByRef(inst_ty)) {
+                _ = dib.insertDeclareAtEnd(arg_val, di_local_var, debug_loc, insert_block);
+            } else {
+                _ = dib.insertDbgValueIntrinsicAtEnd(arg_val, di_local_var, debug_loc, insert_block);
+            }
         }
 
-        return result.operand;
+        return arg_val;
     }
 
     fn getSrcArgIndex(self: *FuncGen, runtime_index: u32) u32 {
src/zig_llvm.cpp
@@ -942,6 +942,19 @@ LLVMValueRef ZigLLVMInsertDeclareAtEnd(ZigLLVMDIBuilder *dibuilder, LLVMValueRef
     return wrap(result);
 }
 
+LLVMValueRef ZigLLVMInsertDbgValueIntrinsicAtEnd(ZigLLVMDIBuilder *dib, LLVMValueRef val,
+        ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc,
+        LLVMBasicBlockRef basic_block_ref)
+{
+    Instruction *result = reinterpret_cast<DIBuilder*>(dib)->insertDbgValueIntrinsic(
+            unwrap(val),
+            reinterpret_cast<DILocalVariable *>(var_info),
+            reinterpret_cast<DIBuilder*>(dib)->createExpression(),
+            reinterpret_cast<DILocation*>(debug_loc),
+            static_cast<BasicBlock*>(unwrap(basic_block_ref)));
+    return wrap(result);
+}
+
 LLVMValueRef ZigLLVMInsertDeclare(ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage,
         ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc, LLVMValueRef insert_before_instr)
 {
src/zig_llvm.h
@@ -273,13 +273,20 @@ ZIG_EXTERN_C void ZigLLVMFnSetSubprogram(LLVMValueRef fn, struct ZigLLVMDISubpro
 
 ZIG_EXTERN_C void ZigLLVMDIBuilderFinalize(struct ZigLLVMDIBuilder *dibuilder);
 
-ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDeclareAtEnd(struct ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage,
-        struct ZigLLVMDILocalVariable *var_info, struct ZigLLVMDILocation *debug_loc,
-        LLVMBasicBlockRef basic_block_ref);
+ZIG_EXTERN_C struct ZigLLVMDILocation *ZigLLVMGetDebugLoc(unsigned line, unsigned col,
+        struct ZigLLVMDIScope *scope);
+
+ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDeclareAtEnd(struct ZigLLVMDIBuilder *dib,
+        LLVMValueRef storage, struct ZigLLVMDILocalVariable *var_info,
+        struct ZigLLVMDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref);
+
+ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDeclare(struct ZigLLVMDIBuilder *dib,
+        LLVMValueRef storage, struct ZigLLVMDILocalVariable *var_info,
+        struct ZigLLVMDILocation *debug_loc, LLVMValueRef insert_before_instr);
 
-ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDeclare(struct ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage,
-        struct ZigLLVMDILocalVariable *var_info, struct ZigLLVMDILocation *debug_loc, LLVMValueRef insert_before_instr);
-ZIG_EXTERN_C struct ZigLLVMDILocation *ZigLLVMGetDebugLoc(unsigned line, unsigned col, struct ZigLLVMDIScope *scope);
+ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDbgValueIntrinsicAtEnd(struct ZigLLVMDIBuilder *dib,
+        LLVMValueRef val, struct ZigLLVMDILocalVariable *var_info,
+        struct ZigLLVMDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref);
 
 ZIG_EXTERN_C void ZigLLVMSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state);
 ZIG_EXTERN_C void ZigLLVMSetTailCall(LLVMValueRef Call);