Implemented inlining of Math.Cbrt builtin

* Added tests

Change-Id: I678ee101b377c9b2d52e61a280913852fcd3477e
Signed-off-by: Mikhail Kaskov <kaskov.mikhail@huawei.com>
This commit is contained in:
Mikhail Kaskov 2024-03-13 17:57:31 +08:00 committed by Chernykh Sergey
parent a238795bf7
commit 576e7da305
16 changed files with 251 additions and 1 deletions

View File

@ -42,7 +42,7 @@
V("atan", Atan, 1, MathAtan) /* Math.atan ( x ) */ \
V("atan2", Atan2, 2, MathAtan2) /* Math.atan2 ( y, x ) */ \
V("atanh", Atanh, 1, MathAtanh) /* Math.atanh ( x ) */ \
V("cbrt", Cbrt, 1, INVALID) /* Math.cbrt ( x ) */ \
V("cbrt", Cbrt, 1, MathCbrt) /* Math.cbrt ( x ) */ \
V("ceil", Ceil, 1, INVALID) /* Math.ceil ( x ) */ \
V("clz32", Clz32, 1, INVALID) /* Math.clz32 ( x ) */ \
V("cos", Cos, 1, MathCos) /* Math.cos ( x ) */ \

View File

@ -147,6 +147,7 @@ namespace panda::ecmascript::kungfu {
V(MathSin) \
V(MathSinh) \
V(MathTan) \
V(MathCbrt) \
V(MathTanh) \
V(MathLog) \
V(MathLog2) \
@ -313,6 +314,8 @@ public:
return ConstantIndex::MATH_EXPM1_INDEX;
case BuiltinsStubCSigns::ID::MathPow:
return ConstantIndex::MATH_POW_INDEX;
case BuiltinsStubCSigns::ID::MathCbrt:
return ConstantIndex::MATH_CBRT_INDEX;
case BuiltinsStubCSigns::ID::FLOOR:
return ConstantIndex::MATH_FLOOR_FUNCTION_INDEX;
case BuiltinsStubCSigns::ID::SQRT:
@ -366,6 +369,7 @@ public:
{"Exp", MathExp},
{"Expm1", MathExpm1},
{"sqrt", SQRT},
{"cbrt", MathCbrt},
{"abs", MathAbs},
{"pow", MathPow},
{"floor", FLOOR},

View File

@ -1914,6 +1914,7 @@ DEF_FLOAT_UNARY_CALL_SIGNATURE_BY_NAME(FloatLog)
DEF_FLOAT_UNARY_CALL_SIGNATURE_BY_NAME(FloatLog2)
DEF_FLOAT_UNARY_CALL_SIGNATURE_BY_NAME(FloatLog10)
DEF_FLOAT_UNARY_CALL_SIGNATURE_BY_NAME(FloatLog1p)
DEF_FLOAT_UNARY_CALL_SIGNATURE_BY_NAME(FloatCbrt)
#undef DEF_FLOAT_UNARY_CALL_SIGNATURE_BY_NAME

View File

@ -461,6 +461,7 @@ private:
V(FloatLog1p) \
V(FloatExp) \
V(FloatExpm1) \
V(FloatCbrt) \
V(FloatFloor) \
V(FloatPow) \
V(FindElementWithCache) \

View File

@ -82,6 +82,7 @@ namespace panda::ecmascript::kungfu {
V(MathExpm1, MATH_EXPM1, GateFlags::NO_WRITE, 1, 1, 1) \
V(MathAbs, MATH_ABS, GateFlags::NO_WRITE, 1, 1, 1) \
V(MathPow, MATH_POW, GateFlags::NO_WRITE, 1, 1, 2) \
V(MathCbrt, MATH_CBRT, GateFlags::NO_WRITE, 1, 1, 1) \
MCR_BINARY_GATE_META_DATA_CACHE_LIST(V)
#define MCR_GATE_META_DATA_LIST_WITH_PC_OFFSET(V) \

View File

@ -125,6 +125,9 @@ void NativeInlineLowering::RunNativeInlineLowering()
case BuiltinsStubCSigns::ID::MathPow:
TryInlineMathBinaryBuiltin(gate, argc, id, circuit_->MathPow());
break;
case BuiltinsStubCSigns::ID::MathCbrt:
TryInlineMathUnaryBuiltin(gate, argc, id, circuit_->MathCbrt());
break;
default:
break;
}

View File

@ -153,6 +153,7 @@ GateRef NumberSpeculativeRetype::VisitGate(GateRef gate)
case OpCode::MATH_TAN:
case OpCode::MATH_TANH:
case OpCode::MATH_POW:
case OpCode::MATH_CBRT:
return VisitMathBuiltin(gate);
case OpCode::MATH_ABS:
return VisitMathAbs(gate);

View File

@ -84,6 +84,9 @@ GateRef TypedNativeInlineLowering::VisitGate(GateRef gate)
case OpCode::MATH_POW:
LowerMathPow(gate);
break;
case OpCode::MATH_CBRT:
LowerGeneralUnaryMath(gate, RTSTUB_ID(FloatCbrt));
break;
default:
break;
}

View File

@ -173,6 +173,7 @@ class ObjectFactory;
V(JSTaggedValue, MathExpm1, MATH_EXPM1_INDEX, ecma_roots_special) \
V(JSTaggedValue, MathAbs, MATH_ABS_INDEX, ecma_roots_special) \
V(JSTaggedValue, MathPow, MATH_POW_INDEX, ecma_roots_special) \
V(JSTaggedValue, MathCbrt, MATH_CBRT_INDEX, ecma_roots_special) \
V(JSTaggedValue, MathFloorFunction, MATH_FLOOR_FUNCTION_INDEX, ecma_roots_special) \
V(JSTaggedValue, LocaleCompareFunction, LOCALE_COMPARE_FUNCTION_INDEX, ecma_roots_special) \
V(JSTaggedValue, ArraySortFunction, ARRAY_SORT_FUNCTION_INDEX, ecma_roots_special) \

View File

@ -2950,6 +2950,11 @@ double RuntimeStubs::FloatTanh(double x)
return std::tanh(x);
}
double RuntimeStubs::FloatCbrt(double x)
{
return std::cbrt(x);
}
double RuntimeStubs::FloatFloor(double x)
{
ASSERT(!std::isnan(x));

View File

@ -136,6 +136,7 @@ using FastCallAotEntryType = JSTaggedValue (*)(uintptr_t glue, uint32_t argc, co
V(FloatLog1p) \
V(FloatExp) \
V(FloatExpm1) \
V(FloatCbrt) \
V(FloatFloor) \
V(FloatPow) \
V(FindElementWithCache) \
@ -498,6 +499,7 @@ public:
static double FloatExp(double x);
static double FloatExpm1(double x);
static double FloatPow(double base, double exp);
static double FloatCbrt(double x);
static int32_t FindElementWithCache(uintptr_t argGlue, JSTaggedType hclass,
JSTaggedType key, int32_t num);
static bool StringsAreEquals(EcmaString *str1, EcmaString *str2);

View File

@ -35,6 +35,7 @@ group("ark_aot_builtin_inlining_math_test") {
"Tanh",
"Abs",
"Pow",
"Cbrt",
]
deps = []

View File

@ -0,0 +1,23 @@
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//arkcompiler/ets_runtime/test/test_helper.gni")
host_aot_test_action("builtinMathCbrt") {
is_enable_pgo = true
is_only_typed_path = true
is_enable_opt_inlining = true
is_enable_trace_deopt = true
log_option = " --log-info=trace"
deps = []
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare function print(arg:any):string;
declare interface ArkTools {
isAOTCompiled(args: any): boolean;
}
function replace(a : number)
{
return a;
}
function doCbrt(x: any): number {
return Math.cbrt(x);
}
function printCbrt(x: any) {
try {
print(doCbrt(x));
} finally {
}
}
let res:number = 1;
// Check without params
print(Math.cbrt()); // NaN
// Check with single param
print(Math.cbrt(-0.027)); // -0.3
print(Math.cbrt(0.125)); // 0.5
print(Math.cbrt(1)); // 1
print(Math.cbrt(8)); // 2
print(Math.cbrt(2146689000)); // 1290
print(Math.cbrt(1_0000_0000_0000)); // 10000
print(Math.cbrt(-1_0000_0000_0000)); // -10000
print(Math.cbrt(10e80)); // 1e27
// Check with three param
print(Math.cbrt(64, 4, 6)); // 4
// If n is NaN, +0.0f or -0.0f, return n
res = Math.cbrt(+0.0);
print(res); // 0
print("1/x: " + 1.0/res); // +inf
res = Math.cbrt(-0.0);
print(res); // -0
print("1/x: " + 1.0/res); // -inf
print(Math.cbrt(NaN)); // NaN
// Replace standard builtin
let true_cbrt = Math.cbrt;
Math.cbrt = replace;
print(Math.cbrt(0.001)); // 0.001, no deopt
Math.cbrt = true_cbrt;
printCbrt("abcd"); // NaN
printCbrt("-125"); // -5
printCbrt("abcdef"); // NaN
if (ArkTools.isAOTCompiled(printCbrt)) {
// Replace standard builtin after call to standard builtin was profiled
Math.cbrt = replace
}
printCbrt(-216); // -6; or -216, deopt
printCbrt("abcd"); // NaN; or "abcd", deopt
Math.cbrt = true_cbrt;
// Check IR correctness inside try-block
try {
printCbrt(10); // 2.1544346900318834
printCbrt("abc"); // NaN
} catch (e) {
}
let obj = {};
obj.valueOf = (() => { return -64; })
print(Math.cbrt(obj)); // -8
function Throwing() {
this.value = 100;
}
Throwing.prototype.valueOf = function() {
if (this.value > 999) {
throw new Error("big value")
}
return this.value;
}
let throwingObj = new Throwing();
try {
print(Math.cbrt(throwingObj)); // 4.641588834
throwingObj.value = 1000;
print(Math.cbrt(throwingObj)); // exception
} catch(e) {
print(e);
} finally {
print(Math.cbrt(obj)); // -8
}

View File

@ -0,0 +1,47 @@
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
NaN
-0.29999999999999992
0.49999999999999992
1
2
1290.0000000000002
10000
-10000
1.0000000000000002e+27
4
0
1/x: Infinity
0
1/x: -Infinity
NaN
0.001
[trace] Check Type: NotNumber1
NaN
[trace] Check Type: NotNumber1
-5
[trace] Check Type: NotNumber1
NaN
[trace] Check Type: NotCallTarget1
-216
[trace] Check Type: NotCallTarget1
abcd
2.1544346900318834
[trace] Check Type: NotNumber1
NaN
[trace] Check Type: NotNumber1
-4
4.641588833612778
Error: big value
-4

View File

@ -0,0 +1,40 @@
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
NaN
-0.29999999999999992
0.49999999999999992
1
2
1290.0000000000002
10000
-10000
1.0000000000000002e+27
4
0
1/x: Infinity
0
1/x: -Infinity
NaN
0.001
NaN
-5
NaN
-6.000000000000001
NaN
2.1544346900318834
NaN
-4
4.641588833612778
Error: big value
-4