public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Sandiford <richard.sandiford@arm.com>
To: "Richard Earnshaw \(lists\)" <Richard.Earnshaw@arm.com>
Cc: Victor Do Nascimento <Victor.DoNascimento@arm.com>,
	 Richard Earnshaw <Richard.Earnshaw@foss.arm.com>,
	 gcc-patches@gcc.gnu.org,  kyrylo.tkachov@arm.com
Subject: Re: [PATCH 6/6] aarch64: Add front-end argument type checking for target builtins
Date: Thu, 12 Oct 2023 11:37:33 +0100	[thread overview]
Message-ID: <mpt7cnscej6.fsf@arm.com> (raw)
In-Reply-To: <11df7b7b-9c52-ff67-1b54-c9839b36e23e@arm.com> (Richard Earnshaw's message of "Tue, 10 Oct 2023 12:20:57 +0100")

"Richard Earnshaw (lists)" <Richard.Earnshaw@arm.com> writes:
> On 09/10/2023 14:12, Victor Do Nascimento wrote:
>> 
>> 
>> On 10/7/23 12:53, Richard Sandiford wrote:
>>> Richard Earnshaw <Richard.Earnshaw@foss.arm.com> writes:
>>>> On 03/10/2023 16:18, Victor Do Nascimento wrote:
>>>>> In implementing the ACLE read/write system register builtins it was
>>>>> observed that leaving argument type checking to be done at expand-time
>>>>> meant that poorly-formed function calls were being "fixed" by certain
>>>>> optimization passes, meaning bad code wasn't being properly picked up
>>>>> in checking.
>>>>>
>>>>> Example:
>>>>>
>>>>>     const char *regname = "amcgcr_el0";
>>>>>     long long a = __builtin_aarch64_rsr64 (regname);
>>>>>
>>>>> is reduced by the ccp1 pass to
>>>>>
>>>>>     long long a = __builtin_aarch64_rsr64 ("amcgcr_el0");
>>>>>
>>>>> As these functions require an argument of STRING_CST type, there needs
>>>>> to be a check carried out by the front-end capable of picking this up.
>>>>>
>>>>> The introduced `check_general_builtin_call' function will be called by
>>>>> the TARGET_CHECK_BUILTIN_CALL hook whenever a call to a builtin
>>>>> belonging to the AARCH64_BUILTIN_GENERAL category is encountered,
>>>>> carrying out any appropriate checks associated with a particular
>>>>> builtin function code.
>>>>
>>>> Doesn't this prevent reasonable wrapping of the __builtin... names with
>>>> something more palatable?  Eg:
>>>>
>>>> static inline __attribute__(("always_inline")) long long get_sysreg_ll
>>>> (const char *regname)
>>>> {
>>>>     return __builtin_aarch64_rsr64 (regname);
>>>> }
>>>>
>>>> ...
>>>>     long long x = get_sysreg_ll("amcgcr_el0");
>>>> ...
>>>
>>> I think it's case of picking your poison.  If we didn't do this,
>>> and only checked later, then it's unlikely that GCC and Clang would
>>> be consistent about when a constant gets folded soon enough.
>>>
>>> But yeah, it means that the above would need to be a macro in C.
>>> Enlightened souls using C++ could instead do:
>>>
>>>    template<const char *regname>
>>>    long long get_sysreg_ll()
>>>    {
>>>      return __builtin_aarch64_rsr64(regname);
>>>    }
>>>
>>>    ... get_sysreg_ll<"amcgcr_el0">() ...
>>>
>>> Or at least I hope so.  Might be nice to have a test for this.
>>>
>>> Thanks,
>>> Richard
>> 
>> As Richard Earnshaw mentioned, this does break the use of `static inline __attribute__(("always_inline"))', something I had found out in my testing.  My chosen implementation was indeed, to quote Richard Sandiford, a case of "picking your poison" to have things line up with Clang and behaving consistently across optimization levels.
>> 
>> Relaxing the the use of `TARGET_CHECK_BUILTIN_CALL' meant optimizations were letting too many things through. Example:
>> 
>> const char *regname = "amcgcr_el0";
>> long long a = __builtin_aarch64_rsr64 (regname);
>> 
>> gets folded to
>> 
>> long long a = __builtin_aarch64_rsr64 ("amcgcr_el0");
>> 
>> and compilation passes at -01 even though it fails at -O0.
>> 
>> I had, however, not given any thought to the use of a template as a valid C++ alternative.
>> 
>> I will evaluate the use of templates and add tests accordingly.
>
> This just seems inconsistent with all the builtins we already have that require literal constants for parameters.  For example (to pick just one of many), vshr_n_q8(), where the second parameter must be a literal value.  In practice we accept anything that resolves to a compile-time constant integer expression and rely on that to avoid having to have hundreds of macros binding the ACLE names to the underlying builtin equivalents.

That's true for the way that GCC handles things like Advanced SIMD.
But Clang behaves differently.  So does GCC's SVE ACLE implementation.
Both of those (try to) follow the language rules about what is a constant
expression.

Thanks,
Richard

      reply	other threads:[~2023-10-12 10:37 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-03 15:18 [PATCH 0/6] aarch64: Add support for __arm_rsr and __arm_wsr ACLE function family Victor Do Nascimento
2023-10-03 15:18 ` [PATCH 1/6] aarch64: Sync system register information with Binutils Victor Do Nascimento
2023-10-05 11:42   ` Richard Earnshaw
2023-10-05 13:04     ` Victor Do Nascimento
2023-10-09  0:02       ` Ramana Radhakrishnan
2023-10-09 12:52         ` Victor Do Nascimento
2023-10-03 15:18 ` [PATCH 2/6] aarch64: Add support for aarch64-sys-regs.def Victor Do Nascimento
2023-10-05 12:03   ` Richard Earnshaw
2023-10-03 15:18 ` [PATCH 3/6] aarch64: Implement system register validation tools Victor Do Nascimento
2023-10-05 12:24   ` Richard Earnshaw
2023-10-03 15:18 ` [PATCH 4/6] aarch64: Add basic target_print_operand support for CONST_STRING Victor Do Nascimento
2023-10-05 12:26   ` Richard Earnshaw
2023-10-05 12:33     ` Richard Earnshaw
2023-10-05 12:47     ` Victor Do Nascimento
2023-10-03 15:18 ` [PATCH 5/6] aarch64: Implement system register r/w arm ACLE intrinsic functions Victor Do Nascimento
2023-10-05 12:40   ` Richard Earnshaw
2023-10-03 15:18 ` [PATCH 6/6] aarch64: Add front-end argument type checking for target builtins Victor Do Nascimento
2023-10-05 12:48   ` Richard Earnshaw
2023-10-07 11:53     ` Richard Sandiford
2023-10-09 13:12       ` Victor Do Nascimento
2023-10-10 11:20         ` Richard Earnshaw (lists)
2023-10-12 10:37           ` Richard Sandiford [this message]

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=mpt7cnscej6.fsf@arm.com \
    --to=richard.sandiford@arm.com \
    --cc=Richard.Earnshaw@arm.com \
    --cc=Richard.Earnshaw@foss.arm.com \
    --cc=Victor.DoNascimento@arm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=kyrylo.tkachov@arm.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).