Skip to content

Conversation

laxmansole
Copy link
Contributor

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_lit0; DW_OP_not.

@llvmbot
Copy link
Member

llvmbot commented Sep 5, 2025

@llvm/pr-subscribers-debuginfo

Author: Laxman Sole (laxmansole)

Changes

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_lit0; DW_OP_not.


Full diff: https://github.com/llvm/llvm-project/pull/157167.diff

4 Files Affected:

  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp (+4-2)
  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp (+9)
  • (modified) llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h (+3)
  • (added) llvm/test/DebugInfo/debug-bool-const-location.ll (+38)
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index c27f100775625..2090157a1a91c 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -3111,8 +3111,10 @@ void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
                             &AP](const DbgValueLocEntry &Entry,
                                  DIExpressionCursor &Cursor) -> bool {
     if (Entry.isInt()) {
-      if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
-                 BT->getEncoding() == dwarf::DW_ATE_signed_char))
+      if (BT && (BT->getEncoding() == dwarf::DW_ATE_boolean))
+        DwarfExpr.addBooleanConstant(Entry.getInt());
+      else if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
+                      BT->getEncoding() == dwarf::DW_ATE_signed_char))
         DwarfExpr.addSignedConstant(Entry.getInt());
       else
         DwarfExpr.addUnsignedConstant(Entry.getInt());
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
index e684054ffa3e4..8a30714db2fdf 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
@@ -194,6 +194,15 @@ void DwarfExpression::addStackValue() {
     emitOp(dwarf::DW_OP_stack_value);
 }
 
+void DwarfExpression::addBooleanConstant(int64_t Value) {
+  assert(isImplicitLocation() || isUnknownLocation());
+  LocationKind = Implicit;
+  if (Value == 0)
+    emitOp(dwarf::DW_OP_lit0);
+  else
+    emitOp(dwarf::DW_OP_lit1);
+}
+
 void DwarfExpression::addSignedConstant(int64_t Value) {
   assert(isImplicitLocation() || isUnknownLocation());
   LocationKind = Implicit;
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
index 06809ab263875..700e0ec5813ee 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
@@ -229,6 +229,9 @@ class DwarfExpression {
   /// This needs to be called last to commit any pending changes.
   void finalize();
 
+  /// Emit a boolean constant.
+  void addBooleanConstant(int64_t Value);
+
   /// Emit a signed constant.
   void addSignedConstant(int64_t Value);
 
diff --git a/llvm/test/DebugInfo/debug-bool-const-location.ll b/llvm/test/DebugInfo/debug-bool-const-location.ll
new file mode 100644
index 0000000000000..fc986d3688fb1
--- /dev/null
+++ b/llvm/test/DebugInfo/debug-bool-const-location.ll
@@ -0,0 +1,38 @@
+; REQUIRES: object-emission
+; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
+
+; CHECK: {{.*}} DW_TAG_variable
+; CHECK-NEXT: {{.*}} DW_AT_location
+; CHECK-NEXT: {{.*}} DW_OP_lit0
+; CHECK-NEXT: {{.*}} DW_OP_lit1
+; CHECK-NOT: {{.*}} DW_OP_lit0, DW_OP_not
+; CHECK: {{.*}} DW_AT_name    ("arg")
+
+define void @foo() !dbg !5
+{
+entry:
+  %arg = alloca i1
+  store i1 false, i1* %arg
+  call void @"llvm.dbg.value"(metadata i1 false , metadata !9, metadata !10), !dbg !6
+  store i1 true, i1* %arg
+  call void @"llvm.dbg.value"(metadata i1 true , metadata !9, metadata !10), !dbg !7
+  ret void, !dbg !8
+}
+
+declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
+
+!llvm.dbg.cu = !{ !2 }
+!llvm.module.flags = !{ !11, !12 }
+
+!1 = !DIFile(directory: "", filename: "test")
+!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0)
+!3 = !DIBasicType(encoding: DW_ATE_boolean, name: "bool", size: 8)
+!4 = !DISubroutineType(types: !{null})
+!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)
+!6 = !DILocation(column: 1, line: 5, scope: !5)
+!7 = !DILocation(column: 1, line: 7, scope: !5)
+!8 = !DILocation(column: 1, line: 8, scope: !5)
+!9 = !DILocalVariable(arg: 0, file: !1, line: 5, name: "arg", scope: !5, type: !3)
+!10 = !DIExpression()
+!11 = !{ i32 2, !"Dwarf Version", i32 4 }
+!12 = !{ i32 2, !"Debug Info Version", i32 3 }

@laxmansole
Copy link
Contributor Author

This PR is created by splitting changes from #151225. These changes were merged once in #155539 but reverted due to unit test failures on AArch64/ARM targets.

The original failing test is simplified to work with other targets.

Could we please run the testsuite before merging?

CC: @enferex @Michael137 @dwblaikie @adrian-prantl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants