-
Notifications
You must be signed in to change notification settings - Fork 1.7k
winch: respect the enable_nan_canonicalization setting #12939
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
saulecabrera
merged 9 commits into
bytecodealliance:main
from
r-near:winch-nan-canonicalization
Apr 7, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0c5e168
winch: respect the enable_nan_canonicalization setting
r-near c476a6a
add disas tests for NaN canonicalization
r-near 7cdd58e
rename canonicalize_nan to maybe_canonicalize_nan
r-near 75cecea
extract canonical NaN constants to shared masm module
r-near 1722807
remove canonicalize_nan_for_round, inline at call sites
r-near cdef987
implement SIMD NaN canonicalization for x64
r-near 3649937
cargo fmt
r-near 63ad118
add comment about branchless scalar canonicalization opportunity
r-near 9db6a79
add canonicalize-nan.wast to no-AVX skip list
r-near 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,40 @@ | ||
| ;;! target = "x86_64" | ||
| ;;! test = "winch" | ||
| ;;! flags = "-Wnan-canonicalization" | ||
|
|
||
| (module | ||
| (func (param f32 f32) (result f32) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32.add | ||
| ) | ||
| ) | ||
| ;; wasm[0]::function[0]: | ||
| ;; pushq %rbp | ||
| ;; movq %rsp, %rbp | ||
| ;; movq 8(%rdi), %r11 | ||
| ;; movq 0x18(%r11), %r11 | ||
| ;; addq $0x20, %r11 | ||
| ;; cmpq %rsp, %r11 | ||
| ;; ja 0x69 | ||
| ;; 1c: movq %rdi, %r14 | ||
| ;; subq $0x20, %rsp | ||
| ;; movq %rdi, 0x18(%rsp) | ||
| ;; movq %rsi, 0x10(%rsp) | ||
| ;; movss %xmm0, 0xc(%rsp) | ||
| ;; movss %xmm1, 8(%rsp) | ||
| ;; movss 8(%rsp), %xmm0 | ||
| ;; movss 0xc(%rsp), %xmm1 | ||
| ;; addss %xmm0, %xmm1 | ||
| ;; ucomiss %xmm1, %xmm1 | ||
| ;; jnp 0x5d | ||
| ;; 55: movss 0x13(%rip), %xmm1 | ||
| ;; movaps %xmm1, %xmm0 | ||
| ;; addq $0x20, %rsp | ||
| ;; popq %rbp | ||
| ;; retq | ||
| ;; 69: ud2 | ||
| ;; 6b: addb %al, (%rax) | ||
| ;; 6d: addb %al, (%rax) | ||
| ;; 6f: addb %al, (%rax) | ||
| ;; 71: addb %al, %al |
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,42 @@ | ||
| ;;! target = "x86_64" | ||
| ;;! test = "winch" | ||
| ;;! flags = ["-Wnan-canonicalization", "-Ccranelift-has-avx"] | ||
|
|
||
| (module | ||
| (func (param v128 v128) (result v128) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32x4.add | ||
| ) | ||
| ) | ||
| ;; wasm[0]::function[0]: | ||
| ;; pushq %rbp | ||
| ;; movq %rsp, %rbp | ||
| ;; movq 8(%rdi), %r11 | ||
| ;; movq 0x18(%r11), %r11 | ||
| ;; addq $0x30, %r11 | ||
| ;; cmpq %rsp, %r11 | ||
| ;; ja 0x6c | ||
| ;; 1c: movq %rdi, %r14 | ||
| ;; subq $0x30, %rsp | ||
| ;; movq %rdi, 0x28(%rsp) | ||
| ;; movq %rsi, 0x20(%rsp) | ||
| ;; movdqu %xmm0, 0x10(%rsp) | ||
| ;; movdqu %xmm1, (%rsp) | ||
| ;; movdqu (%rsp), %xmm0 | ||
| ;; movdqu 0x10(%rsp), %xmm1 | ||
| ;; vaddps %xmm0, %xmm1, %xmm1 | ||
| ;; vcmpunordps %xmm1, %xmm1, %xmm15 | ||
| ;; vandnps %xmm1, %xmm15, %xmm1 | ||
| ;; vandps 0x15(%rip), %xmm15, %xmm15 | ||
| ;; vorps %xmm1, %xmm15, %xmm1 | ||
| ;; movdqa %xmm1, %xmm0 | ||
| ;; addq $0x30, %rsp | ||
| ;; popq %rbp | ||
| ;; retq | ||
| ;; 6c: ud2 | ||
| ;; 6e: addb %al, (%rax) | ||
| ;; 70: addb %al, (%rax) | ||
| ;; 72: sarb $0, (%rdi) | ||
| ;; 76: sarb $0, (%rdi) | ||
| ;; 7a: sarb $0, (%rdi) |
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,43 @@ | ||
| ;;! target = "x86_64" | ||
| ;;! test = "winch" | ||
| ;;! flags = "-Wnan-canonicalization" | ||
|
|
||
| (module | ||
| (func (param f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.div | ||
| ) | ||
| ) | ||
| ;; wasm[0]::function[0]: | ||
| ;; pushq %rbp | ||
| ;; movq %rsp, %rbp | ||
| ;; movq 8(%rdi), %r11 | ||
| ;; movq 0x18(%r11), %r11 | ||
| ;; addq $0x20, %r11 | ||
| ;; cmpq %rsp, %r11 | ||
| ;; ja 0x68 | ||
| ;; 1c: movq %rdi, %r14 | ||
| ;; subq $0x20, %rsp | ||
| ;; movq %rdi, 0x18(%rsp) | ||
| ;; movq %rsi, 0x10(%rsp) | ||
| ;; movsd %xmm0, 8(%rsp) | ||
| ;; movsd %xmm1, (%rsp) | ||
| ;; movsd (%rsp), %xmm0 | ||
| ;; movsd 8(%rsp), %xmm1 | ||
| ;; divsd %xmm0, %xmm1 | ||
| ;; ucomisd %xmm1, %xmm1 | ||
| ;; jnp 0x5c | ||
| ;; 54: movsd 0x14(%rip), %xmm1 | ||
| ;; movaps %xmm1, %xmm0 | ||
| ;; addq $0x20, %rsp | ||
| ;; popq %rbp | ||
| ;; retq | ||
| ;; 68: ud2 | ||
| ;; 6a: addb %al, (%rax) | ||
| ;; 6c: addb %al, (%rax) | ||
| ;; 6e: addb %al, (%rax) | ||
| ;; 70: addb %al, (%rax) | ||
| ;; 72: addb %al, (%rax) | ||
| ;; 74: addb %al, (%rax) | ||
| ;; 76: clc |
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,153 @@ | ||
| ;;! nan_canonicalization = true | ||
|
|
||
| ;; Scalar counterpart to simd/canonicalize-nan.wast. | ||
|
|
||
| (module | ||
| (func (export "f32.add") (param f32 f32) (result f32) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32.add) | ||
| (func (export "f32.sub") (param f32 f32) (result f32) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32.sub) | ||
| (func (export "f32.mul") (param f32 f32) (result f32) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32.mul) | ||
| (func (export "f32.div") (param f32 f32) (result f32) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32.div) | ||
| (func (export "f32.min") (param f32 f32) (result f32) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32.min) | ||
| (func (export "f32.max") (param f32 f32) (result f32) | ||
| local.get 0 | ||
| local.get 1 | ||
| f32.max) | ||
| (func (export "f32.sqrt") (param f32) (result f32) | ||
| local.get 0 | ||
| f32.sqrt) | ||
| (func (export "f32.ceil") (param f32) (result f32) | ||
| local.get 0 | ||
| f32.ceil) | ||
| (func (export "f32.floor") (param f32) (result f32) | ||
| local.get 0 | ||
| f32.floor) | ||
| (func (export "f32.trunc") (param f32) (result f32) | ||
| local.get 0 | ||
| f32.trunc) | ||
| (func (export "f32.nearest") (param f32) (result f32) | ||
| local.get 0 | ||
| f32.nearest) | ||
|
|
||
| (func (export "f64.add") (param f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.add) | ||
| (func (export "f64.sub") (param f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.sub) | ||
| (func (export "f64.mul") (param f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.mul) | ||
| (func (export "f64.div") (param f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.div) | ||
| (func (export "f64.min") (param f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.min) | ||
| (func (export "f64.max") (param f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.max) | ||
| (func (export "f64.sqrt") (param f64) (result f64) | ||
| local.get 0 | ||
| f64.sqrt) | ||
| (func (export "f64.ceil") (param f64) (result f64) | ||
| local.get 0 | ||
| f64.ceil) | ||
| (func (export "f64.floor") (param f64) (result f64) | ||
| local.get 0 | ||
| f64.floor) | ||
| (func (export "f64.trunc") (param f64) (result f64) | ||
| local.get 0 | ||
| f64.trunc) | ||
| (func (export "f64.nearest") (param f64) (result f64) | ||
| local.get 0 | ||
| f64.nearest) | ||
|
|
||
r-near marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (func (export "reinterpret-and-demote") (param i64) (result i32) | ||
| local.get 0 | ||
| f64.reinterpret_i64 | ||
| f32.demote_f64 | ||
| i32.reinterpret_f32) | ||
| (func (export "reinterpret-and-promote") (param i32) (result i64) | ||
| local.get 0 | ||
| f32.reinterpret_i32 | ||
| f64.promote_f32 | ||
| i64.reinterpret_f64) | ||
|
|
||
| ;; Expose raw bits of 0/0 to verify exact canonical NaN bit patterns. | ||
| (func (export "f32.div-nan-bits") (result i32) | ||
| f32.const 0 | ||
| f32.const 0 | ||
| f32.div | ||
| i32.reinterpret_f32) | ||
| (func (export "f64.div-nan-bits") (result i64) | ||
| f64.const 0 | ||
| f64.const 0 | ||
| f64.div | ||
| i64.reinterpret_f64) | ||
| ) | ||
|
|
||
| ;; Exact bit patterns: canonical f32 NaN = 0x7fc00000, f64 = 0x7ff8000000000000 | ||
| (assert_return (invoke "f32.div-nan-bits") (i32.const 0x7fc00000)) | ||
| (assert_return (invoke "f64.div-nan-bits") (i64.const 0x7ff8000000000000)) | ||
|
|
||
| ;; NaN-producing operations | ||
| (assert_return (invoke "f32.div" (f32.const 0) (f32.const 0)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f64.div" (f64.const 0) (f64.const 0)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f32.sqrt" (f32.const -1)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f64.sqrt" (f64.const -1)) (f64.const nan:0x8000000000000)) | ||
|
|
||
| ;; NaN propagation through f32 arithmetic | ||
| (assert_return (invoke "f32.add" (f32.const nan) (f32.const 1)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f32.sub" (f32.const nan) (f32.const 1)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f32.mul" (f32.const nan) (f32.const 1)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f32.min" (f32.const nan) (f32.const 1)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f32.max" (f32.const nan) (f32.const 1)) (f32.const nan:0x400000)) | ||
|
|
||
| ;; NaN propagation through f64 arithmetic | ||
| (assert_return (invoke "f64.add" (f64.const nan) (f64.const 1)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f64.sub" (f64.const nan) (f64.const 1)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f64.mul" (f64.const nan) (f64.const 1)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f64.min" (f64.const nan) (f64.const 1)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f64.max" (f64.const nan) (f64.const 1)) (f64.const nan:0x8000000000000)) | ||
|
|
||
| ;; Rounding NaN (f32) | ||
| (assert_return (invoke "f32.ceil" (f32.const nan)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f32.floor" (f32.const nan)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f32.trunc" (f32.const nan)) (f32.const nan:0x400000)) | ||
| (assert_return (invoke "f32.nearest" (f32.const nan)) (f32.const nan:0x400000)) | ||
|
|
||
| ;; Rounding NaN (f64) | ||
| (assert_return (invoke "f64.ceil" (f64.const nan)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f64.floor" (f64.const nan)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f64.trunc" (f64.const nan)) (f64.const nan:0x8000000000000)) | ||
| (assert_return (invoke "f64.nearest" (f64.const nan)) (f64.const nan:0x8000000000000)) | ||
|
|
||
| ;; Demote/promote with non-canonical NaN bit patterns | ||
| (assert_return (invoke "reinterpret-and-demote" (i64.const 0xfffefdfccccdcecf)) (i32.const 0x7fc00000)) | ||
| (assert_return (invoke "reinterpret-and-promote" (i32.const 0xfffefdfc)) (i64.const 0x7ff8000000000000)) | ||
|
|
||
| ;; Normal values pass through unchanged | ||
| (assert_return (invoke "f32.add" (f32.const 1) (f32.const 2)) (f32.const 3)) | ||
| (assert_return (invoke "f64.div" (f64.const 10) (f64.const 2)) (f64.const 5)) | ||
| (assert_return (invoke "f32.sqrt" (f32.const 4)) (f32.const 2)) | ||
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.
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.
Could we also implement this functionality for the non-scalar counterparts? I believe that all vector instructions are implemented in order to respect canonicalization. Once implemented we should be able to fully test
simd/canonicalize-nan.wastin x86-64, by removing this line https://github.com/bytecodealliance/wasmtime/blob/main/crates/test-util/src/wast.rs#L614There 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.
Ok here we go:
cdef987(this PR).Added
maybe_canonicalize_v128_nanto the masm trait and implemented it for x64I removed the
skipand it looks like the tests pass 🎉