rust-gpu: Capability is required unexpectedly for constant cast
I have a following trivial situation:
const K: u8 = 20;
...
let position = 2u32;
let global_y_offset_bits = position * K as u32;
...
Here it should be trivial for compiler to cast u8 to u32 at compile time and make last line effectively this:
let global_y_offset_bits = position * 20u32;
Yet for some reason it doesn’t happen and I’m getting this instead:
error: `u8` without `OpCapability Int8`
--> /home/nazar-pc/.rustup/toolchains/nightly-2024-11-22-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/num/mod.rs:1298:5
|
1298 | / uint_impl! {
1299 | | Self = usize,
1300 | | ActualT = u32,
1301 | | SignedT = isize,
... |
1315 | | bound_condition = " on 32-bit targets",
1316 | | }
| |_____^
|
= note: used by unnamed global (%1504)
note: used from within `ab_proof_of_space_gpu::shader::compute_f1::compute_f1`
--> ../src/shader/compute_f1.rs:85:47
|
85 | let global_y_offset_bits = position * K as u32;
| ^^^^^^^^
I think this is a bug. Even if it was a variable that is assigned a literal I’d expect the compiler to inline it with a correct type. And I believe rustc actually does this for CPU architectures like x86-64.
I imagine inability to handle cases like this will result in suboptimal output in many cases, so it’d be nice to address the root cause of it.
UPD: const K_U32: u32 = K as u32; as a workaround helps, but ideally shouldn’t be necessary.
About this issue
- Original URL
- State: open
- Created a year ago
- Comments: 27 (18 by maintainers)
Commits related to this issue
- Optimize constant casts to avoid unnecessary capabilities Add constant folding for widening casts (u8->u32, f32->f64) to avoid creating intermediate types. Remove capability checks during type creati... — committed to LegNeato/rust-gpu by LegNeato a year ago
- Optimize constant casts to avoid unnecessary capabilities Add constant folding for widening casts (u8->u32, f32->f64) to avoid creating intermediate types. Remove capability checks during type creati... — committed to LegNeato/rust-gpu by LegNeato a year ago
- Optimize constant casts to avoid unnecessary capabilities Add constant folding for widening casts (u8->u32, f32->f64) to avoid creating intermediate types. Remove capability checks during type creati... — committed to LegNeato/rust-gpu by LegNeato a year ago
- Optimize constant casts to avoid unnecessary capabilities Add constant folding for widening casts (u8->u32, f32->f64) to avoid creating intermediate types. Remove capability checks during type creati... — committed to Rust-GPU/rust-gpu by LegNeato a year ago
With the current system, inlining function calls is not trivial and will likely need a full redesign, or rather a separate const folding system. Currently, we only const folds things as they are emitted. Afaik at codegen time, we have no idea what kind functions do exist around us nor whether they’re const or not. We just call it blindly and hope some other codegen unit emits the body of the function so the linker can happily link them together. So const folding function calls would require link-time const folding, where we actually know of all the functions that exist and their contents.
For now, wrapping it in
const {}to make rustc do the const eval would probably be the easiest. And feel free to turn your comment into a new issue aboutconst fnevaluation.Only optimizations can cause a runtime call to a function that happens to be
const fn, to be constant-folded, across any target - you need to test with something like-C no-prepopulate-passesto disable all LLVM passes, even at optimization level0(and possibly even-Z inline-mir=off) to observe a similar effect.(And even then, LLVM fundamentally has something akin to #317 as part of “attempting to create a runtime instruction”, i.e. it will aggressively constant-fold operations between constant values w/o even any passes)
Weird, it should at least massively reduce the number of errors, but it could be something unimplemented etc. (for better or worse, those changes are nowhere near landing, and I’m glad to play around with them to see what could be made to work)
Yeah, that’s so open-ended you could put yourself into a position where
const {...}compiles but spends a long time evaluating e.g. a very long computation, that no mainstream optimizing compiler would bother trying to unfold at compile-time to get a constant result (but you’d see such a thing also remain unoptimized in LLVM, in that case).I should say that while I expect we might be able to remove the hack added in #302 (and rely on inlining + const-folding via
reduceor something similar), I don’t see a reason not to do #317 (at most I would want it to share code with thereduceimplementation, but that’s it),rustc_codegen_ssaalready expects that kind of behavior.This is part of standard Rust semantics:
const fnmerely allows calls to be done at compile-time, but does not force that - and in fact, there are some subtleties (e.g. the callee panicking, which cannot be arbitrarily moved from runtime to compile-time), which mean that care has been taken (withinrustc) to ensure runtime calls ofconst fns do not get moved to compile-time evaluation. (with a few exceptions due to&'staticpromotion rules being overly lax for a long while - mostly the fault of yours truly, I’m afraid)<del>I do have constant-folding added to
--spirt-passes=reducein a branch somewhere, but it’s sadly entangled with other SPIR-T changes and unlikely to land any time soon.</del>EDIT: so I drafted this comment days ago, but finally got back to this and you might want to try:
(you might be able to get away without
--no-early-report-zombies, but I included it just in case)It might not even need #317, if I implemented all the same operations you need (I forget, it’s been a while).
I would say we would need to move the inliner to SPIR-T, and have a simple heuristic of “how much can the callee shrink based on some subset of arguments being constant”. (or even interprocedural const-folding - LLVM has that and it always trips people up who assume
#[inline(never)]means the same as “optimizer shouldn’t peek at the body” - AFAIK nothing like that exists without separate compilation or some type-checking-only features of proof assistants, e.g. “treat this as an axiom and never unfold its definition”)https://github.com/Rust-GPU/rust-gpu/pull/317 now has const folding support for compare operations (
icmp) with ints and bools, that should fix this case as well.@nazar-pc this should inline most common integer ops and needs some testing 😄 https://github.com/Rust-GPU/rust-gpu/pull/317
It has:
Perhaps we are not using that.