Skip to content

Commit ed7bb70

Browse files
[mlir][Transforms] Dialect conversion: Simplify handling of dropped arguments
This commit simplifies the handling of dropped arguments and updates some dialect conversion documentation that is outdated. When converting a block signature, a `BlockTypeConversionRewrite` object and potentially multiple `ReplaceBlockArgRewrite` are created. During the "commit" phase, uses of the old block arguments are replaced with the new block arguments, but the old implementation was written in an inconsistent way: some block arguments were replaced in `BlockTypeConversionRewrite::commit` and some were replaced in `ReplaceBlockArgRewrite::commit`. The new `BlockTypeConversionRewrite::commit` implementation is much simpler and no longer modifies any IR; that is done only in `ReplaceBlockArgRewrite` now. The `ConvertedArgInfo` data structure is no longer needed. To that end, materializations of dropped arguments are now built in `applySignatureConversion` instead of `materializeLiveConversions`; the latter function no longer has to deal with dropped arguments. Other minor improvements: - Improve variable name: `origOutputType` -> `origArgType`. Add an assertion to check that this field is only used for argument materializations. - Add more comments to `applySignatureConversion`. Note: Error messages around failed materializations for dropped basic block arguments changed slightly. That is because those materializations are now built in `legalizeUnresolvedMaterialization` instead of `legalizeConvertedArgumentTypes`.
1 parent 949d9d8 commit ed7bb70

File tree

4 files changed

+111
-150
lines changed

4 files changed

+111
-150
lines changed

mlir/docs/DialectConversion.md

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ depending on the situation.
246246
247247
- An argument materialization is used when converting the type of a block
248248
argument during a [signature conversion](#region-signature-conversion).
249+
The new block argument types are specified in a `SignatureConversion`
250+
object. An original block argument can be converted into multiple
251+
block arguments, which is not supported everywhere in the dialect
252+
conversion. (E.g., adaptors support only a single replacement value for
253+
each original value.) Therefore, an argument materialization is used to
254+
convert potentially multiple new block arguments back into a single SSA
255+
value.
249256
250257
* Source Materialization
251258
@@ -259,6 +266,9 @@ depending on the situation.
259266
* When a block argument has been converted to a different type, but
260267
the original argument still has users that will remain live after
261268
the conversion process has finished.
269+
* When a block argument has been dropped, but the argument still has
270+
users that will remain live after the conversion process has
271+
finished.
262272
* When the result type of an operation has been converted to a
263273
different type, but the original result still has users that will
264274
remain live after the conversion process is finished.
@@ -330,34 +340,37 @@ class TypeConverter {
330340
331341
/// Register a materialization function, which must be convertible to the
332342
/// following form:
333-
/// `Optional<Value> (OpBuilder &, T, ValueRange, Location)`,
334-
/// where `T` is any subclass of `Type`.
335-
/// This function is responsible for creating an operation, using the
336-
/// OpBuilder and Location provided, that "converts" a range of values into a
337-
/// single value of the given type `T`. It must return a Value of the
338-
/// converted type on success, an `std::nullopt` if it failed but other
339-
/// materialization can be attempted, and `nullptr` on unrecoverable failure.
340-
/// It will only be called for (sub)types of `T`.
343+
/// `std::optional<Value>(OpBuilder &, T, ValueRange, Location)`,
344+
/// where `T` is any subclass of `Type`. This function is responsible for
345+
/// creating an operation, using the OpBuilder and Location provided, that
346+
/// "casts" a range of values into a single value of the given type `T`. It
347+
/// must return a Value of the converted type on success, an `std::nullopt` if
348+
/// it failed but other materialization can be attempted, and `nullptr` on
349+
/// unrecoverable failure. It will only be called for (sub)types of `T`.
350+
/// Materialization functions must be provided when a type conversion may
351+
/// persist after the conversion has finished.
341352
///
342353
/// This method registers a materialization that will be called when
343-
/// converting an illegal block argument type, to a legal type.
354+
/// converting potentially multiple replacement block arguments (of a single
355+
/// original block argument), to a single SSA value with a legal type.
344356
template <typename FnT,
345357
typename T = typename llvm::function_traits<FnT>::template arg_t<1>>
346358
void addArgumentMaterialization(FnT &&callback) {
347359
argumentMaterializations.emplace_back(
348360
wrapMaterialization<T>(std::forward<FnT>(callback)));
349361
}
350362
/// This method registers a materialization that will be called when
351-
/// converting a legal type to an illegal source type. This is used when
352-
/// conversions to an illegal type must persist beyond the main conversion.
363+
/// converting a legal replacement value back to an illegal source type.
364+
/// This is used when some uses of the original, illegal value must persist
365+
/// beyond the main conversion.
353366
template <typename FnT,
354367
typename T = typename llvm::function_traits<FnT>::template arg_t<1>>
355368
void addSourceMaterialization(FnT &&callback) {
356369
sourceMaterializations.emplace_back(
357370
wrapMaterialization<T>(std::forward<FnT>(callback)));
358371
}
359372
/// This method registers a materialization that will be called when
360-
/// converting type from an illegal, or source, type to a legal type.
373+
/// converting an illegal (source) value to a legal (target) type.
361374
template <typename FnT,
362375
typename T = typename llvm::function_traits<FnT>::template arg_t<1>>
363376
void addTargetMaterialization(FnT &&callback) {

mlir/include/mlir/Transforms/DialectConversion.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,26 @@ class TypeConverter {
181181
/// persist after the conversion has finished.
182182
///
183183
/// This method registers a materialization that will be called when
184-
/// converting an illegal block argument type, to a legal type.
184+
/// converting potentially multiple replacement block arguments (of a single
185+
/// original block argument), to a single SSA value with a legal type.
185186
template <typename FnT, typename T = typename llvm::function_traits<
186187
std::decay_t<FnT>>::template arg_t<1>>
187188
void addArgumentMaterialization(FnT &&callback) {
188189
argumentMaterializations.emplace_back(
189190
wrapMaterialization<T>(std::forward<FnT>(callback)));
190191
}
191192
/// This method registers a materialization that will be called when
192-
/// converting a legal type to an illegal source type. This is used when
193-
/// conversions to an illegal type must persist beyond the main conversion.
193+
/// converting a legal replacement value back to an illegal source type.
194+
/// This is used when some uses of the original, illegal value must persist
195+
/// beyond the main conversion.
194196
template <typename FnT, typename T = typename llvm::function_traits<
195197
std::decay_t<FnT>>::template arg_t<1>>
196198
void addSourceMaterialization(FnT &&callback) {
197199
sourceMaterializations.emplace_back(
198200
wrapMaterialization<T>(std::forward<FnT>(callback)));
199201
}
200202
/// This method registers a materialization that will be called when
201-
/// converting type from an illegal, or source, type to a legal type.
203+
/// converting an illegal (source) value to a legal (target) type.
202204
template <typename FnT, typename T = typename llvm::function_traits<
203205
std::decay_t<FnT>>::template arg_t<1>>
204206
void addTargetMaterialization(FnT &&callback) {

0 commit comments

Comments
 (0)