Skip to content

OptimizeInstruction: Optimize any boolean & (No overlap with boolean's LSB) ==> 0 #7505

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 31 commits into from
May 23, 2025

Conversation

xuruiyang2002
Copy link
Contributor

There are so many rules for and, but we still cannot optimize the following one:

(i32.and
 (i32.eqz
  (i32.load $0
   (i32.const 0)
  )
 )
 (i32.const 4)
)

to zero.

Adding rule for add: any boolean & (No overlap with boolean's LSB) ==> 0

Fixes: #7481

@xuruiyang2002

This comment was marked as outdated.

xuruiyang2002 and others added 2 commits May 2, 2025 19:01
Co-authored-by: Alon Zakai <alonzakai@gmail.com>
Co-authored-by: Alon Zakai <alonzakai@gmail.com>
@xuruiyang2002
Copy link
Contributor Author

I've been fuzzing for 60 minutes; and it looks goods enough.

@xuruiyang2002
Copy link
Contributor Author

Since the PR has been carefully reviewed many times and lots of time has been invested, we should not miss the opportunity to optimize.

Could you please review the code when you have a moment? @kripken

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay.

xuruiyang2002 and others added 5 commits May 24, 2025 00:56
Co-authored-by: Alon Zakai <alonzakai@gmail.com>
Co-authored-by: Alon Zakai <alonzakai@gmail.com>
Co-authored-by: Alon Zakai <alonzakai@gmail.com>
Co-authored-by: Alon Zakai <alonzakai@gmail.com>
Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kripken kripken merged commit c9ec43d into WebAssembly:main May 23, 2025
16 checks passed
@xuruiyang2002 xuruiyang2002 deleted the bool&even branch May 24, 2025 01:40
@kripken
Copy link
Member

kripken commented May 27, 2025

The fuzzer found a problem here,

(module
 (type $0 (func))
 (type $16 (func (param f64) (result f32)))
 (memory $0 16 17)
 (export "func_28" (func $24))
 (export "func_80" (func $76))
 (func $24 (type $0)
  (local $1 i32)
  (i32.store offset=22 align=2
   (local.tee $1
    (i32.const 1)
   )
   (i32.const 1)
  )
  (drop
   (block (result i32)
    (if
     (i32.const 0)
     (then
      (nop)
     )
     (else
      (i32.store16 offset=2 align=1
       (i32.and
        (local.get $1)
        (i32.const 1)
       )
       (i32.load16_u offset=22
        (i32.const 0)
       )
      )
      (block
       (unreachable)
       (local.tee $1
        (unreachable)
       )
      )
     )
    )
    (i32.const 0)
   )
  )
 )
 (func $76 (type $16) (param $0 f64) (result f32)
  (local $1 f32)
  (local.set $1
   (f32.load offset=4 align=2
    (i32.const 0)
   )
  )
  (local.get $1)
 )
)
$ bin/wasm-opt b.wat --optimize-instructions --fuzz-exec
[fuzz-exec] calling func_28
[trap unreachable]
[fuzz-exec] calling func_80
[fuzz-exec] note result: func_80 => 1.401298464324817e-45
[fuzz-exec] calling func_28
[trap unreachable]
[fuzz-exec] calling func_80
[fuzz-exec] note result: func_80 => 0
[fuzz-exec] comparing func_28
[fuzz-exec] comparing func_80
values not identical! 0 != 1.401298464324817e-45
[fuzz-exec] optimization passes changed results

I haven't investigated yet.

@kripken
Copy link
Member

kripken commented May 27, 2025

Turns out this just uncovered an existing bug, fix in #7623

kripken added a commit that referenced this pull request May 27, 2025
…#7623)

Previously we set such locals to 64 bits, even if they were i32. This
interacted with #7505 which compared the bits to the true bits of
the type, which led to 64 != 32, which suggested we knew something
nontrivial about the bits, and a misoptimization.
@xuruiyang2002
Copy link
Contributor Author

xuruiyang2002 commented May 28, 2025

Turns out this just uncovered an existing bug, fix in #7623

I am glad to hear the PR by myself didn't bring new bug into opt and even uncovered an existing one. 👍

Update: I've understood the bug, and it's somewhat interesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

O3 fails to const propagation & folding but O2 can
2 participants