Skip to content

Commit 6fe3ecc

Browse files
authored
[llvm][DebugInfo] Emit 0/1 for constant boolean values (#151225)
Previously, sign-extending a 1-bit boolean operand in `#DBG_VALUE` would convert `true` to -1 (i.e., 0xffffffffffffffff). However, DWARF treats booleans as unsigned values, so this resulted in the attribute `DW_AT_const_value(0xffffffffffffffff)` being emitted. As a result, the debugger would display the value as `255` instead of `true`. This change modifies the behavior to use zero-extension for 1-bit values instead, ensuring that `true` is represented as 1. Consequently, the DWARF attribute emitted is now `DW_AT_const_value(1)`, which allows the debugger to correctly display the boolean as `true`.
1 parent 25da15f commit 6fe3ecc

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ MachineInstrBuilder MachineIRBuilder::buildConstDbgValue(const Constant &C,
109109
if (auto *CI = dyn_cast<ConstantInt>(NumericConstant)) {
110110
if (CI->getBitWidth() > 64)
111111
MIB.addCImm(CI);
112-
else
112+
else if (CI->getBitWidth() == 1)
113113
MIB.addImm(CI->getZExtValue());
114+
else
115+
MIB.addImm(CI->getSExtValue());
114116
} else if (auto *CFP = dyn_cast<ConstantFP>(NumericConstant)) {
115117
MIB.addFPImm(CFP);
116118
} else if (isa<ConstantPointerNull>(NumericConstant)) {

llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ MachineOperand GetMOForConstDbgOp(const SDDbgOperand &Op) {
733733
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
734734
if (CI->getBitWidth() > 64)
735735
return MachineOperand::CreateCImm(CI);
736+
if (CI->getBitWidth() == 1)
737+
return MachineOperand::CreateImm(CI->getZExtValue());
736738
return MachineOperand::CreateImm(CI->getSExtValue());
737739
}
738740
if (const ConstantFP *CF = dyn_cast<ConstantFP>(V))

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3416,7 +3416,11 @@ DIExpression *llvm::getExpressionForConstant(DIBuilder &DIB, const Constant &C,
34163416
// Create integer constant expression.
34173417
auto createIntegerExpression = [&DIB](const Constant &CV) -> DIExpression * {
34183418
const APInt &API = cast<ConstantInt>(&CV)->getValue();
3419-
std::optional<int64_t> InitIntOpt = API.trySExtValue();
3419+
std::optional<int64_t> InitIntOpt;
3420+
if (API.getBitWidth() == 1)
3421+
InitIntOpt = API.tryZExtValue();
3422+
else
3423+
InitIntOpt = API.trySExtValue();
34203424
return InitIntOpt ? DIB.createConstantValueExpression(
34213425
static_cast<uint64_t>(*InitIntOpt))
34223426
: nullptr;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; REQUIRES: object-emission
2+
; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
3+
4+
; CHECK: {{.*}}DW_TAG_variable
5+
; CHECK-NEXT: {{.*}} DW_AT_const_value (1)
6+
; CHECK-NEXT: {{.*}} DW_AT_name ("arg")
7+
8+
define void @test() !dbg !5
9+
{
10+
entry:
11+
call void @"llvm.dbg.value"(metadata i1 true, metadata !7, metadata !8), !dbg !6
12+
ret void, !dbg !6
13+
}
14+
15+
declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
16+
17+
!llvm.dbg.cu = !{ !2 }
18+
!llvm.module.flags = !{ !9, !10 }
19+
20+
!1 = !DIFile(directory: "", filename: "test")
21+
!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0)
22+
!3 = !DIBasicType(encoding: DW_ATE_boolean, name: "bool", size: 8)
23+
!4 = !DISubroutineType(types: !{null})
24+
!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)
25+
!6 = !DILocation(column: 1, line: 5, scope: !5)
26+
!7 = !DILocalVariable(arg: 0, file: !1, line: 5, name: "arg", scope: !5, type: !3)
27+
!8 = !DIExpression()
28+
!9 = !{ i32 2, !"Dwarf Version", i32 4 }
29+
!10 = !{ i32 2, !"Debug Info Version", i32 3 }

llvm/unittests/Transforms/Utils/LocalTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ TEST(Local, ExpressionForConstant) {
11531153
IntegerType *Int1Ty = Type::getInt1Ty(Context);
11541154
Expr = createExpression(ConstantInt::getTrue(Context), Int1Ty);
11551155
EXPECT_NE(Expr, nullptr);
1156-
EXPECT_EQ(Expr->getElement(1), 18446744073709551615U);
1156+
EXPECT_EQ(Expr->getElement(1), 1U);
11571157

11581158
Expr = createExpression(ConstantInt::getFalse(Context), Int1Ty);
11591159
EXPECT_NE(Expr, nullptr);

0 commit comments

Comments
 (0)