Skip to content

Commit ab1c490

Browse files
authored
[flang][do concurrent] Extned getAllocaBlock() and emit yields correctly (#146853)
Handles some loose ends in `do concurrent` reduction declarations. This PR extends `getAllocaBlock` to handle declare ops, and also emit `fir.yield` in all regions.
1 parent 0e5f9f6 commit ab1c490

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

flang/lib/Lower/Support/ReductionProcessor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ static void createReductionAllocAndInitRegions(
529529
converter, loc, type, initValue, initBlock,
530530
reductionDecl.getInitializerAllocArg(),
531531
reductionDecl.getInitializerMoldArg(), reductionDecl.getCleanupRegion(),
532-
DeclOperationKind::Reduction);
532+
DeclOperationKind::Reduction, /*sym=*/nullptr,
533+
/*cannotHaveLowerBounds=*/false,
534+
/*isDoConcurrent*/ std::is_same_v<OpType, fir::DeclareReductionOp>);
533535
}
534536

535537
if (fir::isa_trivial(ty)) {

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ mlir::Block *fir::FirOpBuilder::getAllocaBlock() {
286286
if (auto firLocalOp = getRegion().getParentOfType<fir::LocalitySpecifierOp>())
287287
return &getRegion().front();
288288

289+
if (auto firLocalOp = getRegion().getParentOfType<fir::DeclareReductionOp>())
290+
return &getRegion().front();
291+
289292
return getEntryBlock();
290293
}
291294

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Tests that `fir.local` ops are able to provide an alloca block when required.
2+
3+
// RUN: fir-opt %s -convert-hlfir-to-fir | FileCheck %s
4+
5+
fir.declare_reduction @add_reduction_byref_box_heap_UxUxf32 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>> alloc {
6+
%0 = fir.alloca !fir.box<!fir.heap<!fir.array<?x?xf32>>>
7+
fir.yield(%0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>)
8+
} init {
9+
^bb0(%arg0: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>, %arg1: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>):
10+
%cst = arith.constant 0.000000e+00 : f32
11+
%0 = fir.load %arg1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>
12+
hlfir.assign %cst to %0 : f32, !fir.box<!fir.heap<!fir.array<?x?xf32>>>
13+
fir.yield(%arg1 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>)
14+
} combiner {
15+
^bb0(%arg0: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>, %arg1: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>):
16+
fir.yield(%arg0 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>)
17+
}
18+
19+
// CHECK-LABEL: fir.declare_reduction @add_reduction_byref_box_heap_UxUxf32 : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>> alloc {
20+
// CHECK: %[[VAL_0:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?x?xf32>>>
21+
// CHECK: fir.yield(%[[VAL_0]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>)
22+
23+
// CHECK-LABEL: } init {
24+
// CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>, %[[VAL_1:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>):
25+
// CHECK: %[[VAL_2:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?x?xf32>>>
26+
// CHECK: fir.yield(%[[VAL_1]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>)
27+
28+
// CHECK-LABEL: } combiner {
29+
// CHECK: ^bb0(%[[VAL_0:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>, %[[VAL_1:.*]]: !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>):
30+
// CHECK: fir.yield(%[[VAL_0]] : !fir.ref<!fir.box<!fir.heap<!fir.array<?x?xf32>>>>)
31+
// CHECK: }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s
2+
3+
subroutine do_concurrent_allocatable
4+
integer :: i
5+
real, allocatable, dimension(:,:) :: x
6+
7+
do concurrent (i = 1:10) reduce(+: x)
8+
end do
9+
end subroutine
10+
11+
! CHECK: fir.declare_reduction @[[RED_OP:.*]] : ![[RED_TYPE:.*]] alloc {
12+
! CHECK: %[[ALLOC:.*]] = fir.alloca
13+
! CHECK: fir.yield(%[[ALLOC]] : ![[RED_TYPE]])
14+
! CHECK: } init {
15+
! CHECK: ^bb0(%{{.*}}: ![[RED_TYPE]], %[[RED_ARG:.*]]: ![[RED_TYPE]]):
16+
! CHECK: fir.yield(%[[RED_ARG]] : !{{.*}})
17+
! CHECK: } combiner {
18+
! CHECK: ^bb0(%[[COMB_RES:.*]]: ![[RED_TYPE]], %{{.*}}: ![[RED_TYPE]]):
19+
! CHECK: fir.yield(%[[COMB_RES]] : !{{.*}})
20+
! CHECK: } cleanup {
21+
! CHECK: fir.yield
22+
! CHECK: }

0 commit comments

Comments
 (0)