Skip to content

[SPIR-V] static const arrays lose their initializers in library mode #8425

@castano

Description

@castano

Description
static const arrays lose their initializers when compiling in library mode (-T lib_6_*) because deferred OpStore initialization needs an entry-function body that doesn't exist in library modules.

Steps to Reproduce

Here's a simple repro case:

static const uint table[4] = { 10u, 20u, 30u, 40u };

export uint lookup(uint i) {
    return table[i];
}

Here's the output on godbolt:

https://godbolt.org/z/r1Y37z5d7

This was blocking my progress toward shipping the Spark codecs using SPIR-V libraries, so I went ahead and tried to fix it on my end.

The solution I've implemented is to fold the InitListExpr aggregate initializers into an OpConstantComposite at the OpVariable's Initializer slot when we are in library mode and the initializer is constant-foldable. I also had to extend ConstEvaluator::translateAPValue to handle APValue::Array, which clang's APValue carries for evaluated array initializers.

If this approach sounds reasonable, I'd be happy to submit a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug, regression, crashneeds-triageAwaiting triagespirvWork related to SPIR-V

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions