Skip to content

Commit f968780

Browse files
authored
Merge pull request #2 from coredac/lower_llvm
Lower mlir.llvm to neura
2 parents 5e2adc4 + fc981d0 commit f968780

File tree

11 files changed

+116
-9
lines changed

11 files changed

+116
-9
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef NEURA_CONVERSION_LLVMTONEURA_LLVMTONEURAPASS_H
2+
#define NEURA_CONVERSION_LLVMTONEURA_LLVMTONEURAPASS_H
3+
4+
#include "mlir/Pass/Pass.h"
5+
6+
namespace mlir {
7+
namespace neura {
8+
std::unique_ptr<Pass> createLowerLlvmToNeuraPass();
9+
} // namespace neura
10+
} // namespace mlir
11+
12+
#endif // NEURA_CONVERSION_LLVMTONEURA_LLVMTONEURAPASS_H

lib/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
add_subdirectory(NeuraDialect)
2-
add_subdirectory(Conversion/ArithToNeura)
2+
add_subdirectory(Conversion)
33
add_subdirectory(Transforms)

lib/Conversion/ArithToNeura/ArithToNeura.cpp renamed to lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct LowerArithToNeuraPass
2727

2828
StringRef getArgument() const override { return "lower-arith-to-neura"; }
2929
StringRef getDescription() const override {
30-
return "Lower arithmetic operations to Neura dialect operations";
30+
return "Lower arith dialect operations to Neura dialect operations";
3131
}
3232

3333
void getDependentDialects(DialectRegistry &registry) const override {
@@ -36,11 +36,7 @@ struct LowerArithToNeuraPass
3636

3737
void runOnOperation() override {
3838
RewritePatternSet patterns(&getContext());
39-
llvm::errs() << "[cheng] check runOnOperation: ";
4039
getOperation().dump();
41-
getOperation().walk([](Operation *op) {
42-
llvm::errs() << "[cheng] Saw op: " << op->getName() << "\n";
43-
});
4440
patterns.add<ArithAddFOpLowering>(&getContext());
4541
if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) {
4642
signalPassFailure();

lib/Conversion/ArithToNeura/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
add_mlir_library(NeuraArithToNeura
2-
ArithToNeura.cpp
2+
ArithToNeuraPass.cpp
33

44
DEPENDS
55
NeuraOpsIncGen

lib/Conversion/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_subdirectory(ArithToNeura)
2+
add_subdirectory(LlvmToNeura)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
add_mlir_library(NeuraLlvmToNeura
2+
LlvmToNeuraPass.cpp
3+
4+
DEPENDS
5+
NeuraOpsIncGen
6+
NeuraDialectIncGen
7+
NeuraDialect
8+
9+
LINK_LIBS PUBLIC
10+
MLIRArithDialect
11+
MLIRFuncDialect
12+
MLIRLLVMDialect
13+
MLIRIR
14+
MLIRPass
15+
MLIRTransforms
16+
)
17+
18+
target_include_directories(NeuraLlvmToNeura PUBLIC
19+
${CMAKE_BINARY_DIR}/lib/NeuraDialect
20+
${MLIR_INCLUDE_DIRS}
21+
${LLVM_INCLUDE_DIRS}
22+
${CMAKE_SOURCE_DIR}/include
23+
${CMAKE_BINARY_DIR}/include
24+
)
25+
26+
target_compile_definitions(NeuraLlvmToNeura
27+
PRIVATE ${LLVM_DEFINITIONS}
28+
)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "Conversion/LlvmToNeura/LlvmToNeura.h"
2+
#include "NeuraDialect/NeuraDialect.h"
3+
#include "NeuraDialect/NeuraOps.h"
4+
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
5+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
6+
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
7+
#include "mlir/Dialect/Func/IR/FuncOps.h"
8+
#include "mlir/IR/PatternMatch.h"
9+
#include "mlir/Pass/Pass.h"
10+
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
11+
12+
using namespace mlir;
13+
14+
namespace {
15+
struct LlvmAddFOpLowering : public OpRewritePattern<mlir::LLVM::FAddOp> {
16+
using OpRewritePattern::OpRewritePattern;
17+
18+
LogicalResult matchAndRewrite(mlir::LLVM::FAddOp op,
19+
PatternRewriter &rewriter) const override {
20+
rewriter.replaceOpWithNewOp<neura::AddOp>(op, op.getType(), op.getLhs(), op.getRhs());
21+
return success();
22+
}
23+
};
24+
25+
struct LowerLlvmToNeuraPass
26+
: public PassWrapper<LowerLlvmToNeuraPass, OperationPass<func::FuncOp>> {
27+
28+
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LowerLlvmToNeuraPass)
29+
30+
StringRef getArgument() const override { return "lower-llvm-to-neura"; }
31+
StringRef getDescription() const override {
32+
return "Lower LLVM operations to Neura dialect operations";
33+
}
34+
35+
void getDependentDialects(DialectRegistry &registry) const override {
36+
registry.insert<mlir::neura::NeuraDialect>();
37+
}
38+
39+
void runOnOperation() override {
40+
RewritePatternSet patterns(&getContext());
41+
patterns.add<LlvmAddFOpLowering>(&getContext());
42+
if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) {
43+
signalPassFailure();
44+
}
45+
}
46+
};
47+
} // namespace
48+
49+
std::unique_ptr<Pass> mlir::neura::createLowerLlvmToNeuraPass() {
50+
return std::make_unique<LowerLlvmToNeuraPass>();
51+
}

test/neura/llvm_add.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: mlir-neura-opt --lower-llvm-to-neura --insert-mov %s | FileCheck %s
2+
3+
func.func @test(%a: f32) -> f32 {
4+
%b = llvm.mlir.constant(2.0 : f32) : f32
5+
%res = llvm.fadd %a, %b : f32
6+
// CHECK: [[LHS:%.*]] = neura.mov %{{.*}} : f32 -> f32
7+
// CHECK: [[RHS:%.*]] = neura.mov %{{.*}} : f32 -> f32
8+
// CHECK: [[RES:%.*]] = neura.add [[LHS]], [[RHS]] : f32
9+
return %res : f32
10+
}

tools/mlir-neura-opt/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ add_executable(mlir-neura-opt
66

77
# Links MLIR libraries.
88
target_link_libraries(mlir-neura-opt PRIVATE
9-
MLIROptLib # MLIR optimizer library
9+
MLIRDialect # MLIR Dialect
1010
MLIRIR # MLIR Core IR
11+
MLIRLLVMDialect
12+
MLIROptLib # MLIR optimizer library
1113
MLIRSupport # MLIR Support utilities
1214
MLIRTransforms # MLIR transformation passes
13-
MLIRDialect # MLIR Dialect
1415
NeuraDialect # Custom dialect
1516
MLIRFuncDialect # Builtin dialect required by custom dialect
1617
MLIRArithDialect
1718
NeuraArithToNeura
19+
NeuraLlvmToNeura
1820
NeuraTransforms
1921
)
2022

0 commit comments

Comments
 (0)