Skip to content

[Flang][OpenMP] Per-sym checks to introduce barriers #127074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2025

Conversation

skatrak
Copy link
Member

@skatrak skatrak commented Feb 13, 2025

Whenever there is a lastprivate variable and another unrelated variable sets the mightHaveReadHostSym flag during Flang lowering privatization, this will result in the insertion of a barrier.

This patch modifies this behavior such that this barrier will not be inserted unless the same symbol both sets the flag and has lastprivate.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 13, 2025

@llvm/pr-subscribers-flang-openmp

@llvm/pr-subscribers-flang-fir-hlfir

Author: Sergio Afonso (skatrak)

Changes

Whenever there is a lastprivate variable and another unrelated variable sets the mightHaveReadHostSym flag during Flang lowering privatization, this will result in the insertion of a barrier.

This patch modifies this behavior such that this barrier will not be inserted unless the same symbol both sets the flag and has lastprivate.


Full diff: https://github.com/llvm/llvm-project/pull/127074.diff

2 Files Affected:

  • (modified) flang/lib/Lower/OpenMP/DataSharingProcessor.cpp (+3-3)
  • (modified) flang/lib/Lower/OpenMP/DataSharingProcessor.h (+1-1)
diff --git a/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp b/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
index 55f543ca38178..d13f101f516e7 100644
--- a/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
+++ b/flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
@@ -166,7 +166,7 @@ void DataSharingProcessor::cloneSymbol(const semantics::Symbol *sym) {
 
   if (needInitClone()) {
     Fortran::lower::initializeCloneAtRuntime(converter, *sym, symTable);
-    mightHaveReadHostSym = true;
+    mightHaveReadHostSym.insert(sym);
   }
 }
 
@@ -222,7 +222,7 @@ bool DataSharingProcessor::needBarrier() {
   for (const semantics::Symbol *sym : allPrivatizedSymbols) {
     if (sym->test(semantics::Symbol::Flag::OmpLastPrivate) &&
         (sym->test(semantics::Symbol::Flag::OmpFirstPrivate) ||
-         mightHaveReadHostSym))
+         mightHaveReadHostSym.contains(sym)))
       return true;
   }
   return false;
@@ -594,7 +594,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
       // TODO: currently there are false positives from dead uses of the mold
       // arg
       if (!result.getInitMoldArg().getUses().empty())
-        mightHaveReadHostSym = true;
+        mightHaveReadHostSym.insert(sym);
     }
 
     // Populate the `copy` region if this is a `firstprivate`.
diff --git a/flang/lib/Lower/OpenMP/DataSharingProcessor.h b/flang/lib/Lower/OpenMP/DataSharingProcessor.h
index 8e15c6d260389..54a42fd199831 100644
--- a/flang/lib/Lower/OpenMP/DataSharingProcessor.h
+++ b/flang/lib/Lower/OpenMP/DataSharingProcessor.h
@@ -86,7 +86,7 @@ class DataSharingProcessor {
   lower::pft::Evaluation &eval;
   bool shouldCollectPreDeterminedSymbols;
   bool useDelayedPrivatization;
-  bool mightHaveReadHostSym = false;
+  llvm::SmallSet<const semantics::Symbol *, 16> mightHaveReadHostSym;
   lower::SymMap &symTable;
   OMPConstructSymbolVisitor visitor;
 

@kiranchandramohan
Copy link
Contributor

Can you add a test as the first commit and show the difference (removal of barrier) in the second commit along with the code in this patch?

Copy link
Contributor

@luporl luporl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except for @kiranchandramohan's comments, LGTM.

Whenever there is a `lastprivate` variable and another unrelated variable sets
the `mightHaveReadHostSym` flag during Flang lowering privatization, this will
result in the insertion of a barrier.

This patch modifies this behavior such that this barrier will not be inserted
unless it has to be the same variable to set the flag and be `lastprivate` for
this to be done.
@skatrak skatrak force-pushed the fix-privatization-barrier branch from 3c477df to c11820a Compare February 14, 2025 11:07
@skatrak
Copy link
Member Author

skatrak commented Feb 14, 2025

Can you add a test as the first commit and show the difference (removal of barrier) in the second commit along with the code in this patch?

Done, let me know if that works for you.

Copy link
Contributor

@kiranchandramohan kiranchandramohan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG. Thanks @skatrak

@skatrak skatrak merged commit 8a3d7ce into llvm:main Feb 14, 2025
9 checks passed
@skatrak skatrak deleted the fix-privatization-barrier branch February 14, 2025 12:24
joaosaffran pushed a commit to joaosaffran/llvm-project that referenced this pull request Feb 14, 2025
Whenever there is a `lastprivate` variable and another unrelated
variable sets the `mightHaveReadHostSym` flag during Flang lowering
privatization, this will result in the insertion of a barrier.

This patch modifies this behavior such that this barrier will not be
inserted unless the same symbol both sets the flag and has
`lastprivate`.
Copy link
Contributor

@tblah tblah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(late) +1

Thanks for fixing this. LGTM

sivan-shani pushed a commit to sivan-shani/llvm-project that referenced this pull request Feb 24, 2025
Whenever there is a `lastprivate` variable and another unrelated
variable sets the `mightHaveReadHostSym` flag during Flang lowering
privatization, this will result in the insertion of a barrier.

This patch modifies this behavior such that this barrier will not be
inserted unless the same symbol both sets the flag and has
`lastprivate`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang:openmp flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants