Skip to content

Commit 538d730

Browse files
committed
MandatoryPerformanceOptimizations: better handling of statically initialized globals which contain a pointer to another global
E.g. ``` var p = Point(x: 10, y: 20) let o = UnsafePointer(&p) ``` MandatoryPerformanceOptimization must inline the accessor to the referenced global.
1 parent 7e33e55 commit 538d730

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ private extension Value {
225225
var singleUseValue: any Value = self
226226
var path = SmallProjectionPath()
227227
while true {
228-
guard let use = singleUseValue.uses.singleNonDebugUse else {
228+
guard let use = singleUseValue.uses.singleRelevantUse else {
229229
return nil
230230
}
231231

@@ -247,6 +247,8 @@ private extension Value {
247247
default:
248248
return nil
249249
}
250+
case is PointerToAddressInst, is AddressToPointerInst, is BeginAccessInst:
251+
break
250252
default:
251253
return nil
252254
}
@@ -334,3 +336,27 @@ fileprivate struct FunctionWorklist {
334336
}
335337
}
336338
}
339+
340+
private extension UseList {
341+
var singleRelevantUse: Operand? {
342+
var singleUse: Operand?
343+
for use in self {
344+
switch use.instruction {
345+
case is DebugValueInst,
346+
// The initializer value of a global can contain access instructions if it references another
347+
// global variable by address, e.g.
348+
// var p = Point(x: 10, y: 20)
349+
// let o = UnsafePointer(&p)
350+
// Therefore ignore the `end_access` use of a `begin_access`.
351+
is EndAccessInst:
352+
continue
353+
default:
354+
if singleUse != nil {
355+
return nil
356+
}
357+
singleUse = use
358+
}
359+
}
360+
return singleUse
361+
}
362+
}

0 commit comments

Comments
 (0)