How enter this optimization path?

28 views
Skip to first unread message

zljj...@gmail.com

unread,
Oct 10, 2023, 6:52:46 AM10/10/23
to v8-dev
Hi, 
How to enter this optimaization path: JSAdd(x:string, y) => JSAdd(x, JSToString(y)) or 
JSAdd(x, y:string) => JSAdd(JSToString(x), y)  ?  The code below cannot enter this optimazation path, for input type is unknow.

function add2(a, b) {
  return a + b;
}
// should create feedback vector at first, or type info can not collect.
%PrepareFunctionForOptimization(add2);
console.log(add2(1, "hello"));
%OptimizeFunctionOnNextCall(add2);
console.log(add2(2, "world"));

Leszek Swirski

unread,
Oct 10, 2023, 7:50:32 AM10/10/23
to v8-...@googlegroups.com
Hi,

Most likely there would need to be some type inference inferring that one of the inputs is a string, like an operation with known string result, e.g.

function add2(a, b) {
  let x = a + "string";
  return x + b; // This would become JSAdd(x, JSToString(b)) 
}

Hope that helps,
Leszek

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/ba5e9972-14ce-414b-95c3-55e915b508b9n%40googlegroups.com.

dmerc...@google.com

unread,
Oct 10, 2023, 8:23:16 AM10/10/23
to v8-dev
Hi,

Interestingly, even

    function Foo(s) {
      return s + "42";
    }

Is enough to reach this case.
Basically, as Leszek said, this happens when we have generic JSAdd and we can infer that one of the inputs is a String.

In your example ("a + b"), we don't go into this path because we cannot infer the types of the inputs (the inputs are just Parameter[1] and Parameter[2], and we don't have feedback for parameters, just for operations). Still, because we have String feedback on the JSAdd, we decide a few lines later that we'll check that the inputs are string and we'll emit a StringConcat: https://chromium.googlesource.com/v8/v8.git/+/refs/tags/12.0.52/src/compiler/js-typed-lowering.cc#581.

Cheers,
Darius

liang zhou

unread,
Oct 10, 2023, 9:50:24 AM10/10/23
to v8-...@googlegroups.com
Very detailed, thanks a lot
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/ba3d873b-7882-4a29-9984-448f224e71afn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages