From fb4a5ccdeeddf56a0849b4168fe7a9525f36b107 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 30 Nov 2022 19:31:01 +0200 Subject: [PATCH] llvm: make debuggers actually usable `@llvm.dbg.value` is absolutely useless, adding a temporary alloca to store the constant in will make it actually show up in debuggers. The effect on performance should be minimal since there is only one store and it the change is not applied to ReleaseSafe builds. ```zig fn foo(a: u32, b: []const u8, c: bool, d: enum { yes, no }) void { _ = a; _ = b; _ = c; _ = d; } ``` before: ``` Breakpoint 1, a.foo (a=, b=..., c=, d=) at a.zig:18 18 _ = d; ``` after: ``` Breakpoint 1, a.foo (a=1, b=..., c=false, d=yes) at a.zig:15 15 _ = a; _ = b; _ = c; _ = d; (gdb) p b $1 = {ptr = 0x20854f "bar", len = 3} ``` --- src/codegen/llvm.zig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 3a7d82e24f..6215a11e6f 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -6088,6 +6088,12 @@ pub const FuncGen = struct { const insert_block = self.builder.getInsertBlock(); if (isByRef(operand_ty)) { _ = dib.insertDeclareAtEnd(operand, di_local_var, debug_loc, insert_block); + } else if (self.dg.module.comp.bin_file.options.optimize_mode == .Debug) { + const alignment = operand_ty.abiAlignment(self.dg.module.getTarget()); + const alloca = self.buildAlloca(operand.typeOf(), alignment); + const store_inst = self.builder.buildStore(operand, alloca); + store_inst.setAlignment(alignment); + _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block); } else { _ = dib.insertDbgValueIntrinsicAtEnd(operand, di_local_var, debug_loc, insert_block); } @@ -8026,6 +8032,12 @@ pub const FuncGen = struct { const insert_block = self.builder.getInsertBlock(); if (isByRef(inst_ty)) { _ = dib.insertDeclareAtEnd(arg_val, di_local_var, debug_loc, insert_block); + } else if (self.dg.module.comp.bin_file.options.optimize_mode == .Debug) { + const alignment = inst_ty.abiAlignment(self.dg.module.getTarget()); + const alloca = self.buildAlloca(arg_val.typeOf(), alignment); + const store_inst = self.builder.buildStore(arg_val, alloca); + store_inst.setAlignment(alignment); + _ = dib.insertDeclareAtEnd(alloca, di_local_var, debug_loc, insert_block); } else { _ = dib.insertDbgValueIntrinsicAtEnd(arg_val, di_local_var, debug_loc, insert_block); }