18 #define GEN_PASS_DEF_CONVERTELEMENTWISETOLINALGPASS
19 #include "mlir/Dialect/Linalg/Passes.h.inc"
30 return llvm::all_of(op->
getOperandTypes(), llvm::IsaPred<RankedTensorType>);
53 res.reserve(rankedTensorTypes.size());
54 for (
Type t : rankedTensorTypes) {
57 for (
Value v : operands) {
58 if (v.getType() == t) {
68 res.push_back(b.
create<tensor::EmptyOp>(
70 cast<RankedTensorType>(t).getElementType()));
76 struct ConvertAnyElementwiseMappableOpOnRankedTensors :
public RewritePattern {
77 ConvertAnyElementwiseMappableOpOnRankedTensors(
MLIRContext *context)
79 LogicalResult matchAndRewrite(
Operation *op,
82 return rewriter.notifyMatchFailure(
83 op,
"requires elementwise op on ranked tensors");
88 rewriter.getMultiDimIdentityMap(rank));
90 rank, utils::IteratorType::parallel);
92 rewriter.replaceOpWithNewOp<linalg::GenericOp>(
100 auto resultTypes = llvm::to_vector<6>(
102 return cast<TensorType>(type).getElementType();
108 builder.create<linalg::YieldOp>(loc, scalarOp->getResults());
117 patterns.
add<ConvertAnyElementwiseMappableOpOnRankedTensors>(
122 class ConvertElementwiseToLinalgPass
123 :
public impl::ConvertElementwiseToLinalgPassBase<
124 ConvertElementwiseToLinalgPass> {
125 using impl::ConvertElementwiseToLinalgPassBase<
126 ConvertElementwiseToLinalgPass>::ConvertElementwiseToLinalgPassBase;
128 void runOnOperation() final {
129 auto *func = getOperation();
135 target.markUnknownOpDynamicallyLegal([](
Operation *op) {
static bool isElementwiseMappableOpOnRankedTensors(Operation *op)
static SmallVector< Value, 4 > getOrCreateOperandsMatchingResultTypes(OpBuilder &b, Operation *op)
Given op assumed isElementwiseMappableOpOnRankedTensors, iterate over the result types and return a l...
static MLIRContext * getContext(OpFoldResult val)
This class describes a specific conversion target.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
MLIRContext is the top-level object for a collection of MLIR operations.
This class helps build Operations.
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
StringAttr getIdentifier() const
Return the name of this operation as a StringAttr.
Operation is the basic unit of execution within MLIR.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Location getLoc()
The source location the operation was defined or derived from.
unsigned getNumOperands()
ArrayRef< NamedAttribute > getAttrs()
Return all of the attributes on this operation.
OperationName getName()
The name of an operation is the key identifier for it.
operand_type_range getOperandTypes()
result_type_range getResultTypes()
operand_range getOperands()
Returns an iterator on the underlying Value's.
unsigned getNumResults()
Return the number of results held by this operation.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
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.
RewritePattern is the common base class for all DAG to DAG replacements.
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...
Type getType() const
Return the type of this value.
bool hasElementwiseMappableTraits(Operation *op)
Together, Elementwise, Scalarizable, Vectorizable, and Tensorizable provide an easy way for scalar op...
void populateElementwiseToLinalgConversionPatterns(RewritePatternSet &patterns)
Populate patterns that convert ElementwiseMappable ops to linalg parallel loops.
SmallVector< OpFoldResult > getMixedSizes(OpBuilder &builder, Location loc, Value value)
Return the dimensions of the given tensor value.
Include the generated interface declarations.
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.