|
| 1 | +//===--- FunctionSignatureTransforms.swift ---------------------------------==// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SIL |
| 14 | + |
| 15 | +/// Replace an apply with metatype arguments with an apply to a specialized function, where the |
| 16 | +/// metatype values are not passed, but rematerialized in the entry block of the specialized function |
| 17 | +/// |
| 18 | +/// ``` |
| 19 | +/// func caller() { |
| 20 | +/// callee(Int.self) |
| 21 | +/// } |
| 22 | +/// func callee(_ t: Int.Type) { // a thick metatype |
| 23 | +/// // ... |
| 24 | +/// } |
| 25 | +/// ``` |
| 26 | +/// -> |
| 27 | +/// ``` |
| 28 | +/// func caller() { |
| 29 | +/// specialized_callee() |
| 30 | +/// } |
| 31 | +/// func specialized_callee() { |
| 32 | +/// let t: Int.Type = Int.self |
| 33 | +/// // ... |
| 34 | +/// } |
| 35 | +/// // remains a thunk |
| 36 | +/// func callee(_ t: Int.Type) { |
| 37 | +/// specialized_callee() |
| 38 | +/// } |
| 39 | +/// ``` |
| 40 | +/// |
| 41 | +func specializeByRemovingMetatypeArguments(apply: FullApplySite, _ context: ModulePassContext) { |
| 42 | + guard let callee = apply.referencedFunction, |
| 43 | + !callee.isGeneric |
| 44 | + else { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + let deadArgIndices = callee.argumentTypes.enumerated() |
| 49 | + .filter { $0.element.isRemovableMetatype(in: callee) } |
| 50 | + .map { $0.offset } |
| 51 | + if deadArgIndices.isEmpty { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + let specializedFuncName = context.mangle(withDeadArguments: deadArgIndices, from: callee) |
| 56 | + |
| 57 | + let specializedCallee: Function |
| 58 | + if let existingSpecialization = context.lookupFunction(name: specializedFuncName) { |
| 59 | + specializedCallee = existingSpecialization |
| 60 | + } else { |
| 61 | + if !context.loadFunction(function: callee, loadCalleesRecursively: true) { |
| 62 | + return |
| 63 | + } |
| 64 | + specializedCallee = createSpecializedFunction(withName: specializedFuncName, |
| 65 | + withRemovedMetatypeArgumentsOf: apply, |
| 66 | + originalFunction: callee, |
| 67 | + context) |
| 68 | + } |
| 69 | + |
| 70 | + context.transform(function: apply.parentFunction) { funcContext in |
| 71 | + replace(apply: apply, to: specializedCallee, funcContext) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Creates a specialized function by moving the whole function body of `originalFunction` to the new specialized |
| 76 | +/// function and calling the specialized function in the original function (which is now a thunk). |
| 77 | +private func createSpecializedFunction( |
| 78 | + withName name: String, |
| 79 | + withRemovedMetatypeArgumentsOf apply: FullApplySite, |
| 80 | + originalFunction: Function, |
| 81 | + _ context: ModulePassContext |
| 82 | +) -> Function { |
| 83 | + let (aliveParameters, hasSelfParameter) = getAliveParameters(of: originalFunction) |
| 84 | + |
| 85 | + let specializedFunction = context.createSpecializedFunctionDeclaration( |
| 86 | + name: name, |
| 87 | + parameters: aliveParameters, |
| 88 | + hasSelfParameter: hasSelfParameter, |
| 89 | + fromOriginal: originalFunction) |
| 90 | + |
| 91 | + let thunkLoc = originalFunction.entryBlock.instructions.first!.location.autoGenerated |
| 92 | + |
| 93 | + context.moveFunctionBody(from: originalFunction, to: specializedFunction) |
| 94 | + // originalFunction is now empty and used as the thunk. |
| 95 | + let thunk = originalFunction |
| 96 | + |
| 97 | + context.transform(function: thunk) { funcContext in |
| 98 | + thunk.set(thunkKind: .signatureOptimizedThunk, funcContext) |
| 99 | + createEntryBlock(in: thunk, usingArguments: specializedFunction.arguments, funcContext) |
| 100 | + } |
| 101 | + |
| 102 | + context.transform(function: specializedFunction) { funcContext in |
| 103 | + removeMetatypArguments(in: specializedFunction, funcContext) |
| 104 | + } |
| 105 | + |
| 106 | + context.transform(function: thunk) { funcContext in |
| 107 | + createForwardingApply(to: specializedFunction, |
| 108 | + in: thunk, |
| 109 | + originalApply: apply, |
| 110 | + debugLocation: thunkLoc, |
| 111 | + funcContext) |
| 112 | + } |
| 113 | + |
| 114 | + return specializedFunction |
| 115 | +} |
| 116 | + |
| 117 | +private func getAliveParameters(of originalFunction: Function) -> ([ParameterInfo], hasSelfParameter: Bool) { |
| 118 | + let convention = originalFunction.convention |
| 119 | + var aliveParams = [ParameterInfo]() |
| 120 | + var hasSelfParameter = originalFunction.hasSelfArgument |
| 121 | + for (paramIdx, origParam) in convention.parameters.enumerated() { |
| 122 | + let argIdx = paramIdx + convention.indirectSILResultCount |
| 123 | + if !originalFunction.argumentTypes[argIdx].isRemovableMetatype(in: originalFunction) { |
| 124 | + aliveParams.append(origParam) |
| 125 | + } else if hasSelfParameter && originalFunction.selfArgumentIndex == argIdx { |
| 126 | + hasSelfParameter = false |
| 127 | + } |
| 128 | + } |
| 129 | + return (aliveParams, hasSelfParameter) |
| 130 | +} |
| 131 | + |
| 132 | +private func createEntryBlock( |
| 133 | + in function: Function, |
| 134 | + usingArguments: some Sequence<FunctionArgument>, |
| 135 | + _ context: FunctionPassContext |
| 136 | +) { |
| 137 | + let entryBlock = function.appendNewBlock(context) |
| 138 | + for arg in usingArguments { |
| 139 | + _ = entryBlock.addFunctionArgument(type: arg.type, context) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +private func removeMetatypArguments(in specializedFunction: Function, _ context: FunctionPassContext) { |
| 144 | + let entryBlock = specializedFunction.entryBlock |
| 145 | + var funcArgIdx = 0 |
| 146 | + while funcArgIdx < specializedFunction.entryBlock.arguments.count { |
| 147 | + let funcArg = specializedFunction.arguments[funcArgIdx] |
| 148 | + if funcArg.type.isRemovableMetatype(in: specializedFunction) { |
| 149 | + // Rematerialize the metatype value in the entry block. |
| 150 | + let builder = Builder(atBeginOf: entryBlock, context) |
| 151 | + let instanceType = funcArg.type.instanceTypeOfMetatype(in: specializedFunction) |
| 152 | + let metatype = builder.createMetatype(of: instanceType, representation: .Thick) |
| 153 | + funcArg.uses.replaceAll(with: metatype, context) |
| 154 | + entryBlock.eraseArgument(at: funcArgIdx, context) |
| 155 | + } else { |
| 156 | + funcArgIdx += 1 |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +private func createForwardingApply( |
| 162 | + to specializedFunction: Function, |
| 163 | + in thunk: Function, |
| 164 | + originalApply: FullApplySite, |
| 165 | + debugLocation: Location, |
| 166 | + _ context: FunctionPassContext |
| 167 | +) { |
| 168 | + let applyArgs = Array(thunk.arguments.filter { !$0.type.isRemovableMetatype(in: thunk) }) |
| 169 | + |
| 170 | + let builder = Builder(atEndOf: thunk.entryBlock, location: debugLocation, context) |
| 171 | + let callee = builder.createFunctionRef(specializedFunction) |
| 172 | + |
| 173 | + // Use the original apply as template to create the forwarding apply |
| 174 | + switch originalApply { |
| 175 | + case let ai as ApplyInst: |
| 176 | + let newApply = builder.createApply(function: callee, |
| 177 | + ai.substitutionMap, |
| 178 | + arguments: applyArgs, |
| 179 | + isNonThrowing: ai.isNonThrowing, |
| 180 | + isNonAsync: ai.isNonAsync, |
| 181 | + specializationInfo: ai.specializationInfo) |
| 182 | + let builder = Builder(after: newApply, context) |
| 183 | + builder.createReturn(of: newApply) |
| 184 | + case let tai as TryApplyInst: |
| 185 | + let normalBlock = thunk.appendNewBlock(context) |
| 186 | + let errorBlock = thunk.appendNewBlock(context) |
| 187 | + builder.createTryApply(function: callee, |
| 188 | + tai.substitutionMap, |
| 189 | + arguments: applyArgs, |
| 190 | + normalBlock: normalBlock, |
| 191 | + errorBlock: errorBlock, |
| 192 | + specializationInfo: tai.specializationInfo) |
| 193 | + let originalArg = tai.normalBlock.arguments[0] |
| 194 | + let returnVal = normalBlock.addArgument(type: originalArg.type, ownership: originalArg.ownership, context) |
| 195 | + let returnBuilder = Builder(atEndOf: normalBlock, location: debugLocation, context) |
| 196 | + returnBuilder.createReturn(of: returnVal) |
| 197 | + |
| 198 | + let errorArg = tai.errorBlock.arguments[0] |
| 199 | + let errorVal = errorBlock.addArgument(type: errorArg.type, ownership: errorArg.ownership, context) |
| 200 | + let errorBuilder = Builder(atEndOf: errorBlock, location: debugLocation, context) |
| 201 | + errorBuilder.createThrow(of: errorVal) |
| 202 | + default: |
| 203 | + fatalError("unknown full apply instruction \(originalApply)") |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +private func replace(apply: FullApplySite, to specializedCallee: Function, _ context: FunctionPassContext) { |
| 208 | + let builder = Builder(before: apply, context) |
| 209 | + let callee = builder.createFunctionRef(specializedCallee) |
| 210 | + let args = Array(apply.arguments.filter { !$0.type.isRemovableMetatype(in: apply.parentFunction) }) |
| 211 | + switch apply { |
| 212 | + case let ai as ApplyInst: |
| 213 | + let newApply = builder.createApply(function: callee, |
| 214 | + ai.substitutionMap, |
| 215 | + arguments: args, |
| 216 | + isNonThrowing: ai.isNonThrowing, |
| 217 | + isNonAsync: ai.isNonAsync, |
| 218 | + specializationInfo: ai.specializationInfo) |
| 219 | + ai.uses.replaceAll(with: newApply, context) |
| 220 | + case let tai as TryApplyInst: |
| 221 | + builder.createTryApply(function: callee, |
| 222 | + tai.substitutionMap, |
| 223 | + arguments: args, |
| 224 | + normalBlock: tai.normalBlock, |
| 225 | + errorBlock: tai.errorBlock, |
| 226 | + specializationInfo: tai.specializationInfo) |
| 227 | + default: |
| 228 | + fatalError("unknown full apply instruction \(apply)") |
| 229 | + } |
| 230 | + context.erase(instruction: apply) |
| 231 | +} |
| 232 | + |
| 233 | +private extension Type { |
| 234 | + func isRemovableMetatype(in function: Function) -> Bool { |
| 235 | + if isMetatype { |
| 236 | + if representationOfMetatype(in: function) == .Thick { |
| 237 | + let instanceTy = instanceTypeOfMetatype(in: function) |
| 238 | + // For structs and enums we know the metatype statically. |
| 239 | + return instanceTy.isStruct || instanceTy.isEnum |
| 240 | + } |
| 241 | + } |
| 242 | + return false |
| 243 | + } |
| 244 | +} |
0 commit comments