Skip to content

Conversation

@alister-chowdhury
Copy link
Contributor

This change adds the following folding ops:

And + Xor/Or

0b1110 & (a | 0b0001) = a & 0b1110
0b1110 & (a ^ 0b0001) = a & 0b1110
0b0110 & (a | 0b1110) = 0b0110

Which should resolve #4902, but since this doesn't add any kind of global variable tracking would be laundered away with any kind of OpPhi.

And + Add/Sub

1 & (b + 2) = b & 1
1 & (b - 2) = b & 1

And + Shifts

1 & (b << 1) = 0
 0x80000000 & (b >> 1) = 0

@alister-chowdhury alister-chowdhury changed the title Adding folding rules for bitwise and spirv-opt: Adding folding rules for bitwise and Oct 20, 2025
@s-perron s-perron enabled auto-merge (squash) October 21, 2025 14:32
@s-perron s-perron disabled auto-merge October 21, 2025 14:40
Copy link
Collaborator

@s-perron s-perron left a comment

Choose a reason for hiding this comment

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

Sorry, I only saw the commit with the test changes in the my first review, and that is all I thought it did.

I looked at the changes. One of the rules does not seem correct to me.

Also the tests all uses the same bit patterns, and you are not getting full testing. For the add and subtract cases you need to think about cases with a carry, and what happens if there is overflow or underflow.

This change adds the following folding ops:
And + Xor/Or:
* 0b1110 & (a | 0b0001) = a & 0b1110
* 0b1110 & (a ^ 0b0001) = a & 0b1110
* 0b0110 & (a | 0b1110) = 0b0110
Which *should* resolve KhronosGroup#4902, but since this doesn't
add any kind of global variable tracking would be laundered
away with any kind of OpPhi.

And + Add/Sub:
* 1 & (b + 2) = b & 1
* 1 & (b - 2) = b & 1

And + Shifts:
* 1 & (b << 1) = 0
* 0x80000000 & (b >> 1) = 0
@alister-chowdhury
Copy link
Contributor Author

alister-chowdhury commented Oct 22, 2025

For the add and subtract cases you need to think about cases with a carry, and what happens if there is overflow or underflow.

My logic was based around an add/subtract can only affect bits >= to its lsb, even with overflow/underflow.

For example (using an 8bit integer as example):

Add

0b11111111 + 0b00000001 = 0b00000000   : bits <= #0 affected
                      ^            ^
0b11111111 + 0b00000010 = 0b00000001   : bits <= #1 affected
                     ^            ^
0b11111111 + 0b00001000 = 0b00000111   : bits <= #3 affected
                   ^            ^

Sub

0b00000000 - 0b00000001 = 0b11111111   : bits >= #0 affected
                      ^            ^
0b00000000 - 0b00000010 = 0b11111110   : bits >= #1 affected
                     ^            ^
0b00000000 - 0b00001000 = 0b11111000   : bits >= #3 affected
                   ^            ^

@alister-chowdhury alister-chowdhury force-pushed the bitwise_folding_funcs branch 2 times, most recently from caaab1c to bd170e0 Compare October 23, 2025 20:07
* Renaming ZipIntegerConstants to ForEachIntegerConstantPair
* Fixing logical add/sub + and folding rule, where it didn't take
into account that masks can have holes in them.
* Adding more values in the tests

re-running clang-format, after strange formatting
Copy link
Collaborator

@s-perron s-perron left a comment

Choose a reason for hiding this comment

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

Looks good, but a few very minor issues.

* Updated description of `ForEachIntegerConstantPair`
* Changed nested Op type if, to instead be an early exit
* Added `utils::LSB` and replaced `RepeatBitsForward`
@s-perron s-perron enabled auto-merge (squash) October 30, 2025 00:58
auto-merge was automatically disabled October 30, 2025 04:59

Head branch was pushed to by a user without write access

@s-perron s-perron enabled auto-merge (squash) October 30, 2025 15:30
@s-perron s-perron merged commit 2fa0e29 into KhronosGroup:main Oct 30, 2025
26 checks passed
@alister-chowdhury alister-chowdhury deleted the bitwise_folding_funcs branch December 8, 2025 04:28
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.

spirv-opt: Keeping track of bitmasks for constant folding in conditional branching.

3 participants