GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran

Fortran SIMD loops: Privatize DO variable for collapsed loops

Fortran compiler now correctly privatizes the DO variable in collapsed SIMD loops to prevent data races when the loop step is not constant.

This change addresses a data race issue in Fortran’s handling of collapsed SIMD loops (DO SIMD, PARALLEL DO SIMD). When such loops have a non-constant step or a dummy argument as the DO variable, the compiler internally uses a separate counter and assigns its value to the DO variable within the loop body. This made the DO variable not the intended lastprivate iteration variable, causing threads to race on it, leading to incorrect updates and DEFAULT(NONE) rejections. The fix makes the DO variable lastprivate, ensuring correct behavior similar to single loops and preventing races.

In Details

In gfc_trans_omp_do within gcc/fortran/trans-openmp.cc, the DO variable of a collapsed SIMD loop nest is now explicitly marked lastprivate when its step is non-constant or when it's a dummy argument. This situation triggers an internal separate counter and assignment within the loop body. Without this change, the DO variable was not properly privatized for collapsed loops (especially with collapse > 1), leading to data races. This modification aligns its treatment with single loops, where it's implicitly linear or private, and resolves issues with DEFAULT(NONE).

For Context
SIMD
Single Instruction, Multiple Data. A type of parallel processing where the same operation is performed on multiple data points simultaneously.
DO SIMD loop
A Fortran loop construct that enables Single Instruction, Multiple Data (SIMD) vectorization, allowing the compiler to execute loop iterations in parallel on multiple data elements.
collapsed loop
In OpenMP, a loop nest where multiple loop levels are treated as a single, larger iteration space to improve parallelism.
lastprivate
An OpenMP clause specifying that a variable is private to each thread, and its final value is the one from the thread that executed the last iteration.
data race
A concurrency issue where two or more threads access the same memory location concurrently, and at least one of the accesses is a write, leading to unpredictable program behavior.
DEFAULT(NONE)
An OpenMP directive clause that requires all variables used in a parallel region to be explicitly declared with a data-sharing attribute (like private, shared, firstprivate, lastprivate).
Filed Under: fortranopenmpsimdparallelismrace condition