[Mlir-commits] [flang] [mlir] [Flang][OpenMP] Support iterator modifier in map and motion clauses (PR #197757)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Oct 14 21:22:15 PDT 2026


================
@@ -1913,6 +1953,90 @@ void ClauseProcessor::processMapObjects(
   }
 }
 
+// Lower iterator-dependent locators to mapIterated and other locators to
+// mapVars.
+void ClauseProcessor::processMapObjectsWithIterator(
+    lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
+    const omp::ObjectList &objects,
+    llvm::ArrayRef<IteratorRange> iteratorRanges,
+    mlir::omp::ClauseMapFlags mapTypeBits,
+    std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
+    mlir::omp::MapClauseOps &result, llvm::SmallVectorImpl<Object> &mapObjects,
+    llvm::StringRef mapperIdNameRef, bool isMotionModifier,
+    llvm::omp::Directive directive) const {
+  if (iteratorRanges.empty()) {
+    processMapObjects(stmtCtx, clauseLocation, objects, mapTypeBits,
+                      parentMemberIndices, result.mapVars, mapObjects,
+                      mapperIdNameRef, isMotionModifier, directive);
+    return;
+  }
+
+  auto declareMapper = mlir::dyn_cast_if_present<mlir::omp::DeclareMapperOp>(
+      converter.getFirOpBuilder().getRegion().getParentOp());
+  auto isDeclareMapperVariable =
+      [&](const IteratorMapObjectAnalysis &analysis) {
+        if (!declareMapper || !analysis.rootSym)
+          return false;
+
+        mlir::Value rootAddress = converter.getSymbolAddress(*analysis.rootSym);
+        while (rootAddress) {
+          if (auto declare = rootAddress.getDefiningOp<hlfir::DeclareOp>()) {
+            rootAddress = declare.getMemref();
+            continue;
+          }
+          if (auto declare = rootAddress.getDefiningOp<fir::DeclareOp>()) {
+            rootAddress = declare.getMemref();
+            continue;
+          }
+          break;
+        }
+
+        return rootAddress == declareMapper.getRegion().front().getArgument(0);
+      };
+
+  constexpr mlir::omp::ClauseMapFlags unsupportedReferenceModifiers =
+      mlir::omp::ClauseMapFlags::ref_ptr | mlir::omp::ClauseMapFlags::ref_ptee |
+      mlir::omp::ClauseMapFlags::attach_always |
+      mlir::omp::ClauseMapFlags::attach_never |
+      mlir::omp::ClauseMapFlags::attach_auto;
+  bool hasUnsupportedReferenceModifier =
+      (mapTypeBits & unsupportedReferenceModifiers) !=
+      mlir::omp::ClauseMapFlags::none;
+
+  // Objects in an iterator-modified clause may independently reference
+  // iterator variables, so handle each object separately.
+  for (const omp::Object &object : objects) {
+    llvm::SmallVector<IteratorRange> objectRanges =
+        getIteratorRangesForObject(object, iteratorRanges);
+    if (!objectRanges.empty()) {
+      if (hasUnsupportedReferenceModifier)
+        TODO(clauseLocation,
+             "iterator modifier with reference or attach modifier");
+      IteratorMapObjectAnalysis analysis = analyzeIteratorMapObject(object);
+      bool isMapperVariable = isDeclareMapperVariable(analysis);
+      if (analysis.isDerivedTypeMember && !isMapperVariable)
+        TODO(clauseLocation, "iterator modifier with derived type member map");
+      if (declareMapper && analysis.rootSym && !isMapperVariable)
----------------
MattPD wrote:

The mapper-variable check on `analysis.rootSym` covers only the locator's root, but a locator rooted in `v` can still reference a variable of the enclosing procedure. The mapper region cannot use that variable:

```fortran
subroutine captured(idx, n)
  integer, intent(in) :: idx(1), n
  type :: t
    integer :: a(2)
  end type
  !$omp declare mapper(m: t :: v) &
  !$omp& map(iterator(i=1:1), to: v%a(idx(i)))
end subroutine
```

Save this as `repro.f90` and run `flang -c -fopenmp -fopenmp-version=52 repro.f90`. At e3eaf33, Flang reports `'hlfir.designate' op using value defined outside the region` and `verification of lowering to FIR failed`. Replacing the map clause with `map(iterator(i=n:n), to: v%a(1+0*i))` reports the same error for a `fir.load`. At the base commit 560cd8f, Flang reports the not-yet-implemented diagnostic for both inputs. Without the iterator, `map(to: v%a(idx(1)))` already fails verification at both commits.

OpenMP 5.2 section 5.8.8 lets these list items refer to entities that a procedure at the same location could reference, such as `idx`. Could such references in list items and iterator ranges get a not-yet-implemented diagnostic until the mapper region can use them? References through `v` already lower, as in `v%a(v%idx(i))`.

https://github.com/llvm/llvm-project/pull/197757


More information about the Mlir-commits mailing list