[unroll] with a large constant trip count is unbounded
Los mantenedores suelen responder en 2 días
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 55/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- cpp
- Área
- compilers, performance
Línea de trabajo
Start by reading lib/Transforms/Scalar/DxilLoopUnroll.cpp around the DxilLoopUnroll logic at lines 956–963, where known trip counts replace MaxIterationAttempt. Compare that path with the existing limit for unknown trip counts and reproduce the reported behavior using the provided 100,000-iteration HLSL shader and dxc command. Done means compile time and memory stay bounded for large constant trip counts while the loop can remain in place.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Description
When a loop carries [unroll] and its trip count is a compile-time constant, DXC unrolls it completely, however large the count is. For a shader of constant size the compile time and the memory grow linearly with the count, and nothing bounds them. The same loop with [loop], or with no attribute, compiles in a few milliseconds.
The loop is in DxilLoopUnroll, lib/Transforms/Scalar/DxilLoopUnroll.cpp, lines 956–963 at b43c5651:
unsigned MaxAttempt = this->MaxIterationAttempt; // 1024 by default
// If we were able to figure out the definitive trip count,
// just unroll that many times.
if (TripCount != 0) {
MaxAttempt = TripCount;
} else if (HasExplicitLoopCount) {
MaxAttempt = ExplicitUnrollCount;
}
for (unsigned IterationI = 0; IterationI < MaxAttempt; IterationI++) {
The MaxIterationAttempt limit of 1024 only applies when the trip count is not known.
This is a resource problem, not a correctness one: nothing is miscompiled, and nothing is read or written out of bounds. It matters for programs that compile shaders they are given, where a few hundred bytes of source can keep a core busy for minutes.
#8789 keeps a bare [unroll] as a full-unroll request for every language version, so it does not change this.
Steps to Reproduce
A compute shader with a loop of 100 000 iterations under [unroll]:
poc.hlsl
RWByteAddressBuffer buf : register(u0);
[numthreads(1,1,1)]
void main(uint3 tid : SV_GroupThreadID) {
uint x = tid.x;
[unroll]
for (uint i = 0; i < 100000; i++) { x = x * 1664525u + 1013904223u; }
buf.Store(0, x);
}
/usr/bin/time dxc -T cs_6_0 -E main poc.hlsl -Fo poc.dxil
Control, control.hlsl: the same shader with [loop].
control.hlsl
RWByteAddressBuffer buf : register(u0);
[numthreads(1,1,1)]
void main(uint3 tid : SV_GroupThreadID) {
uint x = tid.x;
[loop]
for (uint i = 0; i < 100000; i++) { x = x * 1664525u + 1013904223u; }
buf.Store(0, x);
}
Changing the trip count is the only other variable in the table below.
Actual Behavior
Every run exits with code 0 and produces DXIL. Wall-clock time and peak resident memory with Release 1.9.2609.5 on the host listed under Environment:
| Trip count | [unroll] |
[loop] |
|---|---|---|
| 1 000 | 0.02 s, 27 MiB | 0.00 s, 22 MiB |
| 20 000 | 1.5 s, 145 MiB | 0.01 s, 22 MiB |
| 100 000 | 41 s, 699 MiB | 0.00 s, 21 MiB |
Without any attribute the 100 000-iteration loop also compiles in 0.00 s with 22 MiB.
The loop body is cloned once per iteration, so both numbers keep growing with the count. A build with AddressSanitizer and assertions takes roughly twice as long (100 000: 84 s, 1.25 GiB).
Expected: compile time and memory stay bounded. When the trip count is not known, the loop quoted above runs at most MaxIterationAttempt times; a similar bound for a known but very large trip count would cap the cost and leave the loop in place.
Environment
- DXC version
- Release 1.9.2609.5:
libdxcompiler.so: 1.9(5512-01b62ad4)(1.9.2609.5) - Built from source at
b43c5651d744e606552b3274668a160be21db0f8(main, 2026-11-24):libdxcompiler.so: 1.11(1-b43c5651)(1.10.0.10001), with AddressSanitizer and assertions. - The code quoted above is the same on current main (
2600ae89a615); I have not compiled that revision.
- Release 1.9.2609.5:
- Host Operating System
- Ubuntu 24.04.4 LTS, x86_64, kernel 6.8, AMD EPYC 9554P.
- Lenguaje dominante
- C++
- Estrellas
- 3.7k
- Forks
- 926
- Merge medio
- 3 d 22 h
- PR fusionados (30 d)
- 45
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de microsoft/DirectXShaderCompiler
-
bug needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
microsoft/DirectXShaderCompiler#9173 ·
Los mantenedores suelen responder en 2 días
-
bug needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
microsoft/DirectXShaderCompiler#9155 · 4 comentarios ·
Los mantenedores suelen responder en 2 días
-
bug needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
microsoft/DirectXShaderCompiler#9135 ·
Los mantenedores suelen responder en 2 días
-
DXC emits DW_LANG_C_plus_plus instead of DW_LANG_HLSL for HLSLPosiblemente ocupada @rj123-nv la tomó hace 18 días. Abiertobug needs-triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
microsoft/DirectXShaderCompiler#9087 ·
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
microsoft/DirectXShaderCompiler#9086 ·
Los mantenedores suelen responder en 2 días
Todos los issues de microsoft/DirectXShaderCompiler
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
feature:config kind:docs os:windows toolchain:cmake triaged
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día
-
arch-based bug niri
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
noctalia-dev/noctalia#5552 ·
Los mantenedores suelen responder en 1 día