microsoft / microsoft/DirectXShaderCompiler
[SPIR-V] static const arrays lose their initializers in library mode
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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