Skip to content

Commit b26225c

Browse files
authored
Merge pull request #61724 from meg-gupta/revertaddrphi
Revert changes to ArrayPropertyOpt to avoid address phis
2 parents 26a3c45 + fdf4d13 commit b26225c

File tree

3 files changed

+27
-63
lines changed

3 files changed

+27
-63
lines changed

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,24 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
985985
return InstNumbers[a] < InstNumbers[b];
986986
}
987987

988+
// FIXME: For sanity, address-type phis should be prohibited at all SIL
989+
// stages. However, the optimizer currently breaks the invariant in three
990+
// places:
991+
// 1. Normal Simplify CFG during conditional branch simplification
992+
// (sneaky jump threading).
993+
// 2. Simplify CFG via Jump Threading.
994+
// 3. Loop Rotation.
995+
//
996+
// BasicBlockCloner::canCloneInstruction and sinkAddressProjections is
997+
// designed to avoid this issue, we just need to make sure all passes use it
998+
// correctly.
999+
//
1000+
// Minimally, we must prevent address-type phis as long as access markers are
1001+
// preserved. A goal is to preserve access markers in OSSA.
1002+
bool prohibitAddressPhis() {
1003+
return F.hasOwnership();
1004+
}
1005+
9881006
void visitSILPhiArgument(SILPhiArgument *arg) {
9891007
// Verify that the `isPhiArgument` property is sound:
9901008
// - Phi arguments come from branches.
@@ -1008,7 +1026,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
10081026
"All phi argument inputs must be from branches.");
10091027
}
10101028
}
1011-
if (arg->isPhi()) {
1029+
if (arg->isPhi() && prohibitAddressPhis()) {
10121030
// As a property of well-formed SIL, we disallow address-type
10131031
// phis. Supporting them would prevent reliably reasoning about the
10141032
// underlying storage of memory access. This reasoning is important for

lib/SILOptimizer/LoopTransforms/ArrayPropertyOpt.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,18 @@
5353
#define DEBUG_TYPE "array-property-opt"
5454

5555
#include "ArrayOpt.h"
56-
#include "swift/SIL/BasicBlockBits.h"
57-
#include "swift/SIL/CFG.h"
58-
#include "swift/SIL/DebugUtils.h"
59-
#include "swift/SIL/InstructionUtils.h"
60-
#include "swift/SIL/LoopInfo.h"
61-
#include "swift/SIL/Projection.h"
62-
#include "swift/SIL/SILCloner.h"
6356
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
6457
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
6558
#include "swift/SILOptimizer/PassManager/Transforms.h"
66-
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
6759
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
6860
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
61+
#include "swift/SIL/CFG.h"
62+
#include "swift/SIL/DebugUtils.h"
63+
#include "swift/SIL/InstructionUtils.h"
64+
#include "swift/SIL/Projection.h"
65+
#include "swift/SIL/LoopInfo.h"
66+
#include "swift/SIL/BasicBlockBits.h"
67+
#include "swift/SIL/SILCloner.h"
6968
#include "llvm/ADT/SmallSet.h"
7069
#include "llvm/Support/CommandLine.h"
7170
#include "llvm/Support/Debug.h"
@@ -86,8 +85,6 @@ class ArrayPropertiesAnalysis {
8685
SILBasicBlock *Preheader;
8786
DominanceInfo *DomTree;
8887

89-
SinkAddressProjections sinkProj;
90-
9188
llvm::DenseMap<SILFunction *, uint32_t> InstCountCache;
9289
llvm::SmallSet<SILValue, 16> HoistableArray;
9390

@@ -171,18 +168,13 @@ class ArrayPropertiesAnalysis {
171168

172169
bool FoundHoistable = false;
173170
uint32_t LoopInstCount = 0;
174-
175171
for (auto *BB : Loop->getBlocks()) {
176172
for (auto &Inst : *BB) {
177173
// Can't clone alloc_stack instructions whose dealloc_stack is outside
178174
// the loop.
179175
if (!Loop->canDuplicate(&Inst))
180176
return false;
181177

182-
if (!sinkProj.analyzeAddressProjections(&Inst)) {
183-
return false;
184-
}
185-
186178
ArraySemanticsCall ArrayPropsInst(&Inst, "array.props", true);
187179
if (!ArrayPropsInst)
188180
continue;
@@ -544,15 +536,10 @@ class RegionCloner : public SILCloner<RegionCloner> {
544536
for (auto *arg : origBB->getArguments())
545537
updateSSAForValue(origBB, arg, SSAUp);
546538

547-
SinkAddressProjections sinkProj;
548539
// Update outside used instruction values.
549540
for (auto &inst : *origBB) {
550-
for (auto result : inst.getResults()) {
551-
bool success = sinkProj.analyzeAddressProjections(&inst);
552-
assert(success);
553-
sinkProj.cloneProjections();
541+
for (auto result : inst.getResults())
554542
updateSSAForValue(origBB, result, SSAUp);
555-
}
556543
}
557544
}
558545
}

test/SILOptimizer/array_property_opt.sil

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -258,44 +258,3 @@ bb10: // Exit dominated by bb3
258258
bb11: // Non-exit dominated by bb1
259259
return %4 : $Builtin.Int1
260260
}
261-
262-
class Klass {
263-
}
264-
265-
struct WrapperStruct {
266-
var val: Klass
267-
}
268-
269-
sil @use_klass : $@convention(thin) (@in_guaranteed Klass) -> ()
270-
271-
sil @test_sink_address_proj : $@convention(thin) (@inout MyArray<MyClass>, @in_guaranteed WrapperStruct) -> () {
272-
bb0(%0 : $*MyArray<MyClass>, %1 : $*WrapperStruct):
273-
%3 = load %0 : $*MyArray<MyClass>
274-
br bb1
275-
276-
bb1:
277-
%2 = function_ref @arrayPropertyIsNative : $@convention(method) (@owned MyArray<MyClass>) -> Bool
278-
retain_value %3 : $MyArray<MyClass>
279-
%5 = apply %2(%3) : $@convention(method) (@owned MyArray<MyClass>) -> Bool
280-
%ele = struct_element_addr %1 : $*WrapperStruct, #WrapperStruct.val
281-
cond_br undef, bb5, bb2
282-
283-
bb2:
284-
%6 = integer_literal $Builtin.Int1, -1
285-
cond_br %6, bb3, bb4
286-
287-
bb3:
288-
br bb1
289-
290-
bb4:
291-
br bb6
292-
293-
bb5:
294-
%f = function_ref @use_klass : $@convention(thin) (@in_guaranteed Klass) -> ()
295-
%a = apply %f(%ele) : $@convention(thin) (@in_guaranteed Klass) -> ()
296-
br bb6
297-
298-
bb6:
299-
%t = tuple ()
300-
return %t : $()
301-
}

0 commit comments

Comments
 (0)