[flang-commits] [flang] [mlir] [MLIR][OpenMP] Support basic materialization for `omp.private` ops (PR #81715)

Sergio Afonso via flang-commits flang-commits at lists.llvm.org
Thu Feb 15 09:10:40 PST 2024


================
@@ -1092,6 +1113,86 @@ convertOmpParallel(omp::ParallelOp opInst, llvm::IRBuilderBase &builder,
                     llvm::Value *&replacementValue) -> InsertPointTy {
     replacementValue = &vPtr;
 
+    // If this is a private value, this lambda will return the corresponding
+    // mlir value and its `PrivateClauseOp`. Otherwise, empty values are
+    // returned.
+    auto [privVar, privatizerClone] =
+        [&]() -> std::pair<mlir::Value, omp::PrivateClauseOp> {
+      if (!opInst.getPrivateVars().empty()) {
+        auto privVars = opInst.getPrivateVars();
+        auto privatizers = opInst.getPrivatizers();
+        assert(privatizers && privatizers->size() == privVars.size());
+
+        const auto *privInitIt = privatizers->begin();
+        for (auto privVarIt = privVars.begin(); privVarIt != privVars.end();
+             ++privVarIt, ++privInitIt) {
+          // Find the MLIR private variable corresponding to the LLVM value
+          // being privatized.
+          llvm::Value *llvmPrivVar = moduleTranslation.lookupValue(*privVarIt);
+          if (llvmPrivVar != &vPtr)
+            continue;
+
+          SymbolRefAttr privSym = llvm::cast<SymbolRefAttr>(*privInitIt);
+          omp::PrivateClauseOp privatizer =
+              SymbolTable::lookupNearestSymbolFrom<omp::PrivateClauseOp>(
+                  opInst, privSym);
+
+          assert(privatizer);
+          // Clone the privatizer in case it used by more than one parallel
+          // region. The privatizer is processed in-place (see below) before it
+          // gets inlined in the parallel region and therefore processing the
+          // original op is dangerous.
+          return {*privVarIt, privatizer.clone()};
+        }
+      }
+
+      return {mlir::Value(), omp::PrivateClauseOp()};
+    }();
+
+    if (privVar) {
+      assert(privatizerClone.getDataSharingType() !=
+                 omp::DataSharingClauseType::FirstPrivate &&
+             "TODO: delayed privatization is not supported for `firstprivate` "
+             "clauses yet.");
+      Region &allocRegion = privatizerClone.getAllocRegion();
+      assert(allocRegion.getNumArguments() == 1);
+      assert(allocRegion.hasOneBlock() &&
+             "TODO: multi-block alloc regions are not supported yet. Seems "
+             "like there is a difference in `inlineConvertOmpRegions`'s "
+             "pre-conditions for single- and multi-block regions.");
----------------
skatrak wrote:

Nit: I think it's better to use `privatizerClone.emitError` rather than assert here, since users should probably know about this limitation rather than silently crashing or generating incorrect code.

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


More information about the flang-commits mailing list