Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][Lowering] Add MLIR lowering support for CIR sin operations #586

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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> {
Copy link
Contributor

Choose a reason for hiding this comment

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

May I suggest to introduce a template for the 1:1 lowerings from cir to math ops instead? Here, it would just be used by the existing cos and new sin pattern, but looking at #592, the boilerplate duplication would become quite significant otherwise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right, we can use a template for the 1:1 lowerings from cir to mlir math ops, and we can also use the same template for the 1:1 lowerings from cir to llvm math ops. But the cir.rint cir.shift ops cannot use template, these ops should use the old way.

Copy link
Member

Choose a reason for hiding this comment

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

Instead of using templates I'd rather leverage tablegen to do these. There's a similar idea introduced for LLVM lowering in #434 we could leverage, once that lands (I might commander that PR soon, but happy to let someone else take it) we could do similar for MLIR lowering.

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: }
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Please add the newline.