Skip to content

Commit 8992fd5

Browse files
committed
[MLIR][MathToEmitC] Add language standard option, create LanguageTarget enum class
- Created LanguageTarget enum class in MathToEmitC.h. An enum that specifies the language target for code generation. - Added language standard target option (c99, cpp11): allowing users to choose between C99 and C++11.
1 parent 23ada46 commit 8992fd5

File tree

8 files changed

+128
-86
lines changed

8 files changed

+128
-86
lines changed
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- MathToEmitC.h - Math to EmitCPatterns -------------------*- C++ -*-===//
1+
//===- MathToEmitC.h - Math to EmitC Patterns -------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -9,13 +9,17 @@
99
#ifndef MLIR_CONVERSION_MATHTOEMITC_MATHTOEMITC_H
1010
#define MLIR_CONVERSION_MATHTOEMITC_MATHTOEMITC_H
1111
#include "mlir/Dialect/EmitC/IR/EmitC.h"
12-
1312
namespace mlir {
1413
class RewritePatternSet;
14+
namespace emitc {
15+
16+
/// Enum to specify the language target for EmitC code generation.
17+
enum class LanguageTarget { c99, cpp11 };
18+
19+
} // namespace emitc
1520

16-
void populateConvertMathToEmitCPatterns(
17-
RewritePatternSet &patterns,
18-
emitc::MathToEmitCLanguageTarget languageTarget);
21+
void populateConvertMathToEmitCPatterns(RewritePatternSet &patterns,
22+
emitc::LanguageTarget languageTarget);
1923
} // namespace mlir
2024

2125
#endif // MLIR_CONVERSION_MATHTOEMITC_MATHTOEMITC_H

mlir/include/mlir/Conversion/MathToEmitC/MathToEmitCPass.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#ifndef MLIR_CONVERSION_MATHTOEMITC_MATHTOEMITCPASS_H
1010
#define MLIR_CONVERSION_MATHTOEMITC_MATHTOEMITCPASS_H
1111

