Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:83529 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27327 invoked from network); 23 Feb 2015 01:59:06 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Feb 2015 01:59:06 -0000 Authentication-Results: pb1.pair.com header.from=zeev@zend.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=zeev@zend.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.213.177 as permitted sender) X-PHP-List-Original-Sender: zeev@zend.com X-Host-Fingerprint: 209.85.213.177 mail-ig0-f177.google.com Received: from [209.85.213.177] ([209.85.213.177:60839] helo=mail-ig0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6D/D5-33016-A698AE45 for ; Sun, 22 Feb 2015 20:59:06 -0500 Received: by mail-ig0-f177.google.com with SMTP id z20so15009147igj.4 for ; Sun, 22 Feb 2015 17:59:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:references:in-reply-to:mime-version :thread-index:date:message-id:subject:to:cc:content-type; bh=eaYaDv2SLvbprIkGrJYD67RI7VYpfisXDjwulhGxgUY=; b=K5sd1kZpwbiCBRUHobGvlmq7QjtzSDL3CHW775P+9jFfAUfzutFF2zengUW7zom9T2 ZRPN+CJgqxcCi+0khuJKcgbz5yAfCRnn1cRql+oZDmyGk53agOXCN2R3KwbeAD4iD3Ct Y/w4qaFhU/o0KyFNh9nmOR3tQmbipEZwnuyngBKS3vs8B4qya3tiKIDwm74goRBZxJGn 1kdfBjUvBu21wlasDjkpqMJ1YG+Y8z4y3HpR98PG/GGxLgjxF7R+MShvi/rr87012GLQ m9MYr+ceFoLJIqdja/YnEla1Iy9R7+sBf+lNS/vkhgu4gvSGbv4uLnUm2prct19NWEfg e3Kw== X-Gm-Message-State: ALoCoQmnP+/6vjm0D2WQnGJ9TbgcaJR3glH2LhL4X31kH9JzzZro35ISYEqjp2skq87YcpuvOkoqklVBKkA+cvW0SOEnGU6ww58FNRiHO+lZdf9JIHiTxNbhHRjhYwntQigscuJ7BEEGm5ASx9UGv+YlnkGNCu0LWg== X-Received: by 10.107.131.83 with SMTP id f80mr11122181iod.50.1424656743357; Sun, 22 Feb 2015 17:59:03 -0800 (PST) References: <2e4694f9805ee81ea0b2c79eab06c2d6@mail.gmail.com> <83921f861c3378dfc6ea34b6681f2edd@mail.gmail.com> <26a5bb62bd37a3f610b2a6c10f84d855@mail.gmail.com> <6d317b6cce0cc1aaded1dae11218234d@mail.gmail.com> <7d58f9b893c257c7289166b31bbdd9ac@mail.gmail.com> In-Reply-To: MIME-Version: 1.0 X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQIDXD7ehmaYKzbAddTs7O1sB4MzmgIw+OLdAfG9oisCHTTwrQKy9+dWAdPf8sIB5yj4egFvLJYPAS37ZyYB8FQrZ5wNepzA Date: Mon, 23 Feb 2015 03:59:02 +0200 Message-ID: To: Anthony Ferrara Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Subject: RE: [PHP-DEV] JIT (was RE: [PHP-DEV] Coercive Scalar Type Hints RFC) From: zeev@zend.com (Zeev Suraski) > -----Original Message----- > From: Anthony Ferrara [mailto:ircmaxell@gmail.com] > Sent: Monday, February 23, 2015 3:21 AM > To: Zeev Suraski > Cc: PHP internals > Subject: Re: [PHP-DEV] JIT (was RE: [PHP-DEV] Coercive Scalar Type Hints > RFC) > > Zeev, > > > >> Partially. > >> > >> The static analysis and compilation would be pure AOT. So the errors > >> would be told to the user when they try to analyze the program, not run > it. > >> Similar > >> to HHVM's hh_client. > > > > How about that then: > > > > 1. The developers runs a static analyzer on the program. > > 2. It fails because the static analyzer detects float being fed to an > > int. > > 3. The user changes the code to convert the input to int. > > 4. You can now optimize the whole flow better, since you know for a > > fact it's an int. > > > > Is that an accurate flow? > > Yes. At least for what I was talking about in this thread. OK. So the code after the fix would look like this: Let me explain how this could play out with coercive type hints: > >> However, there could be a "runtime compiler" which compiles in PHP's > >> compile flow (leveraging opcache, etc). In that case, if the type > >> assertion isn't stable, the function wouldn't be compiled (the > >> external analyzer would error, here it just doesn't compile). Then > >> the code would be run under the Zend engine (and error when called). > > > > Got you. Is it fair to say that if we got to that case, it no longer > > matters what type of type hints we have? > > Once you get to the end, no. Recki-CT proves that. Do you mean that the statement is unfair or that it no longer matters? If it's the former, can you elaborate as to why? > > The difference though is the journey. The static analyzer can reason about > far more code with strict types than it can without (due to the limited > number of possibilities presented at each call). So this leaves the > dilema: > compiled code that behaves slightly differently (what Recki does) or > whether > it always behaves the same. > > >> So think of it as a graph. When you start the type analysis, there's > >> one edge between $input and foo() with type mixed. Looking at foo's > >> argument, you can say that the type of that graph edge must be int. > >> Therefore it becomes an int. Then, when you look at $input, you see > >> that it can be other things, and therefore there are unstable states > >> which can error at runtime. > > > > So when you say it 'must be an int', what you mean is that you assume > > it needs to be an int, and attempt to either prove that or refute > > that. Is that correct? > > If you manage to prove it - you can generate optimal code. > > If you manage to refute that - the static analyzer will emit an error. > > If you can't determine - you defer to runtime. > > > > Is that correct? > > Basically yes. Let me describe here too how it may look with coercive hints. Instead of beginning with the assertion that it must be an int, we make no guess as to what it may be(*). We would use the very same methods you would use to prove or refute that it's an int, to determine whether it's an int. Our ability to deduce that it's an int is going to be identical to your ability to prove that it's an int. If we see that it comes from an int type hint, from an int typed function, etc. - we'd be able to generate the same ultra optimized C-level call. If we manage to deduce that it may be an int or a float, we can still create an ultra-optimized calling code that would deal with just these two cases, or call coerce_to_int(). If we deduce that it's a type that cannot be converted to an int (e.g. array or resource) - we can emit a compile-time error. And if we have no idea what it is, we emit a regular function call. Going back to that (*) from earlier, even if we're unable to deduce what it is, we can actually assume/hope that it'll be an integer and if it is - pass it on directly to the C implementation with a C level function call; And if not, go with the regular function call. The machine code you're left with is pretty much equivalent in case we reached the conclusion that the variable is an integer (which would be roughly in the same cases you're able to prove it that it is). The difference would be that it allows for the non-integer types to be accepted according to the coercion rules, which is a functional difference, not performance difference. Again, I'm not at all saying you can't do the optimizations you're saying you're going to do or already doing. In a way I'm saying the opposite - of course you can do them. We can do them as well with coercive type hints. Thanks, Zeev