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

via flang-commits flang-commits at lists.llvm.org
Wed Aug 5 22:25:21 PDT 2026


================
@@ -1924,6 +1968,56 @@ void ClauseProcessor::processMapObjects(
   }
 }
 
+// Process objects in map/motion clauses, lowering iterator-dependent
+// locators into `result.mapIterated` and all others through the regular
+// map-lowering path. A null `ivSyms` indicates that no iterator modifier
+// is present.
+void ClauseProcessor::processMapObjectsWithIterator(
+    lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
+    const omp::ObjectList &objects,
+    llvm::ArrayRef<IteratorRange> iteratorRanges,
+    const llvm::SmallPtrSetImpl<const semantics::Symbol *> *ivSyms,
+    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 (!ivSyms) {
+    processMapObjects(stmtCtx, clauseLocation, objects, mapTypeBits,
+                      parentMemberIndices, result.mapVars, mapObjects,
+                      mapperIdNameRef, isMotionModifier, directive);
+    return;
+  }
+
+  // Inside a declare mapper, the mapper variable's components are mapped
+  // directly, allowing member locators such as v%a(i). Mapping a derived-type
+  // member outside a declare mapper requires parent/member handling that
+  // iterator modifiers do not support yet.
+  bool inDeclareMapper = mlir::isa_and_present<mlir::omp::DeclareMapperOp>(
+      converter.getFirOpBuilder().getRegion().getParentOp());
+
+  // Objects in an iterator-modified clause may independently reference
+  // iterator variables, so handle each object separately.
+  for (const omp::Object &object : objects) {
+    if (hasIteratorIVReference(object, *ivSyms)) {
+      if (!inDeclareMapper && getBaseObject(object, semaCtx))
+        TODO(clauseLocation, "iterator modifier with derived type member map");
----------------
MattPD wrote:

Thanks, comparing the object's base against the declare-mapper variable is the right predicate. One shape still gets past it. `getBaseObject` returns nothing for a locator that is a plain array element. `isDeclareMapperVariable` is therefore never consulted for that locator. With a module array, the address lookup then returns a null value. Lowering then trips an assertion instead of reporting an error.

Save the following as `repro.f90`:

```fortran
module mv
  integer :: tbl(100)
  type :: t
    real :: a(100)
  end type
  !$omp declare mapper(mm: t :: v) map(iterator(i = 1:100): v%a(i), tbl(i))
end module
```

Then run `flang -fc1 -emit-hlfir -fopenmp -fopenmp-version=52 -o /dev/null repro.f90`.

At b98b49c that input aborts with ``Assertion `detail::isPresent(Val) && "dyn_cast on a non-existent value"' failed``. The abort comes from `fir::factory::getDataOperandBaseAddr` in `flang/include/flang/Optimizer/Builder/DirectivesCommon.h`. Line 61 calls `symAddr.getDefiningOp()` on the null value, before the `if (!symAddr)` guard at line 66.

Before this PR, the same spelling gave a clean not-yet-implemented message. The non-iterator `map(tbl)` still gives `symbol is not mapped to any IR value` rather than an assertion.

Would extending the predicate to a locator that has no base object cover this? Dropping the `getBaseObject` conjunct outright would over-reject, since `isDeclareMapperVariable` returns false outside an `omp.declare_mapper`.


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


More information about the flang-commits mailing list