22 for (
const auto &vals : values)
23 llvm::append_range(result, vals);
30 template <
typename SourceOp,
typename ConcretePattern>
35 using OneToNOpAdaptor =
49 matchAndRewrite(SourceOp op, OneToNOpAdaptor adaptor,
55 for (
Type type : op.getResultTypes()) {
56 if (failed(typeConverter->convertTypes(type, dstTypes)))
58 offsets.push_back(dstTypes.size());
62 std::optional<SourceOp> newOp =
63 static_cast<const ConcretePattern *
>(
this)->convertSourceOp(
64 op, adaptor, rewriter, dstTypes);
71 for (
unsigned i = 1, e = offsets.size(); i < e; i++) {
72 unsigned start = offsets[i - 1], end = offsets[i];
73 unsigned len = end - start;
74 ValueRange mappedValue = newOp->getResults().slice(start, len);
75 packedRets.push_back(mappedValue);
83 class ConvertForOpTypes
84 :
public Structural1ToNConversionPattern<ForOp, ConvertForOpTypes> {
86 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
89 std::optional<ForOp> convertSourceOp(ForOp op, OneToNOpAdaptor adaptor,
115 ForOp newOp = rewriter.
create<ForOp>(
116 op.getLoc(), llvm::getSingleElement(adaptor.getLowerBound()),
117 llvm::getSingleElement(adaptor.getUpperBound()),
118 llvm::getSingleElement(adaptor.getStep()),
128 newOp.getRegion().end());
136 class ConvertIfOpTypes
137 :
public Structural1ToNConversionPattern<IfOp, ConvertIfOpTypes> {
139 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
141 std::optional<IfOp> convertSourceOp(IfOp op, OneToNOpAdaptor adaptor,
145 IfOp newOp = rewriter.
create<IfOp>(
146 op.getLoc(), dstTypes, llvm::getSingleElement(adaptor.getCondition()),
156 newOp.getThenRegion().end());
158 newOp.getElseRegion().end());
166 class ConvertWhileOpTypes
167 :
public Structural1ToNConversionPattern<WhileOp, ConvertWhileOpTypes> {
169 using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
171 std::optional<WhileOp> convertSourceOp(WhileOp op, OneToNOpAdaptor adaptor,
174 auto newOp = rewriter.
create<WhileOp>(op.getLoc(), dstTypes,
177 for (
auto i : {0u, 1u}) {
196 matchAndRewrite(scf::YieldOp op, OneToNOpAdaptor adaptor,
210 matchAndRewrite(ConditionOp op, OneToNOpAdaptor adaptor,
213 op, [&]() { op->setOperands(
flattenValues(adaptor.getOperands())); });
221 patterns.add<ConvertForOpTypes, ConvertIfOpTypes, ConvertYieldOpTypes,
222 ConvertWhileOpTypes, ConvertConditionOpTypes>(
223 typeConverter,
patterns.getContext());
229 return typeConverter.
isLegal(op->getResultTypes());
234 if (!isa<ForOp, IfOp, WhileOp>(op->getParentOp()))
236 return typeConverter.
isLegal(op.getOperandTypes());
static SmallVector< Value > flattenValues(ArrayRef< ValueRange > values)
Flatten the given value ranges into a single vector of values.
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.
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