Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109848 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 55255 invoked from network); 25 Apr 2020 18:21:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 25 Apr 2020 18:21:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AF2591804C7 for ; Sat, 25 Apr 2020 09:53:47 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f195.google.com (mail-lj1-f195.google.com [209.85.208.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 25 Apr 2020 09:53:47 -0700 (PDT) Received: by mail-lj1-f195.google.com with SMTP id a21so13164143ljb.9 for ; Sat, 25 Apr 2020 09:53:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=JfdGs0KvwRfhHGjNMg3djfaMtjjwDT1Rora0/OjWZgo=; b=S2Zo2etxwBdjTy4FzPDM7xwFoJ63w3xMC3/mjpJOGiiUgNKuTZLkwOJvEOdOAFFgXm TFs9KT2MNlEt2eNFBIHH4c2HzFHNSPVKidrH7sZTLP8xeFnbX9zxhaVrY5s5O3oPDooS N8L+ZhlQXYwCc09q2Zu5wz/L9PcKOA4dBtoxMEtlsqz1IJ133Pbt7wZNKff6j9K4BQAj Jd3tMeANxotR+dcWmzte/zv6bj89v22veKJ/Jka1FRWGzihG7BoXOugWmTxpoZoEfvm0 HirIGdp33R43IBo1LHn/yI1mYQIb3czSIkPsYAgteJZj5g2XhuDqv3wLwdtweR0vioTh xWDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=JfdGs0KvwRfhHGjNMg3djfaMtjjwDT1Rora0/OjWZgo=; b=sz0VNxemJnnzwZj7LPi7POjZCSzPbzI+BNuTzyij2FAUNZPzdvJGtYauUbm7aahKsw 94s7DYosIr7dPFmR2MsFsZp2K4QeaAKfieQTTSxL4ZOMFl3W12Vmx+W3iSikeFQj/In6 x+q4s4mcyHYEaVrz/rvfU9KQgf3YsgQn2rCng/kGIPq8bF5NXDSAnHzU2hGCoCmUkwnS W34TBQwtjo6c+8g5p9jZbB/NacqC94fhS6oRdT2JruJeMrtXCueGgybNYhRsxIBpPXc8 w2eFHNb8peRAIvIEAryC2TqR1Vk02z76OD0s8w8aYlF6gdEWnlHXxHCTjFzMdq5HujNa LCRA== X-Gm-Message-State: AGi0PubOHkZW/6Yfy/xoovcXiVUU8ytt1vE79ySZKD9tk6txA+ZhF11K C1U2i6Y8E4ObVORV9BRvAyn1RAUq+iZqvzTWieo= X-Google-Smtp-Source: APiQypLhybz1EePE/WDyW/J4UstXS0K5SY0CkS47zsJay4mUno08Q/VEaFc/CoDBG58crmlzsld9U7tjP6BhKae5jEk= X-Received: by 2002:a2e:85da:: with SMTP id h26mr9156475ljj.260.1587833624165; Sat, 25 Apr 2020 09:53:44 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 25 Apr 2020 18:53:28 +0200 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="00000000000021810405a420530f" Subject: Re: [PHP-DEV] Optimizing assembly for spl_object_id(), fdiv(), etc in the JIT? From: nikita.ppv@gmail.com (Nikita Popov) --00000000000021810405a420530f Content-Type: text/plain; charset="UTF-8" On Sat, Apr 25, 2020 at 6:44 PM tyson andre wrote: > Hi internals, > > I was wondering if the maintainers of the Opcache Jit would be open to > work on optimizing functions such as intdiv(), fdiv(), and spl_object_id() > in the JIT to assembly code when it was safe to do so. > These functions have simple C implementations, so the performance overhead > of calling PHP functions in the generated assembly > would be noticeable compared to emitting optimized assembly. > > I'd expect that it'd be safe to do so under the following circumstances: > > - All arguments are Compiled Variables(CV) in the opcodes (i.e. $var) > ( > http://nikic.github.io/2017/04/14/PHP-7-Virtual-machine.html#variable-types > ) > - The types and count of the function arguments are known to be strictly > correct, e.g. > - Opcache infers they will not throw a TypeError or emit undefined > variable warnings > - maybe strictly accept floats but not integers for fdiv > - The return value of the expression won't throw > (e.g. don't perform this optimization for `$y = spl_object_id($x)` when > a CV is the return value) > - The function in question exists and is not disabled. > > For example, these operands would be seen by the JIT for this sequence of > opcodes > > ```php > /* > 0000 CV0($x) = RECV 1 > 0001 INIT_FCALL 1 96 string("spl_object_id") > 0002 SEND_VAR CV0($x) 1 > 0003 V2 = DO_ICALL > 0004 T1 = INIT_ARRAY 1 (packed) CV0($x) V2 > 0005 RETURN T1 > */ > function create_set(stdClass $x) : array { > return [spl_object_id($x) => $x]; > } > ``` > > I expect it to be technically feasible to check for the sequence of > opcodes INIT_FCALL, SEND_VAR (repeated), and DO_ICALL, in > ext/opcache/jit/zend_jit.c. The resulting assembly would be smaller and > much faster. > (this would be done before emitting any assembly - skip over the opcodes > for INIT_ARRAY and SEND_VAR and DO_ICALL) > > - What do the maintainers of the JIT module think of this idea? > (e.g. are there concerns about this making it harder to understand the > JIT codebase, adding potential bugs, or slowing down the generation of > assembly) > - Are there any general guidelines (or talks/articles) you'd recommend for > new contributors to the Opcache JIT? I couldn't find a README and > https://wiki.php.net/rfc/jit doesn't seem to make recommendations that > apply to my question. > > ``` > // core fdiv C implementation (does not throw for a divisor of 0) > RETURN_DOUBLE(dividend / divisor); > // core of spl_object_id C implementation > RETURN_LONG((zend_long)Z_OBJ_HANDLE_P(obj)); > ``` > > I started looking into this because I work on an application that heavily > uses spl_object_id() > and would see a small performance benefit from this, > and was reminded of this when looking at the get_resource_id() proposal. > ( > https://github.com/phan/phan/search?q=spl_object_id&unscoped_q=spl_object_id > ) > > Thanks, > - Tyson > Hi Tyson, Our general approach to this is to first add a VM opcode for the operation, which will also provide a benefit if the JIT is not used. There's already plenty of those, see ZEND_STRLEN for example. Adding JIT support for the opcode would then be the natural second step. There aren't any hard rules for when this should be done, but I believe having some evidence that the operation is both common and performance critical would be good. Nikita --00000000000021810405a420530f--