public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Sandiford <richard.sandiford@arm.com>
To: Richard Biener <rguenther@suse.de>
Cc: Barnaby Wilks <Barnaby.Wilks@arm.com>,
	 "gcc-patches\@gcc.gnu.org" <gcc-patches@gcc.gnu.org>,
	 nd <nd@arm.com>,  "law\@redhat.com" <law@redhat.com>,
	 "ian\@airs.com" <ian@airs.com>,
	 Tamar Christina <Tamar.Christina@arm.com>,
	 Wilco Dijkstra <Wilco.Dijkstra@arm.com>
Subject: Re: [PATCH][GCC] Simplify to single precision where possible for binary/builtin maths operations.
Date: Tue, 03 Sep 2019 14:19:00 -0000	[thread overview]
Message-ID: <mpt5zm9fonh.fsf@arm.com> (raw)
In-Reply-To: <alpine.LSU.2.20.1909031006590.32458@zhemvz.fhfr.qr> (Richard	Biener's message of "Tue, 3 Sep 2019 10:23:33 +0200 (CEST)")

Richard Biener <rguenther@suse.de> writes:
> On Mon, 2 Sep 2019, Barnaby Wilks wrote:
>
>> Hello,
>> 
>> This patch introduces an optimization for narrowing binary and builtin
>> math operations to the smallest type when unsafe math optimizations are
>> enabled (typically -Ofast or -ffast-math).
>> 
>> Consider the example:
>> 
>>    float f (float x) {
>>      return 1.0 / sqrt (x);
>>    }
>> 
>>    f:
>>      fcvt	d0, s0
>>      fmov	d1, 1.0e+0
>>      fsqrt	d0, d0
>>      fdiv	d0, d1, d0
>>      fcvt	s0, d0
>>      ret
>> 
>> Given that all outputs are of float type, we can do the whole 
>> calculation in single precision and avoid any potentially expensive 
>> conversions between single and double precision.
>> 
>> Aka the expression would end up looking more like
>> 
>>    float f (float x) {
>>      return 1.0f / sqrtf (x);
>>    }
>> 
>>    f:
>>      fsqrt	s0, s0
>>      fmov	s1, 1.0e+0
>>      fdiv	s0, s1, s0
>>      ret
>> 
>> This optimization will narrow casts around math builtins, and also
>> not try to find the widest type for calculations when processing binary
>> math operations (if unsafe math optimizations are enable).
>> 
>> Added tests to verify that narrower math builtins are chosen and
>> no unnecessary casts are introduced when appropriate.
>> 
>> Bootstrapped and regtested on aarch64 and x86_64 with no regressions.
>> 
>> I don't have write access, so if OK for trunk then can someone commit on 
>> my behalf?
> [...]
>
> Now - as a general comment I think adding this kind of narrowing is
> good but doing it via match.pd patterns is quite limiting - eventually
> the backprop pass would be a fit for propagating "needed precision"
> and narrowing feeding stmts accordingly in a more general way?
> Richard can probably tell quickest if it is feasible in that framework.

Yeah, I think it would be a good fit, and would for example cope with
cases in which we select between two double results before doing the
truncation to float.  I'd wanted to do something similar for integer
truncation but never found the time...

At the moment, backprop handles a single piece of information: whether
the sign of the value matters.  This is (over?)generalised to be one bit
of information in a word of flags.  I guess we could take the same
approach here and have flags for certain well-known floating-point
types, but it might be cleaner to instead have a field that records the
widest mode that users of the result want.

I think to do this we'd need to build an array that maps floating-point
machine_modes to their order in the FOR_EACH_MODE_IN_CLASS chain.
That'll give us a total ordering over floating-point modes and mean
that operator & (the usage_info confluence function) can just take
whichever of the input usage_info modes has the highest index in this
chain.

Thanks,
Richard

  reply	other threads:[~2019-09-03 14:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-02 17:29 Barnaby Wilks
2019-09-03  8:23 ` Richard Biener
2019-09-03 14:19   ` Richard Sandiford [this message]
2019-09-03 15:23   ` Barnaby Wilks
2019-09-05  9:50     ` Richard Biener

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=mpt5zm9fonh.fsf@arm.com \
    --to=richard.sandiford@arm.com \
    --cc=Barnaby.Wilks@arm.com \
    --cc=Tamar.Christina@arm.com \
    --cc=Wilco.Dijkstra@arm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ian@airs.com \
    --cc=law@redhat.com \
    --cc=nd@arm.com \
    --cc=rguenther@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).