microsoft / microsoft/DirectXShaderCompiler

[SPIR-V] Specifying max number of loops for unroll attribute is ignored

Open
#6,852 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description
When unrolling a loop, if I pass a value to it to specify the max number of iterations the loop should execute (as specified here), the wrong SPIR-V gets generated

Steps to Reproduce
See these godbolts:
https://godbolt.org/z/8eb4crMvd
https://godbolt.org/z/shTjrbToW

Actual Behavior
In the first godbolt I specify a loop with a number of iterations given by an integer literal FOO_VALUE. I also add an attribute for it to unroll and specify a max number of MAX_ITER iterations. You can check by playing around that no matter what value you set for MAX_ITER, the loop WILL be executed FOO_VALUE times (you can see this in line 18 of the generated SPIR-V).
The first godbolt helps showcase that the behaviour is wrong.

In the second godbolt I instead get the loop's end condition at runtime, so now the compiler doesn't run the loop at compile time and slap the result of the loop in the generated SPIR-V. You can see in line 26 of the generated SPIR-V the the only Loop Control Parameters generated for OpLoopMerge is Unroll.

I'd expect the parameters to also have MaxIterations followed by MAX_ITER

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 the two linked Godbolt reproductions and compare the generated SPIR-V for literal and runtime loop bounds. Inspect the OpLoopMerge Loop Control Parameters, especially the second example's Unroll output, and verify that the expected MaxIterations value is emitted when the unroll attribute specifies it.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.