Skip to content

Do not fuse scalar operations in vector_ops when a scalar operand has another type than the result - #707

Open
greenhat wants to merge 1 commit into
Rust-GPU:mainfrom
greenhat:fix-vector-shift-fusion
Open

greenhat wants to merge 1 commit into
Rust-GPU:mainfrom
greenhat:fix-vector-shift-fusion

Conversation

@greenhat

Copy link
Copy Markdown
Contributor

Shifting a glam integer vector by an amount of the other signedness does not build. UVec3 >> 1 (the literal is an i32) fails in spirv-val:

error: Expected Constituents to be scalars or vectors of the same type as Result Type components
  %28 = OpCompositeConstruct %v3uint %25 %26 %27

UVec3 >> i32, IVec3 << u32 and UVec3 >> IVec3 fail the same way. UVec3 >> 1u32 builds.

The fix and the test were written with an AI coding agent. I reviewed and checked them.

Details

The code emitted for UVec3 >> 1 before the fix:

%25 = OpBitwiseAnd %int %int_1 %int_31
%26 = OpBitwiseAnd %int %int_1 %int_31
%27 = OpBitwiseAnd %int %int_1 %int_31
%28 = OpCompositeConstruct %v3uint %25 %26 %27
%29 = OpShiftRightLogical %v3uint %22 %28

vector_ops (linker/peephole_opts.rs) packs the scalar operands of a fused operation into a vector of the result type. This skips the fusion when a scalar operand has another type than the lane result. These shifts stay scalar: three OpShiftRightLogical %uint and the OpCompositeConstruct of their results, as the pass received them. The fix adds no new instructions, so I did not add a difftest.

The new compile test ui/dis/vector_shift_mixed_sign fails without the fix. Its last case (UVec3 >> u32) is still fused into one OpShiftRightLogical %v3uint, and the expected output of the other ui/dis tests does not change.

Alternatively, we can keep the fused shift and pack the amounts into a vector of their own type (OpShiftRightLogical allows it). I did not do it since vector_ops would have to add an OpTypeVector to the module when it is missing. Let me know if you want me to implement it.

…an the result

`vector_ops` packs the scalar operands of a fused operation into a vector of
the result type. A shift amount can have another signedness than the shifted
value (`UVec3 >> 1`, the literal is an `i32`), so the packed
`OpCompositeConstruct` was invalid and `spirv-val` rejected the module.
Such operations now stay scalar.

@Firestar99 Firestar99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I will need some extra time to properly resolve this, I'm unsure:

  • why spirv-val if failing here, cause they define themselves that signed and unsigned doesn't actually matter, what matters is the operands themselves.
  • if the current fix doesn't affect other stuff
  • if we're better off just abandoning signed integers and only emit unsigned. Tried this, but need some other fixups first that will probably ripple through the entire codebase...

This branch has not been deployed

No deployments
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.

2 participants