[flang-commits] [flang] [flang][NFC] static assert intrinsic table is sorted (PR #120399)

via flang-commits flang-commits at lists.llvm.org
Wed Dec 18 02:37:04 PST 2024


https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/120399

This invariant is used below when searching for intrinsic implementation. Currently, if the map is not sorted, the compiler will just silently assume there is no such implementation.

>From f24d9a34e105c1631884a586b02393edfda6967d Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Wed, 18 Dec 2024 02:09:13 -0800
Subject: [PATCH] [flang][NFC] static assert intrinsic table is sorted

---
 flang/lib/Optimizer/Builder/IntrinsicCall.cpp | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
index 547cebefd2df47..7fe233f1090703 100644
--- a/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
+++ b/flang/lib/Optimizer/Builder/IntrinsicCall.cpp
@@ -678,6 +678,20 @@ static constexpr IntrinsicHandler handlers[]{
      /*isElemental=*/true},
 };
 
+template <std::size_t N>
+static constexpr bool isSorted(const IntrinsicHandler (&array)[N]) {
+  // Replace by std::sorted when C++20 is default (will be constexpr).
+  const IntrinsicHandler *lastSeen{nullptr};
+  bool isSorted{true};
+  for (const auto &x : array) {
+    if (lastSeen)
+      isSorted &= std::string_view{lastSeen->name} < std::string_view{x.name};
+    lastSeen = &x;
+  }
+  return isSorted;
+}
+static_assert(isSorted(handlers) && "map must be sorted");
+
 static const IntrinsicHandler *findIntrinsicHandler(llvm::StringRef name) {
   auto compare = [](const IntrinsicHandler &handler, llvm::StringRef name) {
     return name.compare(handler.name) > 0;



More information about the flang-commits mailing list