[01/10] eal: add workaround for __builtin_constant_p
Checks
Commit Message
There's no MSVC equivalent for compiler extension __builtin_constant_p.
EAL already had __rte_constant which was used as a first attempt to
workaround __builtin_constant_p when using MSVC. However, there are
pieces of code that would benefit from being able to provide a default
value to be used instead of it being always 0 like how it was done by
__rte_constant.
A new macro is added here allowing such default to be provided by the
caller.
Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
---
lib/eal/include/generic/rte_pause.h | 2 +-
lib/eal/include/rte_common.h | 12 ++++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
Comments
On Tue, 11 Feb 2025 14:01:57 -0800
Andre Muezerie <andremue@linux.microsoft.com> wrote:
> There's no MSVC equivalent for compiler extension __builtin_constant_p.
> EAL already had __rte_constant which was used as a first attempt to
> workaround __builtin_constant_p when using MSVC. However, there are
> pieces of code that would benefit from being able to provide a default
> value to be used instead of it being always 0 like how it was done by
> __rte_constant.
>
> A new macro is added here allowing such default to be provided by the
> caller.
>
> Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
There is a hack way of determining if expression is constant using sizeof
tricks, maybe that would be ideal solution?
Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
On 2025/2/12 6:01, Andre Muezerie wrote:
> There's no MSVC equivalent for compiler extension __builtin_constant_p.
> EAL already had __rte_constant which was used as a first attempt to
> workaround __builtin_constant_p when using MSVC. However, there are
> pieces of code that would benefit from being able to provide a default
> value to be used instead of it being always 0 like how it was done by
> __rte_constant.
>
> A new macro is added here allowing such default to be provided by the
> caller.
>
> Signed-off-by: Andre Muezerie <andremue@linux.microsoft.com>
@@ -130,7 +130,7 @@ rte_wait_until_equal_64(volatile uint64_t *addr, uint64_t expected,
* rte_memory_order_acquire and rte_memory_order_relaxed.
*/
#define RTE_WAIT_UNTIL_MASKED(addr, mask, cond, expected, memorder) do { \
- RTE_BUILD_BUG_ON(!__builtin_constant_p(memorder)); \
+ RTE_BUILD_BUG_ON(!__rte_constant_with_default(memorder, 1)); \
RTE_BUILD_BUG_ON((memorder) != rte_memory_order_acquire && \
(memorder) != rte_memory_order_relaxed); \
typeof(*(addr)) expected_value = (expected); \
@@ -50,6 +50,18 @@ extern "C" {
#define __rte_constant(e) __extension__(__builtin_constant_p(e))
#endif
+/**
+ * Determine if an expression's value is constant at compile time.
+ * All compilers except MSVC will return 1 if the first argument is a
+ * compile-time constant, and 0 otherwise. MSVC will just return the second
+ * argument as a default value.
+ */
+#ifdef RTE_TOOLCHAIN_MSVC
+#define __rte_constant_with_default(e, def) def
+#else
+#define __rte_constant_with_default(e, def) __extension__(__builtin_constant_p(e))
+#endif
+
/*
* RTE_TOOLCHAIN_GCC is defined if the target is built with GCC,
* while a host application (like pmdinfogen) may have another compiler.