From: Rasmus Villemoes <ravi@prevas.dk>
To: Kees Cook <kees@kernel.org>
Cc: Yury Norov <yury.norov@gmail.com>,
kernel test robot <lkp@intel.com>,
Vineet Gupta <vgupta@kernel.org>,
linux-snps-arc@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] arc: Fix __fls() const-foldability via __builtin_clzl()
Date: Tue, 26 Aug 2025 15:08:59 +0200 [thread overview]
Message-ID: <87ldn644d0.fsf@prevas.dk> (raw)
In-Reply-To: <20250826034354.work.684-kees@kernel.org> (Kees Cook's message of "Mon, 25 Aug 2025 20:43:58 -0700")
On Mon, Aug 25 2025, Kees Cook <kees@kernel.org> wrote:
> While tracking down a problem where constant expressions used by
> BUILD_BUG_ON() suddenly stopped working[1], we found that an added static
> initializer was convincing the compiler that it couldn't track the state
> of the prior statically initialized value. Tracing this down found that
> ffs() was used in the initializer macro, but since it wasn't marked with
> __attribute__const__, the compiler had to assume the function might
> change variable states as a side-effect (which is not true for ffs(),
> which provides deterministic math results).
>
> For arc architecture with CONFIG_ISA_ARCV2=y, the __fls() function
> uses __builtin_arc_fls() which lacks GCC's const attribute, preventing
> compile-time constant folding[2].
That sounds like a compiler bug, or "missed optimization". Looking into
the gcc source code, it seems that most "generic" builtins (well, those
eligible because they're just math) are internally declared with that
const attribute. E.g. there's
gcc/builtins.def:DEF_GCC_BUILTIN (BUILT_IN_CLZL, "clzl", BT_FN_INT_ULONG, ATTR_CONST_NOTHROW_LEAF_LIST)
The DEF_BUILTIN macro for e.g. avr seems to support providing such
attributes, e.g. there's
gcc/config/avr/builtins.def:DEF_BUILTIN (ABSHR, 1, hr_ftype_hr, ssabsqq2, "__ssabs_1", attr_const)
But the arc-specific ones in gcc/config/arc/builtins.def have no such
infrastructure readily available.
Of course, I also don't know if __builtin_arc_fls() is even eligible for
the const attribute (I don't know if it might have some side effects on
a flags register or something). But if it is, perhaps it's possible to
amend gcc's builtin declaration by explicitly (re)declaring it
int __builtin_arc_fls(int x) __attribute_const;
?
If __builtin_arc_fls() simply doesn't qualify for attr_const for
$reason, I think it would be good to have that documented in the commit
msg. If it does, I think a gcc ticket and link to that would be in order.
Rasmus
WARNING: multiple messages have this Message-ID (diff)
From: Rasmus Villemoes <ravi@prevas.dk>
To: Kees Cook <kees@kernel.org>
Cc: Yury Norov <yury.norov@gmail.com>,
kernel test robot <lkp@intel.com>,
Vineet Gupta <vgupta@kernel.org>,
linux-snps-arc@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH] arc: Fix __fls() const-foldability via __builtin_clzl()
Date: Tue, 26 Aug 2025 15:08:59 +0200 [thread overview]
Message-ID: <87ldn644d0.fsf@prevas.dk> (raw)
In-Reply-To: <20250826034354.work.684-kees@kernel.org> (Kees Cook's message of "Mon, 25 Aug 2025 20:43:58 -0700")
On Mon, Aug 25 2025, Kees Cook <kees@kernel.org> wrote:
> While tracking down a problem where constant expressions used by
> BUILD_BUG_ON() suddenly stopped working[1], we found that an added static
> initializer was convincing the compiler that it couldn't track the state
> of the prior statically initialized value. Tracing this down found that
> ffs() was used in the initializer macro, but since it wasn't marked with
> __attribute__const__, the compiler had to assume the function might
> change variable states as a side-effect (which is not true for ffs(),
> which provides deterministic math results).
>
> For arc architecture with CONFIG_ISA_ARCV2=y, the __fls() function
> uses __builtin_arc_fls() which lacks GCC's const attribute, preventing
> compile-time constant folding[2].
That sounds like a compiler bug, or "missed optimization". Looking into
the gcc source code, it seems that most "generic" builtins (well, those
eligible because they're just math) are internally declared with that
const attribute. E.g. there's
gcc/builtins.def:DEF_GCC_BUILTIN (BUILT_IN_CLZL, "clzl", BT_FN_INT_ULONG, ATTR_CONST_NOTHROW_LEAF_LIST)
The DEF_BUILTIN macro for e.g. avr seems to support providing such
attributes, e.g. there's
gcc/config/avr/builtins.def:DEF_BUILTIN (ABSHR, 1, hr_ftype_hr, ssabsqq2, "__ssabs_1", attr_const)
But the arc-specific ones in gcc/config/arc/builtins.def have no such
infrastructure readily available.
Of course, I also don't know if __builtin_arc_fls() is even eligible for
the const attribute (I don't know if it might have some side effects on
a flags register or something). But if it is, perhaps it's possible to
amend gcc's builtin declaration by explicitly (re)declaring it
int __builtin_arc_fls(int x) __attribute_const;
?
If __builtin_arc_fls() simply doesn't qualify for attr_const for
$reason, I think it would be good to have that documented in the commit
msg. If it does, I think a gcc ticket and link to that would be in order.
Rasmus
_______________________________________________
linux-snps-arc mailing list
linux-snps-arc@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-snps-arc
next prev parent reply other threads:[~2025-08-26 13:09 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-26 3:43 [PATCH] arc: Fix __fls() const-foldability via __builtin_clzl() Kees Cook
2025-08-26 3:43 ` Kees Cook
2025-08-26 13:08 ` Rasmus Villemoes [this message]
2025-08-26 13:08 ` Rasmus Villemoes
2025-08-26 16:56 ` Kees Cook
2025-08-26 16:56 ` Kees Cook
2025-08-27 1:24 ` Yury Norov
2025-08-27 1:24 ` Yury Norov
2025-08-27 2:13 ` Kees Cook
2025-08-27 2:13 ` Kees Cook
2025-08-27 3:41 ` Yury Norov
2025-08-27 3:41 ` Yury Norov
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=87ldn644d0.fsf@prevas.dk \
--to=ravi@prevas.dk \
--cc=kees@kernel.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-snps-arc@lists.infradead.org \
--cc=lkp@intel.com \
--cc=vgupta@kernel.org \
--cc=yury.norov@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.