Skip to content

[HLSL 202x] Unroll Attribute #8789

Closed
Closed

Description

From https://github.com/microsoft/hlsl-specs/blob/main/proposals/0022-hlsl-loop-unroll-factor.md:

In HLSL 202X the DXC implementation of the loop unroll factor should no longer alter the number of iteration executions. Ideally the loop unroll factor will also become a compiler hint for partial loop unroll. If this can not be done because of concerns of invalid DXIL generation then the loop unroll factor as a feature should be removed from DXC. Removal in this case means ignoring the unroll factor and issuing a warning to inform the user.

Activity

  1. farzonl commented on Aug 19, 2026

    @farzonl
    Collaborator

    From what I am seeing in DXC we actually do two different loop unrolls. one is the standard loop unroll that exists in clang and the other is a custom loop unroll that preempts the default one and runs first that can change loop execution behavior. The default one seems to only kick in on -O3 while the HLSL one works at all optimization levels. the default one then only really kicks in if the hlsl loop unroll does not impact loop execution and if we are doing the highest level of optimization

    Looking at the DXC code is giving me some new perspectice. if we just turn off the HLSL loop unroll then we also have to accept that the compiler might not unroll and likely won't unless you do O3. I think that should be fine because it is what clang does.

    The good news. I thinkit should be easier to do this feature than we originally though.

  2. farzonl commented on Aug 19, 2026

    @farzonl
    Collaborator

    Here is the behavior I think we will do for DXC

    Attribute and mode Behavior
    [unroll(N)], HLSL 2021 or earlier Preserve the legacy maximum-execution-count behavior.
    [unroll(N)], HLSL 202x or later, -O3 Treat N as a requested partial-unroll factor while preserving the loop termination condition. The optimizer may decline or adjust the request.
    [unroll(N)], HLSL 202x or later, below -O3 Preserve the loop and its termination condition. Record the hint in loop metadata, but do not promise that unrolling occurs.
    [unroll], any language version Preserve the existing full-unroll request.
    [loop], any language version Preserve the existing unroll-disable behavior.
  3. farzonl commented on Aug 19, 2026

    @farzonl
    Collaborator

    As I said earlier, the HLSL optimization pipeline currently runs two unrollers:

    1. DxilLoopUnroll, which implements required HLSL full unrolling and the
      legacy counted-loop behavior.
    2. LLVM's standard loop-unroll pass at -O3, which understands
      llvm.loop.unroll.count as a partial-unroll hint and preserves residual
      iterations.

    Implementation

    The implementation will pass a language-version-derived flag through
    PassManagerBuilder to DxilLoopUnroll. For HLSL 202x and later,
    DxilLoopUnroll will leave loops carrying llvm.loop.unroll.count unchanged.
    The standard unroller may then consume the metadata at -O3. For older
    language versions, DxilLoopUnroll will continue using the count as an
    execution limit.

    The custom pass will continue handling llvm.loop.unroll.full in every
    language version. This keeps bare [unroll] behavior and its diagnostics
    unchanged.

  4. added 4 commits that reference this issue on Aug 24, 2026
    9d355e0
    7309e06
    0a76d68
    b42f0f6
  5. moved this from Planning to Closed in HLSL Supporton Aug 27, 2026
  6. added a commit that references this issue on Aug 27, 2026
    3bac247
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions