[llvm] [llvm][DebugInfo] Emit 0/1 for constant boolean values (PR #151225)

Laxman Sole via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 18 18:14:17 PDT 2025


https://github.com/laxmansole updated https://github.com/llvm/llvm-project/pull/151225

>From 554cf9a13ec24c44319d7329008b83beb1e31519 Mon Sep 17 00:00:00 2001
From: Laxman Sole <lsole at nvidia.com>
Date: Fri, 25 Jul 2025 10:11:18 -0700
Subject: [PATCH 1/3] [DWARF] Emit 0/1 for constant boolean values

---
 .../CodeGen/AsmPrinter/DwarfCompileUnit.cpp   | 34 ++++++++++++-
 .../NVPTX/debug-bool-const-location.ll        | 51 +++++++++++++++++++
 .../DebugInfo/NVPTX/debug-bool-const-value.ll | 37 ++++++++++++++
 3 files changed, 120 insertions(+), 2 deletions(-)
 create mode 100644 llvm/test/DebugInfo/NVPTX/debug-bool-const-location.ll
 create mode 100644 llvm/test/DebugInfo/NVPTX/debug-bool-const-value.ll

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index 7ce014e9fac9a..b6b363dbbf0a1 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -247,6 +247,13 @@ void DwarfCompileUnit::addLocationAttribute(
   DIELoc *Loc = nullptr;
   std::optional<unsigned> NVPTXAddressSpace;
   std::unique_ptr<DIEDwarfExpression> DwarfExpr;
+
+  // Check if this variable is of boolean type
+  bool isBoolean = false;
+  if (GV && GV->getType())
+    if (auto *BasicType = dyn_cast<DIBasicType>(GV->getType()))
+      isBoolean = BasicType->getEncoding() == dwarf::DW_ATE_boolean;
+
   for (const auto &GE : GlobalExprs) {
     const GlobalVariable *Global = GE.Var;
     const DIExpression *Expr = GE.Expr;
@@ -257,11 +264,17 @@ void DwarfCompileUnit::addLocationAttribute(
     // DW_AT_const_value(X).
     if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) {
       addToAccelTable = true;
+
+      // Determine the value to use, normalizing booleans to 0 or 1
+      int64_t valueToUse = Expr->getElement(1);
+      if (isBoolean)
+        valueToUse = valueToUse ? 1 : 0;
+
       addConstantValue(
           *VariableDIE,
           DIExpression::SignedOrUnsignedConstant::UnsignedConstant ==
               *Expr->isConstant(),
-          Expr->getElement(1));
+          valueToUse);
       break;
     }
 
@@ -822,6 +835,22 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
   }
   if (!DVal->isVariadic()) {
     const DbgValueLocEntry *Entry = DVal->getLocEntries().begin();
+
+    // Helper function to handle boolean constant values with type safety
+    auto addConstantValueWithBooleanNormalization =
+        [&](DIE &VariableDie, uint64_t intValue, const DIType *Type) {
+          if (auto *BasicType = dyn_cast_or_null<DIBasicType>(Type)) {
+            if (BasicType->getEncoding() == dwarf::DW_ATE_boolean) {
+              // Normalize boolean values: any non-zero becomes 1, zero stays 0
+              uint64_t normalizedBoolValue = (intValue) ? 1 : 0;
+              addConstantValue(VariableDie, normalizedBoolValue, Type);
+              return;
+            }
+          }
+          // For non-boolean types, use the original constant value
+          addConstantValue(VariableDie, intValue, Type);
+        };
+
     if (Entry->isLocation()) {
       addVariableAddress(DV, VariableDie, Entry->getLoc());
     } else if (Entry->isInt()) {
@@ -838,7 +867,8 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
           addUInt(VariableDie, dwarf::DW_AT_LLVM_tag_offset,
                   dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
       } else
-        addConstantValue(VariableDie, Entry->getInt(), DV.getType());
+        addConstantValueWithBooleanNormalization(VariableDie, Entry->getInt(),
+                                                 DV.getType());
     } else if (Entry->isConstantFP()) {
       addConstantFPValue(VariableDie, Entry->getConstantFP());
     } else if (Entry->isConstantInt()) {
diff --git a/llvm/test/DebugInfo/NVPTX/debug-bool-const-location.ll b/llvm/test/DebugInfo/NVPTX/debug-bool-const-location.ll
new file mode 100644
index 0000000000000..f073d51e7ef99
--- /dev/null
+++ b/llvm/test/DebugInfo/NVPTX/debug-bool-const-location.ll
@@ -0,0 +1,51 @@
+; RUN: llc < %s -asm-verbose -mattr=+ptx76  -O0| FileCheck %s
+; RUN: %if ptxas %{ llc < %s -asm-verbose -mattr=+ptx76  -O0 | %ptxas-verify %}
+
+target triple = "nvptx64-nvidia-cuda"
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
+
+; CHECK: {{.*}}section {{.*}}debug_loc
+; CHECK: .b8 48{{.*}} DW_OP_lit0
+; CHECK: .b8 49{{.*}} DW_OP_lit1
+; CHECK: .b8 144{{.*}} DW_OP_regx
+
+define void @foo(i8 %"arg.arg") !dbg !5
+{
+entry:
+  %".4" = alloca i1
+  %".5" = icmp eq i8 %"arg.arg", 0
+  %arg = alloca i1
+  br i1 %".5", label %"entry.if", label %"entry.else"
+entry.if:
+  store i1 false, i1* %arg
+  call void @"llvm.dbg.value"(metadata i1 false , metadata !9, metadata !10), !dbg !6
+  br label %"entry.endif"
+entry.else:
+  store i1 true, i1* %arg
+  call void @"llvm.dbg.value"(metadata i1 true , metadata !9, metadata !10), !dbg !7
+  br label %"entry.endif"
+entry.endif:
+  %".11" = load i1, i1* %arg
+  store i1 %".11", i1* %".4", !dbg !8
+  call void @"llvm.dbg.value"(metadata i1 %".11" , metadata !9, metadata !10), !dbg !8
+  ret void, !dbg !8
+}
+
+declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
+
+!llvm.dbg.cu = !{ !2 }
+!llvm.module.flags = !{ !11, !12 }
+!nvvm.annotations = !{}
+
+!1 = !DIFile(directory: "/source/dir", filename: "test.cu")
+!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 }
\ No newline at end of file
diff --git a/llvm/test/DebugInfo/NVPTX/debug-bool-const-value.ll b/llvm/test/DebugInfo/NVPTX/debug-bool-const-value.ll
new file mode 100644
index 0000000000000..002a7a801c746
--- /dev/null
+++ b/llvm/test/DebugInfo/NVPTX/debug-bool-const-value.ll
@@ -0,0 +1,37 @@
+; RUN: llc < %s -asm-verbose -mattr=+ptx76 | FileCheck %s
+; RUN: %if ptxas %{ llc < %s -asm-verbose -mattr=+ptx76 | %ptxas-verify %}
+
+target triple = "nvptx64-nvidia-cuda"
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
+
+; CHECK: {{.*}}section {{.*}}debug_info
+; CHECK: {{.*}}DW_TAG_variable
+; CHECK-NEXT: {{.*}} DW_AT_address_class
+; CHECK-NEXT: .b8 1{{.*}} DW_AT_const_value
+; CHECK-NEXT: {{.*}} DW_AT_name
+
+define void @test() !dbg !5
+{
+entry:
+  %arg = alloca i1
+  store i1 true, i1* %arg, !dbg !6
+  call void @"llvm.dbg.value"(metadata i1 true, metadata !7, metadata !8), !dbg !6
+  ret void, !dbg !6
+}
+
+declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
+
+!llvm.dbg.cu = !{ !2 }
+!llvm.module.flags = !{ !9, !10 }
+!nvvm.annotations = !{}
+
+!1 = !DIFile(directory: "/source/dir", filename: "test.cu")
+!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: "test", name: "test", scope: !1, scopeLine: 5, type: !4, unit: !2)
+!6 = !DILocation(column: 1, line: 5, scope: !5)
+!7 = !DILocalVariable(arg: 0, file: !1, line: 5, name: "arg", scope: !5, type: !3)
+!8 = !DIExpression()
+!9 = !{ i32 2, !"Dwarf Version", i32 4 }
+!10 = !{ i32 2, !"Debug Info Version", i32 3 }
\ No newline at end of file

>From da222cff5dd9f863f30b8e05c2e0670100bef900 Mon Sep 17 00:00:00 2001
From: Laxman Sole <lsole at nvidia.com>
Date: Wed, 20 Aug 2025 18:58:20 -0700
Subject: [PATCH 2/3] Move DW_OP_lit0/1 related change to separate PR

---
 llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp    |  6 +--
 .../CodeGen/AsmPrinter/DwarfExpression.cpp    |  9 ----
 llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h |  3 --
 .../NVPTX/debug-bool-const-location.ll        | 51 -------------------
 .../{NVPTX => }/debug-bool-const-value.ll     | 28 +++++-----
 5 files changed, 15 insertions(+), 82 deletions(-)
 delete mode 100644 llvm/test/DebugInfo/NVPTX/debug-bool-const-location.ll
 rename llvm/test/DebugInfo/{NVPTX => }/debug-bool-const-value.ll (56%)

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 8efc6f124a55d..6d8b095073e6d 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -3110,10 +3110,8 @@ 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_boolean))
-        DwarfExpr.addBooleanConstant(Entry.getInt());
-      else if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
-                      BT->getEncoding() == dwarf::DW_ATE_signed_char))
+      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 8a30714db2fdf..e684054ffa3e4 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
@@ -194,15 +194,6 @@ 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 700e0ec5813ee..06809ab263875 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
@@ -229,9 +229,6 @@ 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/NVPTX/debug-bool-const-location.ll b/llvm/test/DebugInfo/NVPTX/debug-bool-const-location.ll
deleted file mode 100644
index f073d51e7ef99..0000000000000
--- a/llvm/test/DebugInfo/NVPTX/debug-bool-const-location.ll
+++ /dev/null
@@ -1,51 +0,0 @@
-; RUN: llc < %s -asm-verbose -mattr=+ptx76  -O0| FileCheck %s
-; RUN: %if ptxas %{ llc < %s -asm-verbose -mattr=+ptx76  -O0 | %ptxas-verify %}
-
-target triple = "nvptx64-nvidia-cuda"
-target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
-
-; CHECK: {{.*}}section {{.*}}debug_loc
-; CHECK: .b8 48{{.*}} DW_OP_lit0
-; CHECK: .b8 49{{.*}} DW_OP_lit1
-; CHECK: .b8 144{{.*}} DW_OP_regx
-
-define void @foo(i8 %"arg.arg") !dbg !5
-{
-entry:
-  %".4" = alloca i1
-  %".5" = icmp eq i8 %"arg.arg", 0
-  %arg = alloca i1
-  br i1 %".5", label %"entry.if", label %"entry.else"
-entry.if:
-  store i1 false, i1* %arg
-  call void @"llvm.dbg.value"(metadata i1 false , metadata !9, metadata !10), !dbg !6
-  br label %"entry.endif"
-entry.else:
-  store i1 true, i1* %arg
-  call void @"llvm.dbg.value"(metadata i1 true , metadata !9, metadata !10), !dbg !7
-  br label %"entry.endif"
-entry.endif:
-  %".11" = load i1, i1* %arg
-  store i1 %".11", i1* %".4", !dbg !8
-  call void @"llvm.dbg.value"(metadata i1 %".11" , metadata !9, metadata !10), !dbg !8
-  ret void, !dbg !8
-}
-
-declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
-
-!llvm.dbg.cu = !{ !2 }
-!llvm.module.flags = !{ !11, !12 }
-!nvvm.annotations = !{}
-
-!1 = !DIFile(directory: "/source/dir", filename: "test.cu")
-!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 }
\ No newline at end of file
diff --git a/llvm/test/DebugInfo/NVPTX/debug-bool-const-value.ll b/llvm/test/DebugInfo/debug-bool-const-value.ll
similarity index 56%
rename from llvm/test/DebugInfo/NVPTX/debug-bool-const-value.ll
rename to llvm/test/DebugInfo/debug-bool-const-value.ll
index 002a7a801c746..71109c6d90486 100644
--- a/llvm/test/DebugInfo/NVPTX/debug-bool-const-value.ll
+++ b/llvm/test/DebugInfo/debug-bool-const-value.ll
@@ -1,20 +1,17 @@
-; RUN: llc < %s -asm-verbose -mattr=+ptx76 | FileCheck %s
-; RUN: %if ptxas %{ llc < %s -asm-verbose -mattr=+ptx76 | %ptxas-verify %}
+; REQUIRES: object-emission
+; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
 
