Skip to content

Commit

Permalink
TranslateToCpp: Emit floating point literals with suffix
Browse files Browse the repository at this point in the history
instead of emitting a double precision literal plus a cast.
This consumers of the emitted code when there are large numbers of floating point
literals.
  • Loading branch information
mgehre-amd committed Mar 14, 2024
1 parent 8bc91d3 commit 52a65bd
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
15 changes: 11 additions & 4 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,17 +1145,16 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
SmallString<128> strValue;
// Use default values of toString except don't truncate zeros.
val.toString(strValue, 0, 0, false);
os << strValue;
switch (llvm::APFloatBase::SemanticsToEnum(val.getSemantics())) {
case llvm::APFloatBase::S_IEEEsingle:
os << "(float)";
os << "f";
break;
case llvm::APFloatBase::S_IEEEdouble:
os << "(double)";
break;
default:
break;
llvm_unreachable("unsupported floating point type");
};
os << strValue;
} else if (val.isNaN()) {
os << "NAN";
} else if (val.isInfinity()) {
Expand All @@ -1167,10 +1166,18 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {

// Print floating point attributes.
if (auto fAttr = dyn_cast<FloatAttr>(attr)) {
if (!fAttr.getType().isF32() && !fAttr.getType().isF64()) {
return emitError(loc,
"expected floating point attribute to be f32 or f64");
}
printFloat(fAttr.getValue());
return success();
}
if (auto dense = dyn_cast<DenseFPElementsAttr>(attr)) {
if (!dense.getElementType().isF32() && !dense.getElementType().isF64()) {
return emitError(loc,
"expected floating point attribute to be f32 or f64");
}
os << '{';
interleaveComma(dense, os, [&](const APFloat &val) { printFloat(val); });
os << '}';
Expand Down
2 changes: 1 addition & 1 deletion mlir/test/Target/Cpp/common-cpp.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func.func @test_multiple_return() -> (i32, i32) {

// CHECK: test_float
func.func @test_float() {
// CHECK: foo::constant({(float)0.0e+00, (float)1.000000000e+00})
// CHECK: foo::constant({0.0e+00f, 1.000000000e+00f})
%0 = emitc.call_opaque "foo::constant"() {args = [dense<[0.000000e+00, 1.000000e+00]> : tensor<2xf32>]} : () -> f32
return
}
Expand Down
8 changes: 4 additions & 4 deletions mlir/test/Target/Cpp/const.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func.func @emitc_constant() {
// CPP-DEFAULT-NEXT: uint8_t [[V4:[^ ]*]] = 255;
// CPP-DEFAULT-NEXT: char [[V5:[^ ]*]] = CHAR_MIN;
// CPP-DEFAULT-NEXT: size_t [[V6:[^ ]*]] = 2;
// CPP-DEFAULT-NEXT: float [[V7:[^ ]*]] = (float)2.000000000e+00;
// CPP-DEFAULT-NEXT: float [[V7:[^ ]*]] = 2.000000000e+00f;
// CPP-DEFAULT-NEXT: Tensor<int32_t> [[V8:[^ ]*]] = {0};
// CPP-DEFAULT-NEXT: Tensor<size_t, 2> [[V9:[^ ]*]] = {0, 1};
// CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};
// CPP-DEFAULT-NEXT: Tensor<float, 2, 2> [[V10:[^ ]*]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};

// CPP-DECLTOP: void emitc_constant() {
// CPP-DECLTOP-NEXT: int32_t [[V0:[^ ]*]];
Expand All @@ -47,7 +47,7 @@ func.func @emitc_constant() {
// CPP-DECLTOP-NEXT: [[V4]] = 255;
// CPP-DECLTOP-NEXT: [[V5]] = CHAR_MIN;
// CPP-DECLTOP-NEXT: [[V6]] = 2;
// CPP-DECLTOP-NEXT: [[V7]] = (float)2.000000000e+00;
// CPP-DECLTOP-NEXT: [[V7]] = 2.000000000e+00f;
// CPP-DECLTOP-NEXT: [[V8]] = {0};
// CPP-DECLTOP-NEXT: [[V9]] = {0, 1};
// CPP-DECLTOP-NEXT: [[V10]] = {(float)0.0e+00, (float)1.000000000e+00, (float)2.000000000e+00, (float)3.000000000e+00};
// CPP-DECLTOP-NEXT: [[V10]] = {0.0e+00f, 1.000000000e+00f, 2.000000000e+00f, 3.000000000e+00f};
4 changes: 2 additions & 2 deletions mlir/test/Target/Cpp/for.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func.func @test_for_yield() {
// CPP-DEFAULT-NEXT: size_t [[STOP:[^ ]*]] = 10;
// CPP-DEFAULT-NEXT: size_t [[STEP:[^ ]*]] = 1;
// CPP-DEFAULT-NEXT: int32_t [[S0:[^ ]*]] = 0;
// CPP-DEFAULT-NEXT: float [[P0:[^ ]*]] = (float)1.000000000e+00;
// CPP-DEFAULT-NEXT: float [[P0:[^ ]*]] = 1.000000000e+00f;
// CPP-DEFAULT-NEXT: int32_t [[SE:[^ ]*]];
// CPP-DEFAULT-NEXT: float [[PE:[^ ]*]];
// CPP-DEFAULT-NEXT: int32_t [[SI:[^ ]*]];
Expand Down Expand Up @@ -96,7 +96,7 @@ func.func @test_for_yield() {
// CPP-DECLTOP-NEXT: [[STOP]] = 10;
// CPP-DECLTOP-NEXT: [[STEP]] = 1;
// CPP-DECLTOP-NEXT: [[S0]] = 0;
// CPP-DECLTOP-NEXT: [[P0]] = (float)1.000000000e+00;
// CPP-DECLTOP-NEXT: [[P0]] = 1.000000000e+00f;
// CPP-DECLTOP-NEXT: ;
// CPP-DECLTOP-NEXT: ;
// CPP-DECLTOP-NEXT: ;
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Target/Cpp/stdops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func.func @two_results() -> (i32, f32) {
}
// CPP-DEFAULT: std::tuple<int32_t, float> two_results() {
// CPP-DEFAULT: int32_t [[V0:[^ ]*]] = 0;
// CPP-DEFAULT: float [[V1:[^ ]*]] = (float)1.000000000e+00;
// CPP-DEFAULT: float [[V1:[^ ]*]] = 1.000000000e+00f;
// CPP-DEFAULT: return std::make_tuple([[V0]], [[V1]]);

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


Expand Down

0 comments on commit 52a65bd

Please sign in to comment.