17#define GEN_PASS_DEF_CONVERTELEMENTWISETOLINALGPASS
18#include "mlir/Dialect/Linalg/Passes.h.inc"
24 return isa<IntegerType, FloatType, IndexType, ComplexType>(t);
34 bool anyRankedTensor = llvm::any_of(types, llvm::IsaPred<RankedTensorType>);
38 bool noneInvalid = llvm::none_of(types, [](
Type t) {
42 return anyRankedTensor && noneInvalid;
65 res.reserve(rankedTensorTypes.size());
66 for (
Type t : rankedTensorTypes) {
70 if (v.getType() == t) {
80 res.push_back(tensor::EmptyOp::create(
82 cast<RankedTensorType>(t).getElementType()));
88struct ConvertAnyElementwiseMappableOpOnRankedTensors :
public RewritePattern {
89 ConvertAnyElementwiseMappableOpOnRankedTensors(
MLIRContext *context)
91 LogicalResult matchAndRewrite(
Operation *op,
95 op,
"requires elementwise op on ranked tensors");
98 auto rank = resTy.getRank();
110 isScalarOperand.push_back(
true);
111 else if (
auto rt = dyn_cast<RankedTensorType>(ty))
112 isScalarOperand.push_back(
false);
116 "unsupported operand type (expected scalar-like or ranked tensor)");
120 SmallVector<AffineMap> indexingMaps;
123 for (
bool isScalar : isScalarOperand)
124 indexingMaps.push_back(isScalar ? scalarMap : idMap);
128 SmallVector<utils::IteratorType> iteratorTypes(
129 rank, utils::IteratorType::parallel);
130 SmallVector<Value> outputs =
139 [&](OpBuilder &builder, Location loc,
ValueRange regionArgs) {
140 SmallVector<Type> resultEltTys = llvm::to_vector<6>(
141 llvm::map_range(op->getResultTypes(), [](Type type) {
142 return cast<TensorType>(type).getElementType();
144 Operation *scalarOp =
145 builder.
create(loc, op->getName().getIdentifier(),
146 regionArgs.take_front(op->getNumOperands()),
147 resultEltTys, op->getAttrs());
148 linalg::YieldOp::create(builder, loc, scalarOp->
getResults());
157 patterns.add<ConvertAnyElementwiseMappableOpOnRankedTensors>(
162class ConvertElementwiseToLinalgPass
164 ConvertElementwiseToLinalgPass> {
166 ConvertElementwiseToLinalgPass>::ConvertElementwiseToLinalgPassBase;
168 void runOnOperation() final {
169 auto *
func = getOperation();
static bool isElementwiseMappableOpOnRankedTensors(Operation *op)
static bool isScalarLike(Type t)
static SmallVector< Value, 4 > getOrCreateOperandsMatchingResultTypes(OpBuilder &b, Operation *op)
Given op assumed isElementwiseMappableOpOnRankedTensors, iterate over the result types and return a l...
A multi-dimensional affine map Affine map's are immutable like Type's, and they are uniqued.
static AffineMap get(MLIRContext *context)
Returns a zero result affine map with no dimensions or symbols: () -> ().
AffineMap getMultiDimIdentityMap(unsigned rank)
MLIRContext * getContext() const
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 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()
operand_type_range getOperandTypes()
result_type_range getResultTypes()
operand_range getOperands()
Returns an iterator on the underlying Value's.
result_range getResults()
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...
RewritePattern is the common base class for all DAG to DAG replacements.
static std::unique_ptr< T > create(Args &&...args)
This method provides a convenient interface for creating and initializing derived rewrite patterns of...
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,...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
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.
const FrozenRewritePatternSet & patterns