-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[flang] do concurrent
: fix reduction symbol resolution when mapping to OpenMP
#155355
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
Conversation
@llvm/pr-subscribers-flang-fir-hlfir Author: Kareem Ergawy (ergawy) ChangesFixes 155273 This PR introduces 2 changes:
The benefit of using a module pass is that the same reduction operation can be used across multiple functions if the reduction type matches. Full diff: https://github.com/llvm/llvm-project/pull/155355.diff 3 Files Affected:
diff --git a/flang/include/flang/Optimizer/OpenMP/Passes.td b/flang/include/flang/Optimizer/OpenMP/Passes.td
index 99202f6ee81e7..e2f092024c250 100644
--- a/flang/include/flang/Optimizer/OpenMP/Passes.td
+++ b/flang/include/flang/Optimizer/OpenMP/Passes.td
@@ -50,7 +50,7 @@ def FunctionFilteringPass : Pass<"omp-function-filtering"> {
];
}
-def DoConcurrentConversionPass : Pass<"omp-do-concurrent-conversion", "mlir::func::FuncOp"> {
+def DoConcurrentConversionPass : Pass<"omp-do-concurrent-conversion", "mlir::ModuleOp"> {
let summary = "Map `DO CONCURRENT` loops to OpenMP worksharing loops.";
let description = [{ This is an experimental pass to map `DO CONCURRENT` loops
diff --git a/flang/lib/Optimizer/OpenMP/DoConcurrentConversion.cpp b/flang/lib/Optimizer/OpenMP/DoConcurrentConversion.cpp
index 2b3ac169e8b5b..3c08ad0a54001 100644
--- a/flang/lib/Optimizer/OpenMP/DoConcurrentConversion.cpp
+++ b/flang/lib/Optimizer/OpenMP/DoConcurrentConversion.cpp
@@ -368,22 +368,28 @@ class DoConcurrentConversion
mlir::OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPointAfter(firReducer);
-
- auto ompReducer = mlir::omp::DeclareReductionOp::create(
- rewriter, firReducer.getLoc(),
- sym.getLeafReference().str() + ".omp",
- firReducer.getTypeAttr().getValue());
-
- cloneFIRRegionToOMP(firReducer.getAllocRegion(),
- ompReducer.getAllocRegion());
- cloneFIRRegionToOMP(firReducer.getInitializerRegion(),
- ompReducer.getInitializerRegion());
- cloneFIRRegionToOMP(firReducer.getReductionRegion(),
- ompReducer.getReductionRegion());
- cloneFIRRegionToOMP(firReducer.getAtomicReductionRegion(),
- ompReducer.getAtomicReductionRegion());
- cloneFIRRegionToOMP(firReducer.getCleanupRegion(),
- ompReducer.getCleanupRegion());
+ std::string ompReducerName = sym.getLeafReference().str() + ".omp";
+
+ auto ompReducer = mlir::SymbolTable::lookupNearestSymbolFrom<
+ mlir::omp::DeclareReductionOp>(
+ loop, rewriter.getStringAttr(ompReducerName));
+
+ if (!ompReducer) {
+ ompReducer = mlir::omp::DeclareReductionOp::create(
+ rewriter, firReducer.getLoc(), ompReducerName,
+ firReducer.getTypeAttr().getValue());
+
+ cloneFIRRegionToOMP(firReducer.getAllocRegion(),
+ ompReducer.getAllocRegion());
+ cloneFIRRegionToOMP(firReducer.getInitializerRegion(),
+ ompReducer.getInitializerRegion());
+ cloneFIRRegionToOMP(firReducer.getReductionRegion(),
+ ompReducer.getReductionRegion());
+ cloneFIRRegionToOMP(firReducer.getAtomicReductionRegion(),
+ ompReducer.getAtomicReductionRegion());
+ cloneFIRRegionToOMP(firReducer.getCleanupRegion(),
+ ompReducer.getCleanupRegion());
+ }
wsloopClauseOps.reductionVars.push_back(op);
wsloopClauseOps.reductionByref.push_back(byRef);
@@ -444,11 +450,7 @@ class DoConcurrentConversionPass
: DoConcurrentConversionPassBase(options) {}
void runOnOperation() override {
- mlir::func::FuncOp func = getOperation();
-
- if (func.isDeclaration())
- return;
-
+ mlir::ModuleOp module = getOperation();
mlir::MLIRContext *context = &getContext();
if (mapTo != flangomp::DoConcurrentMappingKind::DCMK_Host &&
@@ -472,8 +474,8 @@ class DoConcurrentConversionPass
target.markUnknownOpDynamicallyLegal(
[](mlir::Operation *) { return true; });
- if (mlir::failed(mlir::applyFullConversion(getOperation(), target,
- std::move(patterns)))) {
+ if (mlir::failed(
+ mlir::applyFullConversion(module, target, std::move(patterns)))) {
signalPassFailure();
}
}
diff --git a/flang/test/Transforms/DoConcurrent/reduction_symbol_resultion.f90 b/flang/test/Transforms/DoConcurrent/reduction_symbol_resultion.f90
new file mode 100644
index 0000000000000..1b2b8461cca63
--- /dev/null
+++ b/flang/test/Transforms/DoConcurrent/reduction_symbol_resultion.f90
@@ -0,0 +1,34 @@
+! RUN: %flang_fc1 -emit-hlfir -fopenmp -fdo-concurrent-to-openmp=host %s -o - \
+! RUN: | FileCheck %s
+
+subroutine test1(x,s,N)
+ real :: x(N), s
+ integer :: N
+ do concurrent(i=1:N) reduce(+:s)
+ s=s+x(i)
+ end do
+end subroutine test1
+subroutine test2(x,s,N)
+ real :: x(N), s
+ integer :: N
+ do concurrent(i=1:N) reduce(+:s)
+ s=s+x(i)
+ end do
+end subroutine test2
+
+! CHECK: omp.declare_reduction @[[RED_SYM:.*]] : f32 init
+! CHECK-NOT: omp.declare_reduction
+
+! CHECK-LABEL: func.func @_QPtest1
+! CHECK: omp.parallel {
+! CHECK: omp.wsloop reduction(@[[RED_SYM]] {{.*}} : !fir.ref<f32>) {
+! CHECK: }
+! CHECK: }
+
+! CHECK-LABEL: func.func @_QPtest2
+! CHECK: omp.parallel {
+! CHECK: omp.wsloop reduction(@[[RED_SYM]] {{.*}} : !fir.ref<f32>) {
+! CHECK: }
+! CHECK: }
+
+
|
009bcaf
to
084374e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ompReducer.getCleanupRegion()); | ||
std::string ompReducerName = sym.getLeafReference().str() + ".omp"; | ||
|
||
auto ompReducer = mlir::SymbolTable::lookupNearestSymbolFrom< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the prompt fix, Kareem!
This method may be expensive. Please use SymbolTable
object (https://mlir.llvm.org/doxygen/classmlir_1_1SymbolTable.html) to cache the module symbol table, and then use it to insert/lookup the reducer symbols.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review. Done in ef29557.
… to OpenMP Fixes 155273 This PR introduces 2 changes: 1. The `do concurrent` to OpenMP pass is now a module pass rather than a function pass. 2. Reduction ops are looked up in the parent module before being created. The benefit of using a module pass is that the same reduction operation can be used across multiple functions if the reduction type matches.
084374e
to
ef29557
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Fixes #155273
This PR introduces 2 changes:
do concurrent
to OpenMP pass is now a module pass rather than a function pass.The benefit of using a module pass is that the same reduction operation can be used across multiple functions if the reduction type matches.