[Mlir-commits] [mlir] [mlir] Fold interface-map trait packs without recursive templates (NFC) (PR #226084)
Mehdi Amini
llvmlistbot at llvm.org
Thu Sep 24 02:16:05 PDT 2026
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/226084
Count interface traits and insert their models with fold expressions. This keeps insertion order and avoids two recursive template chains for every registered operation.
Assisted-by: Codex
>From 959ad3ee4d5363a3c95ef48d857716c99fd4bc27 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Wed, 23 Sep 2026 17:51:23 -0700
Subject: [PATCH] [mlir] Fold interface-map trait packs without recursive
templates
Count interface traits and insert their models with fold expressions. This
keeps insertion order and avoids two recursive template chains for every
registered operation.
Assisted-by: Codex
---
mlir/include/mlir/Support/InterfaceSupport.h | 32 +++++---------------
1 file changed, 7 insertions(+), 25 deletions(-)
diff --git a/mlir/include/mlir/Support/InterfaceSupport.h b/mlir/include/mlir/Support/InterfaceSupport.h
index c70c081b7fcbc..6f654f1785713 100644
--- a/mlir/include/mlir/Support/InterfaceSupport.h
+++ b/mlir/include/mlir/Support/InterfaceSupport.h
@@ -140,18 +140,6 @@ class Interface : public BaseType {
// InterfaceMap
//===----------------------------------------------------------------------===//
-/// Template utility that computes the number of elements within `T` that
-/// satisfy the given predicate.
-template <template <class> class Pred, size_t N, typename... Ts>
-struct count_if_t_impl : public std::integral_constant<size_t, N> {};
-template <template <class> class Pred, size_t N, typename T, typename... Us>
-struct count_if_t_impl<Pred, N, T, Us...>
- : public std::integral_constant<
- size_t,
- count_if_t_impl<Pred, N + (Pred<T>::value ? 1 : 0), Us...>::value> {};
-template <template <class> class Pred, typename... Ts>
-using count_if_t = count_if_t_impl<Pred, 0, Ts...>;
-
/// This class provides an efficient mapping between a given `Interface` type,
/// and a particular implementation of its concept.
class InterfaceMap {
@@ -160,8 +148,6 @@ class InterfaceMap {
using has_get_interface_id = decltype(T::getInterfaceID());
template <typename T>
using detect_get_interface_id = llvm::is_detected<has_get_interface_id, T>;
- template <typename... Types>
- using num_interface_types_t = count_if_t<detect_get_interface_id, Types...>;
/// Trait to check if T provides a 'initializeInterfaceConcept' method.
template <typename T, typename... Args>
@@ -191,7 +177,8 @@ class InterfaceMap {
/// do not represent interfaces are not added to the interface map.
template <typename... Types>
static InterfaceMap get() {
- constexpr size_t numInterfaces = num_interface_types_t<Types...>::value;
+ constexpr size_t numInterfaces =
+ (size_t{0} + ... + detect_get_interface_id<Types>::value);
if constexpr (numInterfaces == 0) {
return InterfaceMap();
} else {
@@ -218,22 +205,17 @@ class InterfaceMap {
}
private:
- /// Insert the given interface types into the map (recursive expansion to
- /// guarantee sequential, left-to-right evaluation across all compilers).
- template <typename T>
- void insertPotentialInterfaces() {
- insertPotentialInterface<T>();
- }
- template <typename T, typename T2, typename... Rest>
+ /// Insert the given interface types in source order. A comma fold is
+ /// sequenced left to right in C++17.
+ template <typename... Types>
void insertPotentialInterfaces() {
- insertPotentialInterface<T>();
- insertPotentialInterfaces<T2, Rest...>();
+ (insertPotentialInterface<Types>(), ...);
}
/// Insert the given interface type into the map, ignoring it if it doesn't
/// actually represent an interface.
template <typename T>
- inline void insertPotentialInterface() {
+ void insertPotentialInterface() {
if constexpr (detect_get_interface_id<T>::value)
insertModel<typename T::ModelT>();
}
More information about the Mlir-commits
mailing list