EmbarkStudios / rust-gpu

🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧

Home Page:https://shader.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GEP not implemented for type struct [_; 0]

kevinboulain opened this issue · comments

Expected Behaviour

This compiles fine with rustc (I guess this bit is probably eliminated?).

Example & Steps To Reproduce

I was using const generic parameters to determine the length of some arrays. At some point, a base case ended up declaring a zero-length array, like so:

#![cfg_attr(target_arch = "spirv", no_std)]
use spirv_std::spirv;

fn example<const LENGTH: usize>() {
    let mut array = [0; LENGTH];
    for i in 0..array.len() {
        array[i] += i;
    }
}

#[spirv(compute(threads(1, 1, 1)))]
pub fn compute() {
    example::<0>();
}

Resulting in:

error: GEP not implemented for type struct [usize; 0] {  }
 --> examples/shaders/compute-shader/src/lib.rs:7:9
  |
7 |         array[i] += i;
  |         ^^^^^^^^^^^^^

System Info

  • rustc: 1.71.0-nightly (1a5f8bce7 2023-05-26)
  • rust-gpu: b2e5eb7

A slight modification of the previous example can also result in a division by zero in the compiler (can open another issue if you prefer):

#![cfg_attr(target_arch = "spirv", no_std)]
use spirv_std::spirv;

fn example<const LENGTH: usize, R: Copy + Default>(callback: impl Fn() -> R) {
    let mut array = [Default::default(); LENGTH];
    for i in 0..array.len() {
        array[i] = callback();
    }
}

#[spirv(compute(threads(1, 1, 1)))]
pub fn compute() {
    example::<10, ()>(|| ());
}
thread 'rustc' panicked at 'attempt to divide by zero', crates/rustc_codegen_spirv/src/builder/builder_methods.rs:441:34
stack backtrace:
   0: rust_begin_unwind
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/library/std/src/panicking.rs:578:5
   1: core::panicking::panic_fmt
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/library/core/src/panicking.rs:67:14
   2: core::panicking::panic
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/library/core/src/panicking.rs:117:5
   3: rustc_codegen_spirv::builder::builder_methods::<impl rustc_codegen_spirv::builder::Builder>::recover_access_chain_from_offset
             at ./crates/rustc_codegen_spirv/src/builder/builder_methods.rs:441:34
   4: rustc_codegen_spirv::builder::builder_methods::<impl rustc_codegen_ssa::traits::builder::BuilderMethods for rustc_codegen_spirv::builder::Builder>::pointercast
             at ./crates/rustc_codegen_spirv/src/builder/builder_methods.rs:1624:13
   5: rustc_codegen_ssa::base::unsize_ptr
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/base.rs:225:14
   6: rustc_codegen_ssa::mir::rvalue::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_rvalue_operand
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/mir/rvalue.rs:462:29
   7: rustc_codegen_ssa::mir::statement::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_statement
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/mir/statement.rs:22:43
   8: rustc_codegen_ssa::mir::block::<impl rustc_codegen_ssa::mir::FunctionCx<Bx>>::codegen_block
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/mir/block.rs:1178:17
   9: rustc_codegen_ssa::mir::codegen_mir
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/mir/mod.rs:272:9
  10: rustc_codegen_ssa::base::codegen_instance
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/base.rs:395:5
  11: <rustc_middle::mir::mono::MonoItem as rustc_codegen_ssa::mono_item::MonoItemExt>::define
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/mono_item.rs:91:17
  12: <rustc_codegen_spirv::SpirvCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::compile_codegen_unit::{{closure}}
             at ./crates/rustc_codegen_spirv/src/lib.rs:438:17
  13: <rustc_codegen_spirv::SpirvCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::compile_codegen_unit
             at ./crates/rustc_codegen_spirv/src/lib.rs:450:36
  14: rustc_codegen_ssa::base::codegen_crate
             at /rustc/1a5f8bce74ee432f7cc3aa131bc3d6920e06de10/compiler/rustc_codegen_ssa/src/base.rs:728:34
  15: <rustc_codegen_spirv::SpirvCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
             at ./crates/rustc_codegen_spirv/src/lib.rs:241:18
  16: <rustc_session::session::Session>::time::<alloc::boxed::Box<dyn core::any::Any>, rustc_interface::passes::start_codegen::{closure#0}>
  17: rustc_interface::passes::start_codegen
  18: <rustc_middle::ty::context::GlobalCtxt>::enter::<<rustc_interface::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core::result::Result<alloc::boxed::Box<dyn core::any::Any>, rustc_span::ErrorGuaranteed>>
  19: <rustc_interface::queries::Queries>::ongoing_codegen
  20: <rustc_interface::interface::Compiler>::enter::<rustc_driver_impl::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_span::ErrorGuaranteed>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/EmbarkStudios/rust-gpu/issues/new

note: rustc 1.71.0-nightly (1a5f8bce7 2023-05-26) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type dylib --crate-type lib -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 -C incremental=[REDACTED] -Z unstable-options -Z codegen-backend=.../rust-gpu/target/debug/deps/librustc_codegen_spirv.so -Z binary-dep-depinfo -C symbol-mangling-version=v0 -Z crate-attr=feature(register_tool) -Z crate-attr=register_tool(rust_gpu) -C overflow-checks=off -C debug-assertions=off

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
note: `rust-gpu` version `0.8.0`

I also encountered the attempt to divide by zero error. I made this change on a fork and the build succeeded and appeared to work.

I'm not opening a PR though because I made that change blindly (I'm not familiar with the internals of rust-gpu and have no idea what's going on :), so I'm not confident that it's sound.

I made this change on a fork and the build succeeded and appeared to work.

Oh, that looks great! (I would only replace the panic but I haven't checked yet what's valid at that position)

(sorry for not seeing this sooner, up until more recently the only way to guarantee I'd actually get notified would be to use @eddyb or ping me on Discord)