Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ using namespace mlir::neura;

namespace{

struct ArithFMulToNeuraFMul : public OpRewritePattern<mlir::arith::MulFOp> {
using OpRewritePattern::OpRewritePattern;

LogicalResult matchAndRewrite(arith::MulFOp op,
PatternRewriter &rewriter) const override {
Value lhs = op.getLhs();
Value rhs = op.getRhs();
Type resultType = op.getType();

rewriter.replaceOpWithNewOp<neura::FMulOp>(op, resultType, lhs, rhs, Value());
return success();
}
};

struct LowerArithToNeuraPass
: public PassWrapper<LowerArithToNeuraPass, OperationPass<func::FuncOp>> {

Expand All @@ -44,6 +58,7 @@ struct LowerArithToNeuraPass

void runOnOperation() override {
RewritePatternSet patterns(&getContext());
patterns.add<ArithFMulToNeuraFMul>(&getContext());
mlir::neura::arith2neura::populateWithGenerated(patterns);
if (failed(applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)))) {
signalPassFailure();
Expand Down
5 changes: 5 additions & 0 deletions lib/Conversion/LlvmToNeura/LlvmToNeuraPatterns.td
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def : Pat<
(Neura_FSubOp $lhs, $rhs)
>;

def : Pat<
(LLVM_FMulOp $lhs, $rhs, $_fastmath),
(Neura_FMulOp $lhs, $rhs)
>;

def : Pat<
(LLVM_ConstantOp $value),
(Neura_ConstantOp $value)
Expand Down
10 changes: 10 additions & 0 deletions test/neura/interpreter/interpreter.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ module {
return %2 : f32
// CHECK: 7.0
}

func.func @test_mul() -> f32 {
%arg0 = arith.constant 9.0 : f32
%cst = arith.constant 2.0 : f32
%0 = "neura.mov"(%arg0) : (f32) -> f32
%1 = "neura.mov"(%cst) : (f32) -> f32
%2 = "neura.fmul"(%0, %1) : (f32, f32) -> f32
return %2 : f32
// CHECK: 18.0
}
}
42 changes: 42 additions & 0 deletions test/neura/interpreter/multi_ops.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// RUN: mlir-opt %s \
// RUN: --convert-scf-to-cf \
// RUN: --convert-math-to-llvm \
// RUN: --convert-arith-to-llvm \
// RUN: --convert-func-to-llvm \
// RUN: --convert-cf-to-llvm \
// RUN: --reconcile-unrealized-casts \
// RUN: -o %t-lowered-to-llvm.mlir

// RUN: mlir-translate -mlir-to-llvmir \
// RUN: %t-lowered-to-llvm.mlir \
// RUN: -o %t-lower_and_interpreter.ll

// RUN: llc %t-lower_and_interpreter.ll \
// RUN: -filetype=obj -o %t-out.o

// RUN: clang++ main.cpp %t-out.o \
// RUN: -o %t-out.bin

// RUN: %t-out.bin > %t-dumped_output.txt

// RUN: mlir-neura-opt --lower-arith-to-neura --insert-mov %s \
// RUN: -o %t-neura.mlir

// RUN: neura-interpreter %t-neura.mlir >> %t-dumped_output.txt
// RUN: FileCheck %s < %t-dumped_output.txt

// RUN: FileCheck %s -check-prefix=GOLDEN < %t-dumped_output.txt
// GOLDEN: 7.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need change 7.0 to 63.0.


module {
func.func @test() -> f32 attributes { llvm.emit_c_interface }{
%arg0 = arith.constant 9.0 : f32
%cst = arith.constant 2.0 : f32
%0 = arith.subf %arg0, %cst : f32
%1 = arith.mulf %arg0, %0 : f32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to provide the lowering from Arith to Neura. Currently, you only enable LLVM to Neura.

// CHECK: Golden output: [[OUTPUT:[0-9]+\.[0-9]+]]
// CHECK: [neura-interpreter] Output: [[OUTPUT]]
return %1 : f32
}
}

7 changes: 6 additions & 1 deletion tools/neura-interpreter/neura-interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ int main(int argc, char **argv) {
float lhs = valueMap[fsubOp.getLhs()];
float rhs = valueMap[fsubOp.getRhs()];
valueMap[fsubOp.getResult()] = lhs - rhs;
} else if (auto retOp = dyn_cast<func::ReturnOp>(op)) {
} else if (auto fmulOp = dyn_cast<neura::FMulOp>(op)) {
float lhs = valueMap[fmulOp.getLhs()];
float rhs = valueMap[fmulOp.getRhs()];
valueMap[fmulOp.getResult()] = lhs * rhs;
}
else if (auto retOp = dyn_cast<func::ReturnOp>(op)) {
float result = valueMap[retOp.getOperand(0)];
llvm::outs() << "[neura-interpreter] Output: " << llvm::format("%.6f", result) << "\n";
} else {
Expand Down