microsoft / microsoft/DirectXShaderCompiler

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

Open
#8,425 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-triage spirv
Dominant language
C++
Stars
3.7k
Forks
900
Avg merge
2d 11h
Merged PRs (30d)
44

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with library-mode handling for static const array initializers and trace ConstEvaluator::translateAPValue, including its APValue::Array path. Use the HLSL repro and compare the generated SPIR-V; done means the library module preserves the array values in the OpVariable initializer and lookup returns the expected elements.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.