-
Notifications
You must be signed in to change notification settings - Fork 851
Improve indirect call effects in GlobalEffects #8644
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
Open
stevenfontanella
wants to merge
3
commits into
main
Choose a base branch
from
indirect-effects-address
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| #include <ranges> | ||
|
|
||
| #include "ir/effects.h" | ||
| #include "ir/element-utils.h" | ||
| #include "ir/module-utils.h" | ||
| #include "pass.h" | ||
| #include "support/graph_traversal.h" | ||
|
|
@@ -47,6 +48,56 @@ struct FuncInfo { | |
| std::unordered_set<HeapType> indirectCalledTypes; | ||
| }; | ||
|
|
||
| /* | ||
| Only funcs that are 'addressed' may be the target of an indirect call. A | ||
| function is addressed if: | ||
| - It appears in a ref.func expression | ||
| - It appears in an `elem` segment (note that we already ignore `elem declare` | ||
| statements in our IR, but we check separately for funcs that appear in | ||
| `ref.func`). | ||
| - It's exported, because it may flow back to us as a reference. | ||
| - It's imported, which implies it is `elem declare`d. | ||
|
|
||
| If a function doesn't meet any of these criteria, it can't be the target of | ||
| an indirect call and we don't need to include its effects in indirect calls. | ||
| */ | ||
| std::unordered_set<Function*> getAddressedFuncs(Module& module) { | ||
| struct AddressedFuncsWalker : PostWalker<AddressedFuncsWalker> { | ||
| std::unordered_set<Function*>& addressedFuncs; | ||
|
|
||
| AddressedFuncsWalker(std::unordered_set<Function*>& addressedFuncs) | ||
| : addressedFuncs(addressedFuncs) {} | ||
|
|
||
| void visitRefFunc(RefFunc* refFunc) { | ||
| addressedFuncs.insert(getModule()->getFunction(refFunc->func)); | ||
| } | ||
| }; | ||
|
|
||
| std::unordered_set<Function*> addressedFuncs; | ||
| AddressedFuncsWalker walker(addressedFuncs); | ||
| walker.walkModule(&module); | ||
|
|
||
| ModuleUtils::iterImportedFunctions( | ||
| module, [&addressedFuncs, &module](Function* import) { | ||
| addressedFuncs.insert(module.getFunction(import->name)); | ||
| }); | ||
|
|
||
| for (const auto& export_ : module.exports) { | ||
| if (export_->kind != ExternalKind::Function) { | ||
| continue; | ||
| } | ||
|
|
||
| addressedFuncs.insert(module.getFunction(*export_->getInternalName())); | ||
| } | ||
|
|
||
| ElementUtils::iterAllElementFunctionNames( | ||
| &module, [&addressedFuncs, &module](Name func) { | ||
| addressedFuncs.insert(module.getFunction(func)); | ||
| }); | ||
|
Comment on lines
+93
to
+96
Member
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. No need to do this separately. Walking the module code will already walk all the element segments and find the |
||
|
|
||
| return addressedFuncs; | ||
| } | ||
|
|
||
| std::map<Function*, FuncInfo> analyzeFuncs(Module& module, | ||
| const PassOptions& passOptions) { | ||
| ModuleUtils::ParallelFunctionAnalysis<FuncInfo> analysis( | ||
|
|
@@ -146,6 +197,7 @@ using CallGraph = | |
|
|
||
| CallGraph buildCallGraph(const Module& module, | ||
| const std::map<Function*, FuncInfo>& funcInfos, | ||
| const std::unordered_set<Function*>& addressedFuncs, | ||
| bool closedWorld) { | ||
| CallGraph callGraph; | ||
| if (!closedWorld) { | ||
|
|
@@ -181,7 +233,9 @@ CallGraph buildCallGraph(const Module& module, | |
| } | ||
|
|
||
| // Type -> Function | ||
| callGraph[caller->type.getHeapType()].insert(caller); | ||
| if (addressedFuncs.contains(caller)) { | ||
| callGraph[caller->type.getHeapType()].insert(caller); | ||
| } | ||
| } | ||
|
|
||
| // Type -> Type | ||
|
|
@@ -345,8 +399,10 @@ struct GenerateGlobalEffects : public Pass { | |
| std::map<Function*, FuncInfo> funcInfos = | ||
| analyzeFuncs(*module, getPassOptions()); | ||
|
|
||
| auto callGraph = | ||
| buildCallGraph(*module, funcInfos, getPassOptions().closedWorld); | ||
| auto addressedFuncs = getAddressedFuncs(*module); | ||
|
|
||
| auto callGraph = buildCallGraph( | ||
| *module, funcInfos, addressedFuncs, getPassOptions().closedWorld); | ||
|
|
||
| propagateEffects(*module, getPassOptions(), funcInfos, callGraph); | ||
|
|
||
|
|
||
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.
Might as well make this a
WalkerPassso it can be run on all the functions in parallel. Then instead ofwalkModulebelow, you would usewalkModuleCode. Alternatively you could useModuleUtils::ParallelFunctionAnalysis+walkModuleCode.