Skip to content

Commit 6812fc0

Browse files
[mlir][affine] make affine-loop-unroll-jam interface pass (#127402)
Made affine-loop-unroll-jam an interface pass, so it can run on the gpu module.
1 parent d235b72 commit 6812fc0

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

mlir/include/mlir/Dialect/Affine/Passes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ std::unique_ptr<InterfacePass<FunctionOpInterface>> createLoopUnrollPass(
102102
/// Creates a loop unroll jam pass to unroll jam by the specified factor. A
103103
/// factor of -1 lets the pass use the default factor or the one on the command
104104
/// line if provided.
105-
std::unique_ptr<OperationPass<func::FuncOp>>
105+
std::unique_ptr<InterfacePass<FunctionOpInterface>>
106106
createLoopUnrollAndJamPass(int unrollJamFactor = -1);
107107

108108
/// Creates a pass to pipeline explicit movement of data across levels of the

mlir/include/mlir/Dialect/Affine/Passes.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def AffineLoopUnroll : InterfacePass<"affine-loop-unroll", "FunctionOpInterface"
219219
];
220220
}
221221

222-
def AffineLoopUnrollAndJam : Pass<"affine-loop-unroll-jam", "func::FuncOp"> {
222+
def AffineLoopUnrollAndJam : InterfacePass<"affine-loop-unroll-jam", "FunctionOpInterface"> {
223223
let summary = "Unroll and jam affine loops";
224224
let constructor = "mlir::affine::createLoopUnrollAndJamPass()";
225225
let options = [

mlir/lib/Dialect/Affine/Transforms/LoopUnrollAndJam.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ struct LoopUnrollAndJam
7575
};
7676
} // namespace
7777

78-
std::unique_ptr<OperationPass<func::FuncOp>>
78+
std::unique_ptr<InterfacePass<FunctionOpInterface>>
7979
mlir::affine::createLoopUnrollAndJamPass(int unrollJamFactor) {
8080
return std::make_unique<LoopUnrollAndJam>(
8181
unrollJamFactor == -1 ? std::nullopt

mlir/test/Dialect/Affine/unroll-jam.mlir

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// RUN: mlir-opt -allow-unregistered-dialect %s -affine-loop-unroll-jam="unroll-jam-factor=2" | FileCheck %s
2-
// RUN: mlir-opt -allow-unregistered-dialect %s -affine-loop-unroll-jam="unroll-jam-factor=4" | FileCheck --check-prefix=UJAM-FOUR %s
1+
// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline="builtin.module(func.func(affine-loop-unroll-jam{unroll-jam-factor=2}))" | FileCheck %s
2+
// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline="builtin.module(func.func(affine-loop-unroll-jam{unroll-jam-factor=4}))" | FileCheck --check-prefix=UJAM-FOUR %s
3+
// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline="builtin.module(gpu.module(gpu.func(affine-loop-unroll-jam{unroll-jam-factor=2})))" | FileCheck --check-prefix=GPU-HJAM %s
34

45
// CHECK-DAG: [[$MAP_PLUS_1:#map[0-9]*]] = affine_map<(d0) -> (d0 + 1)>
56
// CHECK-DAG: [[$MAP_DIV_OFFSET:#map[0-9]*]] = affine_map<()[s0] -> (((s0 - 1) floordiv 2) * 2 + 1)>
@@ -10,6 +11,8 @@
1011
// UJAM-FOUR-DAG: [[$MAP_PLUS_2:#map[0-9]*]] = affine_map<(d0) -> (d0 + 2)>
1112
// UJAM-FOUR-DAG: [[$MAP_PLUS_3:#map[0-9]*]] = affine_map<(d0) -> (d0 + 3)>
1213

14+
// GPU-HJAM-DAG: [[$MAP_PLUS_1:#map[0-9]*]] = affine_map<(d0) -> (d0 + 1)>
15+
1316
// CHECK-LABEL: func @unroll_jam_imperfect_nest() {
1417
func.func @unroll_jam_imperfect_nest() {
1518
affine.for %i = 0 to 101 {
@@ -46,6 +49,44 @@ func.func @unroll_jam_imperfect_nest() {
4649
// CHECK-NEXT: "foo"(%c100, %{{.*}})
4750
// CHECK-NEXT: return
4851

52+
gpu.module @unroll_jam {
53+
// GPU-HJAM-LABEL: func @unroll_jam_imperfect_nest() {
54+
gpu.func @unroll_jam_imperfect_nest() {
55+
affine.for %i = 0 to 101 {
56+
%x = "addi32"(%i, %i) : (index, index) -> i32
57+
affine.for %j = 0 to 17 {
58+
%y = "addi32"(%i, %i) : (index, index) -> i32
59+
%z = "addi32"(%y, %y) : (i32, i32) -> i32
60+
}
61+
%w = "foo"(%i, %x) : (index, i32) -> i32
62+
}
63+
gpu.return
64+
}
65+
// GPU-HJAM: affine.for [[IV0:%arg[0-9]+]] = 0 to 100 step 2 {
66+
// GPU-HJAM-NEXT: [[RES1:%[0-9]+]] = "addi32"([[IV0]], [[IV0]])
67+
// GPU-HJAM-NEXT: [[INC:%[0-9]+]] = affine.apply [[$MAP_PLUS_1]]([[IV0]])
68+
// GPU-HJAM-NEXT: [[RES2:%[0-9]+]] = "addi32"([[INC]], [[INC]])
69+
// GPU-HJAM-NEXT: affine.for %{{.*}} = 0 to 17 {
70+
// GPU-HJAM-NEXT: [[RES3:%[0-9]+]] = "addi32"([[IV0]], [[IV0]])
71+
// GPU-HJAM-NEXT: "addi32"([[RES3]], [[RES3]]) : (i32, i32) -> i32
72+
// GPU-HJAM-NEXT: [[INC1:%[0-9]+]] = affine.apply [[$MAP_PLUS_1]]([[IV0]])
73+
// GPU-HJAM-NEXT: [[RES4:%[0-9]+]] = "addi32"([[INC1]], [[INC1]])
74+
// GPU-HJAM-NEXT: "addi32"([[RES4]], [[RES4]]) : (i32, i32) -> i32
75+
// GPU-HJAM-NEXT: }
76+
// GPU-HJAM: "foo"([[IV0]], [[RES1]])
77+
// GPU-HJAM-NEXT: affine.apply [[$MAP_PLUS_1]]([[IV0]])
78+
// GPU-HJAM-NEXT: "foo"({{.*}}, [[RES2]])
79+
// GPU-HJAM: }
80+
// Cleanup loop (single iteration).
81+
// GPU-HJAM: "addi32"(%c100, %c100)
82+
// GPU-HJAM-NEXT: affine.for [[IV0]] = 0 to 17 {
83+
// GPU-HJAM-NEXT: [[RESC:%[0-9]+]] = "addi32"(%c100, %c100)
84+
// GPU-HJAM-NEXT: "addi32"([[RESC]], [[RESC]]) : (i32, i32) -> i32
85+
// GPU-HJAM-NEXT: }
86+
// GPU-HJAM-NEXT: "foo"(%c100, %{{.*}})
87+
// GPU-HJAM-NEXT: return
88+
}
89+
4990
// CHECK-LABEL: func @loop_nest_unknown_count_1
5091
// CHECK-SAME: [[N:arg[0-9]+]]: index
5192
func.func @loop_nest_unknown_count_1(%N : index) {

0 commit comments

Comments
 (0)