MLIR  22.0.0git
LowerVectorFromElements.cpp
Go to the documentation of this file.
1 //===- LowerVectorFromElements.cpp - Lower 'vector.from_elements' op -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements target-independent rewrites and utilities to lower the
10 // 'vector.from_elements' operation.
11 //
12 //===----------------------------------------------------------------------===//
13 
16 
17 #define DEBUG_TYPE "lower-vector-from-elements"
18 
19 using namespace mlir;
20 
21 namespace {
22 
23 /// Unrolls 2 or more dimensional `vector.from_elements` ops by unrolling the
24 /// outermost dimension. For example:
25 /// ```
26 /// %v = vector.from_elements %e0, %e1, %e2, %e3, %e4, %e5 : vector<2x3xf32>
27 ///
28 /// ==>
29 ///
30 /// %0 = ub.poison : vector<2x3xf32>
31 /// %v0 = vector.from_elements %e0, %e1, %e2 : vector<3xf32>
32 /// %1 = vector.insert %v0, %0 [0] : vector<3xf32> into vector<2x3xf32>
33 /// %v1 = vector.from_elements %e3, %e4, %e5 : vector<3xf32>
34 /// %v = vector.insert %v1, %1 [1] : vector<3xf32> into vector<2x3xf32>
35 /// ```
36 ///
37 /// When applied exhaustively, this will produce a sequence of 1-d from_elements
38 /// ops.
39 struct UnrollFromElements : OpRewritePattern<vector::FromElementsOp> {
41 
42  LogicalResult matchAndRewrite(vector::FromElementsOp op,
43  PatternRewriter &rewriter) const override {
44  ValueRange allElements = op.getElements();
45 
46  auto unrollFromElementsFn = [&](PatternRewriter &rewriter, Location loc,
47  VectorType subTy, int64_t index) {
48  size_t subTyNumElements = subTy.getNumElements();
49  assert((index + 1) * subTyNumElements <= allElements.size() &&
50  "out of bounds");
51  ValueRange subElements =
52  allElements.slice(index * subTyNumElements, subTyNumElements);
53  return vector::FromElementsOp::create(rewriter, loc, subTy, subElements);
54  };
55 
56  return unrollVectorOp(op, rewriter, unrollFromElementsFn);
57  }
58 };
59 
60 } // namespace
61 
64  patterns.add<UnrollFromElements>(patterns.getContext(), benefit);
65 }
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
Definition: PatternMatch.h:34
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
Definition: PatternMatch.h:783
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:387
void populateVectorFromElementsLoweringPatterns(RewritePatternSet &patterns, PatternBenefit benefit=1)
Populate the pattern set with the following patterns:
LogicalResult unrollVectorOp(Operation *op, PatternRewriter &rewriter, UnrollVectorOpFn unrollFn)
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...
Definition: PatternMatch.h:314
OpRewritePattern(MLIRContext *context, PatternBenefit benefit=1, ArrayRef< StringRef > generatedNames={})
Patterns must specify the root operation name they match against, and can also specify the benefit of...
Definition: PatternMatch.h:319