22 for (
const auto &vals : values)
23 llvm::append_range(result, vals);
29 assert(values.size() == 1 &&
"expected single value");
30 return values.front();
36 template <
typename SourceOp,
typename ConcretePattern>
41 using OneToNOpAdaptor =
55 matchAndRewrite(SourceOp op, OneToNOpAdaptor 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);
81 packedRets.push_back(mappedValue);
89 class ConvertForOpTypes
90 :
public Structural1ToNConversionPattern<ForOp, ConvertForOpTypes> {
92 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
95 std::optional<ForOp> convertSourceOp(ForOp op, OneToNOpAdaptor adaptor,
121 ForOp newOp = rewriter.
create<ForOp>(
134 newOp.getRegion().end());
142 class ConvertIfOpTypes
143 :
public Structural1ToNConversionPattern<IfOp, ConvertIfOpTypes> {
145 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
147 std::optional<IfOp> convertSourceOp(IfOp op, OneToNOpAdaptor adaptor,
151 IfOp newOp = rewriter.
create<IfOp>(
152 op.getLoc(), dstTypes,
getSingleValue(adaptor.getCondition()),
true);
161 newOp.getThenRegion().end());
163 newOp.getElseRegion().end());
171 class ConvertWhileOpTypes
172 :
public Structural1ToNConversionPattern<WhileOp, ConvertWhileOpTypes> {
174 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
176 std::optional<WhileOp> convertSourceOp(WhileOp op, OneToNOpAdaptor adaptor,
179 auto newOp = rewriter.
create<WhileOp>(op.getLoc(), dstTypes,
182 for (
auto i : {0u, 1u}) {
201 matchAndRewrite(scf::YieldOp op, OneToNOpAdaptor adaptor,
215 matchAndRewrite(ConditionOp op, OneToNOpAdaptor adaptor,
218 op, [&]() { op->setOperands(
flattenValues(adaptor.getOperands())); });
226 patterns.add<ConvertForOpTypes, ConvertIfOpTypes, ConvertYieldOpTypes,
227 ConvertWhileOpTypes, ConvertConditionOpTypes>(
228 typeConverter,
patterns.getContext());
234 return typeConverter.
isLegal(op->getResultTypes());
239 if (!isa<ForOp, IfOp, WhileOp>(op->getParentOp()))
241 return typeConverter.
isLegal(op.getOperandTypes());
static SmallVector< Value > flattenValues(ArrayRef< ValueRange > values)
Flatten the given value ranges into a single vector of values.
static Value getSingleValue(ValueRange values)
Assert that the given value range contains a single value and return it.
This class implements a pattern rewriter for use with ConversionPatterns.
FailureOr< Block * > convertRegionTypes(Region *region, const TypeConverter &converter, TypeConverter::SignatureConversion *entryConversion=nullptr)
Apply a signature conversion to each block in the given region.
void replaceOpWithMultiple(Operation *op, ArrayRef< ValueRange > newValues)
Replace the given operation with the new value ranges.
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...
OpConversionPattern(MLIRContext *context, PatternBenefit benefit=1)
typename SourceOp::template GenericAdaptor< ArrayRef< ValueRange > > OneToNOpAdaptor
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'.
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...
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.
const FrozenRewritePatternSet & patterns