Skip to content

Death to baked expressions #4511

@jimblandy

Description

@jimblandy

Naga IR should be changed to eliminate the need to bake expressions.

Baked expressions are a Naga-specific piece of ugly magic: subexpressions that we identify as needing to be evaluated in isolation, not as part of the Statement that actually refers to them. Baking an expression means that it's emitted as an initialized temporary variable. References to the expression are then rendered as uses of that variable.

Examples would be Load expressions, which need to be ordered properly with respect to Call statements that could assign to the variables they read. But it also includes stuff like the operands of MathFunction::Dot applications on Metal, which open-codes the dot product operation, and wants its operands to be cached in locals in advance to avoid repeated evaluation.

We flag expressions for baking pessimistically. For example, most Loads do not need to be separated out from the rest of the expression, but we produce (in WGSL output) a separate let declaration for every load. For the GLSL input:

    vec4 color = Color;

we generate the WGSL:

    let _e4 = global.Color;
    color = _e4;

That _e4 temporary is the result of baking.

The semantics of Naga IR should be such that backends don't need to filter through the expressions covered by an Emit and play strange games with reference count thresholds to decide what to generate.

A better approach would not only simplify Emit statement processing in backends, but also accommodate backend-specific needs like inlined Dot calls (or the broad selection of functions in the HLSL backend).

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind: refactorMaking existing function faster or nicernagaShader Translator

    Type

    No type

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions