Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we modifying GlobalISel? I think it was working correctly there already right?

Copy link
Contributor Author

@laxmansole laxmansole Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediate values need to be sign-extended for correctness. Consider a case where the immediate operand is a negative value.
An exception to this would be 1-bit values, as they don't have a notion of sign, and sign-extending them changes their value like 1 becomes -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)) {
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 5 additions & 1 deletion llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3416,7 +3416,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;
Expand Down
29 changes: 29 additions & 0 deletions llvm/test/DebugInfo/debug-bool-const-value.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
; REQUIRES: object-emission
; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s

; CHECK: {{.*}}DW_TAG_variable
; CHECK-NEXT: {{.*}} DW_AT_const_value (1)
; CHECK-NEXT: {{.*}} DW_AT_name ("arg")

define void @test() !dbg !5
{
entry:
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 }

!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: "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 }
2 changes: 1 addition & 1 deletion llvm/unittests/Transforms/Utils/LocalTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ TEST(Local, ExpressionForConstant) {
IntegerType *Int1Ty = Type::getInt1Ty(Context);
Expr = createExpression(ConstantInt::getTrue(Context), Int1Ty);
EXPECT_NE(Expr, nullptr);
EXPECT_EQ(Expr->getElement(1), 18446744073709551615U);
EXPECT_EQ(Expr->getElement(1), 1U);

Expr = createExpression(ConstantInt::getFalse(Context), Int1Ty);
EXPECT_NE(Expr, nullptr);
Expand Down
Loading