Skip to content
Merged
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
143 changes: 135 additions & 8 deletions clang/lib/CIR/Lowering/ThroughMLIR/LowerCIRToMLIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,129 @@ class CIRCosOpLowering : public mlir::OpConversionPattern<mlir::cir::CosOp> {
}
};

class CIRSqrtOpLowering : public mlir::OpConversionPattern<mlir::cir::SqrtOp> {
public:
using mlir::OpConversionPattern<mlir::cir::SqrtOp>::OpConversionPattern;

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

class CIRFAbsOpLowering : public mlir::OpConversionPattern<mlir::cir::FAbsOp> {
public:
using mlir::OpConversionPattern<mlir::cir::FAbsOp>::OpConversionPattern;

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

class CIRFloorOpLowering
: public mlir::OpConversionPattern<mlir::cir::FloorOp> {
public:
using mlir::OpConversionPattern<mlir::cir::FloorOp>::OpConversionPattern;

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

class CIRCeilOpLowering : public mlir::OpConversionPattern<mlir::cir::CeilOp> {
public:
using mlir::OpConversionPattern<mlir::cir::CeilOp>::OpConversionPattern;

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

class CIRLog10OpLowering
: public mlir::OpConversionPattern<mlir::cir::Log10Op> {
public:
using mlir::OpConversionPattern<mlir::cir::Log10Op>::OpConversionPattern;

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

class CIRLogOpLowering : public mlir::OpConversionPattern<mlir::cir::LogOp> {
public:
using mlir::OpConversionPattern<mlir::cir::LogOp>::OpConversionPattern;

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

class CIRLog2OpLowering : public mlir::OpConversionPattern<mlir::cir::Log2Op> {
public:
using mlir::OpConversionPattern<mlir::cir::Log2Op>::OpConversionPattern;

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

class CIRRoundOpLowering
: public mlir::OpConversionPattern<mlir::cir::RoundOp> {
public:
using mlir::OpConversionPattern<mlir::cir::RoundOp>::OpConversionPattern;

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

class CIRExpOpLowering : public mlir::OpConversionPattern<mlir::cir::ExpOp> {
public:
using mlir::OpConversionPattern<mlir::cir::ExpOp>::OpConversionPattern;

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

class CIRExp2OpLowering : public mlir::OpConversionPattern<mlir::cir::Exp2Op> {
public:
using mlir::OpConversionPattern<mlir::cir::Exp2Op>::OpConversionPattern;

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

class CIRSinOpLowering : public mlir::OpConversionPattern<mlir::cir::SinOp> {
public:
using mlir::OpConversionPattern<mlir::cir::SinOp>::OpConversionPattern;
Expand Down Expand Up @@ -1000,14 +1123,18 @@ 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, CIRSinOpLowering>(converter,
patterns.getContext());
patterns
.add<CIRCmpOpLowering, CIRCallOpLowering, CIRUnaryOpLowering,
CIRBinOpLowering, CIRLoadOpLowering, CIRConstantOpLowering,
CIRStoreOpLowering, CIRAllocaOpLowering, CIRFuncOpLowering,
CIRScopeOpLowering, CIRBrCondOpLowering, CIRTernaryOpLowering,
CIRYieldOpLowering, CIRCosOpLowering, CIRGlobalOpLowering,
CIRGetGlobalOpLowering, CIRCastOpLowering, CIRPtrStrideOpLowering,
CIRSqrtOpLowering, CIRCeilOpLowering, CIRExp2OpLowering,
CIRExpOpLowering, CIRFAbsOpLowering, CIRFloorOpLowering,
CIRLog10OpLowering, CIRLog2OpLowering, CIRLogOpLowering,
CIRRoundOpLowering, CIRPtrStrideOpLowering, CIRSinOpLowering>(
converter, patterns.getContext());
}

static mlir::TypeConverter prepareTypeConverter() {
Expand Down
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/ceil.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() {
%0 = cir.const #cir.fp<1.31> : !cir.float
%1 = cir.const #cir.fp<3.0> : !cir.long_double<!cir.f80>
%2 = cir.const #cir.fp<2.73> : !cir.double
%3 = cir.const #cir.fp<4.67> : !cir.long_double<!cir.double>
%4 = cir.ceil %0 : !cir.float
%5 = cir.ceil %1 : !cir.long_double<!cir.f80>
%6 = cir.ceil %2 : !cir.double
%7 = cir.ceil %3 : !cir.long_double<!cir.double>
cir.return
}
}

// CHECK: module {
// CHECK-NEXT: func.func @foo() {
// CHECK-NEXT: %[[C0:.+]] = arith.constant 1.310000e+00 : f32
// CHECK-NEXT: %[[C1:.+]] = arith.constant 3.000000e+00 : f80
// CHECK-NEXT: %[[C2:.+]] = arith.constant 2.730000e+00 : f64
// CHECK-NEXT: %[[C3:.+]] = arith.constant 4.670000e+00 : f64
// CHECK-NEXT: %{{.+}} = math.ceil %[[C0]] : f32
// CHECK-NEXT: %{{.+}} = math.ceil %[[C1]] : f80
// CHECK-NEXT: %{{.+}} = math.ceil %[[C2]] : f64
// CHECK-NEXT: %{{.+}} = math.ceil %[[C3]] : f64
// CHECK-NEXT: return
// CHECK-NEXT: }
// CHECK-NEXT: }
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/exp.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() {
%0 = cir.const #cir.fp<1.0> : !cir.float
%1 = cir.const #cir.fp<3.0> : !cir.long_double<!cir.f80>
%2 = cir.const #cir.fp<2.0> : !cir.double
%3 = cir.const #cir.fp<4.00> : !cir.long_double<!cir.double>
%4 = cir.exp %0 : !cir.float
%5 = cir.exp %1 : !cir.long_double<!cir.f80>
%6 = cir.exp2 %2 : !cir.double
%7 = cir.exp2 %3 : !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 3.000000e+00 : f80
// CHECK-NEXT: %[[C2:.+]] = arith.constant 2.000000e+00 : f64
// CHECK-NEXT: %[[C3:.+]] = arith.constant 4.000000e+00 : f64
// CHECK-NEXT: %{{.+}} = math.exp %[[C0]] : f32
// CHECK-NEXT: %{{.+}} = math.exp %[[C1]] : f80
// CHECK-NEXT: %{{.+}} = math.exp2 %[[C2]] : f64
// CHECK-NEXT: %{{.+}} = math.exp2 %[[C3]] : f64
// CHECK-NEXT: return
// CHECK-NEXT: }
// CHECK-NEXT: }
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/fabs.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() {
%0 = cir.const #cir.fp<-1.0> : !cir.float
%1 = cir.const #cir.fp<-3.0> : !cir.long_double<!cir.f80>
%2 = cir.const #cir.fp<-2.0> : !cir.double
%3 = cir.const #cir.fp<-4.00> : !cir.long_double<!cir.double>
%4 = cir.fabs %0 : !cir.float
%5 = cir.fabs %1 : !cir.long_double<!cir.f80>
%6 = cir.fabs %2 : !cir.double
%7 = cir.fabs %3 : !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 -3.000000e+00 : f80
// CHECK-NEXT: %[[C2:.+]] = arith.constant -2.000000e+00 : f64
// CHECK-NEXT: %[[C3:.+]] = arith.constant -4.000000e+00 : f64
// CHECK-NEXT: %{{.+}} = math.absf %[[C0]] : f32
// CHECK-NEXT: %{{.+}} = math.absf %[[C1]] : f80
// CHECK-NEXT: %{{.+}} = math.absf %[[C2]] : f64
// CHECK-NEXT: %{{.+}} = math.absf %[[C3]] : f64
// CHECK-NEXT: return
// CHECK-NEXT: }
// CHECK-NEXT: }
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/floor.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() {
%0 = cir.const #cir.fp<1.51> : !cir.float
%1 = cir.const #cir.fp<3.0> : !cir.long_double<!cir.f80>
%2 = cir.const #cir.fp<2.73> : !cir.double
%3 = cir.const #cir.fp<4.67> : !cir.long_double<!cir.double>
%4 = cir.floor %0 : !cir.float
%5 = cir.floor %1 : !cir.long_double<!cir.f80>
%6 = cir.floor %2 : !cir.double
%7 = cir.floor %3 : !cir.long_double<!cir.double>
cir.return
}
}

// CHECK: module {
// CHECK-NEXT: func.func @foo() {
// CHECK-NEXT: %[[C0:.+]] = arith.constant 1.510000e+00 : f32
// CHECK-NEXT: %[[C1:.+]] = arith.constant 3.000000e+00 : f80
// CHECK-NEXT: %[[C2:.+]] = arith.constant 2.730000e+00 : f64
// CHECK-NEXT: %[[C3:.+]] = arith.constant 4.670000e+00 : f64
// CHECK-NEXT: %{{.+}} = math.floor %[[C0]] : f32
// CHECK-NEXT: %{{.+}} = math.floor %[[C1]] : f80
// CHECK-NEXT: %{{.+}} = math.floor %[[C2]] : f64
// CHECK-NEXT: %{{.+}} = math.floor %[[C3]] : f64
// CHECK-NEXT: return
// CHECK-NEXT: }
// CHECK-NEXT: }
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/log.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() {
%0 = cir.const #cir.fp<1.0> : !cir.float
%1 = cir.const #cir.fp<3.0> : !cir.long_double<!cir.f80>
%2 = cir.const #cir.fp<2.0> : !cir.double
%3 = cir.const #cir.fp<4.0> : !cir.long_double<!cir.double>
%4 = cir.log %0 : !cir.float
%5 = cir.log %1 : !cir.long_double<!cir.f80>
%6 = cir.log2 %2 : !cir.double
%7 = cir.log10 %3 : !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 3.000000e+00 : f80
// CHECK-NEXT: %[[C2:.+]] = arith.constant 2.000000e+00 : f64
// CHECK-NEXT: %[[C3:.+]] = arith.constant 4.000000e+00 : f64
// CHECK-NEXT: %{{.+}} = math.log %[[C0]] : f32
// CHECK-NEXT: %{{.+}} = math.log %[[C1]] : f80
// CHECK-NEXT: %{{.+}} = math.log2 %[[C2]] : f64
// CHECK-NEXT: %{{.+}} = math.log10 %[[C3]] : f64
// CHECK-NEXT: return
// CHECK-NEXT: }
// CHECK-NEXT: }
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/round.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() {
%0 = cir.const #cir.fp<1.31> : !cir.float
%1 = cir.const #cir.fp<3.0> : !cir.long_double<!cir.f80>
%2 = cir.const #cir.fp<2.73> : !cir.double
%3 = cir.const #cir.fp<4.67> : !cir.long_double<!cir.double>
%4 = cir.round %0 : !cir.float
%5 = cir.round %1 : !cir.long_double<!cir.f80>
%6 = cir.round %2 : !cir.double
%7 = cir.round %3 : !cir.long_double<!cir.double>
cir.return
}
}

// CHECK: module {
// CHECK-NEXT: func.func @foo() {
// CHECK-NEXT: %[[C0:.+]] = arith.constant 1.310000e+00 : f32
// CHECK-NEXT: %[[C1:.+]] = arith.constant 3.000000e+00 : f80
// CHECK-NEXT: %[[C2:.+]] = arith.constant 2.730000e+00 : f64
// CHECK-NEXT: %[[C3:.+]] = arith.constant 4.670000e+00 : f64
// CHECK-NEXT: %{{.+}} = math.round %[[C0]] : f32
// CHECK-NEXT: %{{.+}} = math.round %[[C1]] : f80
// CHECK-NEXT: %{{.+}} = math.round %[[C2]] : f64
// CHECK-NEXT: %{{.+}} = math.round %[[C3]] : f64
// CHECK-NEXT: return
// CHECK-NEXT: }
// CHECK-NEXT: }
30 changes: 30 additions & 0 deletions clang/test/CIR/Lowering/ThroughMLIR/sqrt.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() {
%0 = cir.const #cir.fp<9.0> : !cir.float
%1 = cir.const #cir.fp<100.0> : !cir.long_double<!cir.f80>
%2 = cir.const #cir.fp<1.0> : !cir.double
%3 = cir.const #cir.fp<2.56> : !cir.long_double<!cir.double>
%4 = cir.sqrt %0 : !cir.float
%5 = cir.sqrt %1 : !cir.long_double<!cir.f80>
%6 = cir.sqrt %2 : !cir.double
%7 = cir.sqrt %3 : !cir.long_double<!cir.double>
cir.return
}
}

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