Skip to content

Naga GLSL front end does not properly short-circuit && and || #4394

@jimblandy

Description

@jimblandy

NOTE 2025-11-24: WGSL was fixed by #7339, so this issue now only applies to GLSL.
NOTE 2024-04-16: description edited, so much of the following discussion may look irrelevant

The WGSL specification says that a && b only evaluates b if a is true, but Naga generally will always evaluate both.

The following input:

fn h() -> bool {
  return f() || g();
}

produces the following output WGSL:

fn h() -> bool {
    let _e0 = f();
    let _e1 = g();
    return (_e0 || _e1);
}

The call to g is hoisted out to a statement and made unconditional, which is incorrect.

This means that even though most backends turn Naga IR's BinaryOperator::LogicalAnd and BinaryOperator::LogicalOr into short-circuiting operations in the target language, it doesn't help because the front end has already hoisted the right-hand side, which ought to be conditional, out into its own unconditional statement.

Metadata

Metadata

Assignees

Labels

area: naga front-endTranslation to Naga IRlang: GLSLOpenGL Shading Languagelang: HLSLD3D Shading Languagelang: SPIR-VVulkan's Shading LanguagenagaShader Translatortype: bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions