public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "aldyh at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/114995] C++23 Assume keyword not being used for vectorization
Date: Tue, 14 May 2024 09:00:01 +0000	[thread overview]
Message-ID: <bug-114995-4-5nDAOb9KOg@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114995-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114995

--- Comment #9 from Aldy Hernandez <aldyh at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #7)
> The above examples just show misunderstanding what __builtin_assume_aligned
> is and what it is not.  You need to use the result of the built-in function
> in the accesses to be able to use the alignment information, if you just try
> to compare __builtin_assume_aligned (x, 32) == x, it will just fold as
> always true.  The design of the builtin is to attach the alignment
> information to the result of the builtin function only.
> 
> CCing Aldy/Andrew for whether prange can or could be taught to handle the
> assume cases with uintptr_t and bitwise and + comparison.

All the pieces are there to make it work, both with the assume aligned and with
the uintptr_t case.  And we could probably get it all without prange.

For example:

#include <cstdint>

void foo (const float *);

void bar1 (const float *array)
{
  [[assume(array != nullptr)]];
  const float *aligned = (const float *) __builtin_assume_aligned (array, 32);
  foo (aligned);
}

The __builtin_assume_aligned hasn't been expanded by evrp, so we should be able
to add a range-op entry for it.  This is what evrp sees:

void bar1 (const float * array)
{
  const float * aligned;

  <bb 2> :
  aligned_2 = __builtin_assume_aligned (array_1(D), 32);
  foo (aligned_2);
  return;

}

All we need is a range-op implementation for builtin_assume_aligned.  The
attached crude implementation does it.

=========== BB 2 ============
    <bb 2> :
    aligned_2 = __builtin_assume_aligned (array_1(D), 32);
    foo (aligned_2);
    return;

aligned_2 : [prange] const float * [0, +INF] MASK 0xffffffff00000000 VALUE 0x0

That is, the bottom 32 bits are cleared.

Andrew will have to comment on the uintptr_t idiom, because it gets expanded
into an .ASSUME() function which I'm unfamiliar with.

For this small function:

void bar2 (const float *array)
{
  [[assume((uintptr_t (array) & (32 - 1)) == 0)]];
  foo (array);
}

evrp expands to:

=========== BB 2 ============
Partial equiv (array.0_3 pe64 array_2(D))
    <bb 2> :
    array.0_3 = (long unsigned int) array_2(D);
    _4 = array.0_3 & 31;
    _5 = _4 == 0;
    return _5;

_4 : [irange] long unsigned int [0, 31] MASK 0x1f VALUE 0x0

I don't see any reason why we couldn't get that array.0_3 and array_2 are
aligned to 32-bits.  Maybe we don't set the value/mask pair for the
bitwise_and::op1_range?  The value/mask stuff is not very fleshed out,
especially for the op1_range operators.

  parent reply	other threads:[~2024-05-14  9:00 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-08 20:14 [Bug tree-optimization/114995] New: " pratikc at live dot co.uk
2024-05-08 20:18 ` [Bug tree-optimization/114995] " pinskia at gcc dot gnu.org
2024-05-08 20:20 ` pinskia at gcc dot gnu.org
2024-05-08 20:26 ` pratikc at live dot co.uk
2024-05-08 20:29 ` pinskia at gcc dot gnu.org
2024-05-08 20:33 ` pinskia at gcc dot gnu.org
2024-05-08 20:53 ` pratikc at live dot co.uk
2024-05-08 21:52 ` jakub at gcc dot gnu.org
2024-05-09 19:59 ` pratikc at live dot co.uk
2024-05-14  9:00 ` aldyh at gcc dot gnu.org [this message]
2024-05-14  9:04 ` aldyh at gcc dot gnu.org
2024-05-14  9:15 ` aldyh at gcc dot gnu.org
2024-05-15 10:33 ` cvs-commit at gcc dot gnu.org
2024-05-15 10:34 ` aldyh at gcc dot gnu.org

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=bug-114995-4-5nDAOb9KOg@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).