See attached patch. With collapse > 1 and a step that is not a constant,
gfc_trans_omp_do lowers each SIMD loop to a counter and assigns the DO
variable from it in the body. The DO variable then carries no
data-sharing clause at all, so under PARALLEL DO SIMD every thread writes
the same shared variable and the body indexes with another thread's
value. The fix gives the DO variable a LASTPRIVATE clause, which also
keeps its value after the loop as the standard requires for an iteration
variable.

The fix is the one new branch in gfc_trans_omp_do in trans-openmp.cc;
the rest is the new libgomp.fortran test. The DO CONCURRENT to OpenMP
series (PR128590) that I will send next applies on top of this.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR128791] Privatize the DO variable of a collapsed SIMD loop

When a loop of a collapsed SIMD, DO SIMD or PARALLEL DO SIMD nest has a
step that is not a constant, gfc_trans_omp_do iterates a counter and sets
the DO variable from it in the body.  The DO variable is then no longer
the predetermined lastprivate iteration variable, and with collapse > 1
nothing privatized it, so threads raced on it and updated wrong elements.
Make it lastprivate, as a single loop makes it linear and the non-SIMD
path makes it private.  POINTER and ALLOCATABLE DO variables, which use a
temporary for another reason, are left alone.

Assisted-by: Claude Opus 5.5

        PR fortran/128791

gcc/fortran/ChangeLog:

        * trans-openmp.cc (gfc_trans_omp_do): Make the DO variable of a
        collapsed SIMD loop with a non-constant step lastprivate.

libgomp/ChangeLog:

        * testsuite/libgomp.fortran/simd-collapse-step-1.f90: New test.
>From fce885600ceb699dee9e3c92463c080d27df1a51 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 10 Nov 2026 12:30:28 -0800
Subject: [PATCH] fortran: [PR128791] Privatize the DO variable of a collapsed
 SIMD loop

When a loop of a collapsed SIMD, DO SIMD or PARALLEL DO SIMD nest has a
step that is not a constant, gfc_trans_omp_do iterates a counter and sets
the DO variable from it in the body.  The DO variable is then no longer
the predetermined lastprivate iteration variable, and with collapse > 1
nothing privatized it, so threads raced on it and updated wrong elements.
Make it lastprivate, as a single loop makes it linear and the non-SIMD
path makes it private.  POINTER and ALLOCATABLE DO variables, which use a
temporary for another reason, are left alone.

Assisted-by: Claude Opus 5.5

	PR fortran/128791

gcc/fortran/ChangeLog:

	* trans-openmp.cc (gfc_trans_omp_do): Make the DO variable of a
	collapsed SIMD loop with a non-constant step lastprivate.

libgomp/ChangeLog:

	* testsuite/libgomp.fortran/simd-collapse-step-1.f90: New test.
---
 gcc/fortran/trans-openmp.cc                   | 11 ++++
 .../libgomp.fortran/simd-collapse-step-1.f90  | 65 +++++++++++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 libgomp/testsuite/libgomp.fortran/simd-collapse-step-1.f90

diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 13b17e573db..03fe5099c09 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -7615,6 +7615,17 @@ gfc_trans_omp_do (gfc_code *code, gfc_exec_op op, stmtblock_t *pblock,
 	      if (local_dovar != dovar)
 		dovar_found = 3;
 	    }
+	  else if (!simple
+		   && !code->ext.iterator->var->symtree->n.sym->attr.pointer
+		   && !code->ext.iterator->var->symtree->n.sym->attr.allocatable)
+	    {
+	      /* The body sets DOVAR from the counter, so it is no longer the
+		 predetermined lastprivate iteration variable.  */
+	      tmp = build_omp_clause (input_location, OMP_CLAUSE_LASTPRIVATE);
+	      OMP_CLAUSE_DECL (tmp) = dovar_decl;
+	      omp_clauses = gfc_trans_add_clause (tmp, omp_clauses);
+	      dovar_found = 2;
+	    }
 	}
       else if (!dovar_found && local_dovar != dovar)
 	{
diff --git a/libgomp/testsuite/libgomp.fortran/simd-collapse-step-1.f90 b/libgomp/testsuite/libgomp.fortran/simd-collapse-step-1.f90
new file mode 100644
index 00000000000..95f182104d2
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/simd-collapse-step-1.f90
@@ -0,0 +1,65 @@
+! { dg-do run }
+! { dg-set-target-env-var OMP_NUM_THREADS "4" }
+!
+! PR 128791
+
+program main
+  implicit none
+  integer :: i, j, k, n, st, st2
+  integer :: a(60,60,8)
+
+  n = 50
+  st = -3
+  st2 = 2
+
+  a = 0
+  !$omp parallel do simd collapse(2)
+  do i = n, 1, st
+     do j = 1, 3
+        a(i,j,1) = a(i,j,1) + 1
+     end do
+  end do
+  call check (1)
+
+  a = 0
+  !$omp parallel
+  !$omp do simd collapse(3)
+  do i = n, 1, st
+     do j = 1, 7, st2
+        do k = 1, 1
+           a(i,j,1) = a(i,j,1) + 1
+        end do
+     end do
+  end do
+  !$omp end parallel
+  call check (2)
+
+  a = 0
+  !$omp target teams distribute parallel do simd collapse(2) map(tofrom: a)
+  do i = n, 1, st
+     do j = 1, 3
+        a(i,j,1) = a(i,j,1) + 1
+     end do
+  end do
+  call check (3)
+
+contains
+
+  subroutine check (which)
+    integer, intent(in) :: which
+    integer :: ii, jj, expect
+    do jj = 1, 60
+       do ii = 1, 60
+          expect = 0
+          if (ii <= n .and. mod (n - ii, -st) == 0) then
+             if (which == 2) then
+                if (jj <= 7 .and. mod (jj - 1, st2) == 0) expect = 1
+             else if (jj <= 3) then
+                expect = 1
+             end if
+          end if
+          if (a(ii,jj,1) /= expect) stop which
+       end do
+    end do
+  end subroutine check
+end program main
-- 
2.56.1

Reply via email to