fix(cpp): emit casts using destination type - #1549
Open
y7nieSEl5 wants to merge 2 commits into
Open
Conversation
Author
|
Reopening after testing the interaction with #1509 more closely. The 2 PRs actually address separate layers. This PR fixes the shared C++ |
wingertge
approved these changes
Aug 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CastOpdestination type instead of reconstructing the source typeBackground
The shared emitter declared the cast result with its destination type but formatted the right-hand side with the input type. For f32-to-bf16 this generated invalid MSL like:
Using the result type restores the cast semantics and emits the explicit conversion Metal requires:
bfloat const v18 = bfloat(v17);The destination-typed behavior existed before the Pliron migration in #1402; the source-typed constructor was introduced during that refactor.
Relationship to #1509
#1509 and this PR address separate layers. #1509 registers BF16 as a WGPU/MSL runtime capability, while this PR fixes the shared C++
CastOpemitter used by native Metal and WGPU/MSL.On an Apple M5, locally enabling the same full BF16 registration produced by #1509 after its probe succeeds made WGPU report
Conversion | Arithmetic | DotProduct | Buffer. An actual f32-to-bf16 kernel still emittedbfloat = float(...)and failed Metal compilation without this change. With this change it emittedbfloat = bfloat(...), compiled, and returned the expected values.Testing
cargo fmt --all -- --checkcargo check -p cubecl-cpp --all-featurescargo test -p cubecl-metal f32_to_bf16_cast_compiles_and_runs -- --nocapturehalf::bf16::from_f32bit-for-bit; NaN was verified by classificationPerformance
No measurable performance regression was observed on the Apple M5. Since f32-to-bf16 does not compile before the fix, the shared
CastOpemitter was compared using f32-to-f16, which compiles on both revisions and exercises the same changed path.The device-timed benchmark used warmed compilation and allocations, 11 alternating samples of 200 large-buffer launches, and an unchanged f32 copy as a drift control. The median normalized cast/copy ratio was
0.760752before the fix and0.759177after it, a-0.21%difference within normal run-to-run dispersion.Fixes #1522.