[Mlir-commits] [mlir] [mlir][Transforms] Make lookup without type converter unambiguous (PR #151747)
Matthias Springer
llvmlistbot at llvm.org
Wed Aug 6 00:03:02 PDT 2025
================
@@ -185,54 +185,55 @@ struct ConversionValueMapping {
};
} // namespace
-ValueVector
-ConversionValueMapping::lookupOrDefault(Value from,
- TypeRange desiredTypes) const {
- // Try to find the deepest values that have the desired types. If there is no
- // such mapping, simply return the deepest values.
- ValueVector desiredValue;
- ValueVector current{from};
- do {
- // Store the current value if the types match.
- if (TypeRange(ValueRange(current)) == desiredTypes)
- desiredValue = current;
-
- // If possible, Replace each value with (one or multiple) mapped values.
- ValueVector next;
- for (Value v : current) {
- auto it = mapping.find({v});
- if (it != mapping.end()) {
- llvm::append_range(next, it->second);
- } else {
- next.push_back(v);
- }
- }
- if (next != current) {
- // If at least one value was replaced, continue the lookup from there.
- current = std::move(next);
- continue;
- }
+/// Marker attribute for pure type conversions. I.e., mappings whose only
+/// purpose is to resolve a type mismatch. (In contrast, mappings that point to
+/// the replacement values of a "replaceOp" call, etc., are not pure type
+/// conversions.)
+static const StringRef kPureTypeConversionMarker = "__pure_type_conversion__";
+
+/// A vector of values is a pure type conversion if all values are defined by
+/// the same operation and the operation has the `kPureTypeConversionMarker`
+/// attribute.
+static bool isPureTypeConversion(const ValueVector &values) {
+ assert(!values.empty() && "expected non-empty value vector");
+ Operation *op = values.front().getDefiningOp();
+ for (Value v : llvm::drop_begin(values))
+ if (v.getDefiningOp() != op)
+ return false;
+ return op && op->hasAttr(kPureTypeConversionMarker);
+}
----------------
matthias-springer wrote:
It's not just target materializations, but also source materializations. But not all source materializations. The source materializations that have no inputs and were created when erasing an op or dropping a block argument are excluded. I was struggling to find a good name for that.
The two kind of mappings that we have to distinguish between:
1. User wants to replace value A with value B. That's a "regular" replacement.
2. Driver notices that it needs to insert a materialization to make the IR pass type checking. That's a pure type conversion.
https://github.com/llvm/llvm-project/pull/151747
More information about the Mlir-commits
mailing list