-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[mlir][linalg] Morphism across linalg named, category and generic ops. #148424
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
+290
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a005f0c
[mlir][linalg] Convert linalg.named to linalg.elementwise op.
javedabsar1 a5d1622
Add linalg-morph pass
javedabsar1 4f763e5
address comment
javedabsar1 de9c5ce
address review comments
javedabsar1 aa5b3e1
review comment address
javedabsar1 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===- MorphOps.cpp - conversion between named,category and generic ops ---===// | ||
// | ||
// 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 conversions between linalg ops: | ||
// named <--> category (elementwise, contraction, ..) <--> generic. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Complex/IR/Complex.h" | ||
#include "mlir/Dialect/Linalg/IR/Linalg.h" | ||
#include "mlir/Dialect/Linalg/IR/LinalgInterfaces.h" | ||
#include "mlir/Dialect/Linalg/Passes.h" | ||
#include "mlir/Dialect/Linalg/Transforms/Transforms.h" | ||
#include "mlir/Dialect/Math/IR/Math.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
namespace mlir { | ||
#define GEN_PASS_DEF_LINALGMORPHOPSPASS | ||
#include "mlir/Dialect/Linalg/Passes.h.inc" | ||
} // namespace mlir | ||
|
||
#define DEBUG_TYPE "linalg-morphism" | ||
|
||
using namespace mlir; | ||
using namespace mlir::linalg; | ||
|
||
namespace { | ||
struct LinalgMorphOpsPass | ||
: public impl::LinalgMorphOpsPassBase<LinalgMorphOpsPass> { | ||
|
||
using impl::LinalgMorphOpsPassBase< | ||
LinalgMorphOpsPass>::LinalgMorphOpsPassBase; | ||
|
||
void runOnOperation() override; | ||
}; | ||
|
||
void LinalgMorphOpsPass::runOnOperation() { | ||
|
||
RewritePatternSet patterns(&getContext()); | ||
|
||
// Lowering paths (named -> category -> generic) | ||
if (namedToCategory) { | ||
populateLinalgNamedToElementwisePatterns(patterns); | ||
} | ||
if (namedToGeneric || categoryToGeneric) { | ||
populateLinalgNamedOpsGeneralizationPatterns(patterns); | ||
} | ||
|
||
// Lifting paths (named <- category <- generic) | ||
if (genericToNamed) { | ||
populateLinalgGenericOpsSpecializationPatterns(patterns); | ||
} | ||
|
||
if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) | ||
signalPassFailure(); | ||
} | ||
} // namespace |
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,98 @@ | ||
//===- NamedToElementwise.cpp - convert linalg named op into elementwise --===// | ||
// | ||
// 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 rewriting those linalg named ops that are essentially | ||
// elementwise e.g. `linalg.exp`, to `linalg.elementwise`. This allows further | ||
// optimization on `linalg.elementwise` such as folding transpose, broadcast. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "mlir/Dialect/Linalg/IR/Linalg.h" | ||
#include "mlir/Dialect/Linalg/Passes.h" | ||
#include "mlir/Dialect/Linalg/Transforms/Transforms.h" | ||
#include "mlir/IR/PatternMatch.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/TypeSwitch.h" | ||
|
||
using namespace mlir; | ||
using namespace mlir::linalg; | ||
|
||
#define DEBUG_TYPE "linalg-named-to-elementwise" | ||
|
||
namespace { | ||
ElementwiseKind getKind(Operation *op) { | ||
rengolin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return llvm::TypeSwitch<Operation *, ElementwiseKind>(op) | ||
.Case([](SelectOp) { return ElementwiseKind::select; }) | ||
.Case([](AddOp) { return ElementwiseKind::add; }) | ||
.Case([](SubOp) { return ElementwiseKind::sub; }) | ||
.Case([](MulOp) { return ElementwiseKind::mul; }) | ||
.Case([](DivOp) { return ElementwiseKind::div; }) | ||
.Case([](DivUnsignedOp) { return ElementwiseKind::div_unsigned; }) | ||
.Case([](PowFOp) { return ElementwiseKind::powf; }) | ||
.Case([](ExpOp) { return ElementwiseKind::exp; }) | ||
.Case([](LogOp) { return ElementwiseKind::log; }) | ||
.Case([](AbsOp) { return ElementwiseKind::abs; }) | ||
.Case([](CeilOp) { return ElementwiseKind::ceil; }) | ||
.Case([](FloorOp) { return ElementwiseKind::floor; }) | ||
.Case([](NegFOp) { return ElementwiseKind::negf; }) | ||
.Case([](ReciprocalOp) { return ElementwiseKind::reciprocal; }) | ||
.Case([](RoundOp) { return ElementwiseKind::round; }) | ||
.Case([](SqrtOp) { return ElementwiseKind::sqrt; }) | ||
.Case([](RsqrtOp) { return ElementwiseKind::rsqrt; }) | ||
.Case([](SquareOp) { return ElementwiseKind::square; }) | ||
.Case([](TanhOp) { return ElementwiseKind::tanh; }) | ||
.Case([](ErfOp) { return ElementwiseKind::erf; }) | ||
.Default([&](Operation *op) { | ||
llvm_unreachable("unhandled case in named to elementwise"); | ||
return ElementwiseKind::sub; | ||
}); | ||
} | ||
|
||
template <typename NamedOpTy> | ||
struct NamedToElementwisePattern : public OpRewritePattern<NamedOpTy> { | ||
using OpRewritePattern<NamedOpTy>::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(NamedOpTy op, | ||
PatternRewriter &rewriter) const override { | ||
SmallVector<NamedAttribute> attrs; | ||
auto kindAttr = ElementwiseKindAttr::get(op.getContext(), getKind(op)); | ||
attrs.push_back(rewriter.getNamedAttr("kind", kindAttr)); | ||
attrs.push_back( | ||
rengolin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rewriter.getNamedAttr("indexing_maps", op.getIndexingMaps())); | ||
|
||
rewriter.replaceOpWithNewOp<ElementwiseOp>(op, op.getDpsInputs(), | ||
op.getDpsInits(), attrs); | ||
return success(); | ||
} | ||
}; | ||
} // namespace | ||
|
||
void mlir::linalg::populateLinalgNamedToElementwisePatterns( | ||
RewritePatternSet &patterns) { | ||
patterns.add<NamedToElementwisePattern<SelectOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<AddOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<SubOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<MulOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<DivOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<DivUnsignedOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<PowFOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<ExpOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<LogOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<AbsOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<CeilOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<FloorOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<NegFOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<ReciprocalOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<RoundOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<SqrtOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<RsqrtOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<SquareOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<TanhOp>>(patterns.getContext()); | ||
patterns.add<NamedToElementwisePattern<ErfOp>>(patterns.getContext()); | ||
} |
56 changes: 56 additions & 0 deletions
56
mlir/test/Dialect/Linalg/elementwise/named-to-elementwise.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// RUN: mlir-opt %s -linalg-morph-ops=named-to-category -split-input-file | FileCheck %s | ||
|
||
// CHECK: @exp(%[[A:.+]]: tensor<16x8xf32>, %[[B:.+]]: tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
// CHECK: {{.*}} = linalg.elementwise | ||
// CHECK-SAME: kind=#linalg.elementwise_kind<exp> | ||
// CHECK-SAME: ins(%[[A]] : tensor<16x8xf32>) | ||
// CHECK-SAME: outs(%[[B]] : tensor<16x8xf32>) -> tensor<16x8xf32> | ||
// | ||
func.func @exp(%A : tensor<16x8xf32>, %B : tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
%exp = linalg.exp ins(%A : tensor<16x8xf32>) outs(%B : tensor<16x8xf32>) -> tensor<16x8xf32> | ||
return %exp : tensor<16x8xf32> | ||
} | ||
|
||
// ---- | ||
|
||
// CHECK: @add(%[[A:.+]]: tensor<16x8xf32>, %[[B:.+]]: tensor<16x8xf32>, %[[C:.+]]: tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
// CHECK: {{.*}} = linalg.elementwise | ||
// CHECK-SAME: kind=#linalg.elementwise_kind<add> | ||
// CHECK-SAME: ins(%[[A]], %[[B]] : tensor<16x8xf32>, tensor<16x8xf32>) | ||
// CHECK-SAME: outs(%[[C]] : tensor<16x8xf32>) -> tensor<16x8xf32> | ||
// | ||
func.func @add(%A : tensor<16x8xf32>, %B: tensor<16x8xf32>, %C : tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
%add = linalg.add ins(%A, %B : tensor<16x8xf32>, tensor<16x8xf32>) outs(%C : tensor<16x8xf32>) -> tensor<16x8xf32> | ||
return %add : tensor<16x8xf32> | ||
} | ||
|
||
// ---- | ||
|
||
// CHECK: @sub(%[[A:.+]]: tensor<16x8xf32>, %[[B:.+]]: tensor<16x8xf32>, %[[C:.+]]: tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
// CHECK: {{.*}} = linalg.elementwise | ||
// CHECK-SAME: kind=#linalg.elementwise_kind<sub> | ||
// CHECK-SAME: ins(%[[A]], %[[B]] : tensor<16x8xf32>, tensor<16x8xf32>) | ||
// CHECK-SAME: outs(%[[C]] : tensor<16x8xf32>) | ||
// | ||
func.func @sub(%A : tensor<16x8xf32>, %B: tensor<16x8xf32>, %C : tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
%sub = linalg.sub ins(%A, %B : tensor<16x8xf32>, tensor<16x8xf32>) outs(%C : tensor<16x8xf32>) -> tensor<16x8xf32> | ||
return %sub : tensor<16x8xf32> | ||
} | ||
|
||
// ---- | ||
|
||
// CHECK: @ternary_select(%[[A:.+]]: tensor<4x8x16xi1>, %[[B:.+]]: tensor<4x8x16xf32>, %[[C:.+]]: tensor<4x8x16xf32>) | ||
// CHECK: %[[E:.+]] = tensor.empty() : tensor<4x8x16xf32> | ||
// CHECK: {{.*}} = linalg.elementwise | ||
// CHECK-SAME: kind=#linalg.elementwise_kind<select> | ||
// CHECK-SAME: ins(%[[A]], %[[B]], %[[C]] : tensor<4x8x16xi1>, tensor<4x8x16xf32>, tensor<4x8x16xf32>) | ||
// CHECK-SAME: outs(%[[E]] : tensor<4x8x16xf32>) -> tensor<4x8x16xf32> | ||
// | ||
func.func @ternary_select(%A: tensor<4x8x16xi1>, %B: tensor<4x8x16xf32>, %C: tensor<4x8x16xf32>) | ||
-> tensor<4x8x16xf32> { | ||
%empty = tensor.empty() : tensor<4x8x16xf32> | ||
%select = linalg.select | ||
ins(%A, %B, %C : tensor<4x8x16xi1>, tensor<4x8x16xf32>, tensor<4x8x16xf32>) | ||
outs(%empty: tensor<4x8x16xf32>) -> tensor<4x8x16xf32> | ||
return %select : tensor<4x8x16xf32> | ||
} |
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,15 @@ | ||
// Forward path `named -> category -> generic` | ||
// RUN: mlir-opt %s -linalg-morph-ops=named-to-category | FileCheck %s --check-prefix=NAMED_TO_CATEGORY | ||
|
||
// RUN: mlir-opt %s -linalg-morph-ops=named-to-category | \ | ||
// RUN: mlir-opt %s -linalg-morph-ops=category-to-generic | FileCheck %s --check-prefix=CATEGORY_TO_GENERIC | ||
|
||
func.func @exp(%A : tensor<16x8xf32>, %B : tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
%exp = linalg.exp ins(%A : tensor<16x8xf32>) outs(%B : tensor<16x8xf32>) -> tensor<16x8xf32> | ||
return %exp : tensor<16x8xf32> | ||
} | ||
// NAMED_TO_CATEGORY: linalg.elementwise | ||
// NAMED_TO_CATEGORY-NOT: linalg.exp | ||
|
||
// CATEGORY_TO_GENERIC: linalg.generic | ||
// CATEGORY_TO_GENERIC-NOT: linalg.elementwise |
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 @@ | ||
// RUN: mlir-opt %s -linalg-morph-ops=named-to-generic | FileCheck %s --check-prefix=NAMED_TO_GENERIC | ||
// RUN: mlir-opt %s -linalg-morph-ops=named-to-generic | mlir-opt %s -linalg-morph-ops=generic-to-named | \ | ||
// RUN: FileCheck %s --check-prefix=ROUND_TRIP | ||
|
||
func.func @exp(%A : tensor<16x8xf32>, %B : tensor<16x8xf32>) -> tensor<16x8xf32> { | ||
%exp = linalg.exp ins(%A : tensor<16x8xf32>) outs(%B : tensor<16x8xf32>) -> tensor<16x8xf32> | ||
return %exp : tensor<16x8xf32> | ||
} | ||
|
||
// NAMED_TO_GENERIC: linalg.generic | ||
// NAMED_TO_GENERIC-NOT: linalg.exp | ||
|
||
// ROUND_TRIP: linalg.exp | ||
// ROUND_TRIP-NOT: linalg.generic |
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.