Skip to content

Commit 22a1973

Browse files
[mlir][linalg][bufferize] Print results of FuncOp read/write analysis
Print more information with test-analysis-only. Differential Revision: https://reviews.llvm.org/D119118
1 parent 00b2a9c commit 22a1973

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

mlir/lib/Dialect/Linalg/ComprehensiveBufferize/ModuleBufferization.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ static bool isValueWritten(Value value, const BufferizationState &state,
239239
return isWritten;
240240
}
241241

242+
static void annotateFuncArgAccess(FuncOp funcOp, BlockArgument bbArg,
243+
bool isRead, bool isWritten) {
244+
OpBuilder b(funcOp.getContext());
245+
Attribute accessType;
246+
if (isRead && isWritten) {
247+
accessType = b.getStringAttr("read-write");
248+
} else if (isRead) {
249+
accessType = b.getStringAttr("read");
250+
} else if (isWritten) {
251+
accessType = b.getStringAttr("write");
252+
} else {
253+
accessType = b.getStringAttr("none");
254+
}
255+
funcOp.setArgAttr(bbArg.getArgNumber(), "bufferization.access", accessType);
256+
}
257+
242258
/// Determine which FuncOp bbArgs are read and which are written. If this
243259
/// PostAnalysisStepFn is run on a function with unknown ops, it will
244260
/// conservatively assume that such ops bufferize to a read + write.
@@ -263,9 +279,13 @@ funcOpBbArgReadWriteAnalysis(Operation *op, BufferizationState &state,
263279
for (BlockArgument bbArg : funcOp.getArguments()) {
264280
if (!bbArg.getType().isa<TensorType>())
265281
continue;
266-
if (state.isValueRead(bbArg))
282+
bool isRead = state.isValueRead(bbArg);
283+
bool isWritten = isValueWritten(bbArg, state, aliasInfo);
284+
if (state.getOptions().testAnalysisOnly)
285+
annotateFuncArgAccess(funcOp, bbArg, isRead, isWritten);
286+
if (isRead)
267287
moduleState.readBbArgs.insert(bbArg);
268-
if (isValueWritten(bbArg, state, aliasInfo))
288+
if (isWritten)
269289
moduleState.writtenBbArgs.insert(bbArg);
270290
}
271291

mlir/test/Dialect/Linalg/comprehensive-module-bufferize-analysis.mlir

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
// -----
1313

14-
// CHECK-LABEL: func @extract_slice_fun
14+
// CHECK-LABEL: func @extract_slice_fun(
1515
func @extract_slice_fun(%A : tensor<?xf32> {linalg.inplaceable = false},
16+
// CHECK-SAME: bufferization.access = "read"
1617
%B : tensor<?xf32> {linalg.inplaceable = true})
18+
// CHECK-SAME: bufferization.access = "read"
1719
-> (tensor<4xf32>, tensor<8xf32>)
1820
{
1921
// tensor.extract_slice is not used in a write, it is not compelled to
@@ -33,10 +35,13 @@ func @extract_slice_fun(%A : tensor<?xf32> {linalg.inplaceable = false},
3335

3436
// -----
3537

36-
// CHECK-LABEL: func @insert_slice_fun
38+
// CHECK-LABEL: func @insert_slice_fun(
3739
func @insert_slice_fun(%A : tensor<?xf32> {linalg.inplaceable = false},
40+
// CHECK-SAME: bufferization.access = "read"
3841
%B : tensor<?xf32> {linalg.inplaceable = true},
42+
// CHECK-SAME: bufferization.access = "read-write"
3943
%C : tensor<4xf32> {linalg.inplaceable = false})
44+
// CHECK-SAME: bufferization.access = "read"
4045
-> (tensor<?xf32>, tensor<?xf32>)
4146
{
4247
// must bufferize out of place.
@@ -56,9 +61,11 @@ func @insert_slice_fun(%A : tensor<?xf32> {linalg.inplaceable = false},
5661

5762
// -----
5863

59-
// CHECK-LABEL: func @conflict_on_B
64+
// CHECK-LABEL: func @conflict_on_B(
6065
func @conflict_on_B(%A : tensor<4x4xf32> {linalg.inplaceable = true},
66+
// CHECK-SAME: bufferization.access = "read"
6167
%B : tensor<4x4xf32> {linalg.inplaceable = true})
68+
// CHECK-SAME: bufferization.access = "read-write"
6269
-> (tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>)
6370
{
6471
// matmul output operand interferes with input operand.
@@ -93,10 +100,12 @@ func @conflict_on_B(%A : tensor<4x4xf32> {linalg.inplaceable = true},
93100

94101
// -----
95102

96-
// CHECK-LABEL: func @extract_slice_extract_slice
103+
// CHECK-LABEL: func @extract_slice_extract_slice(
97104
func @extract_slice_extract_slice(
98105
%A : tensor<?xf32> {linalg.inplaceable = true},
106+
// CHECK-SAME: bufferization.access = "read"
99107
%B : tensor<?xf32> {linalg.inplaceable = false})
108+
// CHECK-SAME: bufferization.access = "read"
100109
-> (tensor<2xf32>, tensor<2xf32>)
101110
{
102111
// tensor.extract_slice is not used in a write, it is not compelled to
@@ -120,14 +129,20 @@ func @extract_slice_extract_slice(
120129

121130
// -----
122131

123-
// CHECK-LABEL: func @insert_slice_insert_slice
132+
// CHECK-LABEL: func @insert_slice_insert_slice(
124133
func @insert_slice_insert_slice(
125134
%A : tensor<?xf32> {linalg.inplaceable = true},
135+
// CHECK-SAME: bufferization.access = "read-write"
126136
%A2 : tensor<4xf32> {linalg.inplaceable = true},
137+
// CHECK-SAME: bufferization.access = "read-write"
127138
%A3 : tensor<2xf32> {linalg.inplaceable = true},
139+
// CHECK-SAME: bufferization.access = "read"
128140
%B : tensor<?xf32> {linalg.inplaceable = false},
141+
// CHECK-SAME: bufferization.access = "read"
129142
%B2 : tensor<4xf32> {linalg.inplaceable = false},
143+
// CHECK-SAME: bufferization.access = "read"
130144
%B3 : tensor<2xf32> {linalg.inplaceable = false})
145+
// CHECK-SAME: bufferization.access = "read"
131146
-> (tensor<?xf32>, tensor<?xf32>)
132147
{
133148
// CHECK: {__inplace_operands_attr__ = ["true", "true"]}
@@ -888,12 +903,16 @@ builtin.func @matmul_on_tensors(
888903
// prioritizing the tensor.insert_slice ops.
889904
//===----------------------------------------------------------------------===//
890905

906+
// CHECK-LABEL: func @insert_slice_chain(
891907
func @insert_slice_chain(
892908
%v1: vector<32x90xf32>,
893909
%v2: vector<30x90xf32>,
894910
%arg0: tensor<62x126xf32> {linalg.buffer_layout = affine_map<(d0, d1) -> (d0, d1)>, linalg.inplaceable = false},
911+
// CHECK-SAME: bufferization.access = "none"
895912
%arg1: tensor<126x90xf32> {linalg.buffer_layout = affine_map<(d0, d1) -> (d0, d1)>, linalg.inplaceable = false},
913+
// CHECK-SAME: bufferization.access = "none"
896914
%arg2: tensor<62x90xf32> {linalg.buffer_layout = affine_map<(d0, d1) -> (d0, d1)>, linalg.inplaceable = true})
915+
// CHECK-SAME: bufferization.access = "write"
897916
-> tensor<62x90xf32> attributes {passthrough = [["target-cpu", "skylake-avx512"], ["prefer-vector-width", "512"]]}
898917
{
899918
%c0 = arith.constant 0 : index
@@ -968,10 +987,13 @@ func @ip(%t: tensor<10x20xf32> {linalg.inplaceable = true},
968987
iterator_types = ["parallel"]
969988
}
970989

971-
// CHECK-LABEL: func @linalg_op_same_out_tensors
990+
// CHECK-LABEL: func @linalg_op_same_out_tensors(
972991
func @linalg_op_same_out_tensors(
973992
%t1: tensor<?xf32> {linalg.inplaceable = true},
974-
%t2: tensor<?xf32> {linalg.inplaceable = true}) -> (tensor<?xf32>, tensor<?xf32>){
993+
// CHECK-SAME: bufferization.access = "read-write"
994+
%t2: tensor<?xf32> {linalg.inplaceable = true})
995+
// CHECK-SAME: bufferization.access = "write"
996+
-> (tensor<?xf32>, tensor<?xf32>){
975997

976998
// CHECK: linalg.generic
977999
// CHECK-SAME: {__inplace_operands_attr__ = ["true", "true", "true"]
@@ -999,10 +1021,12 @@ func @linalg_op_same_out_tensors(
9991021
iterator_types = ["parallel"]
10001022
}
10011023

1002-
// CHECK-LABEL: func @linalg_op_same_out_tensors_2
1024+
// CHECK-LABEL: func @linalg_op_same_out_tensors_2(
10031025
func @linalg_op_same_out_tensors_2(
10041026
%t1: tensor<?xf32> {linalg.inplaceable = true},
1027+
// CHECK-SAME: bufferization.access = "read-write"
10051028
%t2: tensor<?xf32> {linalg.inplaceable = true})
1029+
// CHECK-SAME: bufferization.access = "write"
10061030
-> (tensor<?xf32>, tensor<?xf32>, tensor<?xf32>){
10071031

10081032
// CHECK: linalg.generic

0 commit comments

Comments
 (0)