21 static void unpackUnrealizedConversionCast(
Value v,
24 dyn_cast_or_null<UnrealizedConversionCastOp>(v.
getDefiningOp())) {
25 if (cast.getInputs().size() != 1) {
27 unpacked.append(cast.getInputs().begin(), cast.getInputs().end());
32 unpacked.push_back(v);
38 template <
typename SourceOp,
typename ConcretePattern>
55 matchAndRewrite(SourceOp op, OpAdaptor adaptor,
61 for (
Type type : op.getResultTypes()) {
62 if (failed(typeConverter->convertTypes(type, dstTypes)))
64 offsets.push_back(dstTypes.size());
68 std::optional<SourceOp> newOp =
69 static_cast<const ConcretePattern *
>(
this)->convertSourceOp(
70 op, adaptor, rewriter, dstTypes);
77 for (
unsigned i = 1, e = offsets.size(); i < e; i++) {
78 unsigned start = offsets[i - 1], end = offsets[i];
79 unsigned len = end - start;
80 ValueRange mappedValue = newOp->getResults().slice(start, len);
83 Type origType = op.getResultTypes()[i - 1];
84 Value mat = typeConverter->materializeSourceConversion(
85 rewriter, op.getLoc(), origType, mappedValue);
88 op,
"Failed to materialize 1:N type conversion");
90 packedRets.push_back(mat);
93 packedRets.push_back(mappedValue.front());
102 class ConvertForOpTypes
103 :
public Structural1ToNConversionPattern<ForOp, ConvertForOpTypes> {
105 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
108 std::optional<ForOp> convertSourceOp(ForOp op, OpAdaptor adaptor,
134 for (
Value arg : adaptor.getInitArgs())
135 unpackUnrealizedConversionCast(arg, flatArgs);
139 ForOp newOp = rewriter.
create<ForOp>(op.getLoc(), adaptor.getLowerBound(),
140 adaptor.getUpperBound(),
141 adaptor.getStep(), flatArgs);
150 newOp.getRegion().end());
158 class ConvertIfOpTypes
159 :
public Structural1ToNConversionPattern<IfOp, ConvertIfOpTypes> {
161 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
163 std::optional<IfOp> convertSourceOp(IfOp op, OpAdaptor adaptor,
167 IfOp newOp = rewriter.
create<IfOp>(op.getLoc(), dstTypes,
168 adaptor.getCondition(),
true);
177 newOp.getThenRegion().end());
179 newOp.getElseRegion().end());
187 class ConvertWhileOpTypes
188 :
public Structural1ToNConversionPattern<WhileOp, ConvertWhileOpTypes> {
190 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
192 std::optional<WhileOp> convertSourceOp(WhileOp op, OpAdaptor adaptor,
197 for (
Value arg : adaptor.getOperands())
198 unpackUnrealizedConversionCast(arg, flatArgs);
200 auto newOp = rewriter.
create<WhileOp>(op.getLoc(), dstTypes, flatArgs);
202 for (
auto i : {0u, 1u}) {
221 matchAndRewrite(scf::YieldOp op, OpAdaptor adaptor,
224 for (
Value operand : adaptor.getOperands())
225 unpackUnrealizedConversionCast(operand, unpackedYield);
238 matchAndRewrite(ConditionOp op, OpAdaptor adaptor,
241 for (
Value operand : adaptor.getOperands())
242 unpackUnrealizedConversionCast(operand, unpackedYield);
244 rewriter.
modifyOpInPlace(op, [&]() { op->setOperands(unpackedYield); });
252 patterns.
add<ConvertForOpTypes, ConvertIfOpTypes, ConvertYieldOpTypes,
253 ConvertWhileOpTypes, ConvertConditionOpTypes>(
260 return typeConverter.
isLegal(op->getResultTypes());
265 if (!isa<ForOp, IfOp, WhileOp>(op->getParentOp()))
267 return typeConverter.
isLegal(op.getOperandTypes());
This class implements a pattern rewriter for use with ConversionPatterns.
void replaceOp(Operation *op, ValueRange newValues) override
Replace the given operation with the new values.
FailureOr< Block * > convertRegionTypes(Region *region, const TypeConverter &converter, TypeConverter::SignatureConversion *entryConversion=nullptr)
Apply a signature conversion to each block in the given region.
void eraseBlock(Block *block) override
PatternRewriter hook for erase all operations in a block.
This class describes a specific conversion target.
void addDynamicallyLegalOp(OperationName op, const DynamicLegalityCallbackFn &callback)
Register the given operation as dynamically legal and set the dynamic legalization callback to the on...
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
OpConversionPattern is a wrapper around ConversionPattern that allows for matching and rewriting agai...
typename SourceOp::Adaptor OpAdaptor
OpConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
Operation is the basic unit of execution within MLIR.
void setAttrs(DictionaryAttr newAttrs)
Set the attributes from a dictionary on this operation.
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
void modifyOpInPlace(Operation *root, CallableT &&callable)
This method is a utility wrapper around an in-place modification of an operation.
void inlineRegionBefore(Region ®ion, Region &parent, Region::iterator before)
Move the blocks that belong to "region" before the given position in another region "parent".
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
bool isLegal(Type type) const
Return true if the given type is legal for this type converter, i.e.
This class provides an abstraction over the various different ranges of value types.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
void populateSCFStructuralTypeConversionsAndLegality(const TypeConverter &typeConverter, RewritePatternSet &patterns, ConversionTarget &target)
Populates patterns for SCF structural type conversions and sets up the provided ConversionTarget with...
void populateSCFStructuralTypeConversions(const TypeConverter &typeConverter, RewritePatternSet &patterns)
Similar to populateSCFStructuralTypeConversionsAndLegality but does not populate the conversion targe...
void populateSCFStructuralTypeConversionTarget(const TypeConverter &typeConverter, ConversionTarget &target)
Updates the ConversionTarget with dynamic legality of SCF operations based on the provided type conve...
Include the generated interface declarations.