Skip to content

lower unbounded_shl/unbounded_shr without branching - #710

Merged
LegNeato merged 2 commits into
Rust-GPU:mainfrom
nazar-pc:unbounded-shifts
Nov 28, 2026
Merged

LegNeato merged 2 commits into
Rust-GPU:mainfrom
nazar-pc:unbounded-shifts

Conversation

@nazar-pc

Copy link
Copy Markdown
Contributor

This resolves #403.

Essentially reduces this:

%30 = OpULessThan %bool %29 %uint_32
      OpSelectionMerge %31 None
      OpBranchConditional %30 %32 %33
%33 = OpLabel
      OpBranch %31
%32 = OpLabel
%41 = OpShiftLeftLogical %uint %26 %29
      OpBranch %31
%31 = OpLabel
%42 = OpPhi %uint %41 %32 %uint_0 %33
      OpSelectionMerge %44 None
      OpBranchConditional %30 %45 %46
%46 = OpLabel
      OpBranch %44
%45 = OpLabel
%54 = OpShiftRightLogical %uint %26 %29
      OpBranch %44
%44 = OpLabel
%55 = OpPhi %uint %54 %45 %uint_0 %46
%56 = OpBitwiseXor %uint %42 %55
      OpSelectionMerge %58 None
      OpBranchConditional %30 %59 %60
%60 = OpLabel
%76 = OpShiftRightArithmetic %int %27 %uint_31
      OpBranch %58
%59 = OpLabel
%68 = OpShiftRightArithmetic %int %27 %29
      OpBranch %58
%58 = OpLabel
%77 = OpPhi %int %68 %59 %76 %60
%78 = OpULessThan %bool %29 %uint_64
      OpSelectionMerge %79 None
      OpBranchConditional %78 %80 %81
%81 = OpLabel
      OpBranch %79
%80 = OpLabel
%89 = OpUConvert %ulong %29
%90 = OpShiftLeftLogical %ulong %28 %89
      OpBranch %79
%79 = OpLabel
%91 = OpPhi %ulong %90 %80 %ulong_0 %81

Into this:

%28 = OpULessThan %bool %27 %uint_32
%29 = OpShiftLeftLogical %uint %24 %27
%30 = OpSelect %uint %28 %29 %uint_0
%32 = OpShiftRightLogical %uint %24 %27
%33 = OpSelect %uint %28 %32 %uint_0
%34 = OpBitwiseXor %uint %30 %33
%36 = OpSelect %uint %28 %27 %uint_31
%37 = OpShiftRightArithmetic %int %25 %36
%38 = OpULessThan %bool %27 %uint_64
%39 = OpShiftLeftLogical %ulong %26 %27
%40 = OpSelect %ulong %38 %39 %ulong_0

And hopefully optimizes better in a driver (though I have not benchmarked it).

Implemented by a clanker, but looks reasonable to me.

The `core` implementation of these methods branches on whether the shift
amount is less than the bit width, which turns into structured control flow
with an `OpPhi`. It also goes through `unchecked_shl`/`unchecked_shr`, whose
precondition check forces callers to be inlined because of the panic path.

Intercept calls to the integer `unbounded_shl`/`unbounded_shr` methods from
`core` and emit the shift followed by an `OpSelect` on whether the shift
amount is in range, since SPIR-V leaves shifts by the bit width or more
undefined. Signed `unbounded_shr` clamps the shift amount to `BITS - 1`
instead, which fills the result with the sign bit.

@LegNeato LegNeato left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks reasonable to me!

@LegNeato
LegNeato enabled auto-merge November 27, 2026 00:34
auto-merge was automatically disabled November 28, 2026 01:11

Head branch was pushed to by a user without write access

@nazar-pc

Copy link
Copy Markdown
Contributor Author

Not related to the changes here, but updated url to please CI

@nazar-pc
nazar-pc requested a review from LegNeato November 28, 2026 01:12
@LegNeato
LegNeato added this pull request to the merge queue Nov 28, 2026
Merged via the queue into Rust-GPU:main with commit fab2962 Nov 28, 2026
24 checks passed
@nazar-pc
nazar-pc deleted the unbounded-shifts branch November 28, 2026 14:13
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.

Implement unbounded_shl/unbounded_shr

2 participants