From eb43351d574f22d4425e4df423f01269189f67e2 Mon Sep 17 00:00:00 2001 From: xiaoweidong Date: Tue, 25 Jun 2024 16:20:10 +0800 Subject: [PATCH] [jit] fix load string len from raw value Change-Id: Ieec2fde8cf6c666f0312540b14a3a42c81961bbb Signed-off-by: xiaoweidong --- .../compiler/number_speculative_retype.cpp | 10 ++++++ test/jittest/BUILD.gn | 1 + test/jittest/ldobjbyname/BUILD.gn | 2 +- test/jittest/ldobjbyname/ldobjbyname.ts | 6 ++-- test/jittest/string_length/BUILD.gn | 18 +++++++++++ test/jittest/string_length/expect_output.txt | 15 +++++++++ test/jittest/string_length/string_length.ts | 31 +++++++++++++++++++ 7 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/jittest/string_length/BUILD.gn create mode 100644 test/jittest/string_length/expect_output.txt create mode 100644 test/jittest/string_length/string_length.ts diff --git a/ecmascript/compiler/number_speculative_retype.cpp b/ecmascript/compiler/number_speculative_retype.cpp index 46db57a06e..44af1e2fe3 100644 --- a/ecmascript/compiler/number_speculative_retype.cpp +++ b/ecmascript/compiler/number_speculative_retype.cpp @@ -1413,6 +1413,16 @@ GateRef NumberSpeculativeRetype::VisitLoadStringLength(GateRef gate) return SetOutputType(gate, GateType::IntType()); } + if (IsConvert()) { + GateRef input = acc_.GetValueIn(gate, 0); + TypeInfo typeInfo = GetOutputTypeInfo(input); + if (typeInfo == TypeInfo::CHAR) { + acc_.ReplaceValueIn(gate, ConvertToTagged(input), 0); + } else if (typeInfo != TypeInfo::TAGGED) { + LOG_COMPILER(FATAL) << "should be tagged type"; + UNREACHABLE(); + } + } return Circuit::NullGate(); } diff --git a/test/jittest/BUILD.gn b/test/jittest/BUILD.gn index 4c91022239..4c0ebc3c35 100644 --- a/test/jittest/BUILD.gn +++ b/test/jittest/BUILD.gn @@ -23,6 +23,7 @@ group("ark_jit_ts_test") { "construct_with_normal_function", "addition_assignment", "catch_with_osr", + "string_length", "ts_inline", ] diff --git a/test/jittest/ldobjbyname/BUILD.gn b/test/jittest/ldobjbyname/BUILD.gn index 7dabd4b020..009ad5db91 100644 --- a/test/jittest/ldobjbyname/BUILD.gn +++ b/test/jittest/ldobjbyname/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# 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 diff --git a/test/jittest/ldobjbyname/ldobjbyname.ts b/test/jittest/ldobjbyname/ldobjbyname.ts index 8fc905aa57..83b28b561b 100644 --- a/test/jittest/ldobjbyname/ldobjbyname.ts +++ b/test/jittest/ldobjbyname/ldobjbyname.ts @@ -14,9 +14,9 @@ */ class Rectangle { - constructor(height, width) { - this.height = height; - this.width = width; + constructor(height, width) { + this.height = height; + this.width = width; } } diff --git a/test/jittest/string_length/BUILD.gn b/test/jittest/string_length/BUILD.gn new file mode 100644 index 0000000000..8cc6bbefca --- /dev/null +++ b/test/jittest/string_length/BUILD.gn @@ -0,0 +1,18 @@ +# 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_jit_test_action("string_length") { + deps = [] +} diff --git a/test/jittest/string_length/expect_output.txt b/test/jittest/string_length/expect_output.txt new file mode 100644 index 0000000000..a5a63e6faf --- /dev/null +++ b/test/jittest/string_length/expect_output.txt @@ -0,0 +1,15 @@ +# 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. + +true +97 diff --git a/test/jittest/string_length/string_length.ts b/test/jittest/string_length/string_length.ts new file mode 100644 index 0000000000..06d4c202dd --- /dev/null +++ b/test/jittest/string_length/string_length.ts @@ -0,0 +1,31 @@ +/* + * 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. + */ + +function f(arg) { + let v = arg[0]; + let c = v.charCodeAt(0); + return v.length > 0 ? c : "."; +} + +let ss = "abcdefg" +for (let i = 0; i < 20; i++) { + f(ss) +} + +ArkTools.jitCompileAsync(f); +let res = ArkTools.waitJitCompileFinish(f); +print(res) + +print(f(ss))