Skip to content

Commit 250e268

Browse files
committed
SwiftCompilerSources: add some module-level APIs
* `Context.lookupFunction` * `ModulePassContext.loadFunction` * `ModulePassContext.createSpecializedFunctionDeclaration` * `ModulePassContext.moveFunctionBody` * `ModulePassContext.mangle(withDeadArguments:)`
1 parent fa0be3c commit 250e268

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ extension Context {
5050
}
5151

5252
func getBuiltinIntegerType(bitWidth: Int) -> Type { _bridged.getBuiltinIntegerType(bitWidth).type }
53+
54+
func lookupFunction(name: String) -> Function? {
55+
name._withBridgedStringRef {
56+
_bridged.lookupFunction($0).function
57+
}
58+
}
5359
}
5460

5561
/// A context which allows mutation of a function's SIL.

SwiftCompilerSources/Sources/Optimizer/PassManager/ModulePassContext.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,47 @@ struct ModulePassContext : Context, CustomStringConvertible {
123123
_bridged.endTransformFunction();
124124
}
125125

126+
func loadFunction(function: Function, loadCalleesRecursively: Bool) -> Bool {
127+
if function.isDefinition {
128+
return true
129+
}
130+
_bridged.loadFunction(function.bridged, loadCalleesRecursively)
131+
return function.isDefinition
132+
}
133+
134+
func createSpecializedFunctionDeclaration(
135+
name: String,
136+
parameters: [ParameterInfo],
137+
hasSelfParameter: Bool,
138+
fromOriginal originalFunction: Function
139+
) -> Function {
140+
return name._withBridgedStringRef { nameRef in
141+
let bridgedParamInfos = parameters.map { $0._bridged }
142+
return bridgedParamInfos.withUnsafeBufferPointer { paramBuf in
143+
_bridged.createSpecializedFunction(nameRef, paramBuf.baseAddress, paramBuf.count,
144+
hasSelfParameter, originalFunction.bridged).function
145+
}
146+
}
147+
}
148+
149+
func moveFunctionBody(from sourceFunc: Function, to destFunc: Function) {
150+
precondition(!destFunc.isDefinition, "cannot move body to non-empty function")
151+
_bridged.moveFunctionBody(sourceFunc.bridged, destFunc.bridged)
152+
}
153+
126154
func mangleAsyncRemoved(from function: Function) -> String {
127155
return String(taking: _bridged.mangleAsyncRemoved(function.bridged))
128156
}
157+
158+
func mangle(withDeadArguments: [Int], from function: Function) -> String {
159+
withDeadArguments.withUnsafeBufferPointer { bufPtr in
160+
bufPtr.withMemoryRebound(to: Int.self) { valPtr in
161+
String(taking: _bridged.mangleWithDeadArgs(valPtr.baseAddress,
162+
withDeadArguments.count,
163+
function.bridged))
164+
}
165+
}
166+
}
129167
}
130168

