[Clang][OpenMP] Crash (SIGILL, exit 132) with `collapse(2)` + `simd reduction` in function template using class-type range-for iterators
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
## Summary
Clang 22.1.5 crashes with exit code 132 (SIGILL in the frontend) when compiling a function template that combines:
- `#pragma omp parallel for collapse(2)` on two range-for loops whose iterator is a **class type** (not a plain integer), and
- a nested `#pragma omp simd reduction(+:sum)` on a plain `int` loop inside.
The crash occurs during LLVM IR generation (`EmitDeclRefLValue` → `EmitOMPWorksharingLoop`).
## Reproducer
```cpp
// clang++ -std=c++23 -fopenmp -O2 -c crash.cpp
#include
template
struct int_range {
struct iter {
int v;
int operator*() const { return v; }
iter& operator++() { ++v; return *this; }
iter operator++(int) { iter t=*this; ++v; return t; }
bool operator!=(const iter& o) const { return v != o.v; }
int operator-(const iter& o) const { return v - o.v; }
iter operator+(std::ptrdiff_t n) const { return {v + (int)n}; }
iter& operator+=(std::ptrdiff_t n) { v += (int)n; return *this; }
};
iter begin() const { return {0}; }
iter end() const { return {N}; }
};
template
void matmul(const float* __restrict__ A,
const float* __restrict__ B,
float* __restrict__ C)
{
#pragma omp parallel for collapse(2) schedule(static)
for (auto ii : int_range()) {
for (auto jj : int_range()) {
float sum = 0.f;
#pragma omp simd reduction(+:sum)
for (int k = 0; k < K; ++k)
sum += A[ii * K + k] * B[jj * K + k];
C[ii * N + jj] = sum;
}
}
}
void test(const float* A, const float* B, float* C) {
matmul<64, 512, 512>(A, B, C);
}
```
## Compiler version
```
clang version 22.1.5 (Fedora 22.1.5-1.fc44)
Target: x86_64-redhat-linux-gnu
```
## Stack trace (abridged)
```
clang++-22: error: clang frontend command failed with exit code 132
...
3. /tmp/crash.cpp:24:6: Generating code for declaration 'matmul'
#5 clang::CodeGen::CodeGenFunction::EmitDeclRefLValue(clang::DeclRefExpr const*)
#9 clang::CodeGen::CodeGenFunction::EmitCXXMemberOrOperatorMemberCallExpr(...)
#17 clang::CodeGen::CodeGenFunction::EmitOMPWorksharingLoop(...)
```
## Notes
- Removing either `collapse(2)` or the nested `#pragma omp simd` avoids the crash.
- Using plain `int` loop variables (instead of the class-type iterator) also avoids it.
- GCC 14 compiles the same code without error.
Contributor guide
Assessment
This issue has not been assessed yet.