Skip to content

Commit 3aae4bd

Browse files
authored
Emit DW_OP_lit0/1 for constant boolean values (#155539)
Backends like NVPTX use -1 to indicate `true` and 0 to indicate `false` for boolean values. Machine instruction `#DBG_VALUE` also uses -1 to indicate a `true` boolean constant. However, during the DWARF generation, booleans are treated as unsigned variables, and the debug_loc expression, like `DW_OP_lit0; DW_OP_not` is emitted for the `true` value. This leads to the debugger printing `255` instead of `true` for constant boolean variables. This change emits `DW_OP_lit1` instead of `DW_OP_lot0; DW_OP_not`.
1 parent fe6b611 commit 3aae4bd

File tree

4 files changed

+64
-2
lines changed

4 files changed

+64
-2
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,8 +3111,10 @@ void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
31113111
&AP](const DbgValueLocEntry &Entry,
31123112
DIExpressionCursor &Cursor) -> bool {
31133113
if (Entry.isInt()) {
3114-
if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
3115-
BT->getEncoding() == dwarf::DW_ATE_signed_char))
3114+
if (BT && (BT->getEncoding() == dwarf::DW_ATE_boolean))
3115+
DwarfExpr.addBooleanConstant(Entry.getInt());
3116+
else if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
3117+
BT->getEncoding() == dwarf::DW_ATE_signed_char))
31163118
DwarfExpr.addSignedConstant(Entry.getInt());
31173119
else
31183120
DwarfExpr.addUnsignedConstant(Entry.getInt());

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ void DwarfExpression::addStackValue() {
194194
emitOp(dwarf::DW_OP_stack_value);
195195
}
196196

197+
void DwarfExpression::addBooleanConstant(int64_t Value) {
198+
assert(isImplicitLocation() || isUnknownLocation());
199+
LocationKind = Implicit;
200+
if (Value == 0)
201+
emitOp(dwarf::DW_OP_lit0);
202+
else
203+
emitOp(dwarf::DW_OP_lit1);
204+
}
205+
197206
void DwarfExpression::addSignedConstant(int64_t Value) {
198207
assert(isImplicitLocation() || isUnknownLocation());
199208
LocationKind = Implicit;

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ class DwarfExpression {
229229
/// This needs to be called last to commit any pending changes.
230230
void finalize();
231231

232+
/// Emit a boolean constant.
233+
void addBooleanConstant(int64_t Value);
234+
232235
/// Emit a signed constant.
233236
void addSignedConstant(int64_t Value);
234237

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; REQUIRES: object-emission
2+
; RUN: %llc_dwarf %s -O3 -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
3+
4+
; CHECK: {{.*}}DW_TAG_variable
5+
; CHECK: {{.*}} DW_OP_lit1
6+
; CHECK-NOT: {{.*}} DW_OP_lit0, DW_OP_not
7+
; CHECK: {{.*}} DW_OP_lit0
8+
; CHECK: {{.*}} DW_AT_name ("arg")
9+
10+
define void @foo(i8 %"arg.arg") !dbg !5
11+
{
12+
entry:
13+
%".4" = alloca i1
14+
%".5" = icmp eq i8 %"arg.arg", 0
15+
%arg = alloca i1
16+
br i1 %".5", label %"entry.if", label %"entry.else"
17+
entry.if:
18+
store i1 false, i1* %arg
19+
call void @"llvm.dbg.value"(metadata i1 false , metadata !9, metadata !10), !dbg !6
20+
br label %"entry.endif"
21+
entry.else:
22+
store i1 true, i1* %arg
23+
call void @"llvm.dbg.value"(metadata i1 true , metadata !9, metadata !10), !dbg !7
24+
br label %"entry.endif"
25+
entry.endif:
26+
%".11" = load i1, i1* %arg
27+
store i1 %".11", i1* %".4", !dbg !8
28+
call void @"llvm.dbg.value"(metadata i1 %".11" , metadata !9, metadata !10), !dbg !8
29+
ret void, !dbg !8
30+
}
31+
32+
declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
33+
34+
!llvm.dbg.cu = !{ !2 }
35+
!llvm.module.flags = !{ !11, !12 }
36+
37+
!1 = !DIFile(directory: "", filename: "test")
38+
!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0)
39+
!3 = !DIBasicType(encoding: DW_ATE_boolean, name: "bool", size: 8)
40+
!4 = !DISubroutineType(types: !{null})
41+
!5 = distinct !DISubprogram(file: !1, isDefinition: true, isLocal: false, isOptimized: false, line: 5, linkageName: "foo", name: "foo", scope: !1, scopeLine: 5, type: !4, unit: !2)
42+
!6 = !DILocation(column: 1, line: 5, scope: !5)
43+
!7 = !DILocation(column: 1, line: 7, scope: !5)
44+
!8 = !DILocation(column: 1, line: 8, scope: !5)
45+
!9 = !DILocalVariable(arg: 0, file: !1, line: 5, name: "arg", scope: !5, type: !3)
46+
!10 = !DIExpression()
47+
!11 = !{ i32 2, !"Dwarf Version", i32 4 }
48+
!12 = !{ i32 2, !"Debug Info Version", i32 3 }

0 commit comments

Comments
 (0)