Skip to content

[naga] GLSL backend emits invalid -- token when atomicSub operand starts with a negation #9883

Description

@zero0zero7

Description
When translating WGSL atomicSub to GLSL, naga emulates it with atomicAdd by prepending - to the operand (https://github.com/gfx-rs/wgpu/blob/trunk/naga/src/back/glsl/writer.rs#L2130-L2131)). The operand is written without parentheses, so if the operand expression itself begins with -, which is the case for a negative integer literal, the output contains --, which the GLSL lexer tokenizes as the decrement operator, producing invalid GLSL.

Repro steps

var<workgroup> a: atomic<i32>;

@compute @workgroup_size(1)
fn main() {
    let x = atomicSub(&a, -1i);
}

naga repro.wgsl nagarepro.comp --shader-stage compute --entry-point main --input-kind wgsl

which produces

int _e2 = atomicAdd(a, --1);

which glslang rejects with

ERROR: 0:17: '--' :  l-value required (can't modify a const)
ERROR: 0:17: '--' :  wrong operand type no operation '--' exists that takes an operand of type  const int (or there is no acceptable conversion)
ERROR: 0:17: '' : compilation terminated
ERROR: 3 compilation errors.  No code generated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: correctnessWe're behaving incorrectlyarea: naga back-endOutputs of naga shader conversionbackend: glesIssues with GLES or WebGLlang: GLSLOpenGL Shading LanguagenagaShader Translatortype: bugSomething isn't working

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions