Skip to content

Commit fb4837f

Browse files
committed
Avoid generating address phis in ArrayPropertyOpt
1 parent 9e04e06 commit fb4837f

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

lib/SILOptimizer/LoopTransforms/ArrayPropertyOpt.cpp

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

5555
#include "ArrayOpt.h"
56-
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
57-
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
58-
#include "swift/SILOptimizer/PassManager/Transforms.h"
59-
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
60-
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
56+
#include "swift/SIL/BasicBlockBits.h"
6157
#include "swift/SIL/CFG.h"
6258
#include "swift/SIL/DebugUtils.h"
6359
#include "swift/SIL/InstructionUtils.h"
64-
#include "swift/SIL/Projection.h"
6560
#include "swift/SIL/LoopInfo.h"
66-
#include "swift/SIL/BasicBlockBits.h"
61+
#include "swift/SIL/Projection.h"
6762
#include "swift/SIL/SILCloner.h"
63+
#include "swift/SILOptimizer/Analysis/ArraySemantic.h"
64+
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
65+
#include "swift/SILOptimizer/PassManager/Transforms.h"
66+
#include "swift/SILOptimizer/Utils/BasicBlockOptUtils.h"
67+
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
68+
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
6869
#include "llvm/ADT/SmallSet.h"
6970
#include "llvm/Support/CommandLine.h"
7071
#include "llvm/Support/Debug.h"
@@ -85,6 +86,8 @@ class ArrayPropertiesAnalysis {
8586
SILBasicBlock *Preheader;
8687
DominanceInfo *DomTree;
8788

89+
SinkAddressProjections sinkProj;
90+
8891
llvm::DenseMap<SILFunction *, uint32_t> InstCountCache;
8992
llvm::SmallSet<SILValue, 16> HoistableArray;
9093

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

169172
bool FoundHoistable = false;
170173
uint32_t LoopInstCount = 0;
174+
171175
for (auto *BB : Loop->getBlocks()) {
172176
for (auto &Inst : *BB) {
173177
// Can't clone alloc_stack instructions whose dealloc_stack is outside
174178
// the loop.
175179
if (!Loop->canDuplicate(&Inst))
176180
return false;
177181

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

547+
SinkAddressProjections sinkProj;
539548
// Update outside used instruction values.
540549
for (auto &inst : *origBB) {
541-
for (auto result : inst.getResults())
550+
for (auto result : inst.getResults()) {
551+
bool success = sinkProj.analyzeAddressProjections(&inst);
552+
assert(success);
553+
sinkProj.cloneProjections();
542554
updateSSAForValue(origBB, result, SSAUp);
555+
}
543556
}
544557
}
545558
}

test/SILOptimizer/array_property_opt.sil

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,44 @@ 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)