[flang-commits] [flang] [flang][OpenMP] Support reduction of allocatable variables (PR #88392)

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Mon Apr 15 04:13:28 PDT 2024


================
@@ -464,9 +501,24 @@ createReductionInitRegion(fir::FirOpBuilder &builder, mlir::Location loc,
 
   // all arrays are boxed
   if (auto boxTy = mlir::dyn_cast_or_null<fir::BaseBoxType>(ty)) {
-    assert(isByRef && "passing arrays by value is unsupported");
-    // TODO: support allocatable arrays: !fir.box<!fir.heap<!fir.array<...>>>
-    mlir::Type innerTy = fir::extractSequenceType(boxTy);
+    assert(isByRef && "passing boxes by value is unsupported");
+    mlir::Type innerTy = fir::unwrapRefType(boxTy.getEleTy());
+    if (fir::isa_trivial(innerTy)) {
+      // boxed non-sequence value e.g. !fir.box<!fir.heap<i32>>
+      if (!mlir::isa<fir::HeapType>(boxTy.getEleTy()))
+        TODO(loc, "Reduction of non-allocatable trivial typed box");
+      mlir::Value boxAlloca = builder.create<fir::AllocaOp>(loc, ty);
+      mlir::Value valAlloc = builder.create<fir::AllocMemOp>(loc, innerTy);
+      builder.createStoreWithConvert(loc, initValue, valAlloc);
+      mlir::Value box = builder.create<fir::EmboxOp>(loc, ty, valAlloc);
+      builder.create<fir::StoreOp>(loc, box, boxAlloca);
+
+      auto insPt = builder.saveInsertionPoint();
+      createReductionCleanupRegion(builder, loc, reductionDecl);
+      builder.restoreInsertionPoint(insPt);
+      return boxAlloca;
+    }
----------------
kiranchandramohan wrote:

For creating private copies the rules of allocatables are as follows. Do we have a check for the allocation status? Are these rules redefined in the standard for the reduction case?

```
For a list item or the subobject of a list item with the ALLOCATABLE attribute:

-> If the allocation status is unallocated, the new list item or the subobject of the new list item will have an initial allocation status of unallocated;
-> If the allocation status is allocated, the new list item or the subobject of the new list item will have an initial allocation status of allocated; and
-> If the new list item or the subobject of the new list item is an array, its bounds will be the same as those of the original list item or the subobject of the original list item.
```

https://www.openmp.org/spec-html/5.0/openmpsu105.html

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


More information about the flang-commits mailing list