[PATCH 1/2] eal: add unreachable and precondition hints
Morten Brørup
mb at smartsharesystems.com
Fri Oct 25 13:52:22 CEST 2024
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 at 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
+
/**
* 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)
+#elif defined(RTE_TOOLCHAIN_CLANG)
+#define __rte_assume(condition) __extension__(__builtin_assume(condition))
+#else
+/* MSVC or ICC */
+#define __rte_assume(condition) __assume(condition)
+#endif
+
/**
* Disable AddressSanitizer on some code
*/
--
2.43.0
More information about the dev
mailing list