Skip to content

Commit 39e8e59

Browse files
[Flang][OpenMP] Use fir.if instead of scf.if in lastprivate lowering
For finding the last iteration of a loop, or the last section an if condition is generated. Using scf::if can cause some lowering issues since the code contained inside it can have branches. Using fir::if instead ensures that the fir::if is lowered into branches along with any code contained inside that can generate branches. Fixes #62458. Reviewed By: NimishMishra Differential Revision: https://reviews.llvm.org/D149547
1 parent 52882de commit 39e8e59

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include "flang/Parser/parse-tree.h"
2424
#include "flang/Semantics/tools.h"
2525
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
26-
#include "mlir/Dialect/SCF/IR/SCF.h"
2726
#include "llvm/Frontend/OpenMP/OMPConstants.h"
2827

2928
using namespace mlir;
@@ -222,20 +221,20 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
222221
// omp.section {
223222
// fir.allocate for `private`/`firstprivate`
224223
// <More operations here>
225-
// scf.if %true {
224+
// fir.if %true {
226225
// ^%lpv_update_blk
227226
// }
228227
// }
229228
// }
230229
//
231230
// To keep code consistency while handling privatization
232-
// through this control flow, add a `scf.if` operation
231+
// through this control flow, add a `fir.if` operation
233232
// that always evaluates to true, in order to create
234233
// a dedicated sub-region in `omp.section` where
235234
// lastprivate FIR can reside. Later canonicalizations
236235
// will optimize away this operation.
237236

