Skip to content

Commit

Permalink
[DebugInfo] Salvage integer literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowy1803 committed Apr 26, 2024
1 parent 46909fc commit a4117e1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1964,6 +1964,25 @@ void swift::salvageDebugInfo(SILInstruction *I) {
}
}
}

if (auto *IL = dyn_cast<IntegerLiteralInst>(I)) {
APInt value = IL->getValue();
const SILDIExprElement ExprElements[2] = {
SILDIExprElement::createOperator(value.isNegative() ?
SILDIExprOperator::ConstSInt : SILDIExprOperator::ConstUInt),
SILDIExprElement::createConstInt(value.getLimitedValue()),
};
for (Operand *U : getDebugUses(IL)) {
auto *DbgInst = cast<DebugValueInst>(U->getUser());
auto VarInfo = DbgInst->getVarInfo();
if (!VarInfo)
continue;
VarInfo->DIExpr.prependElements(ExprElements);
// Create a new debug_value, with undef, and the correct const int
SILBuilder(DbgInst, DbgInst->getDebugScope())
.createDebugValue(DbgInst->getLoc(), SILUndef::get(IL), *VarInfo);
}
}
}

void swift::salvageLoadDebugInfo(LoadOperation load) {
Expand Down
13 changes: 13 additions & 0 deletions test/DebugInfo/salvage-integer-literal.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %target-swift-frontend -O -g -emit-sil %s | %FileCheck %s

// In optimized code, a + b will be folded to 5, but we should still keep their
// debug values.

// CHECK-LABEL: sil
public func f() -> Int {
let a = 2
let b = 3
// CHECK: debug_value undef : $Builtin.Int64, let, name "a", type $Int, expr op_constu:2:op_fragment:#Int._value
// CHECK: debug_value undef : $Builtin.Int64, let, name "b", type $Int, expr op_constu:3:op_fragment:#Int._value
return a + b
}

0 comments on commit a4117e1

Please sign in to comment.