-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[mlir] Move vector.{to_elements,from_elements} unrolling to VectorUnroll.cpp
#159118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
08655a9
c891a27
e1f9605
5c3e7d5
04431c5
70a667e
c054f16
28ca33b
5a266fb
f0283ca
5d7afb1
44145b5
d3af4ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -372,7 +372,7 @@ struct LowerGpuOpsToNVVMOpsPass final | |
populateGpuRewritePatterns(patterns); | ||
// Transform N-D vector.from_elements to 1-D vector.from_elements before | ||
// conversion. | ||
vector::populateVectorFromElementsLoweringPatterns(patterns); | ||
vector::populateVectorFromElementsUnrollPatterns(patterns); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to add just the unroll patterns for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kept these since we have transform dialect operations that call these functions. I think I can follow up this PR and remove them, but I would rather keep this PR simple. |
||
if (failed(applyPatternsGreedily(m, std::move(patterns)))) | ||
return signalPassFailure(); | ||
} | ||
|
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||||||
|
||||||||||
#include "mlir/Dialect/Affine/IR/AffineOps.h" | ||||||||||
#include "mlir/Dialect/Utils/IndexingUtils.h" | ||||||||||
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h" | ||||||||||
#include "mlir/Dialect/Vector/Transforms/VectorTransforms.h" | ||||||||||
#include "mlir/Interfaces/VectorInterfaces.h" | ||||||||||
#include "llvm/ADT/MapVector.h" | ||||||||||
|
@@ -809,6 +810,82 @@ struct UnrollBroadcastPattern : public OpRewritePattern<vector::BroadcastOp> { | |||||||||
vector::UnrollVectorOptions options; | ||||||||||
}; | ||||||||||
|
||||||||||
struct UnrollToElements final : public OpRewritePattern<vector::ToElementsOp> { | ||||||||||
amd-eochoalo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
UnrollToElements(MLIRContext *context, | ||||||||||
const vector::UnrollVectorOptions &options, | ||||||||||
PatternBenefit benefit = 1) | ||||||||||
: OpRewritePattern<vector::ToElementsOp>(context, benefit), | ||||||||||
options(options) {} | ||||||||||
|
||||||||||
LogicalResult matchAndRewrite(vector::ToElementsOp op, | ||||||||||
PatternRewriter &rewriter) const override { | ||||||||||
|
||||||||||
TypedValue<VectorType> source = op.getSource(); | ||||||||||
FailureOr<SmallVector<Value>> result = | ||||||||||
vector::unrollVectorValue(source, rewriter); | ||||||||||
if (failed(result)) { | ||||||||||
return failure(); | ||||||||||
} | ||||||||||
SmallVector<Value> vectors = *result; | ||||||||||
|
||||||||||
SmallVector<Value> results; | ||||||||||
for (const Value &vector : vectors) { | ||||||||||
amd-eochoalo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
auto subElements = | ||||||||||
vector::ToElementsOp::create(rewriter, op.getLoc(), vector); | ||||||||||
llvm::append_range(results, subElements.getResults()); | ||||||||||
} | ||||||||||
rewriter.replaceOp(op, results); | ||||||||||
return success(); | ||||||||||
} | ||||||||||
|
||||||||||
private: | ||||||||||
vector::UnrollVectorOptions options; | ||||||||||
}; | ||||||||||
|
||||||||||
/// Unrolls 2 or more dimensional `vector.from_elements` ops by unrolling the | ||||||||||
/// outermost dimension. For example: | ||||||||||
/// ``` | ||||||||||
/// %v = vector.from_elements %e0, %e1, %e2, %e3, %e4, %e5 : vector<2x3xf32> | ||||||||||
/// | ||||||||||
/// ==> | ||||||||||
/// | ||||||||||
/// %0 = ub.poison : vector<2x3xf32> | ||||||||||
/// %v0 = vector.from_elements %e0, %e1, %e2 : vector<3xf32> | ||||||||||
/// %1 = vector.insert %v0, %0 [0] : vector<3xf32> into vector<2x3xf32> | ||||||||||
/// %v1 = vector.from_elements %e3, %e4, %e5 : vector<3xf32> | ||||||||||
/// %v = vector.insert %v1, %1 [1] : vector<3xf32> into vector<2x3xf32> | ||||||||||
/// ``` | ||||||||||
/// | ||||||||||
/// When applied exhaustively, this will produce a sequence of 1-d from_elements | ||||||||||
/// ops. | ||||||||||
amd-eochoalo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
struct UnrollFromElements : OpRewritePattern<vector::FromElementsOp> { | ||||||||||
UnrollFromElements(MLIRContext *context, | ||||||||||
const vector::UnrollVectorOptions &options, | ||||||||||
PatternBenefit benefit = 1) | ||||||||||
: OpRewritePattern<vector::FromElementsOp>(context, benefit), | ||||||||||
options(options) {} | ||||||||||
|
||||||||||
LogicalResult matchAndRewrite(vector::FromElementsOp op, | ||||||||||
PatternRewriter &rewriter) const override { | ||||||||||
ValueRange allElements = op.getElements(); | ||||||||||
|
||||||||||
auto unrollFromElementsFn = [&](PatternRewriter &rewriter, Location loc, | ||||||||||
VectorType subTy, int64_t index) { | ||||||||||
size_t subTyNumElements = subTy.getNumElements(); | ||||||||||
assert((index + 1) * subTyNumElements <= allElements.size() && | ||||||||||
"out of bounds"); | ||||||||||
ValueRange subElements = | ||||||||||
allElements.slice(index * subTyNumElements, subTyNumElements); | ||||||||||
return vector::FromElementsOp::create(rewriter, loc, subTy, subElements); | ||||||||||
}; | ||||||||||
|
||||||||||
return unrollVectorOp(op, rewriter, unrollFromElementsFn); | ||||||||||
} | ||||||||||
|
||||||||||
private: | ||||||||||
vector::UnrollVectorOptions options; | ||||||||||
}; | ||||||||||
|
||||||||||
} // namespace | ||||||||||
|
||||||||||
void mlir::vector::populateVectorUnrollPatterns( | ||||||||||
|
@@ -818,6 +895,18 @@ void mlir::vector::populateVectorUnrollPatterns( | |||||||||
UnrollContractionPattern, UnrollElementwisePattern, | ||||||||||
UnrollReductionPattern, UnrollMultiReductionPattern, | ||||||||||
UnrollTransposePattern, UnrollGatherPattern, UnrollLoadPattern, | ||||||||||
UnrollStorePattern, UnrollBroadcastPattern>( | ||||||||||
patterns.getContext(), options, benefit); | ||||||||||
UnrollStorePattern, UnrollBroadcastPattern, UnrollFromElements, | ||||||||||
UnrollToElements>(patterns.getContext(), options, benefit); | ||||||||||
|
UnrollStorePattern, UnrollBroadcastPattern, UnrollFromElements, | |
UnrollToElements>(patterns.getContext(), options, benefit); | |
UnrollStorePattern, UnrollBroadcastPattern>( | |
patterns.getContext(), options, benefit); |
If it is more convenient I can make this a non-functional change first by removing these patterns and removing the test. Then I can make another PR which adds the patterns.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,3 +96,47 @@ func.func @transpose(%arg0 : vector<2x3xi32>) -> (vector<3x2xi32>) { | |
%0 = vector.transpose %arg0, [1, 0] : vector<2x3xi32> to vector<3x2xi32> | ||
return %0 : vector<3x2xi32> | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like these are the only tests that check for unrolling, although it does not check the conversion from vector to spirv, it just checks that the unrolling was applied. We could also start a new file that runs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we need specific vector unrolling test for SPIR-V but we should add independent test to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm tasked with making sure that |
||
// ----- | ||
|
||
// In order to verify that the pattern is applied, | ||
// we need to make sure that the the 2d vector does not | ||
// come from the parameters. Otherwise, the pattern | ||
// in unrollVectorsInSignatures which splits the 2d vector | ||
// parameter will take precedent. Similarly, let's avoid | ||
// returning a vector as another pattern would take precendence. | ||
|
||
// CHECK-LABEL: @unroll_to_elements_2d | ||
func.func @unroll_to_elements_2d() -> (f32, f32, f32, f32) { | ||
%1 = "test.op"() : () -> (vector<2x2xf32>) | ||
// CHECK: %[[VEC2D:.+]] = "test.op" | ||
// CHECK: %[[VEC0:.+]] = vector.extract %[[VEC2D]][0] : vector<2xf32> from vector<2x2xf32> | ||
// CHECK: %[[VEC1:.+]] = vector.extract %[[VEC2D]][1] : vector<2xf32> from vector<2x2xf32> | ||
// CHECK: %[[RES0:.+]]:2 = vector.to_elements %[[VEC0]] | ||
// CHECK: %[[RES1:.+]]:2 = vector.to_elements %[[VEC1]] | ||
%2:4 = vector.to_elements %1 : vector<2x2xf32> | ||
return %2#0, %2#1, %2#2, %2#3 : f32, f32, f32, f32 | ||
} | ||
|
||
// ----- | ||
|
||
// In order to verify that the pattern is applied, | ||
// we need to make sure that the the 2d vector is used | ||
// by an operation and that extracts are not folded away. | ||
// In other words we can't use "test.op" nor return the | ||
// value `%0 = vector.from_elements` | ||
|
||
// CHECK-LABEL: @unroll_from_elements_2d | ||
// CHECK-SAME: (%[[ARG0:.+]]: f32, %[[ARG1:.+]]: f32, %[[ARG2:.+]]: f32, %[[ARG3:.+]]: f32) | ||
func.func @unroll_from_elements_2d(%arg0: f32, %arg1: f32, %arg2: f32, %arg3: f32) -> (vector<2x2xf32>) { | ||
// CHECK: %[[VEC0:.+]] = vector.from_elements %[[ARG0]], %[[ARG1]] : vector<2xf32> | ||
// CHECK: %[[VEC1:.+]] = vector.from_elements %[[ARG2]], %[[ARG3]] : vector<2xf32> | ||
%0 = vector.from_elements %arg0, %arg1, %arg2, %arg3 : vector<2x2xf32> | ||
|
||
// CHECK: %[[RES0:.+]] = arith.addf %[[VEC0]], %[[VEC0]] | ||
// CHECK: %[[RES1:.+]] = arith.addf %[[VEC1]], %[[VEC1]] | ||
%1 = arith.addf %0, %0 : vector<2x2xf32> | ||
|
||
// return %[[RES0]], %%[[RES1]] : vector<2xf32>, vector<2xf32> | ||
return %1 : vector<2x2xf32> | ||
} |
Uh oh!
There was an error while loading. Please reload this page.