238-
mlir::scf::IfOp ifOp = firOpBuilder.create<mlir::scf::IfOp>(
237+
auto ifOp = firOpBuilder.create<fir::IfOp>(
239238
op->getLoc(),
240239
firOpBuilder.createIntegerConstant(
241240
op->getLoc(), firOpBuilder.getIntegerType(1), 0x1),
@@ -280,7 +279,7 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
280279
// omp.wsloop { ...
281280
// ... store
282281
// store ===> %cmp = llvm.icmp "eq" %iv %ub
283-
// omp.yield scf.if %cmp {
282+
// omp.yield fir.if %cmp {
284283
// } ^%lpv_update_blk:
285284
// }
286285
// omp.yield
@@ -295,8 +294,8 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
295294
op->getRegion(0).front().getArguments()[0],
296295
mlir::dyn_cast<mlir::omp::WsLoopOp>(op).getUpperBound()[0]);
297296
}
298-
mlir::scf::IfOp ifOp = firOpBuilder.create<mlir::scf::IfOp>(
299-
op->getLoc(), cmpOp, /*else*/ false);
297+
auto ifOp =
298+
firOpBuilder.create<fir::IfOp>(op->getLoc(), cmpOp, /*else*/ false);
300299
firOpBuilder.setInsertionPointToStart(&ifOp.getThenRegion().front());
301300
lastPrivIP = firOpBuilder.saveInsertionPoint();
302301
} else {

flang/test/Lower/OpenMP/parallel-lastprivate-clause-scalar.f90

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
! Testing last iteration check
2727
!CHECK-NEXT: %[[IV_CMP:.*]] = arith.cmpi eq, %[[INDX_WS]]
28-
!CHECK: scf.if %[[IV_CMP]] {
28+
!CHECK: fir.if %[[IV_CMP]] {
2929

3030
! Testing lastprivate val update
3131
!CHECK-DAG: %[[CVT:.*]] = fir.convert %[[ARG1_REF]] : (!fir.ref<!fir.char<1,5>>) -> !fir.ref<i8>
@@ -53,7 +53,7 @@ subroutine lastprivate_character(arg1)
5353

5454
! Testing last iteration check
5555
!CHECK-DAG: %[[IV_CMP:.*]] = arith.cmpi eq, %[[INDX_WS]]
56-
!CHECK-DAG: scf.if %[[IV_CMP]] {
56+
!CHECK-DAG: fir.if %[[IV_CMP]] {
5757

5858
! Testing lastprivate val update
5959
!CHECK-NEXT: %[[CLONE_LD:.*]] = fir.load %[[CLONE]] : !fir.ref<i32>
@@ -82,7 +82,7 @@ subroutine lastprivate_int(arg1)
8282

8383
! Testing last iteration check
8484
!CHECK: %[[IV_CMP1:.*]] = arith.cmpi eq, %[[INDX_WS]]
85-
!CHECK-NEXT: scf.if %[[IV_CMP1]] {
85+
!CHECK-NEXT: fir.if %[[IV_CMP1]] {
8686
! Testing lastprivate val update
8787
!CHECK-DAG: %[[CLONE_LD1:.*]] = fir.load %[[CLONE1]] : !fir.ref<i32>
8888
!CHECK-DAG: fir.store %[[CLONE_LD1]] to %[[ARG1]] : !fir.ref<i32>
@@ -113,7 +113,7 @@ subroutine mult_lastprivate_int(arg1, arg2)
113113

114114
!Testing last iteration check
115115
!CHECK: %[[IV_CMP1:.*]] = arith.cmpi eq, %[[INDX_WS]]
116-
!CHECK-NEXT: scf.if %[[IV_CMP1]] {
116+
!CHECK-NEXT: fir.if %[[IV_CMP1]] {
117117
!Testing lastprivate val update
118118
!CHECK-DAG: %[[CLONE_LD2:.*]] = fir.load %[[CLONE2]] : !fir.ref<i32>
119119
!CHECK-DAG: fir.store %[[CLONE_LD2]] to %[[ARG2]] : !fir.ref<i32>
@@ -149,7 +149,7 @@ subroutine mult_lastprivate_int2(arg1, arg2)
149149

150150
! Testing last iteration check
151151
!CHECK: %[[IV_CMP1:.*]] = arith.cmpi eq, %[[INDX_WS]]
152-
!CHECK-NEXT: scf.if %[[IV_CMP1]] {
152+
!CHECK-NEXT: fir.if %[[IV_CMP1]] {
153153
! Testing lastprivate val update
154154
!CHECK-NEXT: %[[CLONE_LD:.*]] = fir.load %[[CLONE2]] : !fir.ref<i32>
155155
!CHECK-NEXT: fir.store %[[CLONE_LD]] to %[[ARG2]] : !fir.ref<i32>
@@ -180,7 +180,7 @@ subroutine firstpriv_lastpriv_int(arg1, arg2)
180180
!CHECK: omp.wsloop for (%[[INDX_WS:.*]]) : {{.*}} {
181181
! Testing last iteration check
182182
!CHECK: %[[IV_CMP1:.*]] = arith.cmpi eq, %[[INDX_WS]]
183-
!CHECK-NEXT: scf.if %[[IV_CMP1]] {
183+
!CHECK-NEXT: fir.if %[[IV_CMP1]] {
184184
! Testing lastprivate val update
185185
!CHECK-NEXT: %[[CLONE_LD:.*]] = fir.load %[[CLONE1]] : !fir.ref<i32>
186186
!CHECK-NEXT: fir.store %[[CLONE_LD]] to %[[ARG1]] : !fir.ref<i32>

flang/test/Lower/OpenMP/sections.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ subroutine lastprivate()
131131
!CHECK: %[[const:.*]] = arith.constant 1 : i32
132132
!CHECK: %[[result:.*]] = arith.addi %[[temp]], %[[const]] : i32
133133
!CHECK: fir.store %[[result]] to %[[PRIVATE_X]] : !fir.ref<i32>
134-
!CHECK: scf.if %[[true]] {
134+
!CHECK: fir.if %[[true]] {
135135
!CHECK: %[[temp:.*]] = fir.load %[[PRIVATE_X]] : !fir.ref<i32>
136136
!CHECK: fir.store %[[temp]] to %[[X]] : !fir.ref<i32>
137137
!CHECK: }
@@ -168,7 +168,7 @@ subroutine lastprivate()
168168
!CHECK: %[[const:.*]] = arith.constant 1 : i32
169169
!CHECK: %[[result:.*]] = arith.addi %[[temp]], %[[const]] : i32
170170
!CHECK: fir.store %[[result]] to %[[PRIVATE_X]] : !fir.ref<i32>
171-
!CHECK: scf.if %true {
171+
!CHECK: fir.if %true {
172172
!CHECK: %[[temp:.*]] = fir.load %[[PRIVATE_X]] : !fir.ref<i32>
173173
!CHECK: fir.store %[[temp]] to %[[X]] : !fir.ref<i32>
174174
!CHECK: }
@@ -205,7 +205,7 @@ subroutine lastprivate()
205205
!CHECK: %[[const:.*]] = arith.constant 1 : i32
206206
!CHECK: %[[result:.*]] = arith.addi %[[temp]], %[[const]] : i32
207207
!CHECK: fir.store %[[result]] to %[[PRIVATE_X]] : !fir.ref<i32>
208-
!CHECK: scf.if %true {
208+
!CHECK: fir.if %true {
209209
!CHECK: %[[temp:.*]] = fir.load %[[PRIVATE_X]] : !fir.ref<i32>
210210
!CHECK: fir.store %[[temp]] to %[[X]] : !fir.ref<i32>
211211
!CHECK: omp.barrier

0 commit comments

Comments
 (0)