MandatoryPerformanceOptimizations: perform function signature optimization to remove metatype arguments

Generic specialization already takes care of removing metatype arguments of generic functions.
But sometimes non-generic functions have metatype arguments which must be removed.
We need handle this case with a function signature optimization.

This enables, for example, to use `OptionSet` in embedded swift.

rdar://121206953
This commit is contained in:
Erik Eckstein
2024-01-30 19:33:31 +01:00
parent 80051feb4c
commit bd7db677ed
10 changed files with 389 additions and 10 deletions

View File

@@ -52,6 +52,13 @@ private func optimizeFunctionsTopDown(using worklist: inout FunctionWorklist,
optimize(function: f, context, &worklist)
}
// Generic specialization takes care of removing metatype arguments of generic functions.
// But sometimes non-generic functions have metatype arguments which must be removed.
// We need handle this case with a function signature optimization.
removeMetatypeArgumentsInCallees(of: f, moduleContext)
worklist.addCallees(of: f)
}
}
@@ -100,6 +107,11 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ work
context.diagnosticEngine.diagnose(destroyAddr.location.sourceLoc, .deinit_not_visible)
}
case let iem as InitExistentialMetatypeInst:
if iem.uses.ignoreDebugUses.isEmpty {
context.erase(instructionIncludingDebugUses: iem)
}
default:
break
}
@@ -113,8 +125,6 @@ private func optimize(function: Function, _ context: FunctionPassContext, _ work
changed = context.optimizeMemoryAccesses(in: function) || changed
changed = context.eliminateDeadAllocations(in: function) || changed
}
worklist.add(calleesOf: function)
}
private func specializeVTableAndAddEntriesToWorklist(for type: Type, in function: Function, _ context: FunctionPassContext, _ worklist: inout FunctionWorklist) {
@@ -153,6 +163,14 @@ private func inlineAndDevirtualize(apply: FullApplySite, alreadyInlinedFunctions
}
}
private func removeMetatypeArgumentsInCallees(of function: Function, _ context: ModulePassContext) {
for inst in function.instructions {
if let apply = inst as? FullApplySite {
specializeByRemovingMetatypeArguments(apply: apply, context)
}
}
}
private func removeUnusedMetatypeInstructions(in function: Function, _ context: FunctionPassContext) {
for inst in function.instructions {
if let mt = inst as? MetatypeInst,
@@ -366,7 +384,7 @@ fileprivate struct FunctionWorklist {
}
}
mutating func add(calleesOf function: Function) {
mutating func addCallees(of function: Function) {
for inst in function.instructions {
switch inst {
case let apply as ApplySite: