Skip to content

Commit

Permalink
[CIR][Lowering] Add MLIR lowering support for CIR sin operations (#586)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
2 people authored and lanza committed Nov 3, 2024
1 parent fa677c8 commit ed93acc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
29 changes: 21 additions & 8 deletions clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
Expand Down Expand Up @@ -208,6 +209,18 @@ class CIRCosOpLowering : public mlir::OpConversionPattern<mlir::cir::CosOp> {
}
};

class CIRSinOpLowering : public mlir::OpConversionPattern<mlir::cir::SinOp> {
public:
using mlir::OpConversionPattern<mlir::cir::SinOp>::OpConversionPattern;

mlir::LogicalResult
matchAndRewrite(mlir::cir::SinOp op, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<mlir::math::SinOp>(op, adaptor.getSrc());
return mlir::LogicalResult::success();
}
};

class CIRConstantOpLowering
: public mlir::OpConversionPattern<mlir::cir::ConstantOp> {
public:
Expand Down Expand Up @@ -987,14 +1000,14 @@ void populateCIRToMLIRConversionPatterns(mlir::RewritePatternSet &patterns,
mlir::TypeConverter &converter) {
patterns.add<CIRReturnLowering, CIRBrOpLowering>(patterns.getContext());

patterns
.add<CIRCmpOpLowering, CIRCallOpLowering, CIRUnaryOpLowering,
CIRBinOpLowering, CIRLoadOpLowering, CIRConstantOpLowering,
CIRStoreOpLowering, CIRAllocaOpLowering, CIRFuncOpLowering,
CIRScopeOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
CIRGetGlobalOpLowering, CIRCastOpLowering, CIRPtrStrideOpLowering>(
converter, patterns.getContext());
patterns.add<CIRCmpOpLowering, CIRCallOpLowering, CIRUnaryOpLowering,
CIRBinOpLowering, CIRLoadOpLowering, CIRConstantOpLowering,
CIRStoreOpLowering, CIRAllocaOpLowering, CIRFuncOpLowering,
CIRScopeOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
CIRGetGlobalOpLowering, CIRCastOpLowering,
CIRPtrStrideOpLowering, CIRSinOpLowering>(converter,
patterns.getContext());
}

static mlir::TypeConverter prepareTypeConverter() {
Expand Down
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/sin.cir
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: cir-opt %s -cir-to-mlir -o %t.mlir
// RUN: FileCheck %s --input-file %t.mlir

module {
cir.func @foo() {
%1 = cir.const #cir.fp<1.0> : !cir.float
%2 = cir.const #cir.fp<1.0> : !cir.double
%3 = cir.const #cir.fp<1.0> : !cir.long_double<!cir.f80>
%4 = cir.const #cir.fp<1.0> : !cir.long_double<!cir.double>
%5 = cir.sin %1 : !cir.float
%6 = cir.sin %2 : !cir.double
%7 = cir.sin %3 : !cir.long_double<!cir.f80>
%8 = cir.sin %4 : !cir.long_double<!cir.double>
cir.return
}
}

// CHECK: module {
// CHECK-NEXT: func.func @foo() {
// CHECK-NEXT: %[[C0:.+]] = arith.constant 1.000000e+00 : f32
// CHECK-NEXT: %[[C1:.+]] = arith.constant 1.000000e+00 : f64
// CHECK-NEXT: %[[C2:.+]] = arith.constant 1.000000e+00 : f80
// CHECK-NEXT: %[[C3:.+]] = arith.constant 1.000000e+00 : f64
// CHECK-NEXT: %{{.+}} = math.sin %[[C0]] : f32
// CHECK-NEXT: %{{.+}} = math.sin %[[C1]] : f64
// CHECK-NEXT: %{{.+}} = math.sin %[[C2]] : f80
// CHECK-NEXT: %{{.+}} = math.sin %[[C3]] : f64
// CHECK-NEXT: return
// CHECK-NEXT: }
// CHECK-NEXT: }

0 comments on commit ed93acc

Please sign in to comment.