diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp index 67f526fe91464..5167d9be4d3fb 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 NVPTXAddressSpace; std::unique_ptr DwarfExpr; + + // Check if this variable is of boolean type + bool isBoolean = false; + if (GV && GV->getType()) + if (auto *BasicType = dyn_cast(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; } @@ -821,6 +834,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(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()) { @@ -837,7 +866,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/debug-bool-const-value.ll b/llvm/test/DebugInfo/debug-bool-const-value.ll new file mode 100644 index 0000000000000..71109c6d90486 --- /dev/null +++ b/llvm/test/DebugInfo/debug-bool-const-value.ll @@ -0,0 +1,35 @@ +; 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") + +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 } +!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) +!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 } +!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