131169
extension GlobalVariable {

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ struct BridgedPassContext {
215215
bool specializeAppliesInFunction(BridgedFunction function, bool isMandatory) const;
216216
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleOutlinedVariable(BridgedFunction function) const;
217217
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleAsyncRemoved(BridgedFunction function) const;
218+
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleWithDeadArgs(const SwiftInt * _Nullable deadArgs,
219+
SwiftInt numDeadArgs,
220+
BridgedFunction function) const;
221+
218222
SWIFT_IMPORT_UNSAFE BridgedGlobalVar createGlobalVariable(BridgedStringRef name, BridgedType type,
219223
bool isPrivate) const;
220224
void inlineFunction(BridgedInstruction apply, bool mandatoryInline) const;
@@ -274,13 +278,20 @@ struct BridgedPassContext {
274278
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDefaultWitnessTable getFirstDefaultWitnessTableInModule() const;
275279
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE static OptionalBridgedDefaultWitnessTable getNextDefaultWitnessTableInModule(
276280
BridgedDefaultWitnessTable table);
281+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedFunction lookupFunction(BridgedStringRef name) const;
277282
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedFunction loadFunction(BridgedStringRef name,
278283
bool loadCalleesRecursively) const;
279284
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE void loadFunction(BridgedFunction function, bool loadCalleesRecursively) const;
280285
SWIFT_IMPORT_UNSAFE OptionalBridgedFunction lookupStdlibFunction(BridgedStringRef name) const;
281286
SWIFT_IMPORT_UNSAFE OptionalBridgedFunction lookUpNominalDeinitFunction(BridgedNominalTypeDecl nominal) const;
282287
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getContextSubstitutionMap(BridgedType type) const;
283288
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getBuiltinIntegerType(SwiftInt bitWidth) const;
289+
SWIFT_IMPORT_UNSAFE BridgedFunction createSpecializedFunction(BridgedStringRef name,
290+
const BridgedParameterInfo * _Nullable bridgedParams,
291+
SwiftInt paramCount,
292+
bool hasSelfParam,
293+
BridgedFunction fromFunc) const;
294+
void moveFunctionBody(BridgedFunction sourceFunc, BridgedFunction destFunc) const;
284295

285296
// Passmanager housekeeping
286297

include/swift/SILOptimizer/OptimizerBridgingImpl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ getNextDefaultWitnessTableInModule(BridgedDefaultWitnessTable table) {
364364
return {&*nextIter};
365365
}
366366

367+
OptionalBridgedFunction BridgedPassContext::lookupFunction(BridgedStringRef name) const {
368+
swift::SILModule *mod = invocation->getPassManager()->getModule();
369+
return {mod->lookUpFunction(name.unbridged())};
370+
}
371+
367372
OptionalBridgedFunction BridgedPassContext::loadFunction(BridgedStringRef name, bool loadCalleesRecursively) const {
368373
swift::SILModule *mod = invocation->getPassManager()->getModule();
369374
return {mod->loadFunction(name.unbridged(),

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,17 @@ BridgedOwnedString BridgedPassContext::mangleAsyncRemoved(BridgedFunction functi
16571657
return Mangler.mangle();
16581658
}
16591659

1660+
BridgedOwnedString BridgedPassContext::mangleWithDeadArgs(const SwiftInt * _Nullable deadArgs,
1661+
SwiftInt numDeadArgs,
1662+
BridgedFunction function) const {
1663+
SILFunction *f = function.getFunction();
1664+
Mangle::FunctionSignatureSpecializationMangler Mangler(Demangle::SpecializationPass::FunctionSignatureOpts, f->isSerialized(), f);
1665+
for (SwiftInt idx = 0; idx < numDeadArgs; idx++) {
1666+
Mangler.setArgumentDead((unsigned)idx);
1667+
}
1668+
return Mangler.mangle();
1669+
}
1670+
16601671
BridgedGlobalVar BridgedPassContext::createGlobalVariable(BridgedStringRef name, BridgedType type, bool isPrivate) const {
16611672
return {SILGlobalVariable::create(
16621673
*invocation->getPassManager()->getModule(),
@@ -1719,6 +1730,54 @@ bool BridgedPassContext::enableSimplificationFor(BridgedInstruction inst) const
17191730
return false;
17201731
}
17211732

1733+
BridgedFunction BridgedPassContext::
1734+
createSpecializedFunction(BridgedStringRef name,
1735+
const BridgedParameterInfo * _Nullable bridgedParams,
1736+
SwiftInt paramCount,
1737+
bool hasSelfParam,
1738+
BridgedFunction fromFunc) const {
1739+
swift::SILModule *mod = invocation->getPassManager()->getModule();
1740+
SILFunction *fromFn = fromFunc.getFunction();
1741+
1742+
llvm::SmallVector<SILParameterInfo> params;
1743+
for (unsigned idx = 0; idx < paramCount; ++idx) {
1744+
params.push_back(bridgedParams[idx].unbridged());
1745+
}
1746+
1747+
CanSILFunctionType fTy = fromFn->getLoweredFunctionType();
1748+
assert(fromFn->getGenericSignature().isNull() && "generic functions are not supported");
1749+
1750+
auto extInfo = fTy->getExtInfo();
1751+
if (fTy->hasSelfParam() && !hasSelfParam)
1752+
extInfo = extInfo.withRepresentation(SILFunctionTypeRepresentation::Thin);
1753+
1754+
CanSILFunctionType newTy = SILFunctionType::get(
1755+
/*GenericSignature=*/nullptr, extInfo, fTy->getCoroutineKind(),
1756+
fTy->getCalleeConvention(), params, fTy->getYields(),
1757+
fTy->getResults(), fTy->getOptionalErrorResult(),
1758+
SubstitutionMap(), SubstitutionMap(),
1759+
mod->getASTContext());
1760+
1761+
SILOptFunctionBuilder functionBuilder(*invocation->getTransform());
1762+
1763+
SILFunction *newF = functionBuilder.createFunction(
1764+
SILLinkage::Shared, name.unbridged(), newTy, nullptr, fromFn->getLocation(), fromFn->isBare(),
1765+
fromFn->isTransparent(), fromFn->isSerialized(), IsNotDynamic, IsNotDistributed,
1766+
IsNotRuntimeAccessible, fromFn->getEntryCount(), fromFn->isThunk(),
1767+
fromFn->getClassSubclassScope(), fromFn->getInlineStrategy(), fromFn->getEffectsKind(),
1768+
nullptr, fromFn->getDebugScope());
1769+
1770+
return {newF};
1771+
}
1772+
1773+
void BridgedPassContext::moveFunctionBody(BridgedFunction sourceFunc, BridgedFunction destFunc) const {
1774+
SILFunction *sourceFn = sourceFunc.getFunction();
1775+
SILFunction *destFn = destFunc.getFunction();
1776+
destFn->moveAllBlocksFromOtherFunction(sourceFn);
1777+
invocation->getPassManager()->invalidateAnalysis(sourceFn, SILAnalysis::InvalidationKind::Everything);
1778+
invocation->getPassManager()->invalidateAnalysis(destFn, SILAnalysis::InvalidationKind::Everything);
1779+
}
1780+
17221781
bool FullApplySite_canInline(BridgedInstruction apply) {
17231782
return swift::SILInliner::canInlineApplySite(
17241783
swift::FullApplySite(apply.unbridged()));

0 commit comments

Comments
 (0)