public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* [RFC] Adding a new attribute to function param to mark it as constant
@ 2021-07-23 10:53 Prathamesh Kulkarni
  2021-07-23 17:59 ` Andrew Pinski
  2021-08-03 21:55 ` Segher Boessenkool
  0 siblings, 2 replies; 31+ messages in thread
From: Prathamesh Kulkarni @ 2021-07-23 10:53 UTC (permalink / raw)
  To: GCC Development, Richard Earnshaw

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);
}

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

^ permalink raw reply	[flat|nested] 31+ messages in thread
* Re: [RFC] Adding a new attribute to function param to mark it as constant
@ 2021-08-06 20:10 Martin Uecker
  0 siblings, 0 replies; 31+ messages in thread
From: Martin Uecker @ 2021-08-06 20:10 UTC (permalink / raw)
  To: prathamesh.kulkarni; +Cc: gcc

> On Wed, 4 Aug 2021 at 03:27, Segher Boessenkool
> <segher@kernel.crashing.org> wrote:
> >
> > Hi!
> >
> > On Fri, Jul 23, 2021 at 04:23:42PM +0530, Prathamesh Kulkarni via Gcc wrote:
> > > The constraint here is that, vshl_n<type> intrinsics require that the
> > > second arg (__b),
> > > should be an immediate value.
> >
> > Something that matches the "n" constraint, not necessarily a literal,
> > but stricter than just "immediate".  It probably is a good idea to allow
> > only "integer constant expression"s, so that the validity of the source
> > code does not depend on what the optimisers do with the code.
> >
> > > As Richard suggested, sth like:
> > > void foo(int x __attribute__((literal_constant (min_val, max_val)));
> >
> > The Linux kernel has a macro __is_constexpr to test if something is an
> > integer constant expression, see <linux/const.h> .  That is a much
> > better idea imo.  There could be a builtin for that of course, but an
> > attribute is less powerful, less usable, less useful.
> Hi Segher,
> Thanks for the suggestions. I am not sure tho if we could use a macro
> similar to __is_constexpr
> to check if parameter is constant inside an inline function (which is
> the case for intrinsics) ?
> 
> For eg:
> #define __is_constexpr(x) \
>         (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))
> 
> inline int foo(const int x)
> {
>   _Static_assert (__is_constexpr (x));
>   return x;
> }
> 
> int main()
> {
>   return foo (1);
> }
> 
> results in:
> foo.c: In function ‘foo’:
> foo.c:8:3: error: static assertion failed
>     8 |   _Static_assert (__is_constexpr (x));
> 
> Initially we tried to use __Static_assert (__builtin_constant_p (arg))
> for the same purpose but that did not work
> because while parsing the intrinsic function, the FE cannot determine
> if the arg is indeed a constant.
> I guess the static assertion or __is_constexpr would work only if the
> intrinsic were defined as a macro instead of an inline function ?
> Or am I misunderstanding ?

You have to use it at the call site:

#define __is_constexpr(x) \
         (sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))

#define foo(x) foo(({ _Static_assert(__is_constexpr(x), "no ICE"); (x); }))

inline int (foo)(const int x)
{
  return x;
}

int main()
{
  foo(1);
  int n = 1;
  foo(n);	// error
}


--Martin



^ permalink raw reply	[flat|nested] 31+ messages in thread

end of thread, other threads:[~2021-08-19  8:10 UTC | newest]

Thread overview: 31+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-23 10:53 [RFC] Adding a new attribute to function param to mark it as constant 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
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

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).