mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
SwiftCompilerSources: add some module-level APIs
* `Context.lookupFunction` * `ModulePassContext.loadFunction` * `ModulePassContext.createSpecializedFunctionDeclaration` * `ModulePassContext.moveFunctionBody` * `ModulePassContext.mangle(withDeadArguments:)`
This commit is contained in:
@@ -50,6 +50,12 @@ extension Context {
|
||||
}
|
||||
|
||||
func getBuiltinIntegerType(bitWidth: Int) -> Type { _bridged.getBuiltinIntegerType(bitWidth).type }
|
||||
|
||||
func lookupFunction(name: String) -> Function? {
|
||||
name._withBridgedStringRef {
|
||||
_bridged.lookupFunction($0).function
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A context which allows mutation of a function's SIL.
|
||||
|
||||
@@ -123,9 +123,47 @@ struct ModulePassContext : Context, CustomStringConvertible {
|
||||
_bridged.endTransformFunction();
|
||||
}
|
||||
|
||||
func loadFunction(function: Function, loadCalleesRecursively: Bool) -> Bool {
|
||||
if function.isDefinition {
|
||||
return true
|
||||
}
|
||||
_bridged.loadFunction(function.bridged, loadCalleesRecursively)
|
||||
return function.isDefinition
|
||||
}
|
||||
|
||||
func createSpecializedFunctionDeclaration(
|
||||
name: String,
|
||||
parameters: [ParameterInfo],
|
||||
hasSelfParameter: Bool,
|
||||
fromOriginal originalFunction: Function
|
||||
) -> Function {
|
||||
return name._withBridgedStringRef { nameRef in
|
||||
let bridgedParamInfos = parameters.map { $0._bridged }
|
||||
return bridgedParamInfos.withUnsafeBufferPointer { paramBuf in
|
||||
_bridged.createSpecializedFunction(nameRef, paramBuf.baseAddress, paramBuf.count,
|
||||
hasSelfParameter, originalFunction.bridged).function
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func moveFunctionBody(from sourceFunc: Function, to destFunc: Function) {
|
||||
precondition(!destFunc.isDefinition, "cannot move body to non-empty function")
|
||||
_bridged.moveFunctionBody(sourceFunc.bridged, destFunc.bridged)
|
||||
}
|
||||
|
||||
func mangleAsyncRemoved(from function: Function) -> String {
|
||||
return String(taking: _bridged.mangleAsyncRemoved(function.bridged))
|
||||
}
|
||||
|
||||
func mangle(withDeadArguments: [Int], from function: Function) -> String {
|
||||
withDeadArguments.withUnsafeBufferPointer { bufPtr in
|
||||
bufPtr.withMemoryRebound(to: Int.self) { valPtr in
|
||||
String(taking: _bridged.mangleWithDeadArgs(valPtr.baseAddress,
|
||||
withDeadArguments.count,
|
||||
function.bridged))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension GlobalVariable {
|
||||
|
||||
@@ -215,6 +215,10 @@ struct BridgedPassContext {
|
||||
bool specializeAppliesInFunction(BridgedFunction function, bool isMandatory) const;
|
||||
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleOutlinedVariable(BridgedFunction function) const;
|
||||
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleAsyncRemoved(BridgedFunction function) const;
|
||||
SWIFT_IMPORT_UNSAFE BridgedOwnedString mangleWithDeadArgs(const SwiftInt * _Nullable deadArgs,
|
||||
SwiftInt numDeadArgs,
|
||||
BridgedFunction function) const;
|
||||
|
||||
SWIFT_IMPORT_UNSAFE BridgedGlobalVar createGlobalVariable(BridgedStringRef name, BridgedType type,
|
||||
bool isPrivate) const;
|
||||
void inlineFunction(BridgedInstruction apply, bool mandatoryInline) const;
|
||||
@@ -274,6 +278,7 @@ struct BridgedPassContext {
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDefaultWitnessTable getFirstDefaultWitnessTableInModule() const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE static OptionalBridgedDefaultWitnessTable getNextDefaultWitnessTableInModule(
|
||||
BridgedDefaultWitnessTable table);
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedFunction lookupFunction(BridgedStringRef name) const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedFunction loadFunction(BridgedStringRef name,
|
||||
bool loadCalleesRecursively) const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE void loadFunction(BridgedFunction function, bool loadCalleesRecursively) const;
|
||||
@@ -281,6 +286,12 @@ struct BridgedPassContext {
|
||||
SWIFT_IMPORT_UNSAFE OptionalBridgedFunction lookUpNominalDeinitFunction(BridgedNominalTypeDecl nominal) const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap getContextSubstitutionMap(BridgedType type) const;
|
||||
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getBuiltinIntegerType(SwiftInt bitWidth) const;
|
||||
SWIFT_IMPORT_UNSAFE BridgedFunction createSpecializedFunction(BridgedStringRef name,
|
||||
const BridgedParameterInfo * _Nullable bridgedParams,
|
||||
SwiftInt paramCount,
|
||||
bool hasSelfParam,
|
||||
BridgedFunction fromFunc) const;
|
||||
void moveFunctionBody(BridgedFunction sourceFunc, BridgedFunction destFunc) const;
|
||||
|
||||
// Passmanager housekeeping
|
||||
|
||||
|
||||
@@ -364,6 +364,11 @@ getNextDefaultWitnessTableInModule(BridgedDefaultWitnessTable table) {
|
||||
return {&*nextIter};
|
||||
}
|
||||
|
||||
OptionalBridgedFunction BridgedPassContext::lookupFunction(BridgedStringRef name) const {
|
||||
swift::SILModule *mod = invocation->getPassManager()->getModule();
|
||||
return {mod->lookUpFunction(name.unbridged())};
|
||||
}
|
||||
|
||||
OptionalBridgedFunction BridgedPassContext::loadFunction(BridgedStringRef name, bool loadCalleesRecursively) const {
|
||||
swift::SILModule *mod = invocation->getPassManager()->getModule();
|
||||
return {mod->loadFunction(name.unbridged(),
|
||||
|
||||
@@ -1657,6 +1657,17 @@ BridgedOwnedString BridgedPassContext::mangleAsyncRemoved(BridgedFunction functi
|
||||
return Mangler.mangle();
|
||||
}
|
||||
|
||||
BridgedOwnedString BridgedPassContext::mangleWithDeadArgs(const SwiftInt * _Nullable deadArgs,
|
||||
SwiftInt numDeadArgs,
|
||||
BridgedFunction function) const {
|
||||
SILFunction *f = function.getFunction();
|
||||
Mangle::FunctionSignatureSpecializationMangler Mangler(Demangle::SpecializationPass::FunctionSignatureOpts, f->isSerialized(), f);
|
||||
for (SwiftInt idx = 0; idx < numDeadArgs; idx++) {
|
||||
Mangler.setArgumentDead((unsigned)idx);
|
||||
}
|
||||
return Mangler.mangle();
|
||||
}
|
||||
|
||||
BridgedGlobalVar BridgedPassContext::createGlobalVariable(BridgedStringRef name, BridgedType type, bool isPrivate) const {
|
||||
return {SILGlobalVariable::create(
|
||||
*invocation->getPassManager()->getModule(),
|
||||
@@ -1719,6 +1730,54 @@ bool BridgedPassContext::enableSimplificationFor(BridgedInstruction inst) const
|
||||
return false;
|
||||
}
|
||||
|
||||
BridgedFunction BridgedPassContext::
|
||||
createSpecializedFunction(BridgedStringRef name,
|
||||
const BridgedParameterInfo * _Nullable bridgedParams,
|
||||
SwiftInt paramCount,
|
||||
bool hasSelfParam,
|
||||
BridgedFunction fromFunc) const {
|
||||
swift::SILModule *mod = invocation->getPassManager()->getModule();
|
||||
SILFunction *fromFn = fromFunc.getFunction();
|
||||
|
||||
llvm::SmallVector<SILParameterInfo> params;
|
||||
for (unsigned idx = 0; idx < paramCount; ++idx) {
|
||||
params.push_back(bridgedParams[idx].unbridged());
|
||||
}
|
||||
|
||||
CanSILFunctionType fTy = fromFn->getLoweredFunctionType();
|
||||
assert(fromFn->getGenericSignature().isNull() && "generic functions are not supported");
|
||||
|
||||
auto extInfo = fTy->getExtInfo();
|
||||
if (fTy->hasSelfParam() && !hasSelfParam)
|
||||
extInfo = extInfo.withRepresentation(SILFunctionTypeRepresentation::Thin);
|
||||
|
||||
CanSILFunctionType newTy = SILFunctionType::get(
|
||||
/*GenericSignature=*/nullptr, extInfo, fTy->getCoroutineKind(),
|
||||
fTy->getCalleeConvention(), params, fTy->getYields(),
|
||||
fTy->getResults(), fTy->getOptionalErrorResult(),
|
||||
SubstitutionMap(), SubstitutionMap(),
|
||||
mod->getASTContext());
|
||||
|
||||
SILOptFunctionBuilder functionBuilder(*invocation->getTransform());
|
||||
|
||||
SILFunction *newF = functionBuilder.createFunction(
|
||||
SILLinkage::Shared, name.unbridged(), newTy, nullptr, fromFn->getLocation(), fromFn->isBare(),
|
||||
fromFn->isTransparent(), fromFn->isSerialized(), IsNotDynamic, IsNotDistributed,
|
||||
IsNotRuntimeAccessible, fromFn->getEntryCount(), fromFn->isThunk(),
|
||||
fromFn->getClassSubclassScope(), fromFn->getInlineStrategy(), fromFn->getEffectsKind(),
|
||||
nullptr, fromFn->getDebugScope());
|
||||
|
||||
return {newF};
|
||||
}
|
||||
|
||||
void BridgedPassContext::moveFunctionBody(BridgedFunction sourceFunc, BridgedFunction destFunc) const {
|
||||
SILFunction *sourceFn = sourceFunc.getFunction();
|
||||
SILFunction *destFn = destFunc.getFunction();
|
||||
destFn->moveAllBlocksFromOtherFunction(sourceFn);
|
||||
invocation->getPassManager()->invalidateAnalysis(sourceFn, SILAnalysis::InvalidationKind::Everything);
|
||||
invocation->getPassManager()->invalidateAnalysis(destFn, SILAnalysis::InvalidationKind::Everything);
|
||||
}
|
||||
|
||||
bool FullApplySite_canInline(BridgedInstruction apply) {
|
||||
return swift::SILInliner::canInlineApplySite(
|
||||
swift::FullApplySite(apply.unbridged()));
|
||||
|
||||
Reference in New Issue
Block a user