diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -267,7 +267,7 @@ return adl_begin(RangeOrContainer) == adl_end(RangeOrContainer); } -/// Returns true of the given range only contains a single element. +/// Returns true if the given container only contains a single element. template bool hasSingleElement(ContainerTy &&c) { auto it = std::begin(c), e = std::end(c); return it != e && std::next(it) == e; @@ -1924,7 +1924,7 @@ /// Return true if the sequence [Begin, End) has N or more items. Runs in O(N) /// time. Not meant for use with random-access iterators. -/// Can optionally take a predicate to filter lazily some items. +/// Can optionally take a predicate to lazily filter some items. template()) &)> bool hasNItemsOrMore( @@ -1944,6 +1944,36 @@ return true; } +/// Returns true if the sequence [Begin, End) has N or less items. Can +/// optionally take a predicate to lazily filter some items +template ()) &)> +bool hasNItemsOrLess( + IterTy &&Begin, IterTy &&End, unsigned N, + Pred &&ShouldBeCounted = [](const decltype(*std::declval()) &) { + return true; + }) { + assert(N != std::numeric_limits::max()); + return !hasNItemsOrMore(Begin, End, N + 1, ShouldBeCounted); +} + +/// Returns true if the given container has exactly N items +template bool hasNItems(ContainerTy &&c, unsigned N) { + return hasNItems(std::begin(c), std::end(c), N); +} + +/// Returns true if the given container has N or more items +template +bool hasNItemsOrMore(ContainerTy &&c, unsigned N) { + return hasNItemsOrMore(std::begin(c), std::end(c), N); +} + +/// Returns true if the given container has N or less items +template +bool hasNItemsOrLess(ContainerTy &&c, unsigned N) { + return hasNItemsOrLess(std::begin(c), std::end(c), N); +} + /// Returns a raw pointer that represents the same address as the argument. /// /// This implementation can be removed once we move to C++20 where it's defined diff --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td --- a/mlir/include/mlir/IR/OpBase.td +++ b/mlir/include/mlir/IR/OpBase.td @@ -1586,7 +1586,7 @@ // A region with the given number of blocks. class SizedRegion : Region< - CPred<"$_self.getBlocks().size() == " # numBlocks>, + CPred<"llvm::hasNItems($_self, " # numBlocks # ")">, "region with " # numBlocks # " blocks">; // A variadic region constraint. It expands to zero or more of the base region.