Skip to content

Commit 5cc2281

Browse files
authored
TranslateToCpp: Emit floating point literals with suffix (#85392)
Emits `2.0e+00f` instead of `(float)2.0e+00`. This helps consumers of the emitted code, especially when there are large numbers of floating point literals, to have a simple AST.
1 parent c07c1c4 commit 5cc2281

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,17 +1171,16 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
11711171
SmallString<128> strValue;
11721172
// Use default values of toString except don't truncate zeros.
11731173
val.toString(strValue, 0, 0, false);
1174+
os << strValue;
11741175
switch (llvm::APFloatBase::SemanticsToEnum(val.getSemantics())) {
11751176
case llvm::APFloatBase::S_IEEEsingle:
1176-
os << "(float)";
1177+
os << "f";
11771178
break;
11781179
case llvm::APFloatBase::S_IEEEdouble:
1179-
os << "(double)";
11801180
break;
11811181
default:
1182-
break;
1182+
llvm_unreachable("unsupported floating point type");
11831183
};
1184-
os << strValue;
11851184
} else if (val.isNaN()) {
11861185
os << "NAN";
11871186
} else if (val.isInfinity()) {
@@ -1193,10 +1192,18 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
11931192

11941193
// Print floating point attributes.
11951194
if (auto fAttr = dyn_cast<FloatAttr>(attr)) {
1195+
if (!isa<Float32Type, Float64Type>(fAttr.getType())) {
1196+
return emitError(loc,
1197+
"expected floating point attribute to be f32 or f64");
1198+
}
11961199
printFloat(fAttr.getValue());
11971200
return success();
11981201
}
11991202
if (auto dense = dyn_cast<DenseFPElementsAttr>(attr)) {
1203+
if (!isa<Float32Type, Float64Type>(dense.getElementType())) {
1204+
return emitError(loc,
1205+
"expected floating point attribute to be f32 or f64");
1206+
}
12001207
os << '{';
12011208
interleaveComma(dense, os, [&](const APFloat &val) { printFloat(val); });
12021209
os << '}';

mlir/test/Target/Cpp/common-cpp.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func.func @test_multiple_return() -> (i32, i32) {
3636

3737
// CHECK: test_float
3838
func.func @test_float() {
39-
// CHECK: foo::constant({(float)0.0e+00, (float)1.000000000e+00})
39+
// CHECK: foo::constant({0.0e+00f, 1.000000000e+00f})
4040
%0 = emitc.call_opaque "foo::constant"() {args = [dense<[0.000000e+00, 1.000000e+00]> : tensor<2xf32>]} : () -> f32
4141
return
4242
}

mlir/test/Target/Cpp/const.mlir

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ func.func @emitc_constant() {
1010
%c5 = "emitc.constant"(){value = #emitc.opaque<"CHAR_MIN">} : () -> !emitc.opaque<"char">
1111
%c6 = "emitc.constant"(){value = 2 : index} : () -> index
1212
%c7 = "emitc.constant"(){value = 2.0 : f32} : () -> f32
13+
%f64 = "emitc.constant"(){value = 4.0 : f64} : () -> f64
1314
%c8 = "emitc.constant"(){value = dense<0> : tensor<i32>} : () -> tensor<i32>
1415
%c9 = "emitc.constant"(){value = dense<[0, 1]> : tensor<2xindex>} : () -> tensor<2xindex>
1516
%c10 = "emitc.constant"(){value = dense<[[0.0, 1.0], [2.0, 3.0]]> : tensor<2x2xf32>} : () -> tensor<2x2xf32>
@@ -23,10 +24,11 @@ func.func @emitc_constant() {
2324
// CPP-DEFAULT-NEXT: uint8_t [[V4:[^ ]*]] = 255;
2425
// CPP-DEFAULT-NEXT: char [[V5:[^ ]*]] = CHAR_MIN;
2526
// CPP-DEFAULT-NEXT: size_t [[V6:[^ ]*]] = 2;
26-
// CPP-DEFAULT-NEXT: float [[V7:[^ ]*]] = (float)2.000000000e+00;
27+
// CPP-DEFAULT-NEXT: float [[V7:[^ ]*]] = 2.000000000e+00f;
28+
// CPP-DEFAULT-NEXT: double [[F64:[^ ]*]] = 4.00000000000000000e+00;
2729
// CPP-DEFAULT-NEXT: Tensor<int32_t> [[V8:[^ ]*]] = {0};
2830
// CPP-DEFAULT-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]] = {0, 1};
29-
// CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};
31+
// CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
3032

3133
// CPP-DECLTOP: void emitc_constant() {
3234
// CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
@@ -37,6 +39,7 @@ func.func @emitc_constant() {
3739
// CPP-DECLTOP-NEXT: char [[V5:[^ ]*]];
3840
// CPP-DECLTOP-NEXT: size_t [[V6:[^ ]*]];
3941
// CPP-DECLTOP-NEXT: float [[V7:[^ ]*]];
42+
// CPP-DECLTOP-NEXT: double [[F64:[^ ]*]];
4043
// CPP-DECLTOP-NEXT: Tensor<int32_t> [[V8:[^ ]*]];
4144
// CPP-DECLTOP-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]];
4245
// CPP-DECLTOP-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]];
@@ -47,7 +50,8 @@ func.func @emitc_constant() {
4750
// CPP-DECLTOP-NEXT: [[V4]] = 255;
4851
// CPP-DECLTOP-NEXT: [[V5]] = CHAR_MIN;
4952
// CPP-DECLTOP-NEXT: [[V6]] = 2;
50-
// CPP-DECLTOP-NEXT: [[V7]] = (float)2.000000000e+00;
53+
// CPP-DECLTOP-NEXT: [[V7]] = 2.000000000e+00f;
54+
// CPP-DECLTOP-NEXT: [[F64]] = 4.00000000000000000e+00;
5155
// CPP-DECLTOP-NEXT: [[V8]] = {0};
5256
// CPP-DECLTOP-NEXT: [[V9]] = {0, 1};
53-
// CPP-DECLTOP-NEXT: [[V10]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};
57+
// CPP-DECLTOP-NEXT: [[V10]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};

mlir/test/Target/Cpp/for.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func.func @test_for_yield() {
6363
// CPP-DEFAULT-NEXT: size_t [[STOP:[^ ]*]] = 10;
6464
// CPP-DEFAULT-NEXT: size_t [[STEP:[^ ]*]] = 1;
6565
// CPP-DEFAULT-NEXT: int32_t [[S0:[^ ]*]] = 0;
66-
// CPP-DEFAULT-NEXT: float [[P0:[^ ]*]] = (float)1.000000000e+00;
66+
// CPP-DEFAULT-NEXT: float [[P0:[^ ]*]] = 1.000000000e+00f;
6767
// CPP-DEFAULT-NEXT: int32_t [[SE:[^ ]*]];
6868
// CPP-DEFAULT-NEXT: float [[PE:[^ ]*]];
6969
// CPP-DEFAULT-NEXT: int32_t [[SI:[^ ]*]];
@@ -96,7 +96,7 @@ func.func @test_for_yield() {
9696
// CPP-DECLTOP-NEXT: [[STOP]] = 10;
9797
// CPP-DECLTOP-NEXT: [[STEP]] = 1;
9898
// CPP-DECLTOP-NEXT: [[S0]] = 0;
99-
// CPP-DECLTOP-NEXT: [[P0]] = (float)1.000000000e+00;
99+
// CPP-DECLTOP-NEXT: [[P0]] = 1.000000000e+00f;
100100
// CPP-DECLTOP-NEXT: ;
101101
// CPP-DECLTOP-NEXT: ;
102102
// CPP-DECLTOP-NEXT: ;

mlir/test/Target/Cpp/stdops.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ func.func @two_results() -> (i32, f32) {
6060
}
6161
// CPP-DEFAULT: std::tuple<int32_t, float> two_results() {
6262
// CPP-DEFAULT: int32_t [[V0:[^ ]*]] = 0;
63-
// CPP-DEFAULT: float [[V1:[^ ]*]] = (float)1.000000000e+00;
63+
// CPP-DEFAULT: float [[V1:[^ ]*]] = 1.000000000e+00f;
6464
// CPP-DEFAULT: return std::make_tuple([[V0]], [[V1]]);
6565

6666
// CPP-DECLTOP: std::tuple<int32_t, float> two_results() {
6767
// CPP-DECLTOP: int32_t [[V0:[^ ]*]];
6868
// CPP-DECLTOP: float [[V1:[^ ]*]];
6969
// CPP-DECLTOP: [[V0]] = 0;
70-
// CPP-DECLTOP: [[V1]] = (float)1.000000000e+00;
70+
// CPP-DECLTOP: [[V1]] = 1.000000000e+00f;
7171
// CPP-DECLTOP: return std::make_tuple([[V0]], [[V1]]);
7272

7373

0 commit comments

Comments
 (0)