-
Notifications
You must be signed in to change notification settings - Fork 15
Lower mlir.llvm to neura #2
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,12 @@ | ||
| #ifndef NEURA_CONVERSION_LLVMTONEURA_LLVMTONEURAPASS_H | ||
| #define NEURA_CONVERSION_LLVMTONEURA_LLVMTONEURAPASS_H | ||
|
|
||
| #include "mlir/Pass/Pass.h" | ||
|
|
||
| namespace mlir { | ||
| namespace neura { | ||
| std::unique_ptr<Pass> createLowerLlvmToNeuraPass(); | ||
| } // namespace neura | ||
| } // namespace mlir | ||
|
|
||
| #endif // NEURA_CONVERSION_LLVMTONEURA_LLVMTONEURAPASS_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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #ifndef NEURA_TRANSFORMS_INSERTMOVPASS_H | ||
| #define NEURA_TRANSFORMS_INSERTMOVPASS_H | ||
|
|
||
| #include "mlir/Pass/Pass.h" | ||
|
|
||
| namespace mlir { | ||
| namespace neura { | ||
| std::unique_ptr<mlir::Pass> createInsertMovPass(); | ||
| } // namespace neura | ||
| } // namespace mlir | ||
|
|
||
| #endif // NEURA_TRANSFORMS_INSERTMOVPASS_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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| add_subdirectory(NeuraDialect) | ||
| add_subdirectory(Conversion/ArithToNeura) | ||
| add_subdirectory(Conversion) | ||
| 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
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,5 +1,5 @@ | ||
| add_mlir_library(NeuraArithToNeura | ||
| ArithToNeura.cpp | ||
| ArithToNeuraPass.cpp | ||
|
|
||
| DEPENDS | ||
| NeuraOpsIncGen | ||
|
|
||
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,2 @@ | ||
| add_subdirectory(ArithToNeura) | ||
| add_subdirectory(LlvmToNeura) |
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,28 @@ | ||
| add_mlir_library(NeuraLlvmToNeura | ||
| LlvmToNeuraPass.cpp | ||
|
|
||
| DEPENDS | ||
| NeuraOpsIncGen | ||
| NeuraDialectIncGen | ||
| NeuraDialect | ||
|
|
||
| LINK_LIBS PUBLIC | ||
| MLIRArithDialect | ||
| MLIRFuncDialect | ||
| MLIRLLVMDialect | ||
| MLIRIR | ||
| MLIRPass | ||
| MLIRTransforms | ||
| ) | ||
|
|
||
| target_include_directories(NeuraLlvmToNeura PUBLIC | ||
| ${CMAKE_BINARY_DIR}/lib/NeuraDialect | ||
| ${MLIR_INCLUDE_DIRS} | ||
| ${LLVM_INCLUDE_DIRS} | ||
| ${CMAKE_SOURCE_DIR}/include | ||
| ${CMAKE_BINARY_DIR}/include | ||
| ) | ||
|
|
||
| target_compile_definitions(NeuraLlvmToNeura | ||
| PRIVATE ${LLVM_DEFINITIONS} | ||
| ) |
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,51 @@ | ||
| #include "Conversion/LlvmToNeura/LlvmToNeura.h" | ||
| #include "NeuraDialect/NeuraDialect.h" | ||
| #include "NeuraDialect/NeuraOps.h" | ||
| #include "mlir/Dialect/LLVMIR/LLVMAttrs.h" | ||
| #include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
| #include "mlir/Dialect/LLVMIR/LLVMTypes.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
| struct LlvmAddFOpLowering : public OpRewritePattern<mlir::LLVM::FAddOp> { | ||
| using OpRewritePattern::OpRewritePattern; | ||
|
|
||
| LogicalResult matchAndRewrite(mlir::LLVM::FAddOp op, | ||
| PatternRewriter &rewriter) const override { | ||
| rewriter.replaceOpWithNewOp<neura::AddOp>(op, op.getType(), op.getLhs(), op.getRhs()); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct LowerLlvmToNeuraPass | ||
| : public PassWrapper<LowerLlvmToNeuraPass, OperationPass<func::FuncOp>> { | ||
|
|
||
| MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LowerLlvmToNeuraPass) | ||
|
|
||
| StringRef getArgument() const override { return "lower-llvm-to-neura"; } | ||
| StringRef getDescription() const override { | ||
| return "Lower LLVM operations to Neura dialect operations"; | ||
| } | ||
|
|
||
| void getDependentDialects(DialectRegistry ®istry) const override { | ||
| registry.insert<mlir::neura::NeuraDialect>(); | ||
| } | ||
|
|
||
| void runOnOperation() override { | ||
| RewritePatternSet patterns(&getContext()); | ||
| patterns.add<LlvmAddFOpLowering>(&getContext()); | ||
| if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) { | ||
| signalPassFailure(); | ||
| } | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
| std::unique_ptr<Pass> mlir::neura::createLowerLlvmToNeuraPass() { | ||
| return std::make_unique<LowerLlvmToNeuraPass>(); | ||
| } |
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,11 @@ | ||
| add_mlir_library(NeuraTransforms | ||
| InsertMovPass.cpp | ||
|
|
||
| LINK_LIBS PUBLIC | ||
| MLIRIR | ||
| MLIRFuncDialect | ||
| MLIRPass | ||
| MLIRSupport | ||
| MLIRTransformUtils | ||
| NeuraDialect | ||
| ) |
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,86 @@ | ||
| #include "NeuraDialect/NeuraDialect.h" | ||
| #include "NeuraDialect/NeuraOps.h" | ||
| #include "mlir/IR/PatternMatch.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
|
|
||
| using namespace mlir; | ||
|
|
||
| namespace { | ||
| struct InsertMovForNeuraOps : public RewritePattern { | ||
| InsertMovForNeuraOps(MLIRContext *context) | ||
| : RewritePattern(/*matchAnyOpTypeTag=*/MatchAnyOpTypeTag(), /*benefit=*/1, context) {} | ||
|
|
||
| LogicalResult matchAndRewrite(Operation *op, PatternRewriter &rewriter) const override { | ||
| if (op->getDialect()->getNamespace() != "neura" || | ||
| isa<neura::MovOp>(op)) { | ||
| return failure(); | ||
| } | ||
|
|
||
| // Skips ops that already being inserted mov on the operands. | ||
| bool allInputsAreMov = llvm::all_of(op->getOperands(), [](Value v) { | ||
| return isa_and_nonnull<neura::MovOp>(v.getDefiningOp()); | ||
| }); | ||
| if (allInputsAreMov) { | ||
| return failure(); | ||
| } | ||
|
|
||
| // Makes sure none of the operand has being processed. | ||
| bool hasAnyMovInput = llvm::any_of(op->getOperands(), [](Value v) { | ||
| return isa_and_nonnull<neura::MovOp>(v.getDefiningOp()); | ||
| }); | ||
| assert(!hasAnyMovInput && "Unexpected: operand already wrapped in neura.mov"); | ||
|
|
||
| Location loc = op->getLoc(); | ||
|
|
||
| // Wraps operands in mov. | ||
| SmallVector<Value> newOperands; | ||
| for (Value operand : op->getOperands()) { | ||
| auto mov = rewriter.create<neura::MovOp>(loc, operand.getType(), operand); | ||
| newOperands.push_back(mov); | ||
| } | ||
|
|
||
| // Clones op with new operands. | ||
| OperationState state(loc, op->getName()); | ||
| state.addOperands(newOperands); | ||
| state.addTypes(op->getResultTypes()); | ||
| state.addAttributes(op->getAttrs()); | ||
|
|
||
| Operation *newOp = rewriter.create(state); | ||
| rewriter.replaceOp(op, newOp->getResults()); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
||
| struct InsertMovPass | ||
| : public PassWrapper<InsertMovPass, OperationPass<func::FuncOp>> { | ||
| MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(InsertMovPass) | ||
|
|
||
| StringRef getArgument() const override { return "insert-mov"; } | ||
| StringRef getDescription() const override { | ||
| return "Insert neura.mov before and after all neura dialect operations."; | ||
| } | ||
|
|
||
| void getDependentDialects(DialectRegistry ®istry) const override { | ||
| registry.insert<mlir::neura::NeuraDialect>(); | ||
| } | ||
|
|
||
| void runOnOperation() override { | ||
| RewritePatternSet patterns(&getContext()); | ||
| patterns.add<InsertMovForNeuraOps>(&getContext()); | ||
| if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) | ||
| signalPassFailure(); | ||
| } | ||
| }; | ||
| } // namespace | ||
|
|
||
| namespace mlir { | ||
| namespace neura { | ||
|
|
||
| std::unique_ptr<Pass> createInsertMovPass() { | ||
| return std::make_unique<InsertMovPass>(); | ||
| } | ||
|
|
||
| } // namespace neura | ||
| } // 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // RUN: mlir-neura-opt --lower-arith-to-neura --insert-mov %s | FileCheck %s | ||
|
|
||
| func.func @test(%a: f32) -> f32 { | ||
| %b = arith.constant 2.0 : f32 | ||
| %res = arith.addf %a, %b : f32 | ||
| // CHECK: neura.mov %arg0 : f32 -> f32 | ||
| // CHECK: neura.mov %cst : f32 -> f32 | ||
| // CHECK: neura.add | ||
| return %res : f32 | ||
| } |
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,10 @@ | ||
| // RUN: mlir-neura-opt --lower-llvm-to-neura --insert-mov %s | FileCheck %s | ||
|
|
||
| func.func @test(%a: f32) -> f32 { | ||
| %b = llvm.mlir.constant(2.0 : f32) : f32 | ||
| %res = llvm.fadd %a, %b : f32 | ||
| // CHECK: [[LHS:%.*]] = neura.mov %{{.*}} : f32 -> f32 | ||
| // CHECK: [[RHS:%.*]] = neura.mov %{{.*}} : f32 -> f32 | ||
| // CHECK: [[RES:%.*]] = neura.add [[LHS]], [[RHS]] : f32 | ||
| return %res : f32 | ||
| } |
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.