-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][ArmNeon] Implements LowerVectorToArmNeon Pattern for SMMLA #81895
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//===- Transforms.h - ArmNeon Transformation Entrypoints --------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef MLIR_DIALECT_ARMNEON_TRANSFORMS_H | ||
#define MLIR_DIALECT_ARMNEON_TRANSFORMS_H | ||
|
||
namespace mlir { | ||
|
||
namespace arm_neon { | ||
void populateLowerContractionToSMMLAPatternPatterns( | ||
RewritePatternSet &patterns); | ||
} // namespace arm_neon | ||
|
||
} // namespace mlir | ||
|
||
#endif // MLIR_DIALECT_ARMNEON_TRANSFORMS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,2 @@ | ||
add_mlir_dialect_library(MLIRArmNeonDialect | ||
IR/ArmNeonDialect.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/ArmNeon | ||
|
||
DEPENDS | ||
MLIRArmNeonIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRIR | ||
MLIRSideEffectInterfaces | ||
) | ||
add_subdirectory(IR) | ||
add_subdirectory(Transforms) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
add_mlir_dialect_library(MLIRArmNeonDialect | ||
ArmNeonDialect.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/ArmNeon | ||
|
||
DEPENDS | ||
MLIRArmNeonIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRIR | ||
MLIRSideEffectInterfaces | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
add_mlir_dialect_library(MLIRArmNeonTransforms | ||
LowerContractionToSMMLAPattern.cpp | ||
|
||
DEPENDS | ||
MLIRArmNeonIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRArmNeonDialect | ||
MLIRFuncDialect | ||
MLIRVectorDialect | ||
MLIRIR | ||
MLIRLLVMCommonConversion | ||
MLIRLLVMDialect | ||
) |
146 changes: 146 additions & 0 deletions
146
mlir/lib/Dialect/ArmNeon/Transforms/LowerContractionToSMMLAPattern.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
//===- LowerContractionToSMMLAPattern.cpp - Contract to SMMLA ---*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements lowering patterns from vector.contract to | ||
// arm_neon.intr.smmla | ||
// | ||
//===--- | ||
|
||
#include "mlir/Dialect/Arith/IR/Arith.h" | ||
#include "mlir/Dialect/ArmNeon/ArmNeonDialect.h" | ||
#include "mlir/Dialect/ArmNeon/Transforms.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "mlir/Dialect/Vector/IR/VectorOps.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
#define DEBUG_TYPE "lower-contract-to-arm-neon" | ||
|
||
using namespace mlir; | ||
using namespace mlir::arm_neon; | ||
|
||
namespace { | ||
|
||
/// Return the shaped type with new element type. | ||
static Type matchContainerType(Type element, Type container) { | ||
if (auto shapedTy = dyn_cast<ShapedType>(container)) { | ||
return shapedTy.clone(element); | ||
} | ||
return element; | ||
} | ||
|
||
/// Lowering from a single vector::contractOp directly to the arm neon smmla | ||
/// intrinsic. The shapes of the contract and intrinsic must match. | ||
class LowerContractionToSMMLAPattern | ||
: public OpRewritePattern<vector::ContractionOp> { | ||
public: | ||
using OpRewritePattern::OpRewritePattern; | ||
LogicalResult matchAndRewrite(vector::ContractionOp op, | ||
PatternRewriter &rewriter) const override { | ||
Location loc = op.getLoc(); | ||
Value lhs = op.getLhs(); | ||
Value rhs = op.getRhs(); | ||
Value res = op.getAcc(); | ||
|
||
// Check index maps that represent M N K in contract. | ||
auto indexingMaps = op.getIndexingMapsArray(); | ||
if (llvm::any_of(indexingMaps, [](mlir::AffineMap affineMap) { | ||
return affineMap.isPermutation() || affineMap.getNumDims() != 3 || | ||
affineMap.getNumResults() != 2; | ||
})) { | ||
return failure(); | ||
} | ||
|
||
// Check iterator types for contract. | ||
auto iteratorTypes = op.getIteratorTypesArray(); | ||
if (iteratorTypes.size() != 3 || | ||
iteratorTypes[0] != vector::IteratorType::parallel || | ||
iteratorTypes[1] != vector::IteratorType::parallel || | ||
iteratorTypes[2] != vector::IteratorType::reduction) { | ||
return failure(); | ||
} | ||
|
||
// Check the tile size by mapping the dimensions of the contract. | ||
mlir::VectorType lhsType = op.getLhsType(); | ||
mlir::VectorType rhsType = op.getRhsType(); | ||
auto dimM = lhsType.getDimSize(0); | ||
auto dimN = rhsType.getDimSize(0); | ||
auto dimK = lhsType.getDimSize(1); | ||
if (rhsType.getDimSize(1) != dimK || dimM != 2 || dimN != 2 || dimK != 8) { | ||
return failure(); | ||
} | ||
|
||
// Check two extsi inputs Rhs Lhs for contract. | ||
arith::ExtSIOp origLhsExtOp = | ||
dyn_cast_or_null<arith::ExtSIOp>(lhs.getDefiningOp()); | ||
arith::ExtSIOp origRhsExtOp = | ||
dyn_cast_or_null<arith::ExtSIOp>(rhs.getDefiningOp()); | ||
if (!origLhsExtOp || !origRhsExtOp) { | ||
return failure(); | ||
} | ||
|
||
// Match any iX to i32 for X<8 then turn into an i8 output. Feed into | ||
// following neon instruction. Check inputs for extsi are <=i8 | ||
Value extsiLhs; | ||
Value extsiRhs; | ||
if (auto lhsExtInType = | ||
origLhsExtOp.getIn().getType().dyn_cast<mlir::VectorType>()) { | ||
if (lhsExtInType.getElementTypeBitWidth() <= 8) { | ||
Type targetLhsExtTy = | ||
matchContainerType(rewriter.getI8Type(), lhsExtInType); | ||
extsiLhs = rewriter.createOrFold<arith::ExtSIOp>(loc, targetLhsExtTy, | ||
origLhsExtOp.getIn()); | ||
} | ||
} | ||
if (auto rhsExtInType = | ||
origRhsExtOp.getIn().getType().dyn_cast<mlir::VectorType>()) { | ||
if (rhsExtInType.getElementTypeBitWidth() <= 8) { | ||
Type targetRhsExtTy = | ||
matchContainerType(rewriter.getI8Type(), rhsExtInType); | ||
extsiRhs = rewriter.createOrFold<arith::ExtSIOp>(loc, targetRhsExtTy, | ||
origRhsExtOp.getIn()); | ||
} | ||
} | ||
|
||
if (!extsiLhs || !extsiRhs) { | ||
return failure(); | ||
} | ||
|
||
// Collapse to 1D vectors required by smmla intrinsic | ||
auto collapsedInputType = VectorType::get( | ||
{16}, extsiLhs.getType().cast<ShapedType>().getElementType()); | ||
auto collapsedOutputType = | ||
VectorType::get({4}, res.getType().cast<ShapedType>().getElementType()); | ||
auto collapsedLhs = rewriter.createOrFold<vector::ShapeCastOp>( | ||
extsiLhs.getLoc(), collapsedInputType, extsiLhs); | ||
auto collapsedRhs = rewriter.createOrFold<vector::ShapeCastOp>( | ||
extsiRhs.getLoc(), collapsedInputType, extsiRhs); | ||
auto collapsedRes = rewriter.createOrFold<vector::ShapeCastOp>( | ||
res.getLoc(), collapsedOutputType, res); | ||
|
||
// Replace the contract with a neon op | ||
auto smmlaOp = rewriter.createOrFold<arm_neon::SmmlaOp>( | ||
op.getLoc(), collapsedRes.getType(), collapsedRes, collapsedLhs, | ||
collapsedRhs); | ||
|
||
// Reshape output back to 2D | ||
rewriter.replaceOpWithNewOp<vector::ShapeCastOp>(op, op.getResultType(), | ||
smmlaOp); | ||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void mlir::arm_neon::populateLowerContractionToSMMLAPatternPatterns( | ||
RewritePatternSet &patterns) { | ||
MLIRContext *context = patterns.getContext(); | ||
patterns.add<LowerContractionToSMMLAPattern>(context, /*benefit=*/1); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// RUN: mlir-opt -test-lower-to-arm-neon -verify-diagnostics -split-input-file %s | FileCheck %s | ||
|
||
// CHECK-LABEL: test_lower_vector_arm_neon_mixed_types | ||
// CHECK-SAME: %[[A0:.*]]: vector<2x8xi8>, %[[A1:.*]]: vector<2x8xi4>, %[[A2:.*]]: vector<2x2xi32> | ||
// CHECK-DAG: %[[D0:.*]] = arith.extsi %[[A1]] : vector<2x8xi4> to vector<2x8xi8> | ||
// CHECK-DAG: %[[D1:.*]] = vector.shape_cast %[[A0]] : vector<2x8xi8> to vector<16xi8> | ||
// CHECK-DAG: %[[D2:.*]] = vector.shape_cast %[[D0]] : vector<2x8xi8> to vector<16xi8> | ||
// CHECK-DAG: %[[D3:.*]] = vector.shape_cast %[[A2]] : vector<2x2xi32> to vector<4xi32> | ||
// CHECK-DAG: %[[D4:.*]] = arm_neon.intr.smmla %[[D3]], %[[D1]], %[[D2]] : vector<16xi8> to vector<4xi32> | ||
// CHECK-DAG: %[[D5:.*]] = vector.shape_cast %[[D4]] : vector<4xi32> to vector<2x2xi32> | ||
func.func @test_lower_vector_arm_neon_mixed_types(%lhs: vector<2x8xi8>, %rhs: vector<2x8xi4>, %acc : vector<2x2xi32>) -> vector<2x2xi32> { | ||
%lhs_extsi = arith.extsi %lhs : vector<2x8xi8> to vector<2x8xi32> | ||
%rhs_extsi = arith.extsi %rhs : vector<2x8xi4> to vector<2x8xi32> | ||
%res = vector.contract {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %lhs_extsi, %rhs_extsi, %acc : vector<2x8xi32>, vector<2x8xi32> into vector<2x2xi32> | ||
return %res : vector<2x2xi32> | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: test_lower_vector_arm_neon_same_types | ||
// CHECK-SAME: %[[A0:.*]]: vector<2x8xi8>, %[[A1:.*]]: vector<2x8xi8>, %[[A2:.*]]: vector<2x2xi32> | ||
// CHECK-DAG: %[[D0:.*]] = vector.shape_cast %[[A0]] : vector<2x8xi8> to vector<16xi8> | ||
// CHECK-DAG: %[[D1:.*]] = vector.shape_cast %[[A1]] : vector<2x8xi8> to vector<16xi8> | ||
// CHECK-DAG: %[[D2:.*]] = vector.shape_cast %[[A2]] : vector<2x2xi32> to vector<4xi32> | ||
// CHECK-DAG: %[[D3:.*]] = arm_neon.intr.smmla %[[D2]], %[[D0]], %[[D1]] : vector<16xi8> to vector<4xi32> | ||
// CHECK-DAG: %[[D4:.*]] = vector.shape_cast %[[D3]] : vector<4xi32> to vector<2x2xi32> | ||
func.func @test_lower_vector_arm_neon_same_types(%lhs: vector<2x8xi8>, %rhs: vector<2x8xi8>, %acc : vector<2x2xi32>) -> vector<2x2xi32> { | ||
%lhs_extsi = arith.extsi %lhs : vector<2x8xi8> to vector<2x8xi32> | ||
%rhs_extsi = arith.extsi %rhs : vector<2x8xi8> to vector<2x8xi32> | ||
%res = vector.contract {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %lhs_extsi, %rhs_extsi, %acc : vector<2x8xi32>, vector<2x8xi32> into vector<2x2xi32> | ||
return %res : vector<2x2xi32> | ||
} | ||
|
||
// ----- | ||
|
||
// CHECK-LABEL: test_lower_vector_arm_neon_without_extsi | ||
// CHECK-SAME: %[[A0:.*]]: vector<2x8xi32>, %[[A1:.*]]: vector<2x8xi32>, %[[A2:.*]]: vector<2x2xi32> | ||
// CHECK-DAG: %[[D0:.*]] = vector.contract | ||
func.func @test_lower_vector_arm_neon_without_extsi(%lhs: vector<2x8xi32>, %rhs: vector<2x8xi32>, %acc : vector<2x2xi32>) -> vector<2x2xi32> { | ||
%res = vector.contract {indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d2)>, affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>], iterator_types = ["parallel", "parallel", "reduction"], kind = #vector.kind<add>} %lhs, %rhs, %acc : vector<2x8xi32>, vector<2x8xi32> into vector<2x2xi32> | ||
return %res : vector<2x2xi32> | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Exclude tests from libMLIR.so | ||
add_mlir_library(MLIRArmNeonTestPasses | ||
TestLowerToArmNeon.cpp | ||
|
||
EXCLUDE_FROM_LIBMLIR | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRArmNeonDialect | ||
MLIRArmNeonTransforms | ||
MLIRIR | ||
MLIRPass | ||
MLIRTransforms | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//===- TestLowerToArmNeon.cpp - Test lowering to ArmNeon as a sink pass -===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file implements a pass for testing the lowering to ArmNeon as a | ||
// generally usable sink pass. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/ArmNeon/ArmNeonDialect.h" | ||
#include "mlir/Dialect/ArmNeon/Transforms.h" | ||
#include "mlir/Dialect/Func/IR/FuncOps.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Pass/PassManager.h" | ||
#include "mlir/Support/LogicalResult.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
#define PASS_NAME "test-lower-to-arm-neon" | ||
|
||
using namespace mlir; | ||
using namespace mlir::arm_neon; | ||
|
||
namespace { | ||
struct TestLowerToArmNeon | ||
: public PassWrapper<TestLowerToArmNeon, OperationPass<func::FuncOp>> { | ||
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestLowerToArmNeon) | ||
|
||
StringRef getArgument() const final { return PASS_NAME; } | ||
StringRef getDescription() const final { return "Tests lower to arm Neon."; } | ||
TestLowerToArmNeon() = default; | ||
TestLowerToArmNeon(const TestLowerToArmNeon &pass) = default; | ||
|
||
void getDependentDialects(DialectRegistry ®istry) const override { | ||
registry.insert<arm_neon::ArmNeonDialect>(); | ||
} | ||
|
||
void runOnOperation() override; | ||
}; | ||
|
||
} // namespace | ||
|
||
void TestLowerToArmNeon::runOnOperation() { | ||
MLIRContext *context = &getContext(); | ||
RewritePatternSet patterns(context); | ||
populateLowerContractionToSMMLAPatternPatterns(patterns); | ||
if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) | ||
return signalPassFailure(); | ||
} | ||
|
||
namespace mlir { | ||
namespace test { | ||
|
||
void registerTestLowerToArmNeon() { PassRegistration<TestLowerToArmNeon>(); } | ||
|
||
} // namespace test | ||
} // namespace mlir |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.