[flang-commits] [flang] [flang][OpenMP] Use LoopRange to implement loop var type checking, NFC (PR #181797)

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Tue Feb 17 02:27:54 PST 2026


https://github.com/kparzysz created https://github.com/llvm/llvm-project/pull/181797

None

>From 1c010f5492533e410503813daa3d0a8028f7a9f7 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Thu, 12 Feb 2026 10:08:31 -0600
Subject: [PATCH] [flang][OpenMP] Use LoopRange to implement loop var type
 checking, NFC

---
 flang/lib/Semantics/check-omp-loop.cpp    | 30 +++++++++--------------
 flang/lib/Semantics/check-omp-structure.h |  2 +-
 flang/test/Semantics/OpenMP/do10.f90      | 14 +++++------
 3 files changed, 20 insertions(+), 26 deletions(-)

diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index 80d2b90baf939..49f78396b4fe8 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -469,7 +469,7 @@ void OmpStructureChecker::Enter(const parser::OpenMPLoopConstruct &x) {
       CheckNoBranching(doBlock, beginName.v, beginName.source);
     }
   }
-  CheckLoopItrVariableIsInt(x);
+  CheckIterationVariableType(x);
   CheckNestedConstruct(x);
   CheckFullUnroll(x);
   CheckAssociatedLoopConstraints(x);
@@ -500,29 +500,23 @@ void OmpStructureChecker::SetLoopInfo(const parser::OpenMPLoopConstruct &x) {
   }
 }
 
-void OmpStructureChecker::CheckLoopItrVariableIsInt(
+void OmpStructureChecker::CheckIterationVariableType(
     const parser::OpenMPLoopConstruct &x) {
-  for (auto &construct : std::get<parser::Block>(x.t)) {
-    for (const parser::DoConstruct *loop{
-             parser::omp::GetDoConstruct(construct)};
-        loop;) {
+  using LoopRange = parser::omp::LoopRange;
+  auto &body{std::get<parser::Block>(x.t)};
+  for (auto &construct : LoopRange(body, LoopRange::Step::Into)) {
+    // 'construct' can also be OpenMPLoopConstruct
+    if (auto *loop{parser::Unwrap<parser::DoConstruct>(construct)}) {
       if (loop->IsDoNormal()) {
-        const parser::Name &itrVal{GetLoopIndex(loop)};
-        if (itrVal.symbol) {
-          const auto *type{itrVal.symbol->GetType()};
+        if (const parser::Name &iv{GetLoopIndex(loop)}; iv.symbol) {
+          const auto *type{iv.symbol->GetType()};
           if (!type->IsNumeric(TypeCategory::Integer)) {
-            context_.Say(itrVal.source,
-                "The DO loop iteration"
-                " variable must be of the type integer."_err_en_US,
-                itrVal.ToString());
+            context_.Say(iv.source,
+                "The DO loop iteration variable must be of integer type"_err_en_US,
+                iv.ToString());
           }
         }
       }
-      // Get the next DoConstruct if block is not empty.
-      const auto &block{std::get<parser::Block>(loop->t)};
-      const auto it{block.begin()};
-      loop = it != block.end() ? parser::Unwrap<parser::DoConstruct>(*it)
-                               : nullptr;
     }
   }
 }
diff --git a/flang/lib/Semantics/check-omp-structure.h b/flang/lib/Semantics/check-omp-structure.h
index 3bbfd61ca4d2b..93d44babf0b43 100644
--- a/flang/lib/Semantics/check-omp-structure.h
+++ b/flang/lib/Semantics/check-omp-structure.h
@@ -281,7 +281,7 @@ class OmpStructureChecker : public OmpStructureCheckerBase {
 
   void CheckIteratorRange(const parser::OmpIteratorSpecifier &x);
   void CheckIteratorModifier(const parser::OmpIterator &x);
-  void CheckLoopItrVariableIsInt(const parser::OpenMPLoopConstruct &x);
+  void CheckIterationVariableType(const parser::OpenMPLoopConstruct &x);
   void CheckDoWhile(const parser::OpenMPLoopConstruct &x);
   void CheckAssociatedLoopConstraints(const parser::OpenMPLoopConstruct &x);
   template <typename T, typename D> bool IsOperatorValid(const T &, const D &);
diff --git a/flang/test/Semantics/OpenMP/do10.f90 b/flang/test/Semantics/OpenMP/do10.f90
index 7e8105e125a92..936d94d591369 100644
--- a/flang/test/Semantics/OpenMP/do10.f90
+++ b/flang/test/Semantics/OpenMP/do10.f90
@@ -1,14 +1,14 @@
 ! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
 ! OpenMP Version 4.5
 ! 2.7.1 Loop Construct
-! The DO loop iteration variable must be of type integer.
+! The DO loop iteration variable must be of integer type.
 
 program omp_do
   real i, j, k
   !$omp do
-  !ERROR: The DO loop iteration variable must be of the type integer.
+  !ERROR: The DO loop iteration variable must be of integer type
   do i = 1, 10
-    !ERROR: The DO loop iteration variable must be of the type integer.
+    !ERROR: The DO loop iteration variable must be of integer type
     do j = 1, 10
       print *, "it", i, j
     end do
@@ -17,9 +17,9 @@ program omp_do
 
   !ERROR: The value of the parameter in the COLLAPSE or ORDERED clause must not be larger than the number of nested loops following the construct.
   !$omp do collapse(3)
-  !ERROR: The DO loop iteration variable must be of the type integer.
+  !ERROR: The DO loop iteration variable must be of integer type
   do i = 1, 10
-    !ERROR: The DO loop iteration variable must be of the type integer.
+    !ERROR: The DO loop iteration variable must be of integer type
     do j = 1, 10
       print *, "it", i, j
     end do
@@ -27,9 +27,9 @@ program omp_do
   !$omp end do
 
   !$omp do collapse(2)
-  !ERROR: The DO loop iteration variable must be of the type integer.
+  !ERROR: The DO loop iteration variable must be of integer type
   do i = 1, 10
-    !ERROR: The DO loop iteration variable must be of the type integer.
+    !ERROR: The DO loop iteration variable must be of integer type
     do j = 1, 10
       print *, "it", i, j
     end do



More information about the flang-commits mailing list