-target triple = "nvptx64-nvidia-cuda"
-target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
 
-; CHECK: {{.*}}section {{.*}}debug_info
 ; CHECK: {{.*}}DW_TAG_variable
-; CHECK-NEXT: {{.*}} DW_AT_address_class
-; CHECK-NEXT: .b8 1{{.*}} DW_AT_const_value
-; CHECK-NEXT: {{.*}} DW_AT_name
+; CHECK-NEXT: {{.*}} DW_AT_name      ("global_bool_const")
+; CHECK: {{.*}} DW_AT_const_value     (1)
+; CHECK: {{.*}}DW_TAG_variable
+; CHECK-NEXT: {{.*}} DW_AT_const_value     (1)
+; CHECK-NEXT: {{.*}} DW_AT_name    ("arg")
 
 define void @test() !dbg !5
 {
 entry:
-  %arg = alloca i1
-  store i1 true, i1* %arg, !dbg !6
   call void @"llvm.dbg.value"(metadata i1 true, metadata !7, metadata !8), !dbg !6
   ret void, !dbg !6
 }
@@ -23,10 +20,9 @@ declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
 
 !llvm.dbg.cu = !{ !2 }
 !llvm.module.flags = !{ !9, !10 }
-!nvvm.annotations = !{}
-
-!1 = !DIFile(directory: "/source/dir", filename: "test.cu")
-!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0)
+!0 = !{ !11 }
+!1 = !DIFile(directory: "", filename: "test")
+!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0, globals: !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: "test", name: "test", scope: !1, scopeLine: 5, type: !4, unit: !2)
@@ -34,4 +30,6 @@ declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
 !7 = !DILocalVariable(arg: 0, file: !1, line: 5, name: "arg", scope: !5, type: !3)
 !8 = !DIExpression()
 !9 = !{ i32 2, !"Dwarf Version", i32 4 }
-!10 = !{ i32 2, !"Debug Info Version", i32 3 }
\ No newline at end of file
+!10 = !{ i32 2, !"Debug Info Version", i32 3 }
+!11 = !DIGlobalVariableExpression(var: !12, expr: !DIExpression(DW_OP_consts, 18446744073709551615, DW_OP_stack_value))
+!12 = distinct !DIGlobalVariable(name: "global_bool_const", scope: !2, file: !1, line: 1, type: !3, isLocal: false, isDefinition: true)
\ No newline at end of file

>From d3ad4309a4ea7c6bb410a87a7f83678651b034ed Mon Sep 17 00:00:00 2001
From: Laxman Sole <lsole at nvidia.com>
Date: Thu, 18 Sep 2025 18:10:46 -0700
Subject: [PATCH 3/3] Zero-extend 1-bit constantInt operands of the DbgValue

---
 .../CodeGen/AsmPrinter/DwarfCompileUnit.cpp   | 34 ++-----------------
 .../CodeGen/GlobalISel/MachineIRBuilder.cpp   |  4 ++-
 .../lib/CodeGen/SelectionDAG/InstrEmitter.cpp |  2 ++
 llvm/lib/Transforms/Utils/Local.cpp           |  6 +++-
 llvm/test/DebugInfo/debug-bool-const-value.ll | 10 ++----
 5 files changed, 14 insertions(+), 42 deletions(-)

diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
index b6b363dbbf0a1..7ce014e9fac9a 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
@@ -247,13 +247,6 @@ void DwarfCompileUnit::addLocationAttribute(
   DIELoc *Loc = nullptr;
   std::optional<unsigned> NVPTXAddressSpace;
   std::unique_ptr<DIEDwarfExpression> DwarfExpr;
-
-  // Check if this variable is of boolean type
-  bool isBoolean = false;
-  if (GV && GV->getType())
-    if (auto *BasicType = dyn_cast<DIBasicType>(GV->getType()))
-      isBoolean = BasicType->getEncoding() == dwarf::DW_ATE_boolean;
-
   for (const auto &GE : GlobalExprs) {
     const GlobalVariable *Global = GE.Var;
     const DIExpression *Expr = GE.Expr;
@@ -264,17 +257,11 @@ void DwarfCompileUnit::addLocationAttribute(
     // DW_AT_const_value(X).
     if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) {
       addToAccelTable = true;
-
-      // Determine the value to use, normalizing booleans to 0 or 1
-      int64_t valueToUse = Expr->getElement(1);
-      if (isBoolean)
-        valueToUse = valueToUse ? 1 : 0;
-
       addConstantValue(
           *VariableDIE,
           DIExpression::SignedOrUnsignedConstant::UnsignedConstant ==
               *Expr->isConstant(),
-          valueToUse);
+          Expr->getElement(1));
       break;
     }
 
@@ -835,22 +822,6 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
   }
   if (!DVal->isVariadic()) {
     const DbgValueLocEntry *Entry = DVal->getLocEntries().begin();
-
-    // Helper function to handle boolean constant values with type safety
-    auto addConstantValueWithBooleanNormalization =
-        [&](DIE &VariableDie, uint64_t intValue, const DIType *Type) {
-          if (auto *BasicType = dyn_cast_or_null<DIBasicType>(Type)) {
-            if (BasicType->getEncoding() == dwarf::DW_ATE_boolean) {
-              // Normalize boolean values: any non-zero becomes 1, zero stays 0
-              uint64_t normalizedBoolValue = (intValue) ? 1 : 0;
-              addConstantValue(VariableDie, normalizedBoolValue, Type);
-              return;
-            }
-          }
-          // For non-boolean types, use the original constant value
-          addConstantValue(VariableDie, intValue, Type);
-        };
-
     if (Entry->isLocation()) {
       addVariableAddress(DV, VariableDie, Entry->getLoc());
     } else if (Entry->isInt()) {
@@ -867,8 +838,7 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
           addUInt(VariableDie, dwarf::DW_AT_LLVM_tag_offset,
                   dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
       } else
-        addConstantValueWithBooleanNormalization(VariableDie, Entry->getInt(),
-                                                 DV.getType());
+        addConstantValue(VariableDie, Entry->getInt(), DV.getType());
     } else if (Entry->isConstantFP()) {
       addConstantFPValue(VariableDie, Entry->getConstantFP());
     } else if (Entry->isConstantInt()) {
diff --git a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
index 27df7e369436a..2f5d8e5cf4506 100644
--- a/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
@@ -109,8 +109,10 @@ MachineInstrBuilder MachineIRBuilder::buildConstDbgValue(const Constant &C,
   if (auto *CI = dyn_cast<ConstantInt>(NumericConstant)) {
     if (CI->getBitWidth() > 64)
       MIB.addCImm(CI);
-    else
+    else if (CI->getBitWidth() == 1)
       MIB.addImm(CI->getZExtValue());
+    else
+      MIB.addImm(CI->getSExtValue());
   } else if (auto *CFP = dyn_cast<ConstantFP>(NumericConstant)) {
     MIB.addFPImm(CFP);
   } else if (isa<ConstantPointerNull>(NumericConstant)) {
diff --git a/llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp b/llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp
index 11bc64c626421..7d84d7845a1c6 100644
--- a/llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp
@@ -733,6 +733,8 @@ MachineOperand GetMOForConstDbgOp(const SDDbgOperand &Op) {
   if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
     if (CI->getBitWidth() > 64)
       return MachineOperand::CreateCImm(CI);
+    if (CI->getBitWidth() == 1)
+      return MachineOperand::CreateImm(CI->getZExtValue());
     return MachineOperand::CreateImm(CI->getSExtValue());
   }
   if (const ConstantFP *CF = dyn_cast<ConstantFP>(V))
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 57dc1b38b8ec3..d8f28c214d0b2 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -3387,7 +3387,11 @@ DIExpression *llvm::getExpressionForConstant(DIBuilder &DIB, const Constant &C,
   // Create integer constant expression.
   auto createIntegerExpression = [&DIB](const Constant &CV) -> DIExpression * {
     const APInt &API = cast<ConstantInt>(&CV)->getValue();
-    std::optional<int64_t> InitIntOpt = API.trySExtValue();
+    std::optional<int64_t> InitIntOpt;
+    if (API.getBitWidth() == 1)
+      InitIntOpt = API.tryZExtValue();
+    else
+      InitIntOpt = API.trySExtValue();
     return InitIntOpt ? DIB.createConstantValueExpression(
                             static_cast<uint64_t>(*InitIntOpt))
                       : nullptr;
diff --git a/llvm/test/DebugInfo/debug-bool-const-value.ll b/llvm/test/DebugInfo/debug-bool-const-value.ll
index 71109c6d90486..84cf993cf4aae 100644
--- a/llvm/test/DebugInfo/debug-bool-const-value.ll
+++ b/llvm/test/DebugInfo/debug-bool-const-value.ll
@@ -1,10 +1,6 @@
 ; REQUIRES: object-emission
 ; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
 
-
-; CHECK: {{.*}}DW_TAG_variable
-; CHECK-NEXT: {{.*}} DW_AT_name      ("global_bool_const")
-; CHECK: {{.*}} DW_AT_const_value     (1)
 ; CHECK: {{.*}}DW_TAG_variable
 ; CHECK-NEXT: {{.*}} DW_AT_const_value     (1)
 ; CHECK-NEXT: {{.*}} DW_AT_name    ("arg")
@@ -20,9 +16,9 @@ declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
 
 !llvm.dbg.cu = !{ !2 }
 !llvm.module.flags = !{ !9, !10 }
-!0 = !{ !11 }
+
 !1 = !DIFile(directory: "", filename: "test")
-!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0, globals: !0)
+!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: "test", name: "test", scope: !1, scopeLine: 5, type: !4, unit: !2)
@@ -31,5 +27,3 @@ declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
 !8 = !DIExpression()
 !9 = !{ i32 2, !"Dwarf Version", i32 4 }
 !10 = !{ i32 2, !"Debug Info Version", i32 3 }
-!11 = !DIGlobalVariableExpression(var: !12, expr: !DIExpression(DW_OP_consts, 18446744073709551615, DW_OP_stack_value))
-!12 = distinct !DIGlobalVariable(name: "global_bool_const", scope: !2, file: !1, line: 1, type: !3, isLocal: false, isDefinition: true)
\ No newline at end of file



More information about the llvm-commits mailing list