[flang-commits] [flang] [flang][openacc/mp] Do not read bounds on absent box (PR #75252)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Tue Dec 12 16:55:49 PST 2023


================
@@ -660,6 +671,58 @@ genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
   return bounds;
 }
 
+/// Generate the bounds operation from the descriptor information.
+template <typename BoundsOp, typename BoundsType>
+llvm::SmallVector<mlir::Value>
+genBoundsOpsFromBox(fir::FirOpBuilder &builder, mlir::Location loc,
+                    Fortran::lower::AbstractConverter &converter,
+                    fir::ExtendedValue dataExv, mlir::Value box,
+                    bool isOptional = false) {
+  llvm::SmallVector<mlir::Value> bounds;
+  mlir::Type idxTy = builder.getIndexType();
+  mlir::Type boundTy = builder.getType<BoundsType>();
+
+  assert(box.getType().isa<fir::BaseBoxType>() &&
+         "expect fir.box or fir.class");
+
+  if (isOptional) {
+    mlir::Value isPresent =
+        builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), box);
+
+    llvm::SmallVector<mlir::Type> resTypes;
+    for (unsigned dim = 0; dim < dataExv.rank(); ++dim)
+      resTypes.push_back(boundTy);
+
+    auto ifOp =
+        builder.genIfOp(loc, resTypes, isPresent, /*withElseRegion=*/true)
+            .genThen([&]() {
+              llvm::SmallVector<mlir::Value> tempBounds =
+                  gatherBoundsFromBox<BoundsOp, BoundsType>(builder, loc,
+                                                            dataExv, box);
+              builder.create<fir::ResultOp>(loc, tempBounds);
+            })
+            .genElse([&] {
+              llvm::SmallVector<mlir::Value> tempBounds;
+              mlir::Value zero = builder.createIntegerConstant(loc, idxTy, 0);
+              mlir::Value minusOne =
+                  builder.createIntegerConstant(loc, idxTy, -1);
+              for (unsigned dim = 0; dim < dataExv.rank(); ++dim) {
+                mlir::Value bound = builder.create<BoundsOp>(
+                    loc, boundTy, zero, minusOne, zero, mlir::Value(), false,
+                    mlir::Value{});
+                tempBounds.push_back(bound);
+              }
+              builder.create<fir::ResultOp>(loc, tempBounds);
+            });
+    bounds.append(ifOp.getResults().begin(), ifOp.getResults().end());
+  } else {
+    llvm::SmallVector<mlir::Value> tempBounds =
----------------
vzakhari wrote:

nit: seems that you can just assign the result to `bounds`.

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


More information about the flang-commits mailing list