12-
#include "mlir/Dialect/EmitC/IR/EmitC.h"
12+
#include "mlir/Conversion/MathToEmitC/MathToEmitC.h"
1313
#include <memory>
1414
namespace mlir {
1515
class Pass;

mlir/include/mlir/Conversion/Passes.td

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,19 +785,19 @@ def ConvertMathToSPIRV : Pass<"convert-math-to-spirv"> {
785785
//===----------------------------------------------------------------------===//
786786

787787
def ConvertMathToEmitC : Pass<"convert-math-to-emitc"> {
788-
let summary = "Convert some Math operations to EmitC Call_opaque";
788+
let summary = "Convert some Math operations to EmitC call_opaque operations";
789789
let description = [{
790-
This pass converts supported Math ops to `opaque_call` ops targeting libc/libm
790+
This pass converts supported Math ops to `call_opaque` ops targeting libc/libm
791791
functions. Unlike convert-math-to-funcs pass, converting to `call_opaque` ops
792792
allows to overload the same function with different argument types.
793793
}];
794794
let dependentDialects = ["emitc::EmitCDialect"];
795795
let options = [
796-
Option<"languageTarget", "language-target", "::mlir::emitc::MathToEmitCLanguageTarget",
797-
/*default=*/"::mlir::emitc::MathToEmitCLanguageTarget::CPP", "Select the language target for callees (C or CPP).",
796+
Option<"languageTarget", "language-target", "::mlir::emitc::LanguageTarget",
797+
/*default=*/"::mlir::emitc::LanguageTarget::c99", "Select the language standard target for callees (c99 or cpp11).",
798798
[{::llvm::cl::values(
799-
clEnumValN(::mlir::emitc::MathToEmitCLanguageTarget::C, "C", "C"),
800-
clEnumValN(::mlir::emitc::MathToEmitCLanguageTarget::CPP, "CPP", "CPP")
799+
clEnumValN(::mlir::emitc::LanguageTarget::c99, "c99", "c99"),
800+
clEnumValN(::mlir::emitc::LanguageTarget::cpp11, "cpp11", "cpp11")
801801
)}]>
802802
];
803803
}

mlir/include/mlir/Dialect/EmitC/IR/EmitCAttributes.td

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,4 @@ def EmitC_OpaqueAttr : EmitC_Attr<"Opaque", "opaque"> {
6262

6363
def EmitC_OpaqueOrTypedAttr : AnyAttrOf<[EmitC_OpaqueAttr, TypedAttrInterface]>;
6464

65-
def MathToEmitCLanguageTarget : I32EnumAttr<"MathToEmitCLanguageTarget",
66-
"Specifies the language target for generating callees.", [
67-
I32EnumAttrCase<"C", 0, "Use C-style function names">,
68-
I32EnumAttrCase<"CPP", 1, "Use C++-style function names">
69-
]> {
70-
let cppNamespace = "::mlir::emitc";
71-
}
72-
73-
7465
#endif // MLIR_DIALECT_EMITC_IR_EMITCATTRIBUTES

mlir/lib/Conversion/MathToEmitC/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ add_mlir_conversion_library(MLIRMathToEmitC
1515
MLIREmitCDialect
1616
MLIRMathDialect
1717
MLIRPass
18-
MLIRTransforms
18+
MLIRTransformUtils
1919
)

mlir/lib/Conversion/MathToEmitC/MathToEmitC.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- MathToEmitC.cpp - Math to EmitC Patterns ----------------*- C++ -*-===//
1+
//===- MathToEmitC.cpp - Math to EmitC Patterns -----------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -18,11 +18,11 @@ namespace {
1818
template <typename OpType>
1919
class LowerToEmitCCallOpaque : public OpRewritePattern<OpType> {
2020
std::string calleeStr;
21-
emitc::MathToEmitCLanguageTarget languageTarget;
21+
emitc::LanguageTarget languageTarget;
2222

2323
public:
2424
LowerToEmitCCallOpaque(MLIRContext *context, std::string calleeStr,
25-
emitc::MathToEmitCLanguageTarget languageTarget)
25+
emitc::LanguageTarget languageTarget)
2626
: OpRewritePattern<OpType>(context), calleeStr(std::move(calleeStr)),
2727
languageTarget(languageTarget) {}
2828

@@ -36,11 +36,12 @@ LogicalResult LowerToEmitCCallOpaque<OpType>::matchAndRewrite(
3636
if (!llvm::all_of(op->getOperandTypes(), llvm::IsaPred<Float32Type, Float64Type>)||
3737
!llvm::all_of(op->getResultTypes(),llvm::IsaPred<Float32Type, Float64Type>))
3838
return rewriter.notifyMatchFailure(
39-
op.getLoc(), "expected all operands and results to be of type f32");
39+
op.getLoc(),
40+
"expected all operands and results to be of type f32 or f64");
4041
std::string modifiedCalleeStr = calleeStr;
41-
if (languageTarget == emitc::MathToEmitCLanguageTarget::CPP) {
42+
if (languageTarget == emitc::LanguageTarget::cpp11) {
4243
modifiedCalleeStr = "std::" + calleeStr;
43-
} else if (languageTarget == emitc::MathToEmitCLanguageTarget::C) {
44+
} else if (languageTarget == emitc::LanguageTarget::c99) {
4445
auto operandType = op->getOperandTypes()[0];
4546
if (operandType.isF32())
4647
modifiedCalleeStr = calleeStr + "f";
@@ -55,8 +56,7 @@ LogicalResult LowerToEmitCCallOpaque<OpType>::matchAndRewrite(
5556
// Populates patterns to replace `math` operations with `emitc.call_opaque`,
5657
// using function names consistent with those in <math.h>.
5758
void mlir::populateConvertMathToEmitCPatterns(
58-
RewritePatternSet &patterns,
59-
emitc::MathToEmitCLanguageTarget languageTarget) {
59+
RewritePatternSet &patterns, emitc::LanguageTarget languageTarget) {
6060
auto *context = patterns.getContext();
6161
patterns.insert<LowerToEmitCCallOpaque<math::FloorOp>>(context, "floor",
6262
languageTarget);

mlir/lib/Conversion/MathToEmitC/MathToEmitCPass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ void ConvertMathToEmitC::runOnOperation() {
4040
ConversionTarget target(getContext());
4141
target.addLegalOp<emitc::CallOpaqueOp>();
4242

43-
target.addIllegalOp<math::FloorOp, math::ExpOp, math::RoundEvenOp,
44-
math::CosOp, math::SinOp, math::Atan2Op, math::CeilOp,
45-
math::AcosOp, math::AsinOp, math::AbsFOp, math::PowFOp>();
43+
target.addIllegalOp<math::FloorOp, math::ExpOp, math::RoundOp, math::CosOp,
44+
math::SinOp, math::Atan2Op, math::CeilOp, math::AcosOp,
45+
math::AsinOp, math::AbsFOp, math::PowFOp>();
4646

4747
RewritePatternSet patterns(&getContext());
4848
populateConvertMathToEmitCPatterns(patterns, languageTarget);
Lines changed: 100 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,112 @@
1-
// RUN: mlir-opt -convert-math-to-emitc=language-target=C %s | FileCheck %s --check-prefix=C
2-
// RUN: mlir-opt -convert-math-to-emitc=language-target=CPP %s | FileCheck %s --check-prefix=CPP
1+
// RUN: mlir-opt -convert-math-to-emitc=language-target=c99 %s | FileCheck %s --check-prefix=c99
2+
// RUN: mlir-opt -convert-math-to-emitc=language-target=cpp11 %s | FileCheck %s --check-prefix=cpp11
33

4-
func.func @absf_to_call_opaque(%arg0: f32) {
5-
// C: emitc.call_opaque "fabsf"
6-
// CPP: emitc.call_opaque "std::fabs"
7-
%1 = math.absf %arg0 : f32
4+
func.func @absf(%arg0: f32, %arg1: f64) {
5+
// c99: emitc.call_opaque "fabsf"
6+
// c99-NEXT: emitc.call_opaque "fabs"
7+
// cpp11: emitc.call_opaque "std::fabs"
8+
// cpp11-NEXT: emitc.call_opaque "std::fabs"
9+
%0 = math.absf %arg0 : f32
10+
%1 = math.absf %arg1 : f64
811
return
9-
}
10-
func.func @floor_to_call_opaque(%arg0: f32) {
11-
// C: emitc.call_opaque "floorf"
12-
// CPP: emitc.call_opaque "std::floor"
13-
%1 = math.floor %arg0 : f32
14-
return
15-
}
16-
func.func @sin_to_call_opaque(%arg0: f32) {
17-
// C: emitc.call_opaque "sinf"
18-
// CPP: emitc.call_opaque "std::sin"
19-
%1 = math.sin %arg0 : f32
12+
}
13+
14+
func.func @floor(%arg0: f32, %arg1: f64) {
15+
// c99: emitc.call_opaque "floorf"
16+
// c99-NEXT: emitc.call_opaque "floor"
17+
// cpp11: emitc.call_opaque "std::floor"
18+
// cpp11-NEXT: emitc.call_opaque "std::floor"
19+
%0 = math.floor %arg0 : f32
20+
%1 = math.floor %arg1 : f64
2021
return
21-
}
22-
func.func @cos_to_call_opaque(%arg0: f32) {
23-
// C: emitc.call_opaque "cosf"
24-
// CPP: emitc.call_opaque "std::cos"
25-
%1 = math.cos %arg0 : f32
22+
}
23+
24+
func.func @sin(%arg0: f32, %arg1: f64) {
25+
// c99: emitc.call_opaque "sinf"
26+
// c99-NEXT: emitc.call_opaque "sin"
27+
// cpp11: emitc.call_opaque "std::sin"
28+
// cpp11-NEXT: emitc.call_opaque "std::sin"
29+
%0 = math.sin %arg0 : f32
30+
%1 = math.sin %arg1 : f64
2631
return
27-
}
28-
func.func @asin_to_call_opaque(%arg0: f32) {
29-
// C: emitc.call_opaque "asinf"
30-
// CPP: emitc.call_opaque "std::asin"
31-
%1 = math.asin %arg0 : f32
32+
}
33+
34+
func.func @cos(%arg0: f32, %arg1: f64) {
35+
// c99: emitc.call_opaque "cosf"
36+
// c99-NEXT: emitc.call_opaque "cos"
37+
// cpp11: emitc.call_opaque "std::cos"
38+
// cpp11-NEXT: emitc.call_opaque "std::cos"
39+
%0 = math.cos %arg0 : f32
40+
%1 = math.cos %arg1 : f64
3241
return
33-
}
34-
func.func @acos_to_call_opaque(%arg0: f64) {
35-
// C: emitc.call_opaque "acos"
36-
// CPP: emitc.call_opaque "std::acos"
37-
%1 = math.acos %arg0 : f64
42+
}
43+
44+
func.func @asin(%arg0: f32, %arg1: f64) {
45+
// c99: emitc.call_opaque "asinf"
46+
// c99-NEXT: emitc.call_opaque "asin"
47+
// cpp11: emitc.call_opaque "std::asin"
48+
// cpp11-NEXT: emitc.call_opaque "std::asin"
49+
%0 = math.asin %arg0 : f32
50+
%1 = math.asin %arg1 : f64
3851
return
39-
}
40-
func.func @atan2_to_call_opaque(%arg0: f64, %arg1: f64) {
41-
// C: emitc.call_opaque "atan2"
42-
// CPP: emitc.call_opaque "std::atan2"
43-
%1 = math.atan2 %arg0, %arg1 : f64
52+
}
53+
54+
func.func @acos(%arg0: f32, %arg1: f64) {
55+
// c99: emitc.call_opaque "acosf"
56+
// c99-NEXT: emitc.call_opaque "acos"
57+
// cpp11: emitc.call_opaque "std::acos"
58+
// cpp11-NEXT: emitc.call_opaque "std::acos"
59+
%0 = math.acos %arg0 : f32
60+
%1 = math.acos %arg1 : f64
4461
return
45-
}
46-
func.func @ceil_to_call_opaque(%arg0: f64) {
47-
// C: emitc.call_opaque "ceil"
48-
// CPP: emitc.call_opaque "std::ceil"
49-
%1 = math.ceil %arg0 : f64
62+
}
63+
64+
func.func @atan2(%arg0: f32, %arg1: f32, %arg2: f64, %arg3: f64) {
65+
// c99: emitc.call_opaque "atan2f"
66+
// c99-NEXT: emitc.call_opaque "atan2"
67+
// cpp11: emitc.call_opaque "std::atan2"
68+
// cpp11-NEXT: emitc.call_opaque "std::atan2"
69+
%0 = math.atan2 %arg0, %arg1 : f32
70+
%1 = math.atan2 %arg2, %arg3 : f64
5071
return
51-
}
52-
func.func @exp_to_call_opaque(%arg0: f64) {
53-
// C: emitc.call_opaque "exp"
54-
// CPP: emitc.call_opaque "std::exp"
55-
%1 = math.exp %arg0 : f64
72+
}
73+
74+
func.func @ceil(%arg0: f32, %arg1: f64) {
75+
// c99: emitc.call_opaque "ceilf"
76+
// c99-NEXT: emitc.call_opaque "ceil"
77+
// cpp11: emitc.call_opaque "std::ceil"
78+
// cpp11-NEXT: emitc.call_opaque "std::ceil"
79+
%0 = math.ceil %arg0 : f32
80+
%1 = math.ceil %arg1 : f64
5681
return
57-
}
58-
func.func @powf_to_call_opaque(%arg0: f64, %arg1: f64) {
59-
// C: emitc.call_opaque "pow"
60-
// CPP: emitc.call_opaque "std::pow"
61-
%1 = math.powf %arg0, %arg1 : f64
82+
}
83+
84+
func.func @exp(%arg0: f32, %arg1: f64) {
85+
// c99: emitc.call_opaque "expf"
86+
// c99-NEXT: emitc.call_opaque "exp"
87+
// cpp11: emitc.call_opaque "std::exp"
88+
// cpp11-NEXT: emitc.call_opaque "std::exp"
89+
%0 = math.exp %arg0 : f32
90+
%1 = math.exp %arg1 : f64
6291
return
63-
}
92+
}
6493

94+
func.func @powf(%arg0: f32, %arg1: f32, %arg2: f64, %arg3: f64) {
95+
// c99: emitc.call_opaque "powf"
96+
// c99-NEXT: emitc.call_opaque "pow"
97+
// cpp11: emitc.call_opaque "std::pow"
98+
// cpp11-NEXT: emitc.call_opaque "std::pow"
99+
%0 = math.powf %arg0, %arg1 : f32
100+
%1 = math.powf %arg2, %arg3 : f64
101+
return
102+
}
65103

104+
func.func @round(%arg0: f32, %arg1: f64) {
105+
// c99: emitc.call_opaque "roundf"
106+
// c99-NEXT: emitc.call_opaque "round"
107+
// cpp11: emitc.call_opaque "std::round"
108+
// cpp11-NEXT: emitc.call_opaque "std::round"
109+
%0 = math.round %arg0 : f32
110+
%1 = math.round %arg1 : f64
111+
return
112+
}

0 commit comments

Comments
 (0)