-
Notifications
You must be signed in to change notification settings - Fork 839
Add a pass to remove relaxed SIMD instructions #8300
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
7 commits
Select commit
Hold shift + click to select a range
8e1f558
Add a pass to remove relaxed SIMD instructions
tlively 53f34a4
Update copyright year
tlively c27cff1
Fix header order
tlively 3df4f38
Merge branch 'main' into remove-relaxed-simd-pass
tlively 8870263
address feedback
tlively 7b16ed1
update help tests
tlively 9081f36
Merge remote-tracking branch 'origin/remove-relaxed-simd-pass' into r…
tlively 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| // | ||
| // Replaces relaxed SIMD instructions with traps. | ||
| // | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "ir/localize.h" | ||
| #include "ir/utils.h" | ||
| #include "pass.h" | ||
| #include "wasm-builder.h" | ||
| #include "wasm.h" | ||
|
|
||
| namespace wasm { | ||
|
|
||
| struct RemoveRelaxedSIMD : WalkerPass<PostWalker<RemoveRelaxedSIMD>> { | ||
| bool isFunctionParallel() override { return true; } | ||
|
|
||
| std::unique_ptr<Pass> create() override { | ||
| return std::make_unique<RemoveRelaxedSIMD>(); | ||
| } | ||
|
|
||
| void replace(Expression* curr) { | ||
| auto* block = | ||
| ChildLocalizer(curr, getFunction(), *getModule(), getPassOptions()) | ||
| .getChildrenReplacement(); | ||
| block->list.push_back(Builder(*getModule()).makeUnreachable()); | ||
| replaceCurrent(block); | ||
| } | ||
|
|
||
| void visitUnary(Unary* curr) { | ||
| switch (curr->op) { | ||
| case RelaxedTruncSVecF32x4ToVecI32x4: | ||
| case RelaxedTruncUVecF32x4ToVecI32x4: | ||
| case RelaxedTruncZeroSVecF64x2ToVecI32x4: | ||
| case RelaxedTruncZeroUVecF64x2ToVecI32x4: | ||
| replace(curr); | ||
| return; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void visitBinary(Binary* curr) { | ||
| switch (curr->op) { | ||
| case RelaxedSwizzleVecI8x16: | ||
| case RelaxedMinVecF32x4: | ||
| case RelaxedMaxVecF32x4: | ||
| case RelaxedMinVecF64x2: | ||
| case RelaxedMaxVecF64x2: | ||
| case RelaxedQ15MulrSVecI16x8: | ||
| case DotI8x16I7x16SToVecI16x8: | ||
| replace(curr); | ||
| return; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void visitSIMDTernary(SIMDTernary* curr) { | ||
| switch (curr->op) { | ||
| case RelaxedMaddVecF16x8: | ||
| case RelaxedNmaddVecF16x8: | ||
| case RelaxedMaddVecF32x4: | ||
| case RelaxedNmaddVecF32x4: | ||
| case RelaxedMaddVecF64x2: | ||
| case RelaxedNmaddVecF64x2: | ||
| case LaneselectI8x16: | ||
| case LaneselectI16x8: | ||
| case LaneselectI32x4: | ||
| case LaneselectI64x2: | ||
| case DotI8x16I7x16AddSToVecI32x4: | ||
| replace(curr); | ||
| return; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| void visitFunction(Function* func) { | ||
| ReFinalize().walkFunctionInModule(func, getModule()); | ||
| } | ||
| }; | ||
|
|
||
| Pass* createRemoveRelaxedSIMDPass() { return new RemoveRelaxedSIMD(); } | ||
|
|
||
| } // 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,207 @@ | ||
| ;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. | ||
|
|
||
| ;; RUN: wasm-opt %s -all --remove-relaxed-simd -S -o - | filecheck %s | ||
|
|
||
| (module | ||
| ;; CHECK: (import "" "" (func $effect (type $1) (result v128))) | ||
| (import "" "" (func $effect (result v128))) | ||
| ;; CHECK: (func $unary (type $0) (param $0 v128) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (v128.not | ||
| ;; CHECK-NEXT: (local.get $0) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $unary (param v128) | ||
| (drop (i32x4.relaxed_trunc_f32x4_s (local.get 0))) | ||
| (drop (i32x4.relaxed_trunc_f32x4_u (local.get 0))) | ||
| (drop (i32x4.relaxed_trunc_f64x2_s_zero (local.get 0))) | ||
| (drop (i32x4.relaxed_trunc_f64x2_u_zero (local.get 0))) | ||
| ;; Normal SIMD instruction | ||
| (drop (v128.not (local.get 0))) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $binary (type $2) (param $0 v128) (param $1 v128) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (v128.xor | ||
| ;; CHECK-NEXT: (local.get $0) | ||
| ;; CHECK-NEXT: (local.get $1) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $binary (param v128 v128) | ||
| (drop (i8x16.relaxed_swizzle (local.get 0) (local.get 1))) | ||
| (drop (f32x4.relaxed_min (local.get 0) (local.get 1))) | ||
| (drop (f32x4.relaxed_max (local.get 0) (local.get 1))) | ||
| (drop (f64x2.relaxed_min (local.get 0) (local.get 1))) | ||
| (drop (f64x2.relaxed_max (local.get 0) (local.get 1))) | ||
| (drop (i16x8.relaxed_q15mulr_s (local.get 0) (local.get 1))) | ||
| (drop (i16x8.dot_i8x16_i7x16_s (local.get 0) (local.get 1))) | ||
| ;; Normal SIMD instruction | ||
| (drop (v128.xor (local.get 0) (local.get 1))) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $ternary (type $3) (param $0 v128) (param $1 v128) (param $2 v128) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (v128.bitselect | ||
| ;; CHECK-NEXT: (local.get $0) | ||
| ;; CHECK-NEXT: (local.get $1) | ||
| ;; CHECK-NEXT: (local.get $2) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $ternary (param v128 v128 v128) | ||
| (drop (i32x4.dot_i8x16_i7x16_add_s (local.get 0) (local.get 1) (local.get 2))) | ||
| (drop (f16x8.relaxed_madd (local.get 0) (local.get 1) (local.get 2))) | ||
| (drop (f16x8.relaxed_nmadd (local.get 0) (local.get 1) (local.get 2))) | ||
| (drop (f32x4.relaxed_madd (local.get 0) (local.get 1) (local.get 2))) | ||
| (drop (f32x4.relaxed_nmadd (local.get 0) (local.get 1) (local.get 2))) | ||
| (drop (f64x2.relaxed_madd (local.get 0) (local.get 1) (local.get 2))) | ||
| (drop (f64x2.relaxed_nmadd (local.get 0) (local.get 1) (local.get 2))) | ||
| ;; Normal SIMD instruction | ||
| (drop (v128.bitselect (local.get 0) (local.get 1) (local.get 2))) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $refinalize (type $0) (param $0 v128) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block $l | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $refinalize (param v128) | ||
| (drop | ||
| ;; This block should become unreachable. | ||
| (block $l (result v128) | ||
| (i32x4.relaxed_trunc_f32x4_s (local.get 0)) | ||
| ) | ||
| ) | ||
| ) | ||
|
|
||
| ;; CHECK: (func $effects (type $0) (param $0 v128) | ||
| ;; CHECK-NEXT: (local $1 v128) | ||
| ;; CHECK-NEXT: (local $2 v128) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (block | ||
| ;; CHECK-NEXT: (local.set $1 | ||
| ;; CHECK-NEXT: (call $effect) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (local.set $2 | ||
| ;; CHECK-NEXT: (block (result v128) | ||
| ;; CHECK-NEXT: (drop | ||
| ;; CHECK-NEXT: (call $effect) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (local.get $0) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: (unreachable) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| ;; CHECK-NEXT: ) | ||
| (func $effects (param v128) | ||
| (drop | ||
| (f16x8.relaxed_madd | ||
| (call $effect) | ||
| (local.get 0) | ||
| (block (result v128) | ||
| (drop | ||
| (call $effect) | ||
| ) | ||
| (local.get 0) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
| ) | ||
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.
Please test a case where a child must be kept around due to effects.