Skip to content

Option<Vec4> fails with "unsupported constant of type u128" since v0.11.0 #703

Open

Description

@greenhat

Since #672 (v0.11.0) a shader does not compile if it uses an enum without repr whose payload is 16-byte aligned. Option<Vec4>, Mat4::try_inverse and Vec4::try_normalize fail with:

error: unsupported constant of type `u128`

With i128:128 in the data layout rustc gives such an enum a u128 tag, and the backend does not support u128 values.

Which direction do you prefer?

  1. Go back to the 8-byte alignment of i128/u128. It undoes Can u128/i128 alignment be made 16? #665.
  2. Keep the 16-byte alignment and lower the u128 tag to supported integer types. rustc_codegen_ssa stores, loads and converts the tag as a plain u128, so I think it's the u128 part of Integer polyfills #307.

I'll be happy to send a PR for 1.

Reproducer, affected cases, bisect

tests/compiletests/ui/option_vec4.rs:

// build-pass
#![no_std]

use spirv_std::glam::Vec4;
use spirv_std::spirv;

#[spirv(fragment)]
pub fn main(input: Vec4, output: &mut Vec4) {
    let o = if input.x > 0.0 { Some(input) } else { None };
    *output = o.unwrap_or(Vec4::ZERO);
}

To reproduce:
cargo run -p compiletests --release --no-default-features --features use-installed-tools -- --target-env vulkan1.2 option_vec4

Error:

error: unsupported constant of type `u128`
   |
note: used from within `option_vec4::main`
  --> <ui/option_vec4.rs>:9:53
   |
LL |     let o = if input.x > 0.0 { Some(input) } else { None };
   |                                                     ^^^^
note: called by `main`
  --> <ui/option_vec4.rs>:8:8
   |
LL | pub fn main(input: Vec4, output: &mut Vec4) {
   |        ^^^^

The same error comes from:

  • Mat4::try_inverse, Vec4::try_normalize and Vec3A::try_normalize (at the None inside glam);
  • Option<Quat>;
  • a user enum enum Hit { Miss, At(Vec4) };
  • Option<A> with #[repr(align(16))] struct A(f32);, so it does not need glam.

Option<Vec3>, Mat3::try_inverse, Vec3::try_normalize and the Hit enum with #[repr(u32)] build.

All the cases above build at 02770fb and fail at its child c4c5e15 (#672), with the same toolchain.

The tag width comes from Integer::for_align in rustc_abi: an enum without repr(C) or repr(<int>) gets a tag as wide as the smallest alignment among the first fields of its variants, when an integer type with that size and alignment exists. The error comes from def_constant_cx at

let const_op = |builder: &mut Builder, op, lhs, maybe_rhs: Option<_>| {
// HACK(eddyb) remove after `OpSpecConstantOp` support gets added to SPIR-T.
let spirt_has_const_op = false;
if !spirt_has_const_op {
let zombie = builder.undef(ty, None);
cx.zombie_with_span(
zombie,
DUMMY_SP,
&format!("unsupported constant of type `{}`", cx.debug_type(ty)),
);
return zombie;
}

#684 removes the spirt_has_const_op hack that prints this error, but the SPIR-T validator still rejects OpTypeInt 128. So I expect the error to change to "u128 type unsupported in SPIR-V" there (read from the code, I did not build it).

  • rust-gpu: main at 60d1e25
  • Rust: nightly-2026-08-15 (rustc 1.99.0-nightly (d453bdd8f 2026-08-14))
  • Target: spirv-unknown-vulkan1.2
  • OS: Debian (Linux, aarch64)
  • SPIR-V: SPIRV-Tools v2026.4 unknown hash, 2026-10-02T11:54:22+00:00

I found it by running glam's own tests in a shader, with the help of an AI coding agent.

Activity

  1. Firestar99 commented on Nov 16, 2026

    @Firestar99
    Member

    rust-gpu is quite messy at it's core and doesn't properly support unions, thus no rust-style enums, thus no Option. It just happens that sometimes rustc optimizes away the enum before we see it, and a few months ago we've had a PR that as a side-effect made some parts of enums work.

    Sadly, we're still at the stance that rust-like enums are unsupported, and to properly support them, we'd likely have to rewrite significant parts of rust-gpu.

  2. greenhat commented on Nov 16, 2026

    @greenhat
    ContributorAuthor

    @Firestar99 Got it. Thank you for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions