Skip to content

Commit 6c7cfbe

Browse files
committed
[flang] Allow derf as alternate spelling for erf
This patch adds derf as an alternate spelling for the erf intrinsic. This spelling is supported by multiple other compilers and used by WRF.
1 parent 457e895 commit 6c7cfbe

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

flang/lib/Evaluate/fold-real.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ Expr<Type<TypeCategory::Real, KIND>> FoldIntrinsicFunction(
153153
auto *intrinsic{std::get_if<SpecificIntrinsic>(&funcRef.proc().u)};
154154
CHECK(intrinsic);
155155
std::string name{intrinsic->name};
156+
if (name == "derf") {
157+
name = "erf";
158+
}
156159
if (name == "acos" || name == "acosh" || name == "asin" || name == "asinh" ||
157160
(name == "atan" && args.size() == 1) || name == "atanh" ||
158161
name == "bessel_j0" || name == "bessel_j1" || name == "bessel_y0" ||

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
403403
{"shift", AnyInt, Rank::dimRemovedOrScalar}, OptionalDIM},
404404
SameType, Rank::conformable, IntrinsicClass::transformationalFunction},
405405
{"dble", {{"a", AnyNumeric, Rank::elementalOrBOZ}}, DoublePrecision},
406+
{"derf", {{"x", SameReal}}, SameReal},
406407
{"digits",
407408
{{"x", AnyIntOrReal, Rank::anyOrAssumedRank, Optionality::required,
408409
common::Intent::In, {ArgFlag::canBeMoldNull}}},

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,11 @@ static constexpr MathOperation mathOperations[] = {
11011101
{"cosh", "ccosh", genFuncType<Ty::Complex<8>, Ty::Complex<8>>, genLibCall},
11021102
{"cosh", RTNAME_STRING(CCoshF128), FuncTypeComplex16Complex16,
11031103
genLibF128Call},
1104+
{"derf", "derff", genFuncType<Ty::Real<4>, Ty::Real<4>>,
1105+
genMathOp<mlir::math::ErfOp>},
1106+
{"derf", "derf", genFuncType<Ty::Real<8>, Ty::Real<8>>,
1107+
genMathOp<mlir::math::ErfOp>},
1108+
{"derf", RTNAME_STRING(ErfF128), FuncTypeReal16Real16, genLibF128Call},
11041109
{"divc",
11051110
{},
11061111
genFuncType<Ty::Complex<2>, Ty::Complex<2>, Ty::Complex<2>>,

flang/test/Evaluate/folding02.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module m
5757
TEST_R4(atanh, atanh(0.8_4), 1.098612308502197265625_4)
5858
TEST_R4(cos, cos(0.5_4), 0.877582550048828125_4)
5959
TEST_R4(cosh, cosh(0.1_4), 1.0050041675567626953125_4)
60+
TEST_R4(derf, derf(1._4), 0.842700779438018798828125_4)
6061
TEST_R4(erf, erf(1._4), 0.842700779438018798828125_4)
6162
TEST_R4(erfc, erfc(0.1_4), 0.887537062168121337890625_4)
6263
TEST_R4(exp, exp(0.1_4), 1.1051709651947021484375_4)
@@ -97,6 +98,8 @@ module m
9798
0.8775825618903727587394314468838274478912353515625_8)
9899
TEST_R8(cosh, cosh(0.1_8), &
99100
1.0050041680558035039894093642942607402801513671875_8)
101+
TEST_R8(derf, derf(1._8), &
102+
0.84270079294971489414223242420121096074581146240234375_8)
100103
TEST_R8(erf, erf(1._8), &
101104
0.84270079294971489414223242420121096074581146240234375_8)
102105
TEST_R8(erfc, erfc(0.1_8), &

flang/test/Intrinsics/math-codegen.fir

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,86 @@ func.func @_QPtest_real8(%arg0: !fir.ref<f64> {fir.bindc_name = "x"}) -> f64 {
862862
func.func private @erff(f32) -> f32
863863
func.func private @erf(f64) -> f64
864864

865+
866+
//--- derf_fast.fir
867+
// RUN: fir-opt %t/derf_fast.fir --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" | FileCheck %t/derf_fast.fir
868+
// CHECK: @_QPtest_real4
869+
// CHECK: {{%[A-Za-z0-9._]+}} = llvm.call @erff({{%[A-Za-z0-9._]+}}) : (f32) -> f32
870+
871+
// CHECK: @_QPtest_real8
872+
// CHECK: {{%[A-Za-z0-9._]+}} = llvm.call @erf({{%[A-Za-z0-9._]+}}) : (f64) -> f64
873+
874+
func.func @_QPtest_real4(%arg0: !fir.ref<f32> {fir.bindc_name = "x"}) -> f32 {
875+
%0 = fir.alloca f32 {bindc_name = "test_real4", uniq_name = "_QFtest_real4Etest_real4"}
876+
%1 = fir.load %arg0 : !fir.ref<f32>
877+
%2 = math.erf %1 : f32
878+
fir.store %2 to %0 : !fir.ref<f32>
879+
%3 = fir.load %0 : !fir.ref<f32>
880+
return %3 : f32
881+
}
882+
func.func @_QPtest_real8(%arg0: !fir.ref<f64> {fir.bindc_name = "x"}) -> f64 {
883+
%0 = fir.alloca f64 {bindc_name = "test_real8", uniq_name = "_QFtest_real8Etest_real8"}
884+
%1 = fir.load %arg0 : !fir.ref<f64>
885+
%2 = math.erf %1 : f64
886+
fir.store %2 to %0 : !fir.ref<f64>
887+
%3 = fir.load %0 : !fir.ref<f64>
888+
return %3 : f64
889+
}
890+
891+
//--- derf_relaxed.fir
892+
// RUN: fir-opt %t/derf_relaxed.fir --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" | FileCheck %t/derf_relaxed.fir
893+
// CHECK: @_QPtest_real4
894+
// CHECK: {{%[A-Za-z0-9._]+}} = llvm.call @erff({{%[A-Za-z0-9._]+}}) : (f32) -> f32
895+
896+
// CHECK: @_QPtest_real8
897+
// CHECK: {{%[A-Za-z0-9._]+}} = llvm.call @erf({{%[A-Za-z0-9._]+}}) : (f64) -> f64
898+
899+
func.func @_QPtest_real4(%arg0: !fir.ref<f32> {fir.bindc_name = "x"}) -> f32 {
900+
%0 = fir.alloca f32 {bindc_name = "test_real4", uniq_name = "_QFtest_real4Etest_real4"}
901+
%1 = fir.load %arg0 : !fir.ref<f32>
902+
%2 = math.erf %1 : f32
903+
fir.store %2 to %0 : !fir.ref<f32>
904+
%3 = fir.load %0 : !fir.ref<f32>
905+
return %3 : f32
906+
}
907+
func.func @_QPtest_real8(%arg0: !fir.ref<f64> {fir.bindc_name = "x"}) -> f64 {
908+
%0 = fir.alloca f64 {bindc_name = "test_real8", uniq_name = "_QFtest_real8Etest_real8"}
909+
%1 = fir.load %arg0 : !fir.ref<f64>
910+
%2 = math.erf %1 : f64
911+
fir.store %2 to %0 : !fir.ref<f64>
912+
%3 = fir.load %0 : !fir.ref<f64>
913+
return %3 : f64
914+
}
915+
916+
//--- derf_precise.fir
917+
// RUN: fir-opt %t/derf_precise.fir --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" | FileCheck %t/derf_precise.fir
918+
// CHECK: @_QPtest_real4
919+
// CHECK: {{%[A-Za-z0-9._]+}} = llvm.call @erff({{%[A-Za-z0-9._]+}}) : (f32) -> f32
920+
921+
// CHECK: @_QPtest_real8
922+
// CHECK: {{%[A-Za-z0-9._]+}} = llvm.call @erf({{%[A-Za-z0-9._]+}}) : (f64) -> f64
923+
924+
func.func @_QPtest_real4(%arg0: !fir.ref<f32> {fir.bindc_name = "x"}) -> f32 {
925+
%0 = fir.alloca f32 {bindc_name = "test_real4", uniq_name = "_QFtest_real4Etest_real4"}
926+
%1 = fir.load %arg0 : !fir.ref<f32>
927+
%2 = fir.call @erff(%1) : (f32) -> f32
928+
fir.store %2 to %0 : !fir.ref<f32>
929+
%3 = fir.load %0 : !fir.ref<f32>
930+
return %3 : f32
931+
}
932+
func.func @_QPtest_real8(%arg0: !fir.ref<f64> {fir.bindc_name = "x"}) -> f64 {
933+
%0 = fir.alloca f64 {bindc_name = "test_real8", uniq_name = "_QFtest_real8Etest_real8"}
934+
%1 = fir.load %arg0 : !fir.ref<f64>
935+
%2 = fir.call @erf(%1) : (f64) -> f64
936+
fir.store %2 to %0 : !fir.ref<f64>
937+
%3 = fir.load %0 : !fir.ref<f64>
938+
return %3 : f64
939+
}
940+
func.func private @erff(f32) -> f32
941+
func.func private @erf(f64) -> f64
942+
943+
944+
865945
//--- exp_fast.fir
866946
// RUN: fir-opt %t/exp_fast.fir --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" | FileCheck %t/exp_fast.fir
867947
// CHECK: @_QPtest_real4

0 commit comments

Comments
 (0)