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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

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
Copy link
Contributor Author

After updating all you suggested, I've fuzzing for about 20 minutes; and all is well.

// Check left's max bits and right is constant.
auto leftMaxBits = Bits::getMaxBits(left, this);
uint64_t maskLeft;
if (leftMaxBits == 64) {
Copy link
Member

Choose a reason for hiding this comment

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

This would be 32 for a 32-bit operation.

Suggested change
if (leftMaxBits == 64) {
if (leftMaxBits == left->type.getByteSize() * 8) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fail in the CI test of clang (LTO)

======================================================================
FAIL: test_run_py (test.unit.test_cluster_fuzz.ClusterFuzz.test_run_py)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/work/binaryen/binaryen/test/unit/test_cluster_fuzz.py", line 116, in test_run_py
    self.assertEqual(stderr, '')
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^
AssertionError: 'invalid type\nUNREACHABLE executed at /ho[56 chars]666!' != ''
- invalid type
- UNREACHABLE executed at /home/runner/work/binaryen/binaryen/src/wasm/wasm-type.cpp:666!

The wasm-type.cpp:666! is the definition of getByteSize:

 unsigned Type::getByteSize() const {
   // TODO: alignment?
   auto getSingleByteSize = [](Type t) {
     switch (t.getBasic()) {
       case Type::i32:
       case Type::f32:
         return 4;
       case Type::i64:
       case Type::f64:
         return 8;
       case Type::v128:
         return 16;
       case Type::none:
       case Type::unreachable:
         break;
     }
     WASM_UNREACHABLE("invalid type"); // <-- issue here
   };

Although we inovke the function optimizeAndNoOverlappingBits when curr is binary operation. It is the left->type requests the getByteSize api... It seems that the currently implementation getByteSize is not complete.

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