Skip to content

Commit 8e279e0

Browse files
committed
[flang] do concurrent: fix reduction symbol resolution when mapping 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.
1 parent 9a81d85 commit 8e279e0

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

flang/include/flang/Optimizer/OpenMP/Passes.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def FunctionFilteringPass : Pass<"omp-function-filtering"> {
5050
];
5151
}
5252

53-
def DoConcurrentConversionPass : Pass<"omp-do-concurrent-conversion", "mlir::func::FuncOp"> {
53+
def DoConcurrentConversionPass : Pass<"omp-do-concurrent-conversion", "mlir::ModuleOp"> {
5454
let summary = "Map `DO CONCURRENT` loops to OpenMP worksharing loops.";
5555

5656
let description = [{ This is an experimental pass to map `DO CONCURRENT` loops

flang/lib/Optimizer/OpenMP/DoConcurrentConversion.cpp

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -368,22 +368,28 @@ class DoConcurrentConversion
368368

369369
mlir::OpBuilder::InsertionGuard guard(rewriter);
370370
rewriter.setInsertionPointAfter(firReducer);
371-
372-
auto ompReducer = mlir::omp::DeclareReductionOp::create(
373-
rewriter, firReducer.getLoc(),
374-
sym.getLeafReference().str() + ".omp",
375-
firReducer.getTypeAttr().getValue());
376-
377-
cloneFIRRegionToOMP(firReducer.getAllocRegion(),
378-
ompReducer.getAllocRegion());
379-
cloneFIRRegionToOMP(firReducer.getInitializerRegion(),
380-
ompReducer.getInitializerRegion());
381-
cloneFIRRegionToOMP(firReducer.getReductionRegion(),
382-
ompReducer.getReductionRegion());
383-
cloneFIRRegionToOMP(firReducer.getAtomicReductionRegion(),
384-
ompReducer.getAtomicReductionRegion());
385-
cloneFIRRegionToOMP(firReducer.getCleanupRegion(),
386-
ompReducer.getCleanupRegion());
371+
std::string ompReducerName = sym.getLeafReference().str() + ".omp";
372+
373+
auto ompReducer = mlir::SymbolTable::lookupNearestSymbolFrom<
374+
mlir::omp::DeclareReductionOp>(
375+
loop, rewriter.getStringAttr(ompReducerName));
376+
377+
if (!ompReducer) {
378+
ompReducer = mlir::omp::DeclareReductionOp::create(
379+
rewriter, firReducer.getLoc(), ompReducerName,
380+
firReducer.getTypeAttr().getValue());
381+
382+
cloneFIRRegionToOMP(firReducer.getAllocRegion(),
383+
ompReducer.getAllocRegion());
384+
cloneFIRRegionToOMP(firReducer.getInitializerRegion(),
385+
ompReducer.getInitializerRegion());
386+
cloneFIRRegionToOMP(firReducer.getReductionRegion(),
387+
ompReducer.getReductionRegion());
388+
cloneFIRRegionToOMP(firReducer.getAtomicReductionRegion(),
389+
ompReducer.getAtomicReductionRegion());
390+
cloneFIRRegionToOMP(firReducer.getCleanupRegion(),
391+
ompReducer.getCleanupRegion());
392+
}
387393

388394
wsloopClauseOps.reductionVars.push_back(op);
389395
wsloopClauseOps.reductionByref.push_back(byRef);
@@ -444,11 +450,7 @@ class DoConcurrentConversionPass
444450
: DoConcurrentConversionPassBase(options) {}
445451

446452
void runOnOperation() override {
447-
mlir::func::FuncOp func = getOperation();
448-
449-
if (func.isDeclaration())
450-
return;
451-
453+
mlir::ModuleOp module = getOperation();
452454
mlir::MLIRContext *context = &getContext();
453455

454456
if (mapTo != flangomp::DoConcurrentMappingKind::DCMK_Host &&
@@ -472,8 +474,8 @@ class DoConcurrentConversionPass
472474
target.markUnknownOpDynamicallyLegal(
473475
[](mlir::Operation *) { return true; });
474476

475-
if (mlir::failed(mlir::applyFullConversion(getOperation(), target,
476-
std::move(patterns)))) {
477+
if (mlir::failed(
478+
mlir::applyFullConversion(module, target, std::move(patterns)))) {
477479
signalPassFailure();
478480
}
479481
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
! RUN: %flang_fc1 -emit-hlfir -fopenmp -fdo-concurrent-to-openmp=host %s -o - \
2+
! RUN: | FileCheck %s
3+
4+
subroutine test1(x,s,N)
5+
real :: x(N), s
6+
integer :: N
7+
do concurrent(i=1:N) reduce(+:s)
8+
s=s+x(i)
9+
end do
10+
end subroutine test1
11+
subroutine test2(x,s,N)
12+
real :: x(N), s
13+
integer :: N
14+
do concurrent(i=1:N) reduce(+:s)
15+
s=s+x(i)
16+
end do
17+
end subroutine test2
18+
19+
! CHECK: omp.declare_reduction @[[RED_SYM:.*]] : f32 init
20+
! CHECK-NOT: omp.declare_reduction
21+
22+
! CHECK-LABEL: func.func @_QPtest1
23+
! CHECK: omp.parallel {
24+
! CHECK: omp.wsloop reduction(@[[RED_SYM]] {{.*}} : !fir.ref<f32>) {
25+
! CHECK: }
26+
! CHECK: }
27+
28+
! CHECK-LABEL: func.func @_QPtest2
29+
! CHECK: omp.parallel {
30+
! CHECK: omp.wsloop reduction(@[[RED_SYM]] {{.*}} : !fir.ref<f32>) {
31+
! CHECK: }
32+
! CHECK: }

0 commit comments

Comments
 (0)