Skip to content

Commit ed93acc

Browse files
GaoXiangYaGao Xiang
authored andcommitted
[CIR][Lowering] Add MLIR lowering support for CIR sin operations (#586)
This PR add cir.sin lowering to MLIR math dialect. In the future, I will submit a PR to lowering cir.floor, cir.fabs and other operations to MLIR. --------- Co-authored-by: Gao Xiang <gaoxiang@gaoxiang>
1 parent fa677c8 commit ed93acc

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp

+21-8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "mlir/IR/BuiltinTypes.h"
3333
#include "mlir/Pass/Pass.h"
3434
#include "mlir/Pass/PassManager.h"
35+
#include "mlir/Support/LogicalResult.h"
3536
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
3637
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
3738
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
@@ -208,6 +209,18 @@ class CIRCosOpLowering : public mlir::OpConversionPattern<mlir::cir::CosOp> {
208209
}
209210
};
210211

212+
class CIRSinOpLowering : public mlir::OpConversionPattern<mlir::cir::SinOp> {
213+
public:
214+
using mlir::OpConversionPattern<mlir::cir::SinOp>::OpConversionPattern;
215+
216+
mlir::LogicalResult
217+
matchAndRewrite(mlir::cir::SinOp op, OpAdaptor adaptor,
218+
mlir::ConversionPatternRewriter &rewriter) const override {
219+
rewriter.replaceOpWithNewOp<mlir::math::SinOp>(op, adaptor.getSrc());
220+
return mlir::LogicalResult::success();
221+
}
222+
};
223+
211224
class CIRConstantOpLowering
212225
: public mlir::OpConversionPattern<mlir::cir::ConstantOp> {
213226
public:
@@ -987,14 +1000,14 @@ void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
9871000
mlir::TypeConverter &converter) {
9881001
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());
9891002

990-
patterns
991-
.add<CIRCmpOpLowering, CIRCallOpLowering, CIRUnaryOpLowering,
992-
CIRBinOpLowering, CIRLoadOpLowering, CIRConstantOpLowering,
993-
CIRStoreOpLowering, CIRAllocaOpLowering, CIRFuncOpLowering,
994-
CIRScopeOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
995-
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
996-
CIRGetGlobalOpLowering, CIRCastOpLowering, CIRPtrStrideOpLowering>(
997-
converter, patterns.getContext());
1003+
patterns.add<CIRCmpOpLowering, CIRCallOpLowering, CIRUnaryOpLowering,
1004+
CIRBinOpLowering, CIRLoadOpLowering, CIRConstantOpLowering,
1005+
CIRStoreOpLowering, CIRAllocaOpLowering, CIRFuncOpLowering,
1006+
CIRScopeOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
1007+
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
1008+
CIRGetGlobalOpLowering, CIRCastOpLowering,
1009+
CIRPtrStrideOpLowering, CIRSinOpLowering>(converter,
1010+
patterns.getContext());
9981011
}
9991012

10001013
static mlir::TypeConverter prepareTypeConverter() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: cir-opt %s -cir-to-mlir -o %t.mlir
2+
// RUN: FileCheck %s --input-file %t.mlir
3+
4+
module {
5+
cir.func @foo() {
6+
%1 = cir.const #cir.fp<1.0> : !cir.float
7+
%2 = cir.const #cir.fp<1.0> : !cir.double
8+
%3 = cir.const #cir.fp<1.0> : !cir.long_double<!cir.f80>
9+
%4 = cir.const #cir.fp<1.0> : !cir.long_double<!cir.double>
10+
%5 = cir.sin %1 : !cir.float
11+
%6 = cir.sin %2 : !cir.double
12+
%7 = cir.sin %3 : !cir.long_double<!cir.f80>
13+
%8 = cir.sin %4 : !cir.long_double<!cir.double>
14+
cir.return
15+
}
16+
}
17+
18+
// CHECK: module {
19+
// CHECK-NEXT: func.func @foo() {
20+
// CHECK-NEXT: %[[C0:.+]] = arith.constant 1.000000e+00 : f32
21+
// CHECK-NEXT: %[[C1:.+]] = arith.constant 1.000000e+00 : f64
22+
// CHECK-NEXT: %[[C2:.+]] = arith.constant 1.000000e+00 : f80
23+
// CHECK-NEXT: %[[C3:.+]] = arith.constant 1.000000e+00 : f64
24+
// CHECK-NEXT: %{{.+}} = math.sin %[[C0]] : f32
25+
// CHECK-NEXT: %{{.+}} = math.sin %[[C1]] : f64
26+
// CHECK-NEXT: %{{.+}} = math.sin %[[C2]] : f80
27+
// CHECK-NEXT: %{{.+}} = math.sin %[[C3]] : f64
28+
// CHECK-NEXT: return
29+
// CHECK-NEXT: }
30+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)