You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SPIR-V] Specifying max number of loops for unroll attribute is ignored #6852
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
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
I suggest that DXC is generating is reasonable. The MaxIterations is suppose to tell us the maximum number iterations the loop will actually execute, and in this case it is not true. The loop conditons say it will execute 10 times, but the max is 5. The compiler can see that, so it is free (and reasonable) to ignore the hint.
I think this test case has undefined behaviour because the max number of iteration is wrong. If a compiler performed an optimization that changed the code assuming the max iterations is correct, the compiler would not have done anything wrong, but it would change the results.
For the second example, we could add the MaxIterations loop control if targeting vulkan1.2 or later. It requires spir-v 1.4. However, that is not required. Some optimizations might be missed in the driver, but it is still functionally correct.
The maintainers will not fix this in DXC. We will try to do it in clang. However, if someone else wants implement it, we would accept it.
the uint should be required to be static const even when emitting DXIL
What difference would that make?
static const gets treated a bit like constexpr in C++, you can actually go around stuffing it into template parameters and other things that need stuff to be compile time constant expressions.
That's not really true, since you can declare any shader input as static const in HLSL, its value will not be determined at compile time.
Also, you can declare local variables static const as well, in which case it's strictly initialized just once, like in C++.
devshgraphicsprogramming commented on Jan 31, 2025
That's not really true, since you can declare any shader input as static const in HLSL, its value will not be determined at compile time. Also, you can declare local variables static const as well, in which case it's strictly initialized just once, like in C++.
<source>:25:11: error: non-type template argument of type 'uint32_t' is not an integral constant expression
Thing<N> thing;
^
<source>:25:11: note: read of non-const variable 'N' is not allowed in a constant expression
<source>:24:14: note: declared here
uint32_t N = 1;
is not an integral constant expression
It will work with const but static const is better practice (DXC can get confused and start spamming your const global variables into the implicit first UBO / Constant Buffer)
ghost added a commit that references this issue on Nov 16, 2026
Activity
s-perron commentedon Aug 8, 2024
For the first case where you have
I suggest that DXC is generating is reasonable. The MaxIterations is suppose to tell us the maximum number iterations the loop will actually execute, and in this case it is not true. The loop conditons say it will execute 10 times, but the max is 5. The compiler can see that, so it is free (and reasonable) to ignore the hint.
I think this test case has undefined behaviour because the max number of iteration is wrong. If a compiler performed an optimization that changed the code assuming the max iterations is correct, the compiler would not have done anything wrong, but it would change the results.
For the second example, we could add the MaxIterations loop control if targeting vulkan1.2 or later. It requires spir-v 1.4. However, that is not required. Some optimizations might be missed in the driver, but it is still functionally correct.
The maintainers will not fix this in DXC. We will try to do it in clang. However, if someone else wants implement it, we would accept it.
Gaktan commentedon Jan 23, 2025
I would also like to add that doing:
(https://godbolt.org/z/56Taq1dWa)
does not unroll the loop when compiling SPIR-V. It does work when compiling DXIL though.
devshgraphicsprogramming commentedon Jan 28, 2025
this irks me, the
uintshould be required to bestatic consteven when emitting DXILGaktan commentedon Jan 29, 2025
What difference would that make?
devshgraphicsprogramming commentedon Jan 29, 2025
static constgets treated a bit likeconstexprin C++, you can actually go around stuffing it into template parameters and other things that need stuff to be compile time constant expressions.Gaktan commentedon Jan 29, 2025
That's not really true, since you can declare any shader input as
static constin HLSL, its value will not be determined at compile time.Also, you can declare local variables
static constas well, in which case it's strictly initialized just once, like in C++.devshgraphicsprogramming commentedon Jan 31, 2025
https://godbolt.org/z/YnzaYor3Y
It will work with
constbutstatic constis better practice (DXC can get confused and start spamming your const global variables into the implicit first UBO / Constant Buffer)Update submodules: DirectX-Headers to 61c77189, SPIRV-Headers to 5c60…
Update submodules: DirectX-Headers to 61c77189, SPIRV-Headers to 5c60…