13c2a74a3SMatthias Springer //===- LinalgTransformOps.cpp - Implementation of Linalg transform ops ----===//
23c2a74a3SMatthias Springer //
33c2a74a3SMatthias Springer // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43c2a74a3SMatthias Springer // See https://llvm.org/LICENSE.txt for license information.
53c2a74a3SMatthias Springer // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63c2a74a3SMatthias Springer //
73c2a74a3SMatthias Springer //===----------------------------------------------------------------------===//
83c2a74a3SMatthias Springer 
93c2a74a3SMatthias Springer #include "mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.h"
103c2a74a3SMatthias Springer 
113c2a74a3SMatthias Springer #include "mlir/Dialect/Linalg/IR/Linalg.h"
123c2a74a3SMatthias Springer #include "mlir/Dialect/Linalg/Transforms/Transforms.h"
133c2a74a3SMatthias Springer #include "mlir/Dialect/PDL/IR/PDL.h"
143c2a74a3SMatthias Springer #include "mlir/Dialect/PDL/IR/PDLTypes.h"
153c2a74a3SMatthias Springer #include "mlir/Dialect/Transform/IR/TransformDialect.h"
163c2a74a3SMatthias Springer #include "mlir/Parser/Parser.h"
17cc6c1592SAlex Zinenko #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
183c2a74a3SMatthias Springer 
193c2a74a3SMatthias Springer using namespace mlir;
203c2a74a3SMatthias Springer using namespace mlir::linalg;
213c2a74a3SMatthias Springer using namespace mlir::transform;
223c2a74a3SMatthias Springer 
233c2a74a3SMatthias Springer /// Extracts a vector of int64_t from an array attribute. Asserts if the
243c2a74a3SMatthias Springer /// attribute contains values other than integers.
253c2a74a3SMatthias Springer static SmallVector<int64_t> extractI64Array(ArrayAttr attr) {
263c2a74a3SMatthias Springer   SmallVector<int64_t> result;
273c2a74a3SMatthias Springer   result.reserve(attr.size());
283c2a74a3SMatthias Springer   for (APInt value : attr.getAsValueRange<IntegerAttr>())
293c2a74a3SMatthias Springer     result.push_back(value.getSExtValue());
303c2a74a3SMatthias Springer   return result;
313c2a74a3SMatthias Springer }
323c2a74a3SMatthias Springer 
333c2a74a3SMatthias Springer /// Extracts a vector of unsigned from an array attribute. Asserts if the
343c2a74a3SMatthias Springer /// attribute contains values other than intergers. May truncate.
353c2a74a3SMatthias Springer static SmallVector<unsigned> extractUIntArray(ArrayAttr attr) {
363c2a74a3SMatthias Springer   SmallVector<unsigned> result;
373c2a74a3SMatthias Springer   result.reserve(attr.size());
383c2a74a3SMatthias Springer   for (APInt value : attr.getAsValueRange<IntegerAttr>())
393c2a74a3SMatthias Springer     result.push_back(value.getZExtValue());
403c2a74a3SMatthias Springer   return result;
413c2a74a3SMatthias Springer }
423c2a74a3SMatthias Springer 
433c2a74a3SMatthias Springer namespace {
443c2a74a3SMatthias Springer /// A simple pattern rewriter that implements no special logic.
453c2a74a3SMatthias Springer class SimpleRewriter : public PatternRewriter {
463c2a74a3SMatthias Springer public:
473c2a74a3SMatthias Springer   SimpleRewriter(MLIRContext *context) : PatternRewriter(context) {}
483c2a74a3SMatthias Springer };
493c2a74a3SMatthias Springer } // namespace
503c2a74a3SMatthias Springer 
51ce2e198bSAlex Zinenko /// Attempts to apply the pattern specified as template argument to the given
52ce2e198bSAlex Zinenko /// operation. The pattern is expected to have a `returningMatchAndRewrite`
53ce2e198bSAlex Zinenko /// function that returns the "main" result or failure. Returns failure if the
54ce2e198bSAlex Zinenko /// pattern failed to apply. Extra arguments are forwarded to the pattern
55ce2e198bSAlex Zinenko /// constructor.
56ce2e198bSAlex Zinenko template <typename PatternTy, typename... Args>
57ce2e198bSAlex Zinenko static FailureOr<LinalgOp> tryApply(Operation *operation, Args &&...args) {
58ce2e198bSAlex Zinenko   // Check if the given operation has the type expected by the pattern.
59ce2e198bSAlex Zinenko   using OpTy = typename llvm::function_traits<
60ce2e198bSAlex Zinenko       decltype(&PatternTy::returningMatchAndRewrite)>::template arg_t<0>;
61ce2e198bSAlex Zinenko   auto op = dyn_cast<OpTy>(operation);
62ce2e198bSAlex Zinenko   if (!op)
63ce2e198bSAlex Zinenko     return failure();
64ce2e198bSAlex Zinenko 
65ce2e198bSAlex Zinenko   // Apply the pattern directly to the op.
66ce2e198bSAlex Zinenko   PatternTy pattern(operation->getContext(), std::forward<Args>(args)...);
67ce2e198bSAlex Zinenko   SimpleRewriter rewriter(operation->getContext());
68ce2e198bSAlex Zinenko   rewriter.setInsertionPoint(operation);
69ce2e198bSAlex Zinenko   auto result = pattern.returningMatchAndRewrite(op, rewriter);
70ce2e198bSAlex Zinenko   if (failed(result))
71ce2e198bSAlex Zinenko     return failure();
72ce2e198bSAlex Zinenko   return cast<LinalgOp>(result->getOperation());
73ce2e198bSAlex Zinenko }
74ce2e198bSAlex Zinenko 
75ce2e198bSAlex Zinenko //===----------------------------------------------------------------------===//
76ce2e198bSAlex Zinenko // DecomposeOp
77ce2e198bSAlex Zinenko //===----------------------------------------------------------------------===//
78ce2e198bSAlex Zinenko 
79ce2e198bSAlex Zinenko FailureOr<LinalgOp> transform::DecomposeOp::applyToOne(LinalgOp target) {
80ce2e198bSAlex Zinenko   FailureOr<LinalgOp> windowed =
81ce2e198bSAlex Zinenko       tryApply<DownscaleSizeOneWindowed2DConvolution>(target);
82ce2e198bSAlex Zinenko   if (succeeded(windowed))
83ce2e198bSAlex Zinenko     return windowed;
84ce2e198bSAlex Zinenko 
85ce2e198bSAlex Zinenko   FailureOr<LinalgOp> depthwise =
86ce2e198bSAlex Zinenko       tryApply<DownscaleDepthwiseConv2DNhwcHwcOp>(target);
87ce2e198bSAlex Zinenko   if (succeeded(depthwise))
88ce2e198bSAlex Zinenko     return depthwise;
89ce2e198bSAlex Zinenko 
905f0d4f20SAlex Zinenko   return reportUnknownTransformError(target);
91ce2e198bSAlex Zinenko }
92ce2e198bSAlex Zinenko 
93ce2e198bSAlex Zinenko //===----------------------------------------------------------------------===//
94b6c58ec4SAlex Zinenko // FuseOp
95b6c58ec4SAlex Zinenko //===----------------------------------------------------------------------===//
96b6c58ec4SAlex Zinenko 
97b6c58ec4SAlex Zinenko /// Apply a tiling transformation to all payload ops and store both the
98b6c58ec4SAlex Zinenko /// tiled operation as well as the created tile loops.
99b6c58ec4SAlex Zinenko static LogicalResult
100b6c58ec4SAlex Zinenko applyTilingToAll(Operation *transformOp, Value target,
101b6c58ec4SAlex Zinenko                  ArrayRef<int64_t> tileSizes,
102b6c58ec4SAlex Zinenko                  transform::TransformResults &transformResults,
103b6c58ec4SAlex Zinenko                  transform::TransformState &state,
104b6c58ec4SAlex Zinenko                  function_ref<FailureOr<TiledLinalgOp>(LinalgOp)> applyFn) {
105b6c58ec4SAlex Zinenko   // Number of loops: Number of tiles sizes that are not zero.
106b6c58ec4SAlex Zinenko   size_t numLoops = tileSizes.size() - llvm::count(tileSizes, 0);
107b6c58ec4SAlex Zinenko   // All payload ops. These should all be LinalgOps for now.
108b6c58ec4SAlex Zinenko   ArrayRef<Operation *> payloadOps = state.getPayloadOps(target);
109b6c58ec4SAlex Zinenko 
110b6c58ec4SAlex Zinenko   SmallVector<Operation *> tiledLinalgOps;
111b6c58ec4SAlex Zinenko   SmallVector<SmallVector<Operation *>> loopOps(numLoops);
112b6c58ec4SAlex Zinenko   for (unsigned int i = 0; i < numLoops; ++i)
113b6c58ec4SAlex Zinenko     loopOps[i].reserve(payloadOps.size());
114b6c58ec4SAlex Zinenko 
115b6c58ec4SAlex Zinenko   for (Operation *target : payloadOps) {
116b6c58ec4SAlex Zinenko     auto linalgOp = dyn_cast<linalg::LinalgOp>(target);
117b6c58ec4SAlex Zinenko     if (!linalgOp)
118b6c58ec4SAlex Zinenko       return transformOp->emitError("only LinalgOps are supported");
119b6c58ec4SAlex Zinenko 
120b6c58ec4SAlex Zinenko     FailureOr<TiledLinalgOp> tiled = applyFn(linalgOp);
121b6c58ec4SAlex Zinenko     if (failed(tiled))
122b6c58ec4SAlex Zinenko       return failure();
123b6c58ec4SAlex Zinenko 
124b6c58ec4SAlex Zinenko     tiledLinalgOps.push_back(tiled->op);
125b6c58ec4SAlex Zinenko     if (tiled->loops.size() != numLoops)
126b6c58ec4SAlex Zinenko       // Not enough loops were generated. This usually means that the input size
127b6c58ec4SAlex Zinenko       // was smaller than the tiling size.
128b6c58ec4SAlex Zinenko       // TODO: LinalgTilingPattern should return failure().
129b6c58ec4SAlex Zinenko       return failure();
130b6c58ec4SAlex Zinenko     for (unsigned int i = 0; i < numLoops; ++i)
131b6c58ec4SAlex Zinenko       loopOps[i].push_back(tiled->loops[i]);
132b6c58ec4SAlex Zinenko   }
133b6c58ec4SAlex Zinenko 
134b6c58ec4SAlex Zinenko   transformResults.set(transformOp->getOpResult(0), tiledLinalgOps);
135b6c58ec4SAlex Zinenko   for (unsigned int i = 0; i < numLoops; ++i)
136b6c58ec4SAlex Zinenko     transformResults.set(transformOp->getOpResult(i + 1), loopOps[i]);
137b6c58ec4SAlex Zinenko   return success();
138b6c58ec4SAlex Zinenko }
139b6c58ec4SAlex Zinenko 
140b6c58ec4SAlex Zinenko /// Parse a tiling-like operation that returns the tiled op as well as the
141b6c58ec4SAlex Zinenko /// created tile loops. The function counts the non-zero tile sizes to compute
142b6c58ec4SAlex Zinenko /// the number of results.
143b6c58ec4SAlex Zinenko static ParseResult parseTileLikeOp(OpAsmParser &parser, OperationState &result,
144b6c58ec4SAlex Zinenko                                    StringRef sizesAttrName) {
145b6c58ec4SAlex Zinenko   OpAsmParser::UnresolvedOperand targetOperand;
146b6c58ec4SAlex Zinenko   SMLoc opLoc = parser.getCurrentLocation();
147b6c58ec4SAlex Zinenko   if (parser.parseOperand(targetOperand) ||
148b6c58ec4SAlex Zinenko       parser.parseOptionalAttrDict(result.attributes))
149b6c58ec4SAlex Zinenko     return failure();
150b6c58ec4SAlex Zinenko   Attribute sizesAttr = result.attributes.get(sizesAttrName);
151b6c58ec4SAlex Zinenko   if (!sizesAttr)
152b6c58ec4SAlex Zinenko     return parser.emitError(opLoc)
153b6c58ec4SAlex Zinenko            << "expected '" << sizesAttrName << "' attribute";
154b6c58ec4SAlex Zinenko   auto sizesArrayAttr = sizesAttr.dyn_cast<ArrayAttr>();
155b6c58ec4SAlex Zinenko   if (!sizesArrayAttr)
156b6c58ec4SAlex Zinenko     return parser.emitError(opLoc)
157b6c58ec4SAlex Zinenko            << "'" << sizesAttrName << "' attribute must be an array";
158b6c58ec4SAlex Zinenko   Type pdlOpType = parser.getBuilder().getType<pdl::OperationType>();
159b6c58ec4SAlex Zinenko   size_t numExpectedLoops =
160b6c58ec4SAlex Zinenko       sizesArrayAttr.size() - llvm::count(extractI64Array(sizesArrayAttr), 0);
161b6c58ec4SAlex Zinenko   result.addTypes(SmallVector<Type>(numExpectedLoops + 1, pdlOpType));
162b6c58ec4SAlex Zinenko   if (parser.resolveOperand(targetOperand, pdlOpType, result.operands))
163b6c58ec4SAlex Zinenko     return failure();
164b6c58ec4SAlex Zinenko   return success();
165b6c58ec4SAlex Zinenko }
166b6c58ec4SAlex Zinenko 
167*1d45282aSAlex Zinenko DiagnosedSilenceableFailure
168b6c58ec4SAlex Zinenko transform::FuseOp::apply(mlir::transform::TransformResults &transformResults,
169b6c58ec4SAlex Zinenko                          mlir::transform::TransformState &state) {
170b6c58ec4SAlex Zinenko   LinalgTilingAndFusionOptions fusionOptions;
171b6c58ec4SAlex Zinenko   fusionOptions.tileSizes = extractI64Array(getTileSizes());
172b6c58ec4SAlex Zinenko   fusionOptions.tileInterchange = extractI64Array(getTileInterchange());
173b6c58ec4SAlex Zinenko 
174e3890b7fSAlex Zinenko   LogicalResult result = applyTilingToAll(
175b6c58ec4SAlex Zinenko       getOperation(), getTarget(), fusionOptions.tileSizes, transformResults,
176b6c58ec4SAlex Zinenko       state, [&](LinalgOp linalgOp) -> FailureOr<TiledLinalgOp> {
177b6c58ec4SAlex Zinenko         LinalgTileAndFuseTensorOpsPattern pattern(getContext(), fusionOptions);
178b6c58ec4SAlex Zinenko         SimpleRewriter rewriter(getContext());
179b6c58ec4SAlex Zinenko         rewriter.setInsertionPoint(linalgOp);
180b6c58ec4SAlex Zinenko         FailureOr<TileLoopNest> tileLoopNest =
181b6c58ec4SAlex Zinenko             pattern.returningMatchAndRewrite(linalgOp, rewriter);
182b6c58ec4SAlex Zinenko         if (failed(tileLoopNest))
183b6c58ec4SAlex Zinenko           return failure();
184b6c58ec4SAlex Zinenko 
185b6c58ec4SAlex Zinenko         TiledLinalgOp tiledLinalgOp;
186b6c58ec4SAlex Zinenko         tiledLinalgOp.op = tileLoopNest->getRootOp();
187b6c58ec4SAlex Zinenko         tiledLinalgOp.loops = {tileLoopNest->getLoopOps().begin(),
188b6c58ec4SAlex Zinenko                                tileLoopNest->getLoopOps().end()};
189b6c58ec4SAlex Zinenko         return tiledLinalgOp;
190b6c58ec4SAlex Zinenko       });
191*1d45282aSAlex Zinenko   return failed(result) ? DiagnosedSilenceableFailure::definiteFailure()
192*1d45282aSAlex Zinenko                         : DiagnosedSilenceableFailure::success();
193b6c58ec4SAlex Zinenko }
194b6c58ec4SAlex Zinenko 
195b6c58ec4SAlex Zinenko ParseResult transform::FuseOp::parse(OpAsmParser &parser,
196b6c58ec4SAlex Zinenko                                      OperationState &result) {
197b6c58ec4SAlex Zinenko   return parseTileLikeOp(
198b6c58ec4SAlex Zinenko       parser, result,
199b6c58ec4SAlex Zinenko       transform::FuseOp::getTileSizesAttrName(result.name).getValue());
200b6c58ec4SAlex Zinenko }
201b6c58ec4SAlex Zinenko 
202b6c58ec4SAlex Zinenko void transform::FuseOp::print(OpAsmPrinter &p) {
203b6c58ec4SAlex Zinenko   p << ' ';
204b6c58ec4SAlex Zinenko   p << getTarget();
205b6c58ec4SAlex Zinenko   p.printOptionalAttrDict((*this)->getAttrs());
206b6c58ec4SAlex Zinenko }
207b6c58ec4SAlex Zinenko 
208b6c58ec4SAlex Zinenko LogicalResult transform::FuseOp::verify() {
209b6c58ec4SAlex Zinenko   SmallVector<int64_t> permutation = extractI64Array(getTileInterchange());
210b6c58ec4SAlex Zinenko   auto sequence = llvm::to_vector(llvm::seq<int64_t>(0, permutation.size()));
211b6c58ec4SAlex Zinenko   if (!std::is_permutation(sequence.begin(), sequence.end(),
212b6c58ec4SAlex Zinenko                            permutation.begin(), permutation.end())) {
213b6c58ec4SAlex Zinenko     return emitOpError() << "expects interchange to be a permutation, found "
214b6c58ec4SAlex Zinenko                          << getTileInterchange();
215b6c58ec4SAlex Zinenko   }
216b6c58ec4SAlex Zinenko   return success();
217b6c58ec4SAlex Zinenko }
218b6c58ec4SAlex Zinenko 
219b6c58ec4SAlex Zinenko //===----------------------------------------------------------------------===//
220ce2e198bSAlex Zinenko // GeneralizeOp
221ce2e198bSAlex Zinenko //===----------------------------------------------------------------------===//
222ce2e198bSAlex Zinenko 
223ce2e198bSAlex Zinenko FailureOr<LinalgOp> transform::GeneralizeOp::applyToOne(LinalgOp target) {
224ce2e198bSAlex Zinenko   // Exit early if no transformation is needed.
225ce2e198bSAlex Zinenko   if (isa<GenericOp>(target))
226ce2e198bSAlex Zinenko     return target;
227ce2e198bSAlex Zinenko 
228ce2e198bSAlex Zinenko   FailureOr<LinalgOp> generic = tryApply<LinalgGeneralizationPattern>(target);
229ce2e198bSAlex Zinenko   if (succeeded(generic))
230ce2e198bSAlex Zinenko     return generic;
231ce2e198bSAlex Zinenko 
2325f0d4f20SAlex Zinenko   return reportUnknownTransformError(target);
233ce2e198bSAlex Zinenko }
234ce2e198bSAlex Zinenko 
2353c2a74a3SMatthias Springer //===----------------------------------------------------------------------===//
2365cde5a57SAlex Zinenko // InterchangeOp
2375cde5a57SAlex Zinenko //===----------------------------------------------------------------------===//
2385cde5a57SAlex Zinenko 
2395cde5a57SAlex Zinenko FailureOr<LinalgOp> transform::InterchangeOp::applyToOne(LinalgOp target) {
2405cde5a57SAlex Zinenko   SmallVector<unsigned> interchangeVector =
2415cde5a57SAlex Zinenko       extractUIntArray(getIteratorInterchange());
2425cde5a57SAlex Zinenko   // Exit early if no transformation is needed.
2435cde5a57SAlex Zinenko   if (interchangeVector.empty())
2445cde5a57SAlex Zinenko     return target;
2455cde5a57SAlex Zinenko 
2465cde5a57SAlex Zinenko   auto genericTarget = dyn_cast<GenericOp>(target.getOperation());
2475cde5a57SAlex Zinenko   if (!genericTarget) {
2485cde5a57SAlex Zinenko     InFlightDiagnostic diag = emitOpError()
2495cde5a57SAlex Zinenko                               << "applies to " << GenericOp::getOperationName()
2505cde5a57SAlex Zinenko                               << " ops";
2515cde5a57SAlex Zinenko     diag.attachNote(target.getLoc()) << "attempted to apply to this op";
2525cde5a57SAlex Zinenko     return diag;
2535cde5a57SAlex Zinenko   }
2545cde5a57SAlex Zinenko 
255ce2e198bSAlex Zinenko   return tryApply<GenericOpInterchangePattern>(target, interchangeVector);
2565cde5a57SAlex Zinenko }
2575cde5a57SAlex Zinenko 
2585cde5a57SAlex Zinenko LogicalResult transform::InterchangeOp::verify() {
2595cde5a57SAlex Zinenko   SmallVector<unsigned> permutation =
2605cde5a57SAlex Zinenko       extractUIntArray(getIteratorInterchange());
2615cde5a57SAlex Zinenko   auto sequence = llvm::to_vector(llvm::seq<unsigned>(0, permutation.size()));
2625cde5a57SAlex Zinenko   if (!std::is_permutation(sequence.begin(), sequence.end(),
2635cde5a57SAlex Zinenko                            permutation.begin(), permutation.end())) {
2645cde5a57SAlex Zinenko     return emitOpError()
2655cde5a57SAlex Zinenko            << "expects iterator_interchange to be a permutation, found "
2665cde5a57SAlex Zinenko            << getIteratorInterchange();
2675cde5a57SAlex Zinenko   }
2685cde5a57SAlex Zinenko   return success();
2695cde5a57SAlex Zinenko }
2705cde5a57SAlex Zinenko 
2715cde5a57SAlex Zinenko //===---------------------------------------------------------------------===//
2725cde5a57SAlex Zinenko // PadOp
2735cde5a57SAlex Zinenko //===---------------------------------------------------------------------===//
2745cde5a57SAlex Zinenko 
2755cde5a57SAlex Zinenko FailureOr<LinalgOp> transform::PadOp::applyToOne(LinalgOp target) {
2765cde5a57SAlex Zinenko   // Convert the integer packing flags to booleans.
2775cde5a57SAlex Zinenko   SmallVector<bool> packPaddings;
2785cde5a57SAlex Zinenko   for (int64_t packPadding : extractI64Array(getPackPaddings()))
2795cde5a57SAlex Zinenko     packPaddings.push_back(static_cast<bool>(packPadding));
2805cde5a57SAlex Zinenko 
2815cde5a57SAlex Zinenko   // Convert the padding values to attributes.
2825cde5a57SAlex Zinenko   SmallVector<Attribute> paddingValues;
2835cde5a57SAlex Zinenko   for (auto const &it :
2845cde5a57SAlex Zinenko        llvm::zip(getPaddingValues(), target->getOperandTypes())) {
2855cde5a57SAlex Zinenko     Attribute attr = std::get<0>(it);
2865cde5a57SAlex Zinenko     Type elementType = getElementTypeOrSelf(std::get<1>(it));
2875cde5a57SAlex Zinenko     // Try to parse string attributes to obtain an attribute of element type.
2885cde5a57SAlex Zinenko     if (auto stringAttr = attr.dyn_cast<StringAttr>()) {
2895cde5a57SAlex Zinenko       paddingValues.push_back(
2905cde5a57SAlex Zinenko           parseAttribute(attr.cast<StringAttr>(), elementType));
2915cde5a57SAlex Zinenko       if (!paddingValues.back()) {
2925cde5a57SAlex Zinenko         InFlightDiagnostic diag = emitOpError()
2935cde5a57SAlex Zinenko                                   << "expects a padding value that parses to "
2945cde5a57SAlex Zinenko                                   << elementType << ", got " << std::get<0>(it);
2955cde5a57SAlex Zinenko         diag.attachNote(target.getLoc()) << "when applied to this op";
2965cde5a57SAlex Zinenko         return diag;
2975cde5a57SAlex Zinenko       }
2985cde5a57SAlex Zinenko       continue;
2995cde5a57SAlex Zinenko     }
3005cde5a57SAlex Zinenko     // Otherwise, add the attribute directly.
3015cde5a57SAlex Zinenko     if (attr.getType() != elementType) {
3025cde5a57SAlex Zinenko       InFlightDiagnostic diag = emitOpError()
3035cde5a57SAlex Zinenko                                 << "expects a padding value of type "
3045cde5a57SAlex Zinenko                                 << elementType << ", got " << attr;
3055cde5a57SAlex Zinenko       diag.attachNote(target.getLoc()) << "when applied to this op";
3065cde5a57SAlex Zinenko       return diag;
3075cde5a57SAlex Zinenko     }
3085cde5a57SAlex Zinenko     paddingValues.push_back(attr);
3095cde5a57SAlex Zinenko   }
3105cde5a57SAlex Zinenko 
3115cde5a57SAlex Zinenko   // Extract the transpose vectors.
3125cde5a57SAlex Zinenko   SmallVector<SmallVector<int64_t>> transposePaddings;
3135cde5a57SAlex Zinenko   for (Attribute transposeVector : getTransposePaddings().cast<ArrayAttr>())
3145cde5a57SAlex Zinenko     transposePaddings.push_back(
3155cde5a57SAlex Zinenko         extractI64Array(transposeVector.cast<ArrayAttr>()));
3165cde5a57SAlex Zinenko 
3175cde5a57SAlex Zinenko   LinalgPaddingOptions paddingOptions;
3185cde5a57SAlex Zinenko   paddingOptions.setPaddingValues(paddingValues);
3195cde5a57SAlex Zinenko   paddingOptions.setPaddingDimensions(extractI64Array(getPaddingDimensions()));
3205cde5a57SAlex Zinenko   paddingOptions.setPackPaddings(packPaddings);
3215cde5a57SAlex Zinenko   paddingOptions.setHoistPaddings(extractI64Array(getHoistPaddings()));
3225cde5a57SAlex Zinenko   paddingOptions.setTransposePaddings(transposePaddings);
3235cde5a57SAlex Zinenko 
324ce2e198bSAlex Zinenko   FailureOr<LinalgOp> result =
325ce2e198bSAlex Zinenko       tryApply<LinalgPaddingPattern>(target, paddingOptions);
326ce2e198bSAlex Zinenko   if (succeeded(result))
327ce2e198bSAlex Zinenko     return result;
328ce2e198bSAlex Zinenko 
3295cde5a57SAlex Zinenko   InFlightDiagnostic diag = emitError()
3305cde5a57SAlex Zinenko                             << "failed to apply pattern to target op";
3315cde5a57SAlex Zinenko   diag.attachNote(target.getLoc()) << "target op";
3325cde5a57SAlex Zinenko   return diag;
3335cde5a57SAlex Zinenko }
3345cde5a57SAlex Zinenko 
3355cde5a57SAlex Zinenko LogicalResult transform::PadOp::verify() {
3365cde5a57SAlex Zinenko   SmallVector<int64_t> packPaddings = extractI64Array(getPackPaddings());
3375cde5a57SAlex Zinenko   if (any_of(packPaddings, [](int64_t packPadding) {
3385cde5a57SAlex Zinenko         return packPadding != 0 && packPadding != 1;
3395cde5a57SAlex Zinenko       })) {
3405cde5a57SAlex Zinenko     return emitOpError()
3415cde5a57SAlex Zinenko            << "expects pack_paddings to contain booleans (0/1), found "
3425cde5a57SAlex Zinenko            << getPackPaddings();
3435cde5a57SAlex Zinenko   }
3445cde5a57SAlex Zinenko 
3455cde5a57SAlex Zinenko   SmallVector<int64_t> paddingDimensions =
3465cde5a57SAlex Zinenko       extractI64Array(getPaddingDimensions());
3475cde5a57SAlex Zinenko   if (any_of(paddingDimensions,
3485cde5a57SAlex Zinenko              [](int64_t paddingDimension) { return paddingDimension < 0; })) {
3495cde5a57SAlex Zinenko     return emitOpError()
3505cde5a57SAlex Zinenko            << "expects padding_dimensions to contain positive integers, found "
3515cde5a57SAlex Zinenko            << getPaddingDimensions();
3525cde5a57SAlex Zinenko   }
3535cde5a57SAlex Zinenko 
3545cde5a57SAlex Zinenko   SmallVector<int64_t> hoistPaddings = extractI64Array(getHoistPaddings());
3555cde5a57SAlex Zinenko   if (any_of(hoistPaddings,
3565cde5a57SAlex Zinenko              [](int64_t hoistPadding) { return hoistPadding < 0; })) {
3575cde5a57SAlex Zinenko     return emitOpError()
3585cde5a57SAlex Zinenko            << "expects hoist_paddings to contain positive integers, found "
3595cde5a57SAlex Zinenko            << getHoistPaddings();
3605cde5a57SAlex Zinenko   }
3615cde5a57SAlex Zinenko 
3625cde5a57SAlex Zinenko   ArrayAttr transposes = getTransposePaddings();
3635cde5a57SAlex Zinenko   for (Attribute attr : transposes) {
3645cde5a57SAlex Zinenko     SmallVector<int64_t> transpose = extractFromI64ArrayAttr(attr);
3655cde5a57SAlex Zinenko     auto sequence = llvm::to_vector(llvm::seq<int64_t>(0, transpose.size()));
3665cde5a57SAlex Zinenko     if (!std::is_permutation(sequence.begin(), sequence.end(),
3675cde5a57SAlex Zinenko                              transpose.begin(), transpose.end())) {
3685cde5a57SAlex Zinenko       return emitOpError()
3695cde5a57SAlex Zinenko              << "expects transpose_paddings to be a permutation, found "
3705cde5a57SAlex Zinenko              << attr;
3715cde5a57SAlex Zinenko     }
3725cde5a57SAlex Zinenko   }
3735cde5a57SAlex Zinenko   return success();
3745cde5a57SAlex Zinenko }
3755cde5a57SAlex Zinenko 
3765cde5a57SAlex Zinenko //===----------------------------------------------------------------------===//
3775cde5a57SAlex Zinenko // ScalarizeOp
3785cde5a57SAlex Zinenko //===----------------------------------------------------------------------===//
3795cde5a57SAlex Zinenko 
3805cde5a57SAlex Zinenko FailureOr<LinalgOp> transform::ScalarizeOp::applyToOne(LinalgOp target) {
3815cde5a57SAlex Zinenko   LinalgTilingOptions tilingOptions;
3825cde5a57SAlex Zinenko   tilingOptions.scalarizeDynamicDims();
3835cde5a57SAlex Zinenko   // Tiling with "scalarize_dyn_dims" actually sets the same lambda as the tile
3845cde5a57SAlex Zinenko   // sizes and asserts that it is not already set.
3855cde5a57SAlex Zinenko   SmallVector<int64_t> emptyTileSizes;
3865cde5a57SAlex Zinenko   LinalgTilingPattern pattern(getContext(), tilingOptions);
3875cde5a57SAlex Zinenko   SimpleRewriter rewriter(getContext());
3885cde5a57SAlex Zinenko   rewriter.setInsertionPoint(target);
3895cde5a57SAlex Zinenko   FailureOr<TiledLinalgOp> result =
3905cde5a57SAlex Zinenko       pattern.returningMatchAndRewrite(target, rewriter);
3915cde5a57SAlex Zinenko   if (failed(result))
3925cde5a57SAlex Zinenko     return failure();
3935cde5a57SAlex Zinenko 
3945cde5a57SAlex Zinenko   return result->op;
3955cde5a57SAlex Zinenko }
3965cde5a57SAlex Zinenko 
3975cde5a57SAlex Zinenko //===----------------------------------------------------------------------===//
3983c2a74a3SMatthias Springer // TileOp
3993c2a74a3SMatthias Springer //===----------------------------------------------------------------------===//
4003c2a74a3SMatthias Springer 
401*1d45282aSAlex Zinenko DiagnosedSilenceableFailure
402e3890b7fSAlex Zinenko transform::TileOp::apply(TransformResults &transformResults,
4033c2a74a3SMatthias Springer                          TransformState &state) {
4043c2a74a3SMatthias Springer   LinalgTilingOptions tilingOptions;
4053c2a74a3SMatthias Springer   SmallVector<int64_t> tileSizes = extractI64Array(getSizes());
4063c2a74a3SMatthias Springer 
4073c2a74a3SMatthias Springer   if (!tileSizes.empty())
4083c2a74a3SMatthias Springer     tilingOptions.setTileSizes(tileSizes);
4093c2a74a3SMatthias Springer   tilingOptions.setInterchange(extractUIntArray(getInterchange()));
4103c2a74a3SMatthias Springer   LinalgTilingPattern pattern(getContext(), tilingOptions);
4113c2a74a3SMatthias Springer 
412e3890b7fSAlex Zinenko   LogicalResult result = applyTilingToAll(
413e3890b7fSAlex Zinenko       getOperation(), getTarget(), tileSizes, transformResults, state,
414e3890b7fSAlex Zinenko       [&](LinalgOp linalgOp) {
4153c2a74a3SMatthias Springer         SimpleRewriter rewriter(linalgOp.getContext());
416e3890b7fSAlex Zinenko         return pattern.returningMatchAndRewrite(linalgOp, rewriter);
4173c2a74a3SMatthias Springer       });
418*1d45282aSAlex Zinenko   return DiagnosedSilenceableFailure(result);
4193c2a74a3SMatthias Springer }
4203c2a74a3SMatthias Springer 
4213c2a74a3SMatthias Springer ParseResult transform::TileOp::parse(OpAsmParser &parser,
4223c2a74a3SMatthias Springer                                      OperationState &result) {
423b6c58ec4SAlex Zinenko   return parseTileLikeOp(parser, result,
424b6c58ec4SAlex Zinenko                          TileOp::getSizesAttrName(result.name).getValue());
4253c2a74a3SMatthias Springer }
4263c2a74a3SMatthias Springer 
4273c2a74a3SMatthias Springer void TileOp::print(OpAsmPrinter &p) {
4283c2a74a3SMatthias Springer   p << ' ';
4293c2a74a3SMatthias Springer   p << getTarget();
4303c2a74a3SMatthias Springer   p.printOptionalAttrDict((*this)->getAttrs());
4313c2a74a3SMatthias Springer }
4323c2a74a3SMatthias Springer 
4333c2a74a3SMatthias Springer //===----------------------------------------------------------------------===//
434cc6c1592SAlex Zinenko // VectorizeOp
435cc6c1592SAlex Zinenko //===----------------------------------------------------------------------===//
436cc6c1592SAlex Zinenko 
437cc6c1592SAlex Zinenko FailureOr<Operation *> VectorizeOp::applyToOne(Operation *target) {
438cc6c1592SAlex Zinenko   if (!target->hasTrait<OpTrait::IsIsolatedFromAbove>()) {
439cc6c1592SAlex Zinenko     InFlightDiagnostic diag = emitOpError()
440cc6c1592SAlex Zinenko                               << "applies only to isolated-from-above targets";
441cc6c1592SAlex Zinenko     diag.attachNote(target->getLoc()) << "non-isolated target";
442cc6c1592SAlex Zinenko     return diag;
443cc6c1592SAlex Zinenko   }
444cc6c1592SAlex Zinenko 
445cc6c1592SAlex Zinenko   MLIRContext *ctx = getContext();
446cc6c1592SAlex Zinenko   RewritePatternSet patterns(ctx);
447cc6c1592SAlex Zinenko   patterns.add<LinalgVectorizationPattern>(ctx);
448cc6c1592SAlex Zinenko 
449cc6c1592SAlex Zinenko   vector::populateVectorTransferPermutationMapLoweringPatterns(patterns);
450cc6c1592SAlex Zinenko   vector::populateVectorReductionToContractPatterns(patterns);
451cc6c1592SAlex Zinenko   patterns.add<linalg::LinalgCopyVTRForwardingPattern,
452cc6c1592SAlex Zinenko                linalg::LinalgCopyVTWForwardingPattern>(ctx,
453cc6c1592SAlex Zinenko                                                        /*benefit=*/2);
454cc6c1592SAlex Zinenko   vector::TransferReadOp::getCanonicalizationPatterns(patterns, ctx);
455cc6c1592SAlex Zinenko   vector::TransferWriteOp::getCanonicalizationPatterns(patterns, ctx);
456cc6c1592SAlex Zinenko   if (getVectorizePadding())
457cc6c1592SAlex Zinenko     linalg::populatePadOpVectorizationPatterns(patterns);
458cc6c1592SAlex Zinenko 
4595f0d4f20SAlex Zinenko   if (failed(applyPatternsAndFoldGreedily(target, std::move(patterns))))
4605f0d4f20SAlex Zinenko     return reportUnknownTransformError(target);
461cc6c1592SAlex Zinenko   return target;
462cc6c1592SAlex Zinenko }
463cc6c1592SAlex Zinenko 
464cc6c1592SAlex Zinenko //===----------------------------------------------------------------------===//
4653c2a74a3SMatthias Springer // Transform op registration
4663c2a74a3SMatthias Springer //===----------------------------------------------------------------------===//
4673c2a74a3SMatthias Springer 
4683c2a74a3SMatthias Springer namespace {
4693c2a74a3SMatthias Springer /// Registers new ops and declares PDL as dependent dialect since the additional
4703c2a74a3SMatthias Springer /// ops are using PDL types for operands and results.
4713c2a74a3SMatthias Springer class LinalgTransformDialectExtension
4723c2a74a3SMatthias Springer     : public transform::TransformDialectExtension<
4733c2a74a3SMatthias Springer           LinalgTransformDialectExtension> {
4743c2a74a3SMatthias Springer public:
4753c2a74a3SMatthias Springer   LinalgTransformDialectExtension() {
4763c2a74a3SMatthias Springer     declareDependentDialect<pdl::PDLDialect>();
4773c2a74a3SMatthias Springer     declareDependentDialect<scf::SCFDialect>();
478cc6c1592SAlex Zinenko     declareDependentDialect<vector::VectorDialect>();
4793c2a74a3SMatthias Springer     registerTransformOps<
4803c2a74a3SMatthias Springer #define GET_OP_LIST
4813c2a74a3SMatthias Springer #include "mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp.inc"
4823c2a74a3SMatthias Springer         >();
4833c2a74a3SMatthias Springer   }
4843c2a74a3SMatthias Springer };
4853c2a74a3SMatthias Springer } // namespace
4863c2a74a3SMatthias Springer 
4873c2a74a3SMatthias Springer #define GET_OP_CLASSES
4883c2a74a3SMatthias Springer #include "mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.cpp.inc"
4893c2a74a3SMatthias Springer 
4903c2a74a3SMatthias Springer void mlir::linalg::registerTransformDialectExtension(
4913c2a74a3SMatthias Springer     DialectRegistry &registry) {
4923c2a74a3SMatthias Springer   registry.addExtensions<LinalgTransformDialectExtension>();
4933c2a74a3SMatthias Springer }
494