From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 0BD103885528 for ; Mon, 20 Feb 2023 09:53:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0BD103885528 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-il1-x12c.google.com with SMTP id l2so882432iln.10 for ; Mon, 20 Feb 2023 01:53:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=UyuC1Xa4raA+KkeMbaFgT8+ICx1VNF2OfgXklSur828=; b=XqgVP4cN71vTMHpD828mBkD5ktJ+zYo3exU90KHW8ghRaiWZDSNYdHd42JvJBfTN83 FiFZN4J+hXLo0hMAD/qvqUPqkqFpity9x5EkhrwsG0/OpROU9xWOSEwsI5PFpGyn5PTe EHdmTijkVzgDNk1GPRNW7HIIWoYcW4qVMnOLtitRvdm6UBeHKVc9Ifj/I8ycRonJg5mg U7bBGq1a93cpIf6Dg40R+WBc/aRzCqWssTYAvwD4lyqDNjrjREOhborzNOIDVLR4LmeQ ZwHrKPYubzorTdrgky3C+OLAjkb8mhMBRGhgF7xxqKYIuHEfH7qUpgzLPKLy6R0/VFCg xknQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=UyuC1Xa4raA+KkeMbaFgT8+ICx1VNF2OfgXklSur828=; b=tsKqScrySFf3yMvkM40mkcgmC95Q4zKLQy2Mvh9p7Oe7AOwgifvq2JCovTW8a/MuY+ TFgRK5TrlN7BWaQUNIuYHGyau6EGGxWarzPHzhZ2zJjJ6H76ySOkGroeAaa/60y9ANMm tWwwFwrCvQ4ciS2vzx1/UVMkjBsXkfpZ9aL/kOSs0GqpgTsWG09zI8WkskMfnHA7ewYK co0Vx6GSwifHIJRdBbKo57KZBNRbyXXhqmIPS1C8UkdwYpfAXPmDjdgJGp/W20v0YK6B 8r1R9p0YfvNMaYM7pkR1MLxzY2MBggjbV3zp5O+v7rUi9rWIwnZOTtartoA0nNeIMgZe pxJA== X-Gm-Message-State: AO0yUKWyDDDAFHFh4kjFA0rOvYHBY6stOH3KtYhq4Xi/gi+y3aN3Prl+ ECGYXkMfKen4RSrfXhRc7npXuf9fQY8kUFgG1BevOdzGbF5y9Cjvt2zCN0mYNuNSp6StUKGkmtV aWMmH4mTLOBR8WL+AIVoyOxUtIcFCFuStY96yq3hHc7kApLNn3xxaEybPejQrD5pwfWYzU9g= X-Google-Smtp-Source: AK7set9ng7XRlYysy45rghTSoBr/WlV3FeNtdAE96Yx8VB9qOxo+kAQWMkif47CjtJpaCncHx2AcnA== X-Received: by 2002:a92:1a49:0:b0:315:33ef:f91e with SMTP id z9-20020a921a49000000b0031533eff91emr307099ill.26.1676856475888; Sun, 19 Feb 2023 17:27:55 -0800 (PST) Received: from mail-io1-f44.google.com (mail-io1-f44.google.com. [209.85.166.44]) by smtp.gmail.com with ESMTPSA id h5-20020a056e021d8500b00313b281ecd2sm527240ila.70.2023.02.19.17.27.55 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Feb 2023 17:27:55 -0800 (PST) Received: by mail-io1-f44.google.com with SMTP id l16so630736iok.4 for ; Sun, 19 Feb 2023 17:27:55 -0800 (PST) X-Received: by 2002:a02:9564:0:b0:3a7:e46f:83d1 with SMTP id y91-20020a029564000000b003a7e46f83d1mr336342jah.3.1676856474867; Sun, 19 Feb 2023 17:27:54 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Andrew Waterman Date: Sun, 19 Feb 2023 17:27:44 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: RISC-V: Add divmod instruction support To: Palmer Dabbelt Cc: jeffreyalaw@gmail.com, gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Sat, Feb 18, 2023 at 1:30 PM Palmer Dabbelt wrote: > > On Sat, 18 Feb 2023 13:06:02 PST (-0800), jeffreyalaw@gmail.com wrote: > > > > > > On 2/18/23 11:26, Palmer Dabbelt wrote: > >> On Fri, 17 Feb 2023 06:02:40 PST (-0800), gcc-patches@gcc.gnu.org wrote: > >>> Hi all, > >>> If we have division and remainder calculations with the same operands: > >>> > >>> a = b / c; > >>> d = b % c; > >>> > >>> We can replace the calculation of remainder with multiplication + > >>> subtraction, using the result from the previous division: > >>> > >>> a = b / c; > >>> d = a * c; > >>> d = b - d; > >>> > >>> Which will be faster. > >> > >> Do you have any benchmarks that show that performance increase? The ISA > >> manual specifically says the suggested sequence is div+mod, and while > >> those suggestions don't always pan out for real hardware it's likely > >> that at least some implementations will end up with the ISA-suggested > >> fusions. > > It'll almost certainly be visible in mcf. Been there, done that. In > > fact, that's why I asked the team Matevos works on to poke at this case > > as I went through this issue on another processor. > > > > It can also be run through LLVM's MCA to estimate counts if you've got a > > pipeline description. THe div+rem will come out at around ~40c while a > > div+mul+sub should weigh in around 25c for Veyron v1. > > Do you have a link to the patches somewhere? I couldn't find them > online, just the custom instruction support. Or even just some docs > describing what the pipeline does, as just basing one performance model > on another is kind of a double-edged sword. > > That said, I think just knowing the processor doesn't do the div+mod > fusion is sufficient to turn something like this on for the mtune for > that processor. That's different than turning it on globally, though -- > unless it turns out nobody is actually doing the fusion suggested in the > ISA manual, which wouldn't be super surprising. > > Maybe some of the SiFive and T-Head folks can chime in on whether or not > their processors perform the fusion in question -- and if so, do the > instructions need to say back-to-back? AFAIK, the sequence with the multiplication will normally be faster on SiFive cores when both the quotient and the remainder are needed. > It doesn't look like we're > really targeting the code sequences the ISA suggests as it stands, so > maybe it's OK to just switch the default over too? > > It also brings up the question of mulh+mul fusions, which I don't think > we've really looked at (though maybe they're a lot less important for > rv64).