From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Bruce Richardson" <bruce.richardson@intel.com>
Cc: "Tyler Retzlaff" <roretzla@linux.microsoft.com>,
"Thomas Monjalon" <thomas@monjalon.net>,
<david.marchand@redhat.com>, <dev@dpdk.org>,
"Ferruh Yigit" <ferruh.yigit@amd.com>,
"Andrew Rybchenko" <andrew.rybchenko@oktetlabs.ru>,
"Ajit Khaparde" <ajit.khaparde@broadcom.com>,
"Somnath Kotur" <somnath.kotur@broadcom.com>,
"Gaetan Rivet" <grive@u256.net>, "Jie Hai" <haijie1@huawei.com>,
"Long Li" <longli@microsoft.com>, "Wei Hu" <weh@microsoft.com>
Subject: RE: [PATCH 1/2] eal: add unreachable and precondition hints
Date: Mon, 4 Nov 2024 13:53:35 +0100 [thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35E9F871@smartserver.smartshare.dk> (raw)
In-Reply-To: <Zyi7uqk-ZGR2BKRO@bricha3-mobl1.ger.corp.intel.com>
> From: Bruce Richardson [mailto:bruce.richardson@intel.com]
> Sent: Monday, 4 November 2024 13.19
>
> On Mon, Nov 04, 2024 at 12:40:49PM +0100, Morten Brørup wrote:
> > Ping for review.
> >
> > > From: Morten Brørup [mailto:mb@smartsharesystems.com]
> > > Sent: Friday, 25 October 2024 13.52
> > >
> > > Added two new compiler/optimizer hints:
> > > * The __rte_unreachable hint for use in points in code known never
> to
> > > be
> > > reached.
> > > * The __rte_assume hint for providing information about
> preconditions
> > > the
> > > compiler/optimizer might be unable to figure out by itself.
> > >
> > > Signed-off-by: Morten Brørup <mb@smartsharesystems.com>
> > > ---
> > > lib/eal/include/rte_common.h | 27 +++++++++++++++++++++++++++
> > > 1 file changed, 27 insertions(+)
> > >
> > > diff --git a/lib/eal/include/rte_common.h
> > > b/lib/eal/include/rte_common.h
> > > index c79f9ed319..2f143c87e4 100644
> > > --- a/lib/eal/include/rte_common.h
> > > +++ b/lib/eal/include/rte_common.h
> > > @@ -366,6 +366,16 @@ static void
> > > __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
> > > #define __rte_noreturn __attribute__((noreturn))
> > > #endif
> > >
> > > +/**
> > > + * Hint point in program never reached
> > > + */
> > > +#if defined(RTE_TOOLCHAIN_GCC) || defined(RTE_TOOLCHAIN_CLANG)
> > > +#define __rte_unreachable() __extension__(__builtin_unreachable())
> > > +#else
> > > +/* MSVC or ICC */
> > > +#define __rte_unreachable() __assume(0)
> > > +#endif
> > > +
>
> Is there already somewhere in the code where we might want to use this?
Yes. It's used in the __rte_assume() macro below for GCC before version 13.
> I'm
> not sure about just adding macros just in case they might be needed in
> future.
IMHO, such hints might be useful for applications, and DPDK could use them in the future.
But if it came to a vote about adding unused/dead code, it would probably not be accepted.
Anyway, this macro is not unused, so no problem here.
> Having unreachable code seems a bit problematic to me generally
> anyway.
Agree.
The correct use of __rte_unreachable() is to provide information to the compiler/optimizer/sanitizer it cannot figure out by itself, and/or as information to the code reviewer.
Using it to prevent compiler warnings from unreachable code would be utterly wrong. I don't know if that is even possible.
>
> > > /**
> > > * Issue a warning in case the function's return value is ignored.
> > > *
> > > @@ -423,6 +433,23 @@ static void
> > > __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
> > > #define __rte_cold __attribute__((cold))
> > > #endif
> > >
> > > +/**
> > > + * Hint precondition
> > > + *
> > > + * @warning Depending on the compiler, any code in ``condition``
> might
> > > be executed.
> > > + * This currently only occurs with GCC prior to version 13.
> > > + */
> > > +#if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION >= 130000)
> > > +#define __rte_assume(condition) __attribute__((assume(condition)))
> > > +#elif defined(RTE_TOOLCHAIN_GCC)
> > > +#define __rte_assume(condition) do { if (!(condition))
> > > __rte_unreachable(); } while (0)
Note: I did not come up with this myself; this seems to be the "official" way of doing it with older GCC versions.
> > > +#elif defined(RTE_TOOLCHAIN_CLANG)
> > > +#define __rte_assume(condition)
> > > __extension__(__builtin_assume(condition))
> > > +#else
> > > +/* MSVC or ICC */
> > > +#define __rte_assume(condition) __assume(condition)
> > > +#endif
> > > +
>
> This part seems ok, I see it used in the next patch, and also looks
> rather
> useful to have.
>
> /Bruce
next prev parent reply other threads:[~2024-11-04 12:53 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-25 11:52 [PATCH 0/2] ethdev: support single queue per port Morten Brørup
2024-10-25 11:52 ` [PATCH 1/2] eal: add unreachable and precondition hints Morten Brørup
2024-11-04 11:40 ` Morten Brørup
2024-11-04 12:19 ` Bruce Richardson
2024-11-04 12:53 ` Morten Brørup [this message]
2024-11-06 11:48 ` Bruce Richardson
2024-10-25 11:52 ` [PATCH 2/2] drivers/net: support single queue per port Morten Brørup
2024-10-25 21:27 ` Ajit Khaparde
2024-10-25 21:56 ` Long Li
2024-11-06 11:52 ` Bruce Richardson
2024-11-06 12:19 ` Morten Brørup
2024-11-06 13:28 ` Bruce Richardson
2024-11-06 16:06 ` Thomas Monjalon
2024-11-07 10:28 ` Morten Brørup
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=98CBD80474FA8B44BF855DF32C47DC35E9F871@smartserver.smartshare.dk \
--to=mb@smartsharesystems.com \
--cc=ajit.khaparde@broadcom.com \
--cc=andrew.rybchenko@oktetlabs.ru \
--cc=bruce.richardson@intel.com \
--cc=david.marchand@redhat.com \
--cc=dev@dpdk.org \
--cc=ferruh.yigit@amd.com \
--cc=grive@u256.net \
--cc=haijie1@huawei.com \
--cc=longli@microsoft.com \
--cc=roretzla@linux.microsoft.com \
--cc=somnath.kotur@broadcom.com \
--cc=thomas@monjalon.net \
--cc=weh@microsoft.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 NNTP newsgroup(s).