Skip to content

Commit 21de514

Browse files
authored
[flang][NFC] static assert intrinsic table is sorted (#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.
1 parent d2413d4 commit 21de514

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,20 @@ static constexpr IntrinsicHandler handlers[]{
684684
/*isElemental=*/true},
685685
};
686686

687+
template <std::size_t N>
688+
static constexpr bool isSorted(const IntrinsicHandler (&array)[N]) {
689+
// Replace by std::sorted when C++20 is default (will be constexpr).
690+
const IntrinsicHandler *lastSeen{nullptr};
691+
bool isSorted{true};
692+
for (const auto &x : array) {
693+
if (lastSeen)
694+
isSorted &= std::string_view{lastSeen->name} < std::string_view{x.name};
695+
lastSeen = &x;
696+
}
697+
return isSorted;
698+
}
699+
static_assert(isSorted(handlers) && "map must be sorted");
700+
687701
static const IntrinsicHandler *findIntrinsicHandler(llvm::StringRef name) {
688702
auto compare = [](const IntrinsicHandler &handler, llvm::StringRef name) {
689703
return name.compare(handler.name) > 0;

0 commit comments

Comments
 (0)