public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
To: Richard Biener <richard.guenther@gmail.com>
Cc: Andrew Pinski <pinskia@gmail.com>,
	GCC Development <gcc@gcc.gnu.org>,
	 Richard Earnshaw <Richard.Earnshaw@foss.arm.com>
Subject: Re: [RFC] Adding a new attribute to function param to mark it as constant
Date: Tue, 3 Aug 2021 15:43:30 +0530	[thread overview]
Message-ID: <CAAgBjMmNSJ7UzrDtv4Ua2dVFeSMQbA30PHcV+78w6jV4B6bRgg@mail.gmail.com> (raw)
In-Reply-To: <CAAgBjMkqiknCk3AQgeWLz2Ur0TPjdy-xkSnzSUHmbpfkQ2z6WA@mail.gmail.com>

On Tue, 3 Aug 2021 at 15:41, Prathamesh Kulkarni
<prathamesh.kulkarni@linaro.org> wrote:
>
> On Tue, 27 Jul 2021 at 13:49, Richard Biener <richard.guenther@gmail.com> wrote:
> >
> > On Mon, Jul 26, 2021 at 11:06 AM Prathamesh Kulkarni via Gcc
> > <gcc@gcc.gnu.org> wrote:
> > >
> > > On Fri, 23 Jul 2021 at 23:29, Andrew Pinski <pinskia@gmail.com> wrote:
> > > >
> > > > On Fri, Jul 23, 2021 at 3:55 AM Prathamesh Kulkarni via Gcc
> > > > <gcc@gcc.gnu.org> wrote:
> > > > >
> > > > > Hi,
> > > > > Continuing from this thread,
> > > > > https://gcc.gnu.org/pipermail/gcc-patches/2021-July/575920.html
> > > > > The proposal is to provide a mechanism to mark a parameter in a
> > > > > function as a literal constant.
> > > > >
> > > > > Motivation:
> > > > > Consider the following intrinsic vshl_n_s32 from arrm/arm_neon.h:
> > > > >
> > > > > __extension__ extern __inline int32x2_t
> > > > > __attribute__  ((__always_inline__, __gnu_inline__, __artificial__))
> > > > > vshl_n_s32 (int32x2_t __a, const int __b)
> > > > > {
> > > > >   return (int32x2_t)__builtin_neon_vshl_nv2si (__a, __b);
> > > > > }
> > > > >
> > > > > and it's caller:
> > > > >
> > > > > int32x2_t f (int32x2_t x)
> > > > > {
> > > > >    return vshl_n_s32 (x, 1);
> > > > > }
> > > >
> > > > Can't you do similar to what is done already in the aarch64 back-end:
> > > > #define __AARCH64_NUM_LANES(__v) (sizeof (__v) / sizeof (__v[0]))
> > > > #define __AARCH64_LANE_CHECK(__vec, __idx)      \
> > > >         __builtin_aarch64_im_lane_boundsi (sizeof(__vec),
> > > > sizeof(__vec[0]), __idx)
> > > >
> > > > ?
> > > > Yes this is about lanes but you could even add one for min/max which
> > > > is generic and such; add an argument to say the intrinsics name even.
> > > > You could do this as a non-target builtin if you want and reuse it
> > > > also for the aarch64 backend.
> > > Hi Andrew,
> > > Thanks for the suggestions. IIUC, we could use this approach to check
> > > if the argument
> > > falls within a certain range (min / max), but I am not sure how it
> > > will help to determine
> > > if the arg is a constant immediate ? AFAIK, vshl_n intrinsics require
> > > that the 2nd arg is immediate ?
> > >
> > > Even the current RTL builtin checking is not consistent across
> > > optimization levels:
> > > For eg:
> > > int32x2_t f(int32_t *restrict a)
> > > {
> > >   int32x2_t v = vld1_s32 (a);
> > >   int b = 2;
> > >   return vshl_n_s32 (v, b);
> > > }
> > >
> > > With pristine trunk, compiling with -O2 results in no errors because
> > > constant propagation replaces 'b' with 2, and during expansion,
> > > expand_builtin_args is happy. But at -O0, it results in the error -
> > > "argument 2 must be a constant immediate".
> > >
> > > So I guess we need some mechanism to mark a parameter as a constant ?
> >
> > I guess you want to mark it in a way that the frontend should force
> > constant evaluation and error if that's not possible?   C++ doesn't
> > allow to declare a parameter as 'constexpr' but something like
> >
> > void foo (consteval int i);
> >
> > since I guess you do want to allow passing constexpr arguments
> > in C++ or in C extended forms of constants like
> >
> > static const int a[4];
> >
> > foo (a[1]);
> >
> > ?  But yes, this looks useful to me.
> Hi Richard,
> Thanks for the suggestions and sorry for late response.
> I have attached a prototype patch that implements consteval attribute.
> As implemented, the attribute takes at least one argument(s), which
> refer to parameter position,
> and the corresponding parameter must be const qualified, failing
> which, the attribute is ignored.
>
> The patch does type-checking for arguments in
> check_function_consteval_attr, which
> simply does a linear search to see if the corresponding argument
> number is included in consteval attribute,
> and if yes, it checks if the argument is CONSTANT_CLASS_P.
>
> This works for simple constants and the intrinsics use-case, but
> rejects "extended constants" as in your above example.
> I guess we can special case to detect cases like a[i] where 'a' is
> const and 'i' is an immediate,
> but I am not sure if there's a way to force constant evaluation in FE ?
> I tried using c_fully_fold (argarray[i], false, &maybe_const) but that
> didn't seem to work.
> Do you have any suggestions on how to detect those in the front-end ?
>
> Example:
>
> __attribute__((consteval(1, 2)))
> void f(const int x, int *p)
Oops typo, this should have been int *const p;
> {}
>
> Calling it with:
> 1) f(0, (int *) 0) is accepted.
>
> 2)
> void g(int *p)
> {
>   f (0, p);
> }
>
> emits the following error:
> test.c: In function ‘g’:
> test.c:7:9: error: argument 2 is not a constant.
>     7 |   f (0, p);
>       |         ^
> test.c:2:6: note: Function ‘f’ has parameter 2 with consteval attribute.
>     2 | void f(const int x, int *const p)
>       |      ^
>
> Thanks,
> Prathamesh
> >
> > Richard.
> >
> > >
> > > Thanks,
> > > Prathamesh
> > > >
> > > > Thanks,
> > > > Andrew Pinski
> > > >
> > > > >
> > > > > The constraint here is that, vshl_n<type> intrinsics require that the
> > > > > second arg (__b),
> > > > > should be an immediate value.
> > > > > Currently, this check is performed by arm_expand_builtin_args, and if
> > > > > a non-constant
> > > > > value gets passed, it emits the following diagnostic:
> > > > >
> > > > > ../armhf-build/gcc/include/arm_neon.h:4904:10: error: argument 2 must
> > > > > be a constant immediate
> > > > >  4904 |   return (int32x2_t)__builtin_neon_vshl_nv2si (__a, __b);
> > > > >       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > > > >
> > > > > However, we're trying to replace builtin calls with gcc's C vector
> > > > > extensions where
> > > > > possible (PR66791), because the builtins are opaque to the optimizers.
> > > > >
> > > > > Unfortunately, we lose type checking of immediate value if we replace
> > > > > the builtin
> > > > > with << operator:
> > > > >
> > > > > __extension__ extern __inline int32x2_t
> > > > > __attribute__  ((__always_inline__, __gnu_inline__, __artificial__))
> > > > > vshl_n_s32 (int32x2_t __a, const int __b)
> > > > > {
> > > > >   return __a << __b;
> > > > > }
> > > > >
> > > > > So, I was wondering if we should have an attribute for a parameter to
> > > > > specifically
> > > > > mark it as a constant value with optional range value info ?
> > > > > As Richard suggested, sth like:
> > > > > void foo(int x __attribute__((literal_constant (min_val, max_val)));
> > > > >
> > > > > Thanks,
> > > > > Prathamesh

  reply	other threads:[~2021-08-03 10:14 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-23 10:53 Prathamesh Kulkarni
2021-07-23 17:59 ` Andrew Pinski
2021-07-26  9:04   ` Prathamesh Kulkarni
2021-07-27  8:19     ` Richard Biener
2021-08-03 10:11       ` Prathamesh Kulkarni
2021-08-03 10:13         ` Prathamesh Kulkarni [this message]
2021-08-03 17:44         ` Martin Sebor
2021-08-04  9:46           ` Richard Earnshaw
2021-08-06  0:06             ` Martin Sebor
2021-08-06 10:51               ` Richard Earnshaw
2021-08-06 20:39                 ` Martin Sebor
2021-08-12  8:32                   ` Prathamesh Kulkarni
2021-08-13 17:14                     ` Martin Sebor
2021-08-18  6:52                       ` Prathamesh Kulkarni
2021-08-18 14:40                         ` Martin Sebor
2021-08-19  8:10                           ` Prathamesh Kulkarni
2021-08-03 21:55 ` Segher Boessenkool
2021-08-04  9:50   ` Prathamesh Kulkarni
2021-08-04 10:17     ` Segher Boessenkool
2021-08-04 11:50       ` Prathamesh Kulkarni
2021-08-04 12:46         ` Segher Boessenkool
2021-08-04 13:00           ` Richard Earnshaw
2021-08-04 13:40             ` Segher Boessenkool
2021-08-04 14:27               ` Richard Earnshaw
2021-08-04 16:16                 ` Segher Boessenkool
2021-08-04 17:08                   ` Florian Weimer
2021-08-04 17:59                     ` Segher Boessenkool
2021-08-05  9:32                       ` Richard Earnshaw
2021-08-05  9:01             ` Prathamesh Kulkarni
2021-08-05 15:06               ` Segher Boessenkool
2021-08-06 20:10 Martin Uecker

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=CAAgBjMmNSJ7UzrDtv4Ua2dVFeSMQbA30PHcV+78w6jV4B6bRgg@mail.gmail.com \
    --to=prathamesh.kulkarni@linaro.org \
    --cc=Richard.Earnshaw@foss.arm.com \
    --cc=gcc@gcc.gnu.org \
    --cc=pinskia@gmail.com \
    --cc=richard.guenther@gmail.com \
    /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).