Skip to content

Commit 585395b

Browse files
committed
Swift Optimizer: add Context.lookupStdlibFunction
1 parent 38de5b1 commit 585395b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,16 @@ struct FunctionPassContext : MutatingContext {
211211
return function.isDefinition
212212
}
213213

214+
/// Looks up a function in the `Swift` module.
215+
/// The `name` is the source name of the function and not the mangled name.
216+
/// Returns nil if no such function or multiple matching functions are found.
217+
func lookupStdlibFunction(name: StaticString) -> Function? {
218+
return name.withUTF8Buffer { (nameBuffer: UnsafeBufferPointer<UInt8>) in
219+
let nameStr = llvm.StringRef(nameBuffer.baseAddress, nameBuffer.count)
220+
return _bridged.lookupStdlibFunction(nameStr).function
221+
}
222+
}
223+
214224
func erase(block: BasicBlock) {
215225
_bridged.eraseBlock(block.bridged)
216226
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ struct BridgedPassContext {
425425
: swift::SILModule::LinkingMode::LinkNormal);
426426
}
427427

428+
SWIFT_IMPORT_UNSAFE
429+
OptionalBridgedFunction lookupStdlibFunction(llvm::StringRef name) const;
430+
428431
SWIFT_IMPORT_UNSAFE
429432
swift::SubstitutionMap getContextSubstitutionMap(swift::SILType type) const {
430433
auto *ntd = type.getASTType()->getAnyNominal();

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,22 @@ void BridgedPassContext::fixStackNesting(BridgedFunction function) const {
15251525
invocation->setNeedFixStackNesting(false);
15261526
}
15271527

1528+
OptionalBridgedFunction BridgedPassContext::lookupStdlibFunction(llvm::StringRef name) const {
1529+
swift::SILModule *mod = invocation->getPassManager()->getModule();
1530+
SmallVector<ValueDecl *, 1> results;
1531+
mod->getASTContext().lookupInSwiftModule(name, results);
1532+
if (results.size() != 1)
1533+
return {nullptr};
1534+
1535+
auto *decl = dyn_cast<FuncDecl>(results.front());
1536+
if (!decl)
1537+
return {nullptr};
1538+
1539+
SILDeclRef declRef(decl, SILDeclRef::Kind::Func);
1540+
SILOptFunctionBuilder funcBuilder(*invocation->getTransform());
1541+
return {funcBuilder.getOrCreateFunction(SILLocation(decl), declRef, NotForDefinition)};
1542+
}
1543+
15281544
bool BridgedPassContext::enableSimplificationFor(BridgedInstruction inst) const {
15291545
// Fast-path check.
15301546
if (SimplifyInstructionTest.empty() && SILDisablePass.empty())

0 commit comments

Comments
 (0)