Skip to content

Commit cb56cbe

Browse files
Automerge: [mlir][acc] Lower sequential acc.loop to scf.for in ACCComputeLowering (#206165)
Sequential loops already have fixed parallelism, so represent them with `scf.for` rather than `scf.parallel`. To prevent further analysis and parallelization, `parDimAttr` is set to seq.
2 parents 5e51ee6 + d939c77 commit cb56cbe

3 files changed

Lines changed: 14 additions & 15 deletions

File tree

mlir/lib/Dialect/OpenACC/Transforms/ACCComputeLowering.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,15 +313,14 @@ class ACCLoopConversion : public OpRewritePattern<LoopOp> {
313313
LoopParMode parMode = loopOp.getDefaultOrDeviceTypeParallelism(deviceType);
314314

315315
if (parMode == LoopParMode::loop_seq || isOpInSerialRegion(loopOp)) {
316-
// Although it might seem unintuitive, scf.parallel is used here because
317-
// the parallelism of the loop is already predetermined (as sequential).
318-
// scf.for will become a candidate for auto-parallelization analysis.
319-
auto parallelOp = convertACCLoopToSCFParallel(loopOp, rewriter);
320-
if (!parallelOp)
316+
// Use scf.for with sequential loops, because the loop's parallelism is
317+
// already determined.
318+
auto forOp =
319+
convertACCLoopToSCFFor(loopOp, rewriter, /*enableCollapse=*/true);
320+
if (!forOp)
321321
return failure();
322-
setParDimsAttr(parallelOp,
323-
GPUParallelDimsAttr::seq(loopOp->getContext()));
324-
rewriter.replaceOp(loopOp, parallelOp);
322+
setParDimsAttr(forOp, GPUParallelDimsAttr::seq(loopOp->getContext()));
323+
rewriter.replaceOp(loopOp, forOp);
325324
} else if (parMode == LoopParMode::loop_auto) {
326325
// All loops in serial regions should have already been handled.
327326
assert(!isOpInSerialRegion(loopOp) &&

mlir/test/Dialect/OpenACC/acc-compute-lowering-compute.mlir

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func.func @parallel_seq_loop(%buf: memref<4xi32>) {
3939
// CHECK: acc.kernel_environment
4040
// CHECK: acc.par_width {{.*}} {par_dim = #acc.par_dim<block_x>}
4141
// CHECK: acc.compute_region launch(
42-
// CHECK: scf.parallel
42+
// CHECK: scf.for
4343
// CHECK: acc.par_dims = #acc<par_dims[sequential]>
4444
acc.parallel num_gangs({%c10_i32 : i32}) dataOperands(%dev : memref<4xi32>) {
4545
acc.loop control(%i : index) = (%c0 : index) to (%c4 : index) step (%c1 : index) {
@@ -66,7 +66,7 @@ func.func @serial_loop(%buf: memref<4xi32>) {
6666
// CHECK: acc.kernel_environment
6767
// CHECK: acc.par_width {par_dim = #acc.par_dim<sequential>}
6868
// CHECK: acc.compute_region launch(
69-
// CHECK: scf.parallel
69+
// CHECK: scf.for
7070
// CHECK: acc.par_dims = #acc<par_dims[sequential]>
7171
acc.serial dataOperands(%dev : memref<4xi32>) {
7272
acc.loop control(%i : index) = (%c0 : index) to (%c4 : index) step (%c1 : index) {
@@ -150,7 +150,7 @@ func.func @parallel_unit_launch_serial_loops(%buf: memref<4xi32>) {
150150
// CHECK: acc.kernel_environment
151151
// CHECK: acc.par_width {par_dim = #acc.par_dim<sequential>}
152152
// CHECK: acc.compute_region launch(
153-
// CHECK: scf.parallel
153+
// CHECK: scf.for
154154
// CHECK: acc.par_dims = #acc<par_dims[sequential]>
155155
acc.parallel num_gangs({%c1_i32 : i32}) num_workers(%c1_i32 : i32) vector_length(%c1_i32 : i32) dataOperands(%dev : memref<4xi32>) {
156156
acc.loop control(%i : index) = (%c0 : index) to (%c4 : index) step (%c1 : index) {
@@ -182,7 +182,7 @@ func.func @kernels_unit_launch_serial_loops(%buf: memref<4xi32>) {
182182
// CHECK: acc.kernel_environment
183183
// CHECK: acc.par_width {par_dim = #acc.par_dim<sequential>}
184184
// CHECK: acc.compute_region launch(
185-
// CHECK: scf.parallel
185+
// CHECK: scf.for
186186
// CHECK: acc.par_dims = #acc<par_dims[sequential]>
187187
acc.kernels num_gangs({%c1_i32 : i32}) num_workers(%c1_i32 : i32) vector_length(%c1_i32 : i32) dataOperands(%dev : memref<4xi32>) {
188188
acc.loop control(%i : index) = (%c0 : index) to (%c4 : index) step (%c1 : index) {

mlir/test/Dialect/OpenACC/acc-compute-lowering-loop.mlir

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func.func @serial_loop_normalized(%buf: memref<1xi32>) {
9595
// CHECK: acc.kernel_environment
9696
// CHECK: acc.par_width {par_dim = #acc.par_dim<sequential>}
9797
// CHECK: acc.compute_region launch(
98-
// CHECK: scf.parallel
98+
// CHECK: scf.for
9999
// CHECK-DAG: arith.muli
100100
// CHECK-DAG: arith.addi
101101
// CHECK: acc.par_dims = #acc<par_dims[sequential]>
@@ -137,9 +137,9 @@ acc.routine @routine_with_loop func(@device_routine_with_loop) seq
137137
// CHECK-LABEL: func.func @device_routine_with_loop
138138
// CHECK: attributes {acc.specialized_routine = #acc.specialized_routine<@routine_with_loop, <seq>, "host_routine_with_loop">}
139139
// CHECK-NOT: acc.loop
140-
// CHECK: scf.parallel
140+
// CHECK: scf.for
141141
// CHECK: acc.par_dims = #acc<par_dims[sequential]>
142-
// CHECK-NOT: scf.for
142+
// CHECK-NOT: scf.parallel
143143
func.func @device_routine_with_loop(%buf: memref<8xi32>) attributes {acc.specialized_routine = #acc.specialized_routine<@routine_with_loop, <seq>, "host_routine_with_loop">} {
144144
%c0 = arith.constant 0 : index
145145
%c1 = arith.constant 1 : index

0 commit comments

Comments
 (0)