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 84dae7c commit 16c57ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 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
}
5 changes: 1 addition & 4 deletions test/SILOptimizer/dead_array_elim.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -O -emit-sil -primary-file %s | %FileCheck %s
// RUN: %target-swift-frontend -O -emit-sil -primary-file %s | grep -v debug_value | %FileCheck %s

// REQUIRES: swift_stdlib_no_asserts
// REQUIRES: swift_in_compiler
Expand Down Expand Up @@ -62,9 +62,7 @@ func testDeadArrayElimWithAddressOnlyValues<T>(x: T, y: T) {
// RLE needs to handle the new init pattern - rdar://117751668
// TODO-LABEL: sil hidden {{.*}}@$s15dead_array_elim31testDeadArrayAfterOptimizationsySiSSF
// TODO: bb0(%0 : $String):
// TODO-NEXT: debug_value
// TODO-NEXT: integer_literal $Builtin.Int{{[0-9]+}}, 21
// TODO-NEXT: debug_value
// TODO-NEXT: struct $Int
// TODO-NEXT: return
// TODO: } // end sil function '$s15dead_array_elim31testDeadArrayAfterOptimizationsySiSSF'
Expand All @@ -85,7 +83,6 @@ func testDeadArrayAfterOptimizations(_ stringParameter: String) -> Int {
// CHECK-LABEL: sil hidden @$s15dead_array_elim15testNestedArraySiyF
// CHECK: bb0:
// CHECK-NEXT: integer_literal $Builtin.Int{{[0-9]+}}, 3
// CHECK-NEXT: debug_value
// CHECK-NEXT: struct $Int
// CHECK-NEXT: return
// CHECK: } // end sil function '$s15dead_array_elim15testNestedArraySiyF'
Expand Down
4 changes: 2 additions & 2 deletions test/SILOptimizer/optionset.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -O -sil-verify-all -module-name=test -emit-sil | %FileCheck %s
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -Osize -sil-verify-all -module-name=test -emit-sil | %FileCheck %s
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -O -sil-verify-all -module-name=test -emit-sil | grep -v debug_value | %FileCheck %s
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -Osize -sil-verify-all -module-name=test -emit-sil | grep -v debug_value | %FileCheck %s
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
// REQUIRES: swift_in_compiler

Expand Down

0 comments on commit 16c57ae

Please sign in to comment.