Skip to content

Commit 02784a2

Browse files
committed
Add a variation on CopyPropagation to handle debug_value correctly.
MandatoryCopyPropagation must be a separate pass in order to preserve all debug_value instructions. CopyPropagation cannot preserve debug_value because, as a rule, debug information cannot affect -O behavior.
1 parent f22d085 commit 02784a2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ PASS(ConstantEvaluableSubsetChecker, "test-constant-evaluable-subset",
123123
PASS(CopyForwarding, "copy-forwarding",
124124
"Copy Forwarding to Remove Redundant Copies")
125125
PASS(CopyPropagation, "copy-propagation",
126-
"Copy propagation to Remove Redundant SSA Copies")
126+
"Copy propagation to Remove Redundant SSA Copies, pruning debug info")
127+
PASS(MandatoryCopyPropagation, "mandatory-copy-propagation",
128+
"Copy propagation to Remove Redundant SSA Copies, preserving debug info")
127129
PASS(COWOpts, "cow-opts",
128130
"Optimize COW operations")
129131
PASS(Differentiation, "differentiation",

lib/SILOptimizer/Transforms/CopyPropagation.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ using namespace swift;
3838

3939
namespace {
4040
class CopyPropagation : public SILFunctionTransform {
41+
/// True if debug_value instructions should be pruned.
42+
bool pruneDebug;
43+
44+
public:
45+
CopyPropagation(bool pruneDebug): pruneDebug(pruneDebug) {}
46+
4147
/// The entry point to this function transformation.
4248
void run() override;
4349
};
@@ -64,7 +70,7 @@ void CopyPropagation::run() {
6470
}
6571
}
6672
// Perform copy propgation for each copied value.
67-
CanonicalizeOSSALifetime canonicalizer(/*pruneDebug*/ true);
73+
CanonicalizeOSSALifetime canonicalizer(pruneDebug);
6874
for (auto &def : copiedDefs) {
6975
canonicalizer.canonicalizeValueLifetime(def);
7076
if (SILValue outerCopy = canonicalizer.createdOuterCopy()) {
@@ -81,4 +87,10 @@ void CopyPropagation::run() {
8187
}
8288
}
8389

84-
SILTransform *swift::createCopyPropagation() { return new CopyPropagation(); }
90+
SILTransform *swift::createCopyPropagation() {
91+
return new CopyPropagation(/*pruneDebug*/ true);
92+
}
93+
94+
SILTransform *swift::createMandatoryCopyPropagation() {
95+
return new CopyPropagation(/*pruneDebug*/ false);
96+
}

0 commit comments

Comments
 (0)