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");
85 auto rank = cast<RankedTensorType>(op->getResult(0).getType()).getRank();
87 op->getNumResults() + op->getNumOperands(),
88 rewriter.getMultiDimIdentityMap(rank));
90 rank, utils::IteratorType::parallel);
92 rewriter.replaceOpWithNewOp<linalg::GenericOp>(
93 op, op->getResultTypes(),
100 auto resultTypes = llvm::to_vector<6>(
101 llvm::map_range(op->getResultTypes(), [](
Type type) {
102 return cast<TensorType>(type).getElementType();
105 builder.create(loc, op->getName().getIdentifier(),
106 regionArgs.take_front(op->getNumOperands()),
107 resultTypes, op->getAttrs());
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.
Operation is the basic unit of execution within MLIR.
Location getLoc()
The source location the operation was defined or derived from.
operand_type_range getOperandTypes()
result_type_range getResultTypes()
operand_range getOperands()
Returns an iterator on the underlying Value's.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
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...
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.
const FrozenRewritePatternSet & patterns
LogicalResult applyPartialConversion(ArrayRef< Operation * > ops, const ConversionTarget &target, const FrozenRewritePatternSet &patterns, ConversionConfig config=ConversionConfig())
Below we define several entry points for operation conversion.