From 9e9c5bb686a387b60a806905c725c779a79cff1a Mon Sep 17 00:00:00 2001 From: wzyyy-lab Date: Tue, 21 Oct 2025 18:17:12 +0800 Subject: [PATCH 1/3] arith.cpmf to neura.fcmp --- .../ArithToNeura/ArithToNeuraPass.cpp | 24 ++++++++++++++++++- test/arith2neura/cmpf.mlir | 11 +++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 test/arith2neura/cmpf.mlir diff --git a/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp b/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp index dc6f4532..afb9276a 100644 --- a/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp +++ b/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp @@ -57,6 +57,28 @@ struct ArithAddIToNeuraAdd : public OpRewritePattern { } }; +struct ArithCmpFToNeuraFCmp : public OpRewritePattern { + using OpRewritePattern::OpRewritePattern; + + LogicalResult matchAndRewrite(arith::CmpFOp op, + PatternRewriter &rewriter) const override { + + Value lhs = op.getLhs(); + Value rhs = op.getRhs(); + + mlir::arith::CmpFPredicate predicateEnum = op.getPredicate(); + + StringRef predicateStr = arith::stringifyCmpFPredicate(predicateEnum); + StringAttr predicateAttr = rewriter.getStringAttr(predicateStr); // <--- 正确声明和赋值 StringAttr + + // Converts arith CmpFOp to Neura FCmpOp. + rewriter.replaceOpWithNewOp( + op, op.getResult().getType(), lhs, rhs, predicateAttr); // <--- 使用 predicateAttr + + return success(); + } +}; + struct ArithFAddToNeuraFAdd : public OpRewritePattern { using OpRewritePattern::OpRewritePattern; @@ -338,7 +360,7 @@ struct LowerArithToNeuraPass mlir::neura::arith2neura::populateWithGenerated(patterns); patterns.add< ArithFAddToNeuraFAdd, ArithConstantToNeuraConstant, - ArithAddIToNeuraAdd, ArithCmpiToNeuraICmp, ArithSelectToNeuraSel, + ArithAddIToNeuraAdd, ArithCmpiToNeuraICmp, ArithCmpFToNeuraFCmp, ArithSelectToNeuraSel, ArithExtUIToNeuraCast, ArithIndexCastToNeuraCast, ArithFDivToNeuraFDiv, ArithExtfToNeuraCast, ArithMulFToNeuraFMul, ArithSubIToNeuraSub, ArithSubFToNeuraFSub, ArithMulIToNeuraMul, diff --git a/test/arith2neura/cmpf.mlir b/test/arith2neura/cmpf.mlir new file mode 100644 index 00000000..28c3dcfe --- /dev/null +++ b/test/arith2neura/cmpf.mlir @@ -0,0 +1,11 @@ +// RUN: mlir-neura-opt --assign-accelerator --lower-arith-to-neura %s | FileCheck %s --check-prefix=OPT + +// CHECK-LABEL: func.func @test_cmpf( +// OPT: %{{.*}} = "neura.fcmp" +// OPT: cmpType = "ogt" +module { + func.func @test_cmpf(%arg0: f32, %arg1: f32) -> i1 { + %0 = arith.cmpf ogt, %arg0, %arg1 : f32 + return %0 : i1 + } +} \ No newline at end of file From 7372dfc3e120f36862896ec1c6248fef305d5be9 Mon Sep 17 00:00:00 2001 From: ZHUOYU WANG Date: Tue, 11 Nov 2025 16:16:40 +0800 Subject: [PATCH 2/3] Rename variables for consistency in ArithToNeuraPass --- lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp b/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp index afb9276a..cec3f673 100644 --- a/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp +++ b/lib/Conversion/ArithToNeura/ArithToNeuraPass.cpp @@ -66,14 +66,14 @@ struct ArithCmpFToNeuraFCmp : public OpRewritePattern { Value lhs = op.getLhs(); Value rhs = op.getRhs(); - mlir::arith::CmpFPredicate predicateEnum = op.getPredicate(); + mlir::arith::CmpFPredicate predicate_enum = op.getPredicate(); - StringRef predicateStr = arith::stringifyCmpFPredicate(predicateEnum); - StringAttr predicateAttr = rewriter.getStringAttr(predicateStr); // <--- 正确声明和赋值 StringAttr - - // Converts arith CmpFOp to Neura FCmpOp. + StringRef predicate_str = arith::stringifyCmpFPredicate(predicate_enum); + + StringAttr predicate_attr = rewriter.getStringAttr(predicate_str); +// Converts arith CmpFOp to Neura FCmpOp. rewriter.replaceOpWithNewOp( - op, op.getResult().getType(), lhs, rhs, predicateAttr); // <--- 使用 predicateAttr + op, op.getResult().getType(), lhs, rhs, predicate_attr); return success(); } From 196fca41e7cbb6b8d60c1cfcef5ec98d96ef9df6 Mon Sep 17 00:00:00 2001 From: ZHUOYU WANG Date: Tue, 11 Nov 2025 16:19:00 +0800 Subject: [PATCH 3/3] Fix formatting and comments in cmpf.mlir Added missing newline at the end of the file and restored previous comments. --- test/arith2neura/cmpf.mlir | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/arith2neura/cmpf.mlir b/test/arith2neura/cmpf.mlir index 28c3dcfe..27b25eca 100644 --- a/test/arith2neura/cmpf.mlir +++ b/test/arith2neura/cmpf.mlir @@ -1,11 +1,12 @@ -// RUN: mlir-neura-opt --assign-accelerator --lower-arith-to-neura %s | FileCheck %s --check-prefix=OPT - -// CHECK-LABEL: func.func @test_cmpf( -// OPT: %{{.*}} = "neura.fcmp" -// OPT: cmpType = "ogt" module { func.func @test_cmpf(%arg0: f32, %arg1: f32) -> i1 { %0 = arith.cmpf ogt, %arg0, %arg1 : f32 return %0 : i1 } -} \ No newline at end of file +} + +// RUN: mlir-neura-opt --assign-accelerator --lower-arith-to-neura %s | FileCheck %s --check-prefix=OPT + +// CHECK-LABEL: func.func @test_cmpf( +// OPT: %{{.*}} = "neura.fcmp" +// OPT: cmpType = "ogt"