-
Notifications
You must be signed in to change notification settings - Fork 860
MarkJSCalled pass #8733
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
MarkJSCalled pass #8733
Changes from all commits
Commits
Show all changes
7 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
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright 2026 WebAssembly Community Group participants | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // | ||
| // Users should mark JS-called functions using @binaryen.js.called. This pass | ||
| // helps by auto-marking them where possible. The main thing this does is to | ||
| // find any configureAll calls and mark the functions referred to there. | ||
| // | ||
| // We do automatically handle configureAll in the start function (in | ||
| // intrinsics.cpp), so this pass is only needed for other uses of configureAll, | ||
| // like from an export. | ||
| // | ||
|
|
||
| #include "ir/find_all.h" | ||
| #include "ir/intrinsics.h" | ||
| #include "ir/module-utils.h" | ||
| #include "pass.h" | ||
| #include "wasm.h" | ||
|
|
||
| namespace wasm { | ||
|
|
||
| struct MarkJSCalled : public Pass { | ||
| void run(Module* module) override { | ||
| Intrinsics intrinsics(*module); | ||
|
|
||
| // See if there even is a configureAll. | ||
| auto hasConfigureAll = false; | ||
| for (auto& func : module->functions) { | ||
| if (intrinsics.isConfigureAll(func.get())) { | ||
| hasConfigureAll = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!hasConfigureAll) { | ||
| return; | ||
| } | ||
|
|
||
| using JSCalledSet = std::unordered_set<Name>; | ||
|
|
||
| ModuleUtils::ParallelFunctionAnalysis<JSCalledSet> analysis( | ||
| *module, [&](Function* func, JSCalledSet& jsCalled) { | ||
| if (func->imported()) { | ||
| return; | ||
| } | ||
|
|
||
| FindAll<Call> calls(func->body); | ||
| for (auto* call : calls.list) { | ||
| if (intrinsics.isConfigureAll(call)) { | ||
| for (auto name : intrinsics.getConfigureAllFunctions(call)) { | ||
| jsCalled.insert(name); | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| for (auto& [_, jsCalled] : analysis.map) { | ||
| for (auto name : jsCalled) { | ||
| module->getFunction(name)->funcAnnotations.jsCalled = true; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| Pass* createMarkJSCalledPass() { return new MarkJSCalled(); } | ||
|
|
||
| } // namespace wasm | ||
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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| ;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. | ||
|
|
||
| ;; RUN: foreach %s %t wasm-opt --mark-js-called -all -S -o - | filecheck %s | ||
|
|
||
| ;; $configured will be marked as @binaryen.js.called. $already is already marked, | ||
| ;; and nothing changess. $unconfigured* are not in configureAll so they are left | ||
| ;; alone. | ||
|
|
||
| (module | ||
| ;; CHECK: (type $0 (func)) | ||
|
|
||
| ;; CHECK: (type $externs (array (mut externref))) | ||
| (type $externs (array (mut externref))) | ||
|
|
||
| ;; CHECK: (type $funcs (array (mut funcref))) | ||
| (type $funcs (array (mut funcref))) | ||
|
|
||
| ;; CHECK: (type $bytes (array (mut i8))) | ||
| (type $bytes (array (mut i8))) | ||
|
|
||
| ;; CHECK: (type $configureAll (func (param (ref null $externs) (ref null $funcs) (ref null $bytes) externref))) | ||
| (type $configureAll (func (param (ref null $externs)) (param (ref null $funcs)) (param (ref null $bytes)) (param externref))) | ||
|
|
||
| ;; CHECK: (import "wasm:js-prototypes" "configureAll" (func $configureAll (type $configureAll) (param (ref null $externs) (ref null $funcs) (ref null $bytes) externref))) | ||
| (import "wasm:js-prototypes" "configureAll" (func $configureAll (type $configureAll))) | ||
|
|
||
| ;; CHECK: (data $bytes "12345678") | ||
| (data $bytes "12345678") | ||
|
|
||
| ;; CHECK: (elem $externs externref (item (ref.null noextern))) | ||
| (elem $externs externref | ||
| (ref.null extern) | ||
| ) | ||
|
|
||
| ;; CHECK: (elem $funcs func $configured $already) | ||
| (elem $funcs funcref | ||
| (ref.func $configured) | ||
| (ref.func $already) | ||
| ) | ||
|
|
||
| ;; CHECK: (elem $other func $unconfigured) | ||
| (elem $other funcref | ||
| (ref.func $unconfigured) | ||
| ) | ||
|
|
||
| ;; CHECK: (start $start) | ||
| (start $start) | ||
|
|
||
| ;; CHECK: (func $start (type $0) | ||
| ;; CHECK-NEXT: (call $configureAll | ||
| ;; CHECK-NEXT: (array.new_elem $externs $externs | ||
| ;; CHECK-NEXT: (i32.const 0) | ||
| ;; CHECK-NEXT: (i32.const 1) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (array.new_elem $funcs $funcs | ||
| ;; CHECK-NEXT: (i32.const 0) | ||
| ;; CHECK-NEXT: (i32.const 2) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (array.new_data $bytes $bytes | ||
| ;; CHECK-NEXT: (i32.const 0) | ||
| ;; CHECK-NEXT: (i32.const 8) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (ref.null noextern) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $start | ||
| (call $configureAll | ||
| (array.new_elem $externs $externs | ||
| (i32.const 0) (i32.const 1)) | ||
| (array.new_elem $funcs $funcs | ||
| (i32.const 0) (i32.const 2)) | ||
| (array.new_data $bytes $bytes | ||
| (i32.const 0) (i32.const 8)) | ||
| (ref.null extern) | ||
| ) | ||
| ) | ||
|
|
||
| ;; CHECK: (@binaryen.js.called) | ||
| ;; CHECK-NEXT: (func $configured (type $0) | ||
| ;; CHECK-NEXT: ) | ||
| (func $configured | ||
| ) | ||
|
|
||
| ;; CHECK: (@binaryen.js.called) | ||
| ;; CHECK-NEXT: (func $already (type $0) | ||
| ;; CHECK-NEXT: ) | ||
| (@binaryen.js.called) | ||
| (func $already | ||
| ) | ||
|
|
||
| ;; CHECK: (func $unconfigured (type $0) | ||
| ;; CHECK-NEXT: ) | ||
| (func $unconfigured | ||
| ) | ||
|
|
||
| ;; CHECK: (@binaryen.js.called) | ||
| ;; CHECK-NEXT: (func $unconfigured-already (type $0) | ||
| ;; CHECK-NEXT: ) | ||
| (@binaryen.js.called) | ||
| (func $unconfigured-already | ||
| ) | ||
| ) |
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.
It would be good to produce a warning or fatal error when there are function operands passed to
configureAllthat we are not able to analyze. As a follow-up to this change we should removegetConfigureAllFunctionsand all its uses in favor of explicitly annotated js-called functions anyway, so I think we can just inline the logic fromgetConfigureAllFunctionshere.In the future, if folks have different usage patterns and are running into the warning or fatal error, we can add support for more usage patterns here as well. (
array.new_fixedusage comes to mind as low-hanging fruit.)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.
I'd rather not do that in this PR, so that it is NFC aside from adding a new pass? I don't want to change behavior for users.
But I agree on the path after this PR, yes, it would be nice to unify and simplify this.
(Given we shouldn't unify it yet, I don't think it's worth adding detailed warnings here - that would lead to a bunch of duplicated code.)
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.
Sure, maybe a TODO about adding a warning/error, then?
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.
Sure, done.