-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[NFC] SILGlobalVariable utilities. #16870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fd68793
SILModule::isVisibleExternally utility for VarDecls.
atrick 438d62b
Fix the SIL parser so it doesn't drop global variable decls.
atrick 8d9207e
Use custom DemangleOptions to lookup global variable identifiers.
atrick 35f0311
SILGlobalVariable utilities.
atrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ | |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "swift/SIL/SILFunction.h" | ||
#include "swift/SIL/SILGlobalVariable.h" | ||
#include "swift/SIL/SILInstruction.h" | ||
#include "swift/SIL/SILLinkage.h" | ||
#include "swift/SIL/SILModule.h" | ||
|
||
|
@@ -137,3 +139,132 @@ ClangNode SILGlobalVariable::getClangNode() const { | |
const clang::Decl *SILGlobalVariable::getClangDecl() const { | ||
return (VDecl ? VDecl->getClangDecl() : nullptr); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Utilities for verification and optimization. | ||
//===----------------------------------------------------------------------===// | ||
|
||
static SILGlobalVariable *getStaticallyInitializedVariable(SILFunction *AddrF) { | ||
if (AddrF->isExternalDeclaration()) | ||
return nullptr; | ||
|
||
auto *RetInst = cast<ReturnInst>(AddrF->findReturnBB()->getTerminator()); | ||
auto *API = dyn_cast<AddressToPointerInst>(RetInst->getOperand()); | ||
if (!API) | ||
return nullptr; | ||
auto *GAI = dyn_cast<GlobalAddrInst>(API->getOperand()); | ||
if (!GAI) | ||
return nullptr; | ||
|
||
return GAI->getReferencedGlobal(); | ||
} | ||
|
||
SILGlobalVariable *swift::getVariableOfGlobalInit(SILFunction *AddrF) { | ||
if (!AddrF->isGlobalInit()) | ||
return nullptr; | ||
|
||
if (auto *SILG = getStaticallyInitializedVariable(AddrF)) | ||
return SILG; | ||
|
||
// If the addressor contains a single "once" call, it calls globalinit_func, | ||
// and the globalinit_func is called by "once" from a single location, | ||
// continue; otherwise bail. | ||
BuiltinInst *CallToOnce; | ||
auto *InitF = findInitializer(&AddrF->getModule(), AddrF, CallToOnce); | ||
|
||
if (!InitF || !InitF->getName().startswith("globalinit_")) | ||
return nullptr; | ||
|
||
// If the globalinit_func is trivial, continue; otherwise bail. | ||
SingleValueInstruction *dummyInitVal; | ||
auto *SILG = getVariableOfStaticInitializer(InitF, dummyInitVal); | ||
|
||
return SILG; | ||
} | ||
|
||
SILFunction *swift::getCalleeOfOnceCall(BuiltinInst *BI) { | ||
assert(BI->getNumOperands() == 2 && "once call should have 2 operands."); | ||
|
||
auto Callee = BI->getOperand(1); | ||
assert(Callee->getType().castTo<SILFunctionType>()->getRepresentation() | ||
== SILFunctionTypeRepresentation::CFunctionPointer && | ||
"Expected C function representation!"); | ||
|
||
if (auto *FR = dyn_cast<FunctionRefInst>(Callee)) | ||
return FR->getReferencedFunction(); | ||
|
||
return nullptr; | ||
} | ||
|
||
// Find the globalinit_func by analyzing the body of the addressor. | ||
SILFunction *swift::findInitializer(SILModule *Module, SILFunction *AddrF, | ||
BuiltinInst *&CallToOnce) { | ||
// We only handle a single SILBasicBlock for now. | ||
if (AddrF->size() != 1) | ||
return nullptr; | ||
|
||
CallToOnce = nullptr; | ||
SILBasicBlock *BB = &AddrF->front(); | ||
for (auto &I : *BB) { | ||
// Find the builtin "once" call. | ||
if (auto *BI = dyn_cast<BuiltinInst>(&I)) { | ||
const BuiltinInfo &Builtin = | ||
BI->getModule().getBuiltinInfo(BI->getName()); | ||
if (Builtin.ID != BuiltinValueKind::Once) | ||
continue; | ||
|
||
// Bail if we have multiple "once" calls in the addressor. | ||
if (CallToOnce) | ||
return nullptr; | ||
|
||
CallToOnce = BI; | ||
} | ||
} | ||
if (!CallToOnce) | ||
return nullptr; | ||
return getCalleeOfOnceCall(CallToOnce); | ||
} | ||
|
||
SILGlobalVariable * | ||
swift::getVariableOfStaticInitializer(SILFunction *InitFunc, | ||
SingleValueInstruction *&InitVal) { | ||
InitVal = nullptr; | ||
SILGlobalVariable *GVar = nullptr; | ||
// We only handle a single SILBasicBlock for now. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this comment and this check repeated here and in |
||
if (InitFunc->size() != 1) | ||
return nullptr; | ||
|
||
SILBasicBlock *BB = &InitFunc->front(); | ||
GlobalAddrInst *SGA = nullptr; | ||
bool HasStore = false; | ||
for (auto &I : *BB) { | ||
// Make sure we have a single GlobalAddrInst and a single StoreInst. | ||
// And the StoreInst writes to the GlobalAddrInst. | ||
if (isa<AllocGlobalInst>(&I) || isa<ReturnInst>(&I) | ||
|| isa<DebugValueInst>(&I)) { | ||
continue; | ||
} else if (auto *sga = dyn_cast<GlobalAddrInst>(&I)) { | ||
if (SGA) | ||
return nullptr; | ||
SGA = sga; | ||
GVar = SGA->getReferencedGlobal(); | ||
} else if (auto *SI = dyn_cast<StoreInst>(&I)) { | ||
if (HasStore || SI->getDest() != SGA) | ||
return nullptr; | ||
HasStore = true; | ||
SILValue value = SI->getSrc(); | ||
|
||
// We only handle StructInst and TupleInst being stored to a | ||
// global variable for now. | ||
if (!isa<StructInst>(value) && !isa<TupleInst>(value)) | ||
return nullptr; | ||
InitVal = cast<SingleValueInstruction>(value); | ||
} else if (!SILGlobalVariable::isValidStaticInitializerInst(&I, | ||
I.getModule())) { | ||
return nullptr; | ||
} | ||
} | ||
if (!InitVal) | ||
return nullptr; | ||
return GVar; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please assert based on
getBuiltinInfo().ID
of theBuiltinInst
? cleaner and safer assertion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. This is all super old code that I just move out of SILOptimizer so it can be used to verify well-formed SIL.