[flang-commits] [flang] [flang][acc] Fix cache directive with mapped component (PR #179335)

via flang-commits flang-commits at lists.llvm.org
Tue Feb 3 00:57:45 PST 2026


================
@@ -4304,20 +4304,26 @@ genACC(Fortran::lower::AbstractConverter &converter,
           fir::substBase(hostExv, cacheOp.getAccVar());
       converter.bindSymbol(symbol, cacheExv);
     } else {
-      // Must be a derived type component reference.
+      // Derived type component reference.
       assert(designator && "expected designator for non-symbol cache operand");
       std::optional<Fortran::evaluate::Component> componentRef =
           extractComponentFromDesignator(designator);
       assert(componentRef &&
              "expected component reference for derived type cache operand");
-      // Component references are lowered to designate operations.
-      auto designate = base.getDefiningOp<hlfir::DesignateOp>();
-      assert(designate && "expected designate op for component reference");
+      // When component is mapped via copyin, base is the mapped address.
+      mlir::Value shape;
+      llvm::SmallVector<mlir::Value> lenParams;
+      fir::FortranVariableFlagsAttr attrs;
+      if (auto designate = base.getDefiningOp<hlfir::DesignateOp>()) {
+        shape = designate.getShape();
+        lenParams = llvm::SmallVector<mlir::Value>(
+            designate.getTypeparams().begin(), designate.getTypeparams().end());
+        attrs = designate.getFortranAttrsAttr();
+      }
       auto declareOp = hlfir::DeclareOp::create(
-          builder, operandLocation, cacheOp.getAccVar(), asFortran.str(),
-          designate.getShape(), designate.getTypeparams(),
-          /*dummyScope=*/nullptr, /*storage=*/nullptr,
-          /*storageOffset=*/0, designate.getFortranAttrsAttr());
+          builder, operandLocation, cacheOp.getAccVar(), asFortran.str(), shape,
+          lenParams, /*dummyScope=*/nullptr, /*storage=*/nullptr,
+          /*storageOffset=*/0, attrs);
----------------
jeanPerier wrote:

Rational: you still need to get the shape/length parameters to create valid declare for explicit shape arrays/non deferred length characters.

Your code worked for your example because it is an allocatable component whose shape is deferred (not constant) and is not known/relevant at the declaration point.

Here is an example derived from your tests where the shape is needed for a component and I think a compile time error would still be hit after your patch.

```
subroutine test_cache_combined_array_comp(data, C, M) 
  type :: dt 
    real, dimension(10) :: A 
  end type 
 
  type(dt), intent(inout) :: data 
  real, dimension(:), intent(out) :: C 
  integer, intent(in) :: M 
  integer :: i 
 
  !$acc parallel loop gang vector copyin(data, data%A(1:M)) copyout(C(1:M)) 
  do i = 1, M 
    !$acc cache(data%A(i:i+4)) 
    C(i) = data%A(i) 
  end do 
end subroutine 
```

It is safe to assume base is a FortranVariableOpInterface because that is what Designator lowering produces EntityWithAttributes, which for variables must be produced by FortranVariableOpInterface.

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


More information about the flang-commits mailing list