17 #define GEN_PASS_DEF_CONVERTELEMENTWISETOLINALGPASS
18 #include "mlir/Dialect/Linalg/Passes.h.inc"
29 return llvm::all_of(op->
getOperandTypes(), llvm::IsaPred<RankedTensorType>);
52 res.reserve(rankedTensorTypes.size());
53 for (
Type t : rankedTensorTypes) {
56 for (
Value v : operands) {
57 if (v.getType() == t) {
67 res.push_back(b.
create<tensor::EmptyOp>(
69 cast<RankedTensorType>(t).getElementType()));
75 struct ConvertAnyElementwiseMappableOpOnRankedTensors :
public RewritePattern {
76 ConvertAnyElementwiseMappableOpOnRankedTensors(
MLIRContext *context)
78 LogicalResult matchAndRewrite(
Operation *op,
81 return rewriter.notifyMatchFailure(
82 op,
"requires elementwise op on ranked tensors");
84 auto rank = cast<RankedTensorType>(op->getResult(0).getType()).getRank();
86 op->getNumResults() + op->getNumOperands(),
87 rewriter.getMultiDimIdentityMap(rank));
89 rank, utils::IteratorType::parallel);
91 rewriter.replaceOpWithNewOp<linalg::GenericOp>(
92 op, op->getResultTypes(),
99 auto resultTypes = llvm::to_vector<6>(
100 llvm::map_range(op->getResultTypes(), [](
Type type) {
101 return cast<TensorType>(type).getElementType();
104 builder.create(loc, op->getName().getIdentifier(),
105 regionArgs.take_front(op->getNumOperands()),
106 resultTypes, op->getAttrs());
107 builder.create<linalg::YieldOp>(loc, scalarOp->getResults());
116 patterns.add<ConvertAnyElementwiseMappableOpOnRankedTensors>(
121 class ConvertElementwiseToLinalgPass
122 :
public impl::ConvertElementwiseToLinalgPassBase<
123 ConvertElementwiseToLinalgPass> {
124 using impl::ConvertElementwiseToLinalgPassBase<
125 ConvertElementwiseToLinalgPass>::ConvertElementwiseToLinalgPassBase;
127 void runOnOperation() final {
128 auto *func = getOperation();
134 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.