[flang-commits] [flang] [mlir] [acc] Support for Optionals in firstprivate recipes. (PR #185764)

via flang-commits flang-commits at lists.llvm.org
Tue Mar 10 15:01:27 PDT 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp,h -- flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.h flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCUtils.h flang/lib/Lower/OpenACC.cpp flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp flang/lib/Optimizer/OpenACC/Support/FIROpenACCUtils.cpp mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.h b/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.h
index 3c4339298..6008488fe 100644
--- a/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.h
+++ b/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.h
@@ -96,8 +96,7 @@ struct OpenACCMappableModel
                     mlir::Location loc,
                     mlir::TypedValue<mlir::acc::MappableType> source,
                     mlir::TypedValue<mlir::acc::MappableType> dest,
-                    mlir::ValueRange bounds,
-                    bool isOptional = false) const;
+                    mlir::ValueRange bounds, bool isOptional = false) const;
 
   bool generateCombiner(mlir::Type type, mlir::OpBuilder &mlirBuilder,
                         mlir::Location loc,
diff --git a/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCUtils.h b/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCUtils.h
index 290db5adf..bef0f397f 100644
--- a/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCUtils.h
+++ b/flang/include/flang/Optimizer/OpenACC/Support/FIROpenACCUtils.h
@@ -68,14 +68,13 @@ createOrGetPrivateRecipe(mlir::OpBuilder &builder, mlir::Location loc,
 /// \param loc The location
 /// \param ty The type of the variable
 /// \param dataBoundOps Optional bounds for the variable
-/// \param isOptional Whether the variable is Fortran optional (enables null-check
+/// \param isOptional Whether the variable is Fortran optional (enables
+/// null-check
 ///        in init/copy; uses a separate recipe with _optional suffix)
 /// \return The existing or created FirstprivateRecipeOp symbol
-mlir::SymbolRefAttr
-createOrGetFirstprivateRecipe(mlir::OpBuilder &builder, mlir::Location loc,
-                              mlir::Type ty,
-                              llvm::SmallVector<mlir::Value> &dataBoundOps,
-                              bool isOptional = false);
+mlir::SymbolRefAttr createOrGetFirstprivateRecipe(
+    mlir::OpBuilder &builder, mlir::Location loc, mlir::Type ty,
+    llvm::SmallVector<mlir::Value> &dataBoundOps, bool isOptional = false);
 
 /// Create or get a reduction recipe for the given type, name and operator.
 /// \param builder The FIR builder
diff --git a/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp b/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp
index f5f8b7ad6..aec1a6a87 100644
--- a/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp
+++ b/flang/lib/Optimizer/OpenACC/Support/FIROpenACCTypeInterfaces.cpp
@@ -846,78 +846,78 @@ mlir::Value OpenACCMappableModel<Ty>::generatePrivateInit(
   if (inputVar.getType() == alloc.getType() && !allocateSection) {
     retVal = alloc;
   } else {
-  // Step4: reconstruct the input variable from the privatized part:
-  // - get a mock base address if the privatized part is a section (so that any
-  // addressing of the input variable can be replaced by the same addressing of
-  // the privatized part even though the allocated part for the private does not
-  // cover all the input variable storage. This is relying on OpenACC
-  // constraint that any addressing of such privatized variable inside the
-  // construct region can only address the variable inside the privatized
-  // section).
-  // - reconstruct a descriptor with the same bounds and type parameters as the
-  // input if needed.
-  // - store this new descriptor in a temporary allocation if the input variable
-  // is a POINTER/ALLOCATABLE.
-  llvm::SmallVector<mlir::Value> inputVarLowerBounds, inputVarExtents;
-  if (dereferencedVar.isArray()) {
-    for (int dim = 0; dim < dereferencedVar.getRank(); ++dim) {
-      inputVarLowerBounds.push_back(
-          hlfir::genLBound(loc, builder, dereferencedVar, dim));
-      inputVarExtents.push_back(
-          hlfir::genExtent(loc, builder, dereferencedVar, dim));
+    // Step4: reconstruct the input variable from the privatized part:
+    // - get a mock base address if the privatized part is a section (so that
+    // any addressing of the input variable can be replaced by the same
+    // addressing of the privatized part even though the allocated part for the
+    // private does not cover all the input variable storage. This is relying on
+    // OpenACC constraint that any addressing of such privatized variable inside
+    // the construct region can only address the variable inside the privatized
+    // section).
+    // - reconstruct a descriptor with the same bounds and type parameters as
+    // the input if needed.
+    // - store this new descriptor in a temporary allocation if the input
+    // variable is a POINTER/ALLOCATABLE.
+    llvm::SmallVector<mlir::Value> inputVarLowerBounds, inputVarExtents;
+    if (dereferencedVar.isArray()) {
+      for (int dim = 0; dim < dereferencedVar.getRank(); ++dim) {
+        inputVarLowerBounds.push_back(
+            hlfir::genLBound(loc, builder, dereferencedVar, dim));
+        inputVarExtents.push_back(
+            hlfir::genExtent(loc, builder, dereferencedVar, dim));
+      }
     }
-  }
 
-  mlir::Value privateVarBaseAddr = alloc;
-  if (allocateSection) {
-    // To compute the mock base address without doing pointer arithmetic,
-    // compute: TYPE, TEMP(ZERO_BASED_SECTION_LB:) MOCK_BASE = TEMP(0)
-    // This addresses the section "backwards" (0 <= ZERO_BASED_SECTION_LB). This
-    // is currently OK, but care should be taken to avoid tripping bound checks
-    // if added in the future.
-    mlir::Type inputBaseAddrType =
-        dereferencedVar.getBoxType().getBaseAddressType();
-    mlir::Value tempBaseAddr =
-        builder.createConvert(loc, inputBaseAddrType, alloc);
-    mlir::Value zero =
-        builder.createIntegerConstant(loc, builder.getIndexType(), 0);
-    llvm::SmallVector<mlir::Value> lowerBounds;
-    llvm::SmallVector<mlir::Value> zeros;
-    for (unsigned i = 0; i < bounds.size(); i += 3) {
-      lowerBounds.push_back(bounds[i]);
-      zeros.push_back(zero);
+    mlir::Value privateVarBaseAddr = alloc;
+    if (allocateSection) {
+      // To compute the mock base address without doing pointer arithmetic,
+      // compute: TYPE, TEMP(ZERO_BASED_SECTION_LB:) MOCK_BASE = TEMP(0)
+      // This addresses the section "backwards" (0 <= ZERO_BASED_SECTION_LB).
+      // This is currently OK, but care should be taken to avoid tripping bound
+      // checks if added in the future.
+      mlir::Type inputBaseAddrType =
+          dereferencedVar.getBoxType().getBaseAddressType();
+      mlir::Value tempBaseAddr =
+          builder.createConvert(loc, inputBaseAddrType, alloc);
+      mlir::Value zero =
+          builder.createIntegerConstant(loc, builder.getIndexType(), 0);
+      llvm::SmallVector<mlir::Value> lowerBounds;
+      llvm::SmallVector<mlir::Value> zeros;
+      for (unsigned i = 0; i < bounds.size(); i += 3) {
+        lowerBounds.push_back(bounds[i]);
+        zeros.push_back(zero);
+      }
+      mlir::Value offsetShapeShift =
+          builder.genShape(loc, lowerBounds, inputVarExtents);
+      mlir::Type eleRefType =
+          builder.getRefType(privatizedVar.getFortranElementType());
+      mlir::Value mockBase = fir::ArrayCoorOp::create(
+          builder, loc, eleRefType, tempBaseAddr, offsetShapeShift,
+          /*slice=*/mlir::Value{}, /*indices=*/zeros,
+          /*typeParams=*/mlir::ValueRange{});
+      privateVarBaseAddr =
+          builder.createConvert(loc, inputBaseAddrType, mockBase);
     }
-    mlir::Value offsetShapeShift =
-        builder.genShape(loc, lowerBounds, inputVarExtents);
-    mlir::Type eleRefType =
-        builder.getRefType(privatizedVar.getFortranElementType());
-    mlir::Value mockBase = fir::ArrayCoorOp::create(
-        builder, loc, eleRefType, tempBaseAddr, offsetShapeShift,
-        /*slice=*/mlir::Value{}, /*indices=*/zeros,
-        /*typeParams=*/mlir::ValueRange{});
-    privateVarBaseAddr =
-        builder.createConvert(loc, inputBaseAddrType, mockBase);
-  }
 
-  retVal = privateVarBaseAddr;
-  if (inputVar.isBoxAddressOrValue()) {
-    // Recreate descriptor with same bounds as the input variable.
-    mlir::Value shape;
-    if (!inputVarExtents.empty())
-      shape = builder.genShape(loc, inputVarLowerBounds, inputVarExtents);
-    mlir::Value box = fir::EmboxOp::create(builder, loc, inputVar.getBoxType(),
-                                           privateVarBaseAddr, shape,
-                                           /*slice=*/mlir::Value{}, typeParams);
-    if (inputVar.isMutableBox()) {
-      mlir::Value boxAlloc =
-          fir::AllocaOp::create(builder, loc, inputVar.getBoxType());
-      fir::StoreOp::create(builder, loc, box, boxAlloc);
-      retVal = boxAlloc;
-    } else {
-      retVal = box;
+    retVal = privateVarBaseAddr;
+    if (inputVar.isBoxAddressOrValue()) {
+      // Recreate descriptor with same bounds as the input variable.
+      mlir::Value shape;
+      if (!inputVarExtents.empty())
+        shape = builder.genShape(loc, inputVarLowerBounds, inputVarExtents);
+      mlir::Value box = fir::EmboxOp::create(
+          builder, loc, inputVar.getBoxType(), privateVarBaseAddr, shape,
+          /*slice=*/mlir::Value{}, typeParams);
+      if (inputVar.isMutableBox()) {
+        mlir::Value boxAlloc =
+            fir::AllocaOp::create(builder, loc, inputVar.getBoxType());
+        fir::StoreOp::create(builder, loc, box, boxAlloc);
+        retVal = boxAlloc;
+      } else {
+        retVal = box;
+      }
     }
   }
-  }
 
   if (isOptional) {
     fir::ResultOp::create(builder, loc, retVal);
diff --git a/flang/lib/Optimizer/OpenACC/Support/FIROpenACCUtils.cpp b/flang/lib/Optimizer/OpenACC/Support/FIROpenACCUtils.cpp
index 0d02d4e66..b9b7c7afe 100644
--- a/flang/lib/Optimizer/OpenACC/Support/FIROpenACCUtils.cpp
+++ b/flang/lib/Optimizer/OpenACC/Support/FIROpenACCUtils.cpp
@@ -12,12 +12,12 @@
 
 #include "flang/Optimizer/OpenACC/Support/FIROpenACCUtils.h"
 #include "flang/Optimizer/Builder/BoxValue.h"
-#include "flang/Optimizer/Dialect/FortranVariableInterface.h"
 #include "flang/Optimizer/Builder/Complex.h"
 #include "flang/Optimizer/Builder/FIRBuilder.h"
 #include "flang/Optimizer/Dialect/FIROps.h"
 #include "flang/Optimizer/Dialect/FIROpsSupport.h"
 #include "flang/Optimizer/Dialect/FIRType.h"
+#include "flang/Optimizer/Dialect/FortranVariableInterface.h"
 #include "flang/Optimizer/Dialect/Support/FIRContext.h"
 #include "flang/Optimizer/Dialect/Support/KindMapping.h"
 #include "flang/Optimizer/HLFIR/HLFIROps.h"
@@ -616,10 +616,9 @@ mlir::SymbolRefAttr fir::acc::createOrGetFirstprivateRecipe(
   mlir::ModuleOp mod =
       mlirBuilder.getBlock()->getParent()->getParentOfType<mlir::ModuleOp>();
   fir::FirOpBuilder builder(mlirBuilder, mod);
-  std::string recipeName =
-      ::getRecipeName(mlir::acc::RecipeKind::firstprivate_recipe, ty,
-                      builder.getKindMap(), dataBoundOps,
-                      mlir::acc::ReductionOperator::AccNone, isOptional);
+  std::string recipeName = ::getRecipeName(
+      mlir::acc::RecipeKind::firstprivate_recipe, ty, builder.getKindMap(),
+      dataBoundOps, mlir::acc::ReductionOperator::AccNone, isOptional);
   if (auto recipe =
           mod.lookupSymbol<mlir::acc::FirstprivateRecipeOp>(recipeName))
     return mlir::SymbolRefAttr::get(builder.getContext(), recipe.getSymName());
diff --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index 1dd3826d2..086480eee 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -1548,7 +1548,8 @@ struct RemoveConstantIfConditionWithRegion : public OpRewritePattern<OpTy> {
 static LogicalResult createInitRegion(OpBuilder &builder, Location loc,
                                       Region &initRegion, Type varType,
                                       StringRef varName, ValueRange bounds,
-                                      bool &needsFree, bool isOptional = false) {
+                                      bool &needsFree,
+                                      bool isOptional = false) {
   // Create init block with arguments: original value + bounds
   SmallVector<Type> argTypes{varType};
   SmallVector<Location> argLocs{loc};
@@ -1829,7 +1830,8 @@ FirstprivateRecipeOp::createAndPopulate(OpBuilder &builder, Location loc,
   if (!isMappable && !isPointerLike)
     return std::nullopt;
 
-  // Derive isOptional from recipe name (e.g. firstprivatization_optional_ref_i32)
+  // Derive isOptional from recipe name (e.g.
+  // firstprivatization_optional_ref_i32)
   bool isOptional = recipeName.contains("_optional");
 
   OpBuilder::InsertionGuard guard(builder);

``````````

</details>


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


More information about the flang-commits mailing list