[flang-commits] [flang] [Flang][OpenMP] Fix negative array indexing with allocatable derived type array maps (PR #154193)

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Tue Aug 19 06:26:35 PDT 2025


================
@@ -331,10 +325,41 @@ mlir::Value createParentSymAndGenIntermediateMaps(
                              subscriptIndices, objectList[i]);
         assert(!subscriptIndices.empty() &&
                "missing expected indices for map clause");
-        curValue = fir::CoordinateOp::create(
-            firOpBuilder, clauseLocation,
-            firOpBuilder.getRefType(arrType.getEleTy()), curValue,
-            subscriptIndices);
+        if (auto boxTy = llvm::dyn_cast<fir::BaseBoxType>(curValue.getType())) {
+          fir::ExtendedValue exv =
+              hlfir::translateToExtendedValue(clauseLocation, firOpBuilder,
+                                              hlfir::Entity{curValue},
+                                              /*contiguousHint=*/
+                                              true)
+                  .first;
+
+          mlir::Type idxTy = firOpBuilder.getIndexType();
+          llvm::SmallVector<mlir::Value> shiftOperands;
+          for (unsigned dim = 0; dim < exv.rank(); ++dim) {
+            mlir::Value d =
+                firOpBuilder.createIntegerConstant(clauseLocation, idxTy, dim);
+            auto dimInfo = fir::BoxDimsOp::create(
+                firOpBuilder, clauseLocation, idxTy, idxTy, idxTy, curValue, d);
+            shiftOperands.push_back(dimInfo.getLowerBound());
+            shiftOperands.push_back(dimInfo.getExtent());
+          }
+          auto shapeShiftType =
+              fir::ShapeShiftType::get(firOpBuilder.getContext(), exv.rank());
+          mlir::Value shapeShift = fir::ShapeShiftOp::create(
+              firOpBuilder, clauseLocation, shapeShiftType, shiftOperands);
+          auto addrOp =
+              fir::BoxAddrOp::create(firOpBuilder, clauseLocation, curValue);
+          curValue = fir::ArrayCoorOp::create(
+              firOpBuilder, clauseLocation,
+              firOpBuilder.getRefType(arrType.getEleTy()), addrOp, shapeShift,
+              /*slice=*/mlir::Value{}, subscriptIndices,
+              /*typeparms=*/mlir::ValueRange{});
+        } else {
+          curValue = fir::CoordinateOp::create(
+              firOpBuilder, clauseLocation,
+              firOpBuilder.getRefType(arrType.getEleTy()), curValue,
+              subscriptIndices);
----------------
kparzysz wrote:

We used to subtract 1 from subscript indices in this case.  Was that always wrong?

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


More information about the flang-commits mailing list