Capability is required unexpectedly for constant cast
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Rust
- Sterne
- 3.4k
- Forks
- 126
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere den Shader-Kompilierungsfehler aus src/shader/compute_f1.rs:85 und vergleiche ihn mit dem K_U32-Workaround. Untersuche die generierten Capability-Anforderungen rund um den u8-to-u32-Cast und die zugehörige Integer-Implementierung in core/src/num/mod.rs:1298; abgeschlossen ist die Aufgabe, wenn der direkte Konstanten-Cast kompiliert, ohne OpCapability Int8 zu erfordern.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- rust
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100