Skip to content

Commit 1042ab1

Browse files
committed
MandatoryPerformanceOptimizations: use eliminateRedundantLoads instead of optimizeMemoryAccesses to optimize redundant loads
1 parent 285ad7f commit 1042ab1

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

Diff for: SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

+15-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
159159
{
160160
fri.referencedFunction.set(linkage: .public, moduleContext)
161161
}
162+
163+
case let copy as CopyAddrInst:
164+
if function.isGlobalInitOnceFunction, copy.source.type.isLoadable(in: function) {
165+
// In global init functions we have to make sure that redundant load elimination can remove all
166+
// loads (from temporary stack locations) so that globals can be statically initialized.
167+
// For this it's necessary to load copy_addr instructions to loads and stores.
168+
copy.replaceWithLoadAndStore(simplifyCtxt)
169+
}
162170

163171
default:
164172
break
@@ -170,7 +178,13 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ modu
170178
removeUnusedMetatypeInstructions(in: function, context)
171179

172180
// If this is a just specialized function, try to optimize copy_addr, etc.
173-
changed = context.optimizeMemoryAccesses(in: function) || changed
181+
if eliminateRedundantLoads(in: function,
182+
variant: function.isGlobalInitOnceFunction ? .mandatoryInGlobalInit : .mandatory,
183+
context)
184+
{
185+
changed = true
186+
}
187+
174188
changed = context.eliminateDeadAllocations(in: function) || changed
175189
}
176190
}

Diff for: SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

-8
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,6 @@ struct FunctionPassContext : MutatingContext {
346346
_bridged.asNotificationHandler().notifyChanges(.effectsChanged)
347347
}
348348

349-
func optimizeMemoryAccesses(in function: Function) -> Bool {
350-
if _bridged.optimizeMemoryAccesses(function.bridged) {
351-
notifyInstructionsChanged()
352-
return true
353-
}
354-
return false
355-
}
356-
357349
func eliminateDeadAllocations(in function: Function) -> Bool {
358350
if _bridged.eliminateDeadAllocations(function.bridged) {
359351
notifyInstructionsChanged()

Diff for: include/swift/SILOptimizer/OptimizerBridging.h

-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ struct BridgedPassContext {
259259
BridgedLinkage linkage, bool isLet) const;
260260
void inlineFunction(BridgedInstruction apply, bool mandatoryInline) const;
261261
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue getSILUndef(BridgedType type) const;
262-
BRIDGED_INLINE bool optimizeMemoryAccesses(BridgedFunction f) const;
263262
BRIDGED_INLINE bool eliminateDeadAllocations(BridgedFunction f) const;
264263

265264
BRIDGED_INLINE bool shouldExpand(BridgedType type) const;

Diff for: include/swift/SILOptimizer/OptimizerBridgingImpl.h

-3
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ BridgedValue BridgedPassContext::getSILUndef(BridgedType type) const {
260260
return {swift::SILUndef::get(invocation->getFunction(), type.unbridged())};
261261
}
262262

263-
bool BridgedPassContext::optimizeMemoryAccesses(BridgedFunction f) const {
264-
return swift::optimizeMemoryAccesses(f.getFunction());
265-
}
266263
bool BridgedPassContext::eliminateDeadAllocations(BridgedFunction f) const {
267264
return swift::eliminateDeadAllocations(f.getFunction(),
268265
this->getDomTree().di);

0 commit comments

Comments
 (0)