From 1046ead0cc409287b88554a90c2f80718e1af46d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 8 Mar 2022 13:01:58 -0700 Subject: [PATCH] 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. --- src/codegen/llvm.zig | 18 ++++++------------ src/codegen/llvm/bindings.zig | 9 +++++++++ src/zig_llvm.cpp | 13 +++++++++++++ src/zig_llvm.h | 19 +++++++++++++------ 4 files changed, 41 insertions(+), 18 deletions(-) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index ee1cdd6141..2466eea8a9 100644 --- a/src/codegen/llvm.zig +++ b/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 { diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig index f20fe86a38..f590e46c23 100644 --- a/src/codegen/llvm/bindings.zig +++ b/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 { diff --git a/src/zig_llvm.cpp b/src/zig_llvm.cpp index aa07a7fed1..21e83319d9 100644 --- a/src/zig_llvm.cpp +++ b/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(dib)->insertDbgValueIntrinsic( + unwrap(val), + reinterpret_cast(var_info), + reinterpret_cast(dib)->createExpression(), + reinterpret_cast(debug_loc), + static_cast(unwrap(basic_block_ref))); + return wrap(result); +} + LLVMValueRef ZigLLVMInsertDeclare(ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage, ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc, LLVMValueRef insert_before_instr) { diff --git a/src/zig_llvm.h b/src/zig_llvm.h index 2b8156d51d..b19ff1f947 100644 --- a/src/zig_llvm.h +++ b/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 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 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 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);