Skip to content

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

Open

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

Activity

  1. added
    bug
    needs-triage
    spirv
    on Aug 8, 2024
  2. s-perron commented on Aug 8, 2024

    @s-perron
    Collaborator

    For the first case where you have

        [unroll(5)]
        for(uint i = 0; i < 10; i++)
            foo++;
    

    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.

  3. Gaktan commented on Jan 23, 2025

    @Gaktan

    I would also like to add that doing:

    (https://godbolt.org/z/56Taq1dWa)

    uint count = min(Count, MAX_COUNT);
    
    [unroll(MAX_COUNT)]
    for(uint i = 0; i < count; i++)
    

    does not unroll the loop when compiling SPIR-V. It does work when compiling DXIL though.

  4. devshgraphicsprogramming commented on Jan 28, 2025

    @devshgraphicsprogramming

    I would also like to add that doing:

    (https://godbolt.org/z/56Taq1dWa)

    uint count = min(Count, MAX_COUNT);
    
    [unroll(MAX_COUNT)]
    for(uint i = 0; i < count; i++)
    

    does not unroll the loop when compiling SPIR-V. It does work when compiling DXIL though.

    this irks me, the uint should be required to be static const even when emitting DXIL

  5. Gaktan commented on Jan 29, 2025

    @Gaktan

    the uint should be required to be static const even when emitting DXIL

    What difference would that make?

  6. devshgraphicsprogramming commented on Jan 29, 2025

    @devshgraphicsprogramming

    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.

  7. Gaktan commented on Jan 29, 2025

    @Gaktan

    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++.

  8. devshgraphicsprogramming commented on Jan 31, 2025

    @devshgraphicsprogramming

    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++.

    https://godbolt.org/z/YnzaYor3Y

        uint32_t N = 1;
        Thing<N> thing;
        return thing();
    <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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugspirv

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions