[flang-commits] [flang] [flang][OpenMP] Implement copyin for pointers and allocatables. (PR #107425)

David Truby via flang-commits flang-commits at lists.llvm.org
Thu Sep 5 10:34:34 PDT 2024


================
@@ -619,9 +620,28 @@ bool ClauseProcessor::processCopyin() const {
               checkAndCopyHostAssociateVar(&*mem, &insPt);
             break;
           }
-          if (semantics::IsAllocatableOrObjectPointer(&sym->GetUltimate()))
-            TODO(converter.getCurrentLocation(),
-                 "pointer or allocatable variables in Copyin clause");
+          if (semantics::IsAllocatable(sym->GetUltimate())) {
+            // copyin should copy the association of the allocatable
+
+            fir::FirOpBuilder &builder = converter.getFirOpBuilder();
+            const Fortran::semantics::Symbol &hsym = sym->GetUltimate();
+
+            Fortran::lower::SymbolBox hsb =
+                converter.lookupOneLevelUpSymbol(hsym);
+            assert(hsb && "Host symbol box not found");
+
+            Fortran::lower::SymbolBox sb = converter.shallowLookupSymbol(*sym);
+            assert(sb && "Host-associated symbol box not found");
+            assert(hsb.getAddr() != sb.getAddr() &&
+                   "Host and associated symbol boxes are the same");
+
+            mlir::Location loc = converter.genLocation(sym->name());
+            mlir::OpBuilder::InsertionGuard ipGuard{builder};
+            builder.setInsertionPointAfter(sb.getAddr().getDefiningOp());
+            builder.create<hlfir::AssignOp>(loc, hsb.getAddr(), sb.getAddr(),
+                                            true, false, false);
+          }
----------------
DavidTruby wrote:

`copyHostAssociateVar` is already being called at line 647 but that wasn't sufficient in my testing to mean that the allocatable got copied in. I needed to add the `hlfir.assign` to get the correct behaviour. I was testing with the following program:

```fortran
program main
  integer, allocatable, save :: a
  !$omp threadprivate(a)
  
  !$omp master
  allocate(a)
  a = 1
  !$omp end master
  
  !$omp parallel copyin(a)
  if (allocated(a)) then
    print *, a
  endif
  !$omp end parallel
end program
```

Without the copyin(a) this should only print once, whereas with the copyin(a) it should print OMP_NUM_THREADS times. Without the code block highlighted here, it only prints once.

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


More information about the flang-commits mailing list