Skip to content

Codegen error for log10 #199

Closed
Closed
@bspeice

Description

@bspeice

Expected Behaviour

Able to compile a SPIR-V compute shader using the log10 operation.

Example & Steps To Reproduce

Minimal reproducer available at: https://github.com/bspeice/rust-gpu-log10-err/blob/main/crates/shader/src/lib.rs

I hit an error when spirv-builder attempted to run a shader through spirv-val:

#![no_std]
use spirv_std::glam::UVec3;
use spirv_std::num_traits::Float;
use spirv_std::spirv;

#[spirv(compute(threads(1)))]
pub fn render_image_histogram(
    #[spirv(global_invocation_id)] _global_id: UVec3,
    #[spirv(storage_buffer, descriptor_set = 0, binding = 0)] input: &[f32],
    #[spirv(storage_buffer, descriptor_set = 0, binding = 1)] output: &mut [f32],
) {
    output[0] = input[0].log10();
}
  error: Expected int scalar or vector type as Result Type: IMul
    |
    = note: module `/home/bspeice/Development/log10/target/spirv-builder/spirv-unknown-vulkan1.2/release/deps/shader.spv`

  warning: an unknown error occurred
    |
    = note: spirv-opt failed, leaving as unoptimized
    = note: module `/home/bspeice/Development/log10/target/spirv-builder/spirv-unknown-vulkan1.2/release/deps/shader.spv`

  error: error:0:0 - Expected int scalar or vector type as Result Type: IMul
           %35 = OpIMul %float %float_0_434294492 %34
    |
    = note: spirv-val failed
    = note: module `/home/bspeice/Development/log10/target/spirv-builder/spirv-unknown-vulkan1.2/release/deps/shader.spv`

  warning: `shader` (lib) generated 1 warning
  error: could not compile `shader` (lib) due to 2 previous errors; 1 warning emitted
  Error: BuildFailed

If I understand the output correctly, codegen produced a SPIR-V output that included an OpIMul, when it should have been an OpFMul. spirv-opt then failed, and the output was left unoptimized. Afterward, spirv-val failed validation for the same problem that spirv-opt failed on.

It seems like log10 is implemented with a change of base, so I'm a bit confused on why this gets turned into an OpIMul. Writing the operation out as 1.0 / 10.0.ln() * input[0].ln() seems to fix things.

Separately, cargo warns me that the Float trait is unused (but fails to compile without); guessing that's a bug, but not sure if related:

warning: unused import: `spirv_std::num_traits::Float`
 --> crates/shader/src/lib.rs:3:5
  |
3 | use spirv_std::num_traits::Float;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

System Info

  • Rust: rustc 1.84.0-nightly (b19329a37 2024-11-21) (spirv-builder 1d76d59)
  • OS: Ubuntu 24.04
  • GPU: NVidia RTX 3070
  • SPIR-V: SPIRV-Tools v2023.6 unknown hash, 2024-02-07T17:19:14+00:00

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions