From fd0ccbb2d5e2d69d55761948c62fc9f7c687473a Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 6 Sep 2025 20:52:06 -0700 Subject: [PATCH 1/2] [ADT] Reduce boilerplate in DenseSet (NFC) The class definitions of DenseSet and SmallDenseSet contain a lot of boilerplate code, repeating the lengthy base class name twice in each definition. This patch simplifies the two definitions by making them type aliases. The patch adds an implicit default constructor. Without the implicit default constructor, the following wouldn't work: Index.exportToDot(OSDot, {}); One caveat is that with the type alias-based solution, forward declarations like "class DenseSet;" are no longer valid. This patch updates the forward declaration in MLIR. --- llvm/include/llvm/ADT/DenseSet.h | 42 +++++++++----------------------- mlir/include/mlir/Support/LLVM.h | 15 +++++++++--- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h index 281d4d1c78cc0..9668eb6640d1f 100644 --- a/llvm/include/llvm/ADT/DenseSet.h +++ b/llvm/include/llvm/ADT/DenseSet.h @@ -66,7 +66,8 @@ class DenseSetImpl { using value_type = ValueT; using size_type = unsigned; - explicit DenseSetImpl(unsigned InitialReserve = 0) : TheMap(InitialReserve) {} + DenseSetImpl() = default; + explicit DenseSetImpl(unsigned InitialReserve) : TheMap(InitialReserve) {} template DenseSetImpl(const InputIt &I, const InputIt &E) @@ -254,40 +255,21 @@ bool operator!=(const DenseSetImpl &LHS, /// Implements a dense probed hash-table based set. template > -class DenseSet : public detail::DenseSetImpl< - ValueT, - DenseMap>, - ValueInfoT> { - using BaseT = - detail::DenseSetImpl>, - ValueInfoT>; - -public: - using BaseT::BaseT; -}; +using DenseSet = + detail::DenseSetImpl>, + ValueInfoT>; /// Implements a dense probed hash-table based set with some number of buckets /// stored inline. template > -class SmallDenseSet - : public detail::DenseSetImpl< - ValueT, - SmallDenseMap>, - ValueInfoT> { - using BaseT = detail::DenseSetImpl< - ValueT, - SmallDenseMap>, - ValueInfoT>; - -public: - using BaseT::BaseT; -}; +using SmallDenseSet = detail::DenseSetImpl< + ValueT, + SmallDenseMap>, + ValueInfoT>; } // end namespace llvm diff --git a/mlir/include/mlir/Support/LLVM.h b/mlir/include/mlir/Support/LLVM.h index 020c0fba726c8..8fd3b7c2f40f2 100644 --- a/mlir/include/mlir/Support/LLVM.h +++ b/mlir/include/mlir/Support/LLVM.h @@ -55,8 +55,13 @@ template class DenseMap; template struct DenseMapInfo; -template -class DenseSet; +namespace detail { +template +class DenseSetImpl; +struct DenseSetEmpty; +template +class DenseSetPair; +} // namespace detail class MallocAllocator; template class MutableArrayRef; @@ -125,7 +130,11 @@ template > using DenseMap = llvm::DenseMap; template > -using DenseSet = llvm::DenseSet; +using DenseSet = llvm::detail::DenseSetImpl< + ValueT, + llvm::DenseMap>, + ValueInfoT>; template , typename Set = DenseSet, unsigned N = 0> using SetVector = llvm::SetVector; From ba7eb1d7fe18755d27776cd77f554fa0155e06c1 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sun, 14 Sep 2025 07:23:35 -0700 Subject: [PATCH 2/2] Address a comment. --- llvm/include/llvm/ADT/DenseSet.h | 38 ++++++++++++++++++++++---------- mlir/include/mlir/Support/LLVM.h | 15 +++---------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/llvm/include/llvm/ADT/DenseSet.h b/llvm/include/llvm/ADT/DenseSet.h index 9668eb6640d1f..60ad9b2eb7762 100644 --- a/llvm/include/llvm/ADT/DenseSet.h +++ b/llvm/include/llvm/ADT/DenseSet.h @@ -66,8 +66,7 @@ class DenseSetImpl { using value_type = ValueT; using size_type = unsigned; - DenseSetImpl() = default; - explicit DenseSetImpl(unsigned InitialReserve) : TheMap(InitialReserve) {} + explicit DenseSetImpl(unsigned InitialReserve = 0) : TheMap(InitialReserve) {} template DenseSetImpl(const InputIt &I, const InputIt &E) @@ -251,25 +250,40 @@ bool operator!=(const DenseSetImpl &LHS, return !(LHS == RHS); } +template +using DenseSet = DenseSetImpl< + ValueT, DenseMap>, + ValueInfoT>; + +template +using SmallDenseSet = + DenseSetImpl>, + ValueInfoT>; + } // end namespace detail /// Implements a dense probed hash-table based set. template > -using DenseSet = - detail::DenseSetImpl>, - ValueInfoT>; +class DenseSet : public detail::DenseSet { + using BaseT = detail::DenseSet; + +public: + using BaseT::BaseT; +}; /// Implements a dense probed hash-table based set with some number of buckets /// stored inline. template > -using SmallDenseSet = detail::DenseSetImpl< - ValueT, - SmallDenseMap>, - ValueInfoT>; +class SmallDenseSet + : public detail::SmallDenseSet { + using BaseT = detail::SmallDenseSet; + +public: + using BaseT::BaseT; +}; } // end namespace llvm diff --git a/mlir/include/mlir/Support/LLVM.h b/mlir/include/mlir/Support/LLVM.h index 8fd3b7c2f40f2..020c0fba726c8 100644 --- a/mlir/include/mlir/Support/LLVM.h +++ b/mlir/include/mlir/Support/LLVM.h @@ -55,13 +55,8 @@ template class DenseMap; template struct DenseMapInfo; -namespace detail { -template -class DenseSetImpl; -struct DenseSetEmpty; -template -class DenseSetPair; -} // namespace detail +template +class DenseSet; class MallocAllocator; template class MutableArrayRef; @@ -130,11 +125,7 @@ template > using DenseMap = llvm::DenseMap; template > -using DenseSet = llvm::detail::DenseSetImpl< - ValueT, - llvm::DenseMap>, - ValueInfoT>; +using DenseSet = llvm::DenseSet; template , typename Set = DenseSet, unsigned N = 0> using SetVector = llvm::SetVector;