Skip to content

Commit ef4800c

Browse files
authored
[mlir][Interfaces][NFC] Update doc of ViewLikeOpInterface parser/printer handlers (#122555)
This PR addresses part of the feedback provided in #115808.
1 parent 565f3bd commit ef4800c

File tree

2 files changed

+80
-41
lines changed

2 files changed

+80
-41
lines changed

mlir/include/mlir/Interfaces/ViewLikeInterface.h

Lines changed: 74 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -86,71 +86,109 @@ class OpWithOffsetSizesAndStridesConstantArgumentFolder final
8686
}
8787
};
8888

89-
/// Printer hook for custom directive in assemblyFormat.
89+
/// Printer hooks for custom directive in assemblyFormat.
9090
///
9191
/// custom<DynamicIndexList>($values, $integers)
9292
/// custom<DynamicIndexList>($values, $integers, type($values))
9393
///
94-
/// where `values` is of ODS type `Variadic<*>` and `integers` is of ODS
95-
/// type `I64ArrayAttr`. Prints a list with either (1) the static integer value
96-
/// in `integers` is `kDynamic` or (2) the next value otherwise. If `valueTypes`
97-
/// is non-empty, it is expected to contain as many elements as `values`
98-
/// indicating their types. This allows idiomatic printing of mixed value and
99-
/// integer attributes in a list. E.g.
100-
/// `[%arg0 : index, 7, 42, %arg42 : i32]`.
101-
///
102-
/// Indices can be scalable. For example, "4" in "[2, [4], 8]" is scalable.
103-
/// This notation is similar to how scalable dims are marked when defining
104-
/// Vectors. For each value in `integers`, the corresponding `bool` in
105-
/// `scalables` encodes whether it's a scalable index. If `scalableVals` is
106-
/// empty then assume that all indices are non-scalable.
94+
/// where `values` is of ODS type `Variadic<*>` and `integers` is of ODS type
95+
/// `I64ArrayAttr`. Print a list where each element is either:
96+
/// 1. the static integer value in `integers`, if it's not `kDynamic` or,
97+
/// 2. the next value in `values`, otherwise.
98+
///
99+
/// If `valueTypes` is provided, the corresponding type of each dynamic value is
100+
/// printed. Otherwise, the type is not printed. Each type must match the type
101+
/// of the corresponding value in `values`. `valueTypes` is redundant for
102+
/// printing as we can retrieve the types from the actual `values`. However,
103+
/// `valueTypes` is needed for parsing and we must keep the API symmetric for
104+
/// parsing and printing. The type for integer elements is `i64` by default and
105+
/// never printed.
106+
///
107+
/// Integer indices can also be scalable in the context of scalable vectors,
108+
/// denoted by square brackets (e.g., "[2, [4], 8]"). For each value in
109+
/// `integers`, the corresponding `bool` in `scalableFlags` encodes whether it's
110+
/// a scalable index. If `scalableFlags` is empty then assume that all indices
111+
/// are non-scalable.
112+
///
113+
/// Examples:
114+
///
115+
/// * Input: `integers = [kDynamic, 7, 42, kDynamic]`,
116+
/// `values = [%arg0, %arg42]` and
117+
/// `valueTypes = [index, index]`
118+
/// prints:
119+
/// `[%arg0 : index, 7, 42, %arg42 : i32]`
120+
///
121+
/// * Input: `integers = [kDynamic, 7, 42, kDynamic]`,
122+
/// `values = [%arg0, %arg42]` and
123+
/// `valueTypes = []`
124+
/// prints:
125+
/// `[%arg0, 7, 42, %arg42]`
126+
///
127+
/// * Input: `integers = [2, 4, 8]`,
128+
/// `values = []` and
129+
/// `scalableFlags = [false, true, false]`
130+
/// prints:
131+
/// `[2, [4], 8]`
132+
///
107133
void printDynamicIndexList(
108134
OpAsmPrinter &printer, Operation *op, OperandRange values,
109-
ArrayRef<int64_t> integers, ArrayRef<bool> scalables,
135+
ArrayRef<int64_t> integers, ArrayRef<bool> scalableFlags,
110136
TypeRange valueTypes = TypeRange(),
111137
AsmParser::Delimiter delimiter = AsmParser::Delimiter::Square);
112138
inline void printDynamicIndexList(
113139
OpAsmPrinter &printer, Operation *op, OperandRange values,
114140
ArrayRef<int64_t> integers, TypeRange valueTypes = TypeRange(),
115141
AsmParser::Delimiter delimiter = AsmParser::Delimiter::Square) {
116-
return printDynamicIndexList(printer, op, values, integers, {}, valueTypes,
117-
delimiter);
142+
return printDynamicIndexList(printer, op, values, integers,
143+
/*scalableFlags=*/{}, valueTypes, delimiter);
118144
}
119145

120-
/// Parser hook for custom directive in assemblyFormat.
146+
/// Parser hooks for custom directive in assemblyFormat.
121147
///
122148
/// custom<DynamicIndexList>($values, $integers)
123149
/// custom<DynamicIndexList>($values, $integers, type($values))
124150
///
125151
/// where `values` is of ODS type `Variadic<*>` and `integers` is of ODS
126-
/// type `I64ArrayAttr`. Parse a mixed list with either (1) static integer
127-
/// values or (2) SSA values. Fill `integers` with the integer ArrayAttr, where
128-
/// `kDynamic` encodes the position of SSA values. Add the parsed SSA values
129-
/// to `values` in-order. If `valueTypes` is non-null, fill it with types
130-
/// corresponding to values; otherwise the caller must handle the types.
131-
///
132-
/// E.g. after parsing "[%arg0 : index, 7, 42, %arg42 : i32]":
133-
/// 1. `result` is filled with the i64 ArrayAttr "[`kDynamic`, 7, 42,
134-
/// `kDynamic`]"
135-
/// 2. `ssa` is filled with "[%arg0, %arg1]".
136-
///
137-
/// Indices can be scalable. For example, "4" in "[2, [4], 8]" is scalable.
138-
/// This notation is similar to how scalable dims are marked when defining
139-
/// Vectors. For each value in `integers`, the corresponding `bool` in
140-
/// `scalableVals` encodes whether it's a scalable index.
152+
/// type `I64ArrayAttr`. Parse a mixed list where each element is either a
153+
/// static integer or an SSA value. Fill `integers` with the integer ArrayAttr,
154+
/// where `kDynamic` encodes the position of SSA values. Add the parsed SSA
155+
/// values to `values` in-order.
156+
///
157+
/// If `valueTypes` is provided, fill it with the types corresponding to each
158+
/// value in `values`. Otherwise, the caller must handle the types and parsing
159+
/// will fail if the type of the value is found (e.g., `[%arg0 : index, 3, %arg1
160+
/// : index]`).
161+
///
162+
/// Integer indices can also be scalable in the context of scalable vectors,
163+
/// denoted by square brackets (e.g., "[2, [4], 8]"). For each value in
164+
/// `integers`, the corresponding `bool` in `scalableFlags` encodes whether it's
165+
/// a scalable index.
166+
///
167+
/// Examples:
168+
///
169+
/// * After parsing "[%arg0 : index, 7, 42, %arg42 : i32]":
170+
/// 1. `result` is filled with `[kDynamic, 7, 42, kDynamic]`
171+
/// 2. `values` is filled with "[%arg0, %arg1]".
172+
/// 3. `scalableFlags` is filled with `[false, true, false]`.
173+
///
174+
/// * After parsing `[2, [4], 8]`:
175+
/// 1. `result` is filled with `[2, 4, 8]`
176+
/// 2. `values` is empty.
177+
/// 3. `scalableFlags` is filled with `[false, true, false]`.
178+
///
141179
ParseResult parseDynamicIndexList(
142180
OpAsmParser &parser,
143181
SmallVectorImpl<OpAsmParser::UnresolvedOperand> &values,
144-
DenseI64ArrayAttr &integers, DenseBoolArrayAttr &scalableVals,
182+
DenseI64ArrayAttr &integers, DenseBoolArrayAttr &scalableFlags,
145183
SmallVectorImpl<Type> *valueTypes = nullptr,
146184
AsmParser::Delimiter delimiter = AsmParser::Delimiter::Square);
147185
inline ParseResult parseDynamicIndexList(
148186
OpAsmParser &parser,
149187
SmallVectorImpl<OpAsmParser::UnresolvedOperand> &values,
150188
DenseI64ArrayAttr &integers, SmallVectorImpl<Type> *valueTypes = nullptr,
151189
AsmParser::Delimiter delimiter = AsmParser::Delimiter::Square) {
152-
DenseBoolArrayAttr scalableVals = {};
153-
return parseDynamicIndexList(parser, values, integers, scalableVals,
190+
DenseBoolArrayAttr scalableFlags;
191+
return parseDynamicIndexList(parser, values, integers, scalableFlags,
154192
valueTypes, delimiter);
155193
}
156194

mlir/lib/Interfaces/ViewLikeInterface.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ static char getRightDelimiter(AsmParser::Delimiter delimiter) {
113113
void mlir::printDynamicIndexList(OpAsmPrinter &printer, Operation *op,
114114
OperandRange values,
115115
ArrayRef<int64_t> integers,
116-
ArrayRef<bool> scalables, TypeRange valueTypes,
116+
ArrayRef<bool> scalableFlags,
117+
TypeRange valueTypes,
117118
AsmParser::Delimiter delimiter) {
118119
char leftDelimiter = getLeftDelimiter(delimiter);
119120
char rightDelimiter = getRightDelimiter(delimiter);
@@ -126,7 +127,7 @@ void mlir::printDynamicIndexList(OpAsmPrinter &printer, Operation *op,
126127
unsigned dynamicValIdx = 0;
127128
unsigned scalableIndexIdx = 0;
128129
llvm::interleaveComma(integers, printer, [&](int64_t integer) {
129-
if (!scalables.empty() && scalables[scalableIndexIdx])
130+
if (!scalableFlags.empty() && scalableFlags[scalableIndexIdx])
130131
printer << "[";
131132
if (ShapedType::isDynamic(integer)) {
132133
printer << values[dynamicValIdx];
@@ -136,7 +137,7 @@ void mlir::printDynamicIndexList(OpAsmPrinter &printer, Operation *op,
136137
} else {
137138
printer << integer;
138139
}
139-
if (!scalables.empty() && scalables[scalableIndexIdx])
140+
if (!scalableFlags.empty() && scalableFlags[scalableIndexIdx])
140141
printer << "]";
141142

142143
scalableIndexIdx++;
@@ -148,7 +149,7 @@ void mlir::printDynamicIndexList(OpAsmPrinter &printer, Operation *op,
148149
ParseResult mlir::parseDynamicIndexList(
149150
OpAsmParser &parser,
150151
SmallVectorImpl<OpAsmParser::UnresolvedOperand> &values,
151-
DenseI64ArrayAttr &integers, DenseBoolArrayAttr &scalables,
152+
DenseI64ArrayAttr &integers, DenseBoolArrayAttr &scalableFlags,
152153
SmallVectorImpl<Type> *valueTypes, AsmParser::Delimiter delimiter) {
153154

154155
SmallVector<int64_t, 4> integerVals;
@@ -183,7 +184,7 @@ ParseResult mlir::parseDynamicIndexList(
183184
return parser.emitError(parser.getNameLoc())
184185
<< "expected SSA value or integer";
185186
integers = parser.getBuilder().getDenseI64ArrayAttr(integerVals);
186-
scalables = parser.getBuilder().getDenseBoolArrayAttr(scalableVals);
187+
scalableFlags = parser.getBuilder().getDenseBoolArrayAttr(scalableVals);
187188
return success();
188189
}
189190

0 commit comments

Comments
 (0)