From: Segher Boessenkool <segher@kernel.crashing.org>
To: Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
Cc: 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: Wed, 4 Aug 2021 07:46:19 -0500 [thread overview]
Message-ID: <20210804124619.GF1583@gate.crashing.org> (raw)
In-Reply-To: <CAAgBjMkv5b32PYiVVOtR6O32620FNnSSYj2eOXdofYh0H98M1w@mail.gmail.com>
On Wed, Aug 04, 2021 at 05:20:58PM +0530, Prathamesh Kulkarni wrote:
> On Wed, 4 Aug 2021 at 15:49, Segher Boessenkool
> <segher@kernel.crashing.org> wrote:
> > Both __builtin_constant_p and __is_constexpr will not work in your use
> > case (since a function argument is not a constant, let alone an ICE).
> > It only becomes a constant value later on. The manual (for the former)
> > says:
> > You may use this built-in function in either a macro or an inline
> > function. However, if you use it in an inlined function and pass an
> > argument of the function as the argument to the built-in, GCC never
> > returns 1 when you call the inline function with a string constant or
> > compound literal (see Compound Literals) and does not return 1 when you
> > pass a constant numeric value to the inline function unless you specify
> > the -O option.
> Indeed, that's why I was thinking if we should use an attribute to mark param as
> a constant, so during type-checking the function call, the compiler
> can emit a diagnostic if the passed arg
> is not a constant.
That will depend on the vagaries of what optimisations the compiler
managed to do :-(
> Alternatively -- as you suggest, we could define a new builtin, say
> __builtin_ice(x) that returns true if 'x' is an ICE.
(That is a terrible name, it's not clear at all to the reader, just
write it out? It is fun if you know what it means, but infuriating
otherwise.)
> And wrap the intrinsic inside a macro that would check if the arg is an ICE ?
That will work yeah. Maybe not as elegant as you'd like, but not all
that bad, and it *works*. Well, hopefully it does :-)
> For eg:
>
> __extension__ extern __inline int32x2_t
> __attribute__ ((__always_inline__, __gnu_inline__, __artificial__))
> vshl_n_s32_1 (int32x2_t __a, const int __b)
> {
> return __builtin_neon_vshl_nv2si (__a, __b);
> }
>
> #define vshl_n_s32(__a, __b) \
> ({ typeof (__a) a = (__a); \
> _Static_assert (__builtin_constant_p ((__b)), #__b " is not an
> integer constant"); \
> vshl_n_s32_1 (a, (__b)); })
>
> void f(int32x2_t x, const int y)
> {
> vshl_n_s32 (x, 2);
> vshl_n_s32 (x, y);
>
> int z = 1;
> vshl_n_s32 (x, z);
> }
>
> With this, the compiler rejects vshl_n_s32 (x, y) and vshl_n_s32 (x,
> z) at all optimization levels since neither 'y' nor 'z' is an ICE.
You used __builtin_constant_p though, which works differently, so the
test is not conclusive, might not show what you want to show.
> Instead of __builtin_constant_p, we could use __builtin_ice.
> Would that be a reasonable approach ?
I think it will work, yes.
> But this changes the semantics of intrinsic from being an inline
> function to a macro, and I am not sure if that's a good idea.
Well, what happens if you call the actual builtin directly, with some
non-constant parameter? That just fails with a more cryptic error,
right? So you can view this as some syntactic sugar to make these
intrinsics easier to use.
Hrm I now remember a place I could have used this:
#define mtspr(n, x) do { asm("mtspr %1,%0" : : "r"(x), "n"(n)); } while (0)
#define mfspr(n) ({ \
u32 x; asm volatile("mfspr %0,%1" : "=r"(x) : "n"(n)); x; \
})
It is quite similar to your builtin code really, and I did resort to
macros there, for similar reasons :-)
Segher
next prev parent reply other threads:[~2021-08-04 12:47 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
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 [this message]
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=20210804124619.GF1583@gate.crashing.org \
--to=segher@kernel.crashing.org \
--cc=Richard.Earnshaw@foss.arm.com \
--cc=gcc@gcc.gnu.org \
--cc=prathamesh.kulkarni@linaro.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).