Skip to content

Commit 787c996

Browse files
committed
LargeTypesReg2Mem: Add a new heuristic that trys harder to keep large
values on the stack This heuristic can be enabled by passing -Xfrontend -enable-aggressive-reg2mem. rdar://123916109
1 parent 5f396cd commit 787c996

File tree

8 files changed

+367
-52
lines changed

8 files changed

+367
-52
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/DeadStoreElimination.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ let deadStoreElimination = FunctionPass(name: "dead-store-elimination") {
7575
}
7676

7777
private func tryEliminate(store: StoreInst, complexityBudget: inout Int, _ context: FunctionPassContext) {
78-
if !store.hasValidOwnershipForDeadStoreElimination {
78+
if !store.hasValidOwnershipForDeadStoreElimination || !store.source.type.shouldExpand(context) {
7979
return
8080
}
8181

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/RedundantLoadElimination.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ private func eliminateRedundantLoads(in function: Function, ignoreArrays: Bool,
9797
{
9898
continue
9999
}
100+
if !load.type.shouldExpand(context) {
101+
continue
102+
}
100103
tryEliminate(load: load, complexityBudget: &complexityBudget, context)
101104
}
102105
}

SwiftCompilerSources/Sources/Optimizer/PassManager/Options.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ struct Options {
2020
_bridged.enableStackProtection()
2121
}
2222

23+
var useAggressiveReg2MemForCodeSize : Bool {
24+
_bridged.useAggressiveReg2MemForCodeSize()
25+
}
26+
2327
var enableMoveInoutStackProtection: Bool {
2428
_bridged.enableMoveInoutStackProtection()
2529
}

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,12 @@ extension CheckedCastAddrBranchInst {
809809
}
810810
}
811811
}
812+
813+
extension Type {
814+
func shouldExpand(_ context: some Context) -> Bool {
815+
if !context.options.useAggressiveReg2MemForCodeSize {
816+
return true
817+
}
818+
return context._bridged.shouldExpand(self.bridged)
819+
}
820+
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ struct BridgedPassContext {
261261
BRIDGED_INLINE bool optimizeMemoryAccesses(BridgedFunction f) const;
262262
BRIDGED_INLINE bool eliminateDeadAllocations(BridgedFunction f) const;
263263

264+
BRIDGED_INLINE bool shouldExpand(BridgedType type) const;
265+
264266
// IRGen
265267

266268
SwiftInt getStaticSize(BridgedType type) const;
@@ -372,6 +374,7 @@ struct BridgedPassContext {
372374
Unchecked = 2
373375
};
374376

377+
BRIDGED_INLINE bool useAggressiveReg2MemForCodeSize() const;
375378
BRIDGED_INLINE bool enableStackProtection() const;
376379
BRIDGED_INLINE bool hasFeature(BridgedFeature feature) const;
377380
BRIDGED_INLINE bool enableMoveInoutStackProtection() const;

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,21 @@ bool BridgedPassContext::enableMoveInoutStackProtection() const {
556556
return mod->getOptions().EnableMoveInoutStackProtection;
557557
}
558558

559+
bool BridgedPassContext::useAggressiveReg2MemForCodeSize() const {
560+
swift::SILModule *mod = invocation->getPassManager()->getModule();
561+
return mod->getOptions().UseAggressiveReg2MemForCodeSize;
562+
}
563+
559564
BridgedPassContext::AssertConfiguration BridgedPassContext::getAssertConfiguration() const {
560565
swift::SILModule *mod = invocation->getPassManager()->getModule();
561566
return (AssertConfiguration)mod->getOptions().AssertConfig;
562567
}
563568

569+
bool BridgedPassContext::shouldExpand(BridgedType ty) const {
570+
swift::SILModule &mod = *invocation->getPassManager()->getModule();
571+
return swift::shouldExpand(mod, ty.unbridged());
572+
}
573+
564574
static_assert((int)BridgedPassContext::SILStage::Raw == (int)swift::SILStage::Raw);
565575
static_assert((int)BridgedPassContext::SILStage::Canonical == (int)swift::SILStage::Canonical);
566576
static_assert((int)BridgedPassContext::SILStage::Lowered == (int)swift::SILStage::Lowered);

0 commit comments

Comments
 (0)