[flang-commits] [flang] [flang][OpenMP] Do not skip privatization of linear variable if it is OmpPreDetermined (PR #144315)

via flang-commits flang-commits at lists.llvm.org
Mon Jun 16 04:16:43 PDT 2025


================
@@ -204,6 +204,42 @@ void DataSharingProcessor::collectOmpObjectListSymbol(
 }
 
 void DataSharingProcessor::collectSymbolsForPrivatization() {
+  // Add checks here for exceptional cases where privatization is not
+  // needed and be deferred to a later phase (like OpenMP IRBuilder).
+  // Such cases are suggested to be clearly documented and explained
+  // instead of being silently skipped
+  auto isException = [&](const Fortran::semantics::Symbol *sym) -> bool {
+    // `OmpPreDetermined` symbols cannot be exceptions since
+    // their privatized symbols are heavily used in FIR.
+    if (sym->test(Fortran::semantics::Symbol::Flag::OmpPreDetermined))
+      return false;
----------------
NimishMishra wrote:

While working on lowering and translation of linear clause, I observed that Flang Semantics marks `OmpLinear` as a symbol to be privatised, which `collectSymbolsForPrivatization` fetches and privatizes. But it does not add code for any of the other linear semantics: (1) adding linear step based on the loop iteration, (2) generating code for finalization based on the last loop iteration, (3) storing the final variable value to its actual storage in the final iteration, and (4) emitting synchronization barrier. These four tasks had to be newly implemented; I added relevant functionality in the IRBuilder for these. Also, tasks (2) and (3) require knowledge of `%lastiter` variable of the canonical loop; I am not sure if FIR has access to this or an equivalent.

I think it would have been possible to let the FIR lowering continue with privatization here, and pass a tuple `(linear-var MLIR value, private-copy MLIR value)` to the IRBuilder to allow it to operate upon the two. But I thought it to be cleaner that _all_ functionality wrt. linear semantics to be contained within the IRBuilder (hence added a new class `LinearClauseProcessor` in https://github.com/llvm/llvm-project/pull/139386/).

I can perhaps modify the comment to reflect that while the FIR can privatize the linear variables, it is cleaner to simply let IRBuilder do so. 

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


More information about the flang-commits mailing list