From 58d602f9180402f59484045bfa92647fa069e374 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Thu, 9 Oct 2025 11:41:29 +0900 Subject: [PATCH 1/3] ADT: Add constructor from uint64_t array for Bitset Avoids exposing the implementation detail of uintptr_t to the constructor. This is a replacement of b738f630f73975bc591a82fdcb4858ae5da67477 which avoids needing tablegen to know the underlying storage type. --- llvm/include/llvm/ADT/Bitset.h | 13 ++++++++- llvm/unittests/ADT/BitsetTest.cpp | 44 +++++++++++++++++++++++++++++++ llvm/unittests/ADT/CMakeLists.txt | 1 + 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 llvm/unittests/ADT/BitsetTest.cpp diff --git a/llvm/include/llvm/ADT/Bitset.h b/llvm/include/llvm/ADT/Bitset.h index b1e539e39aff7..b64648f9bccc4 100644 --- a/llvm/include/llvm/ADT/Bitset.h +++ b/llvm/include/llvm/ADT/Bitset.h @@ -45,7 +45,18 @@ class Bitset { StorageType Bits{}; protected: - constexpr Bitset(const StorageType &B) : Bits{B} {} + constexpr Bitset(const std::array &B) { + if (sizeof(BitWord) == sizeof(uint64_t)) { + for (size_t I = 0; I != B.size(); ++I) + Bits[I] = B[I]; + } else { + for (size_t I = 0; I != B.size(); ++I) { + uint64_t Elt = B[I]; + Bits[2 * I] = static_cast(Elt); + Bits[2 * I + 1] = static_cast(Elt >> 32); + } + } + } public: constexpr Bitset() = default; diff --git a/llvm/unittests/ADT/BitsetTest.cpp b/llvm/unittests/ADT/BitsetTest.cpp new file mode 100644 index 0000000000000..8877397b30c53 --- /dev/null +++ b/llvm/unittests/ADT/BitsetTest.cpp @@ -0,0 +1,44 @@ +//===- llvm/unittest/Support/BitsetTest.cpp -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/Bitset.h" +#include "gtest/gtest.h" + +using namespace llvm; + +namespace { + +template +class TestBitsetUInt64Array : public Bitset { + static constexpr unsigned NumElts = (NumBits + 63) / 64; + +public: + TestBitsetUInt64Array(const std::array &B) + : Bitset(B) {} + + bool verifyValue(const std::array &B) const { + for (unsigned I = 0; I != NumBits; ++I) { + bool ReferenceVal = + (B[(I / 64)] & (static_cast(1) << (I % 64))) != 0; + if (ReferenceVal != this->test(I)) + return false; + } + + return true; + } +}; + +TEST(BitsetTest, Construction) { + std::array TestVals = {0x123456789abcdef3, 0x1337d3a0b22c24}; + TestBitsetUInt64Array<96> Test(TestVals); + EXPECT_TRUE(Test.verifyValue(TestVals)); + + TestBitsetUInt64Array<65> Test1(TestVals); + EXPECT_TRUE(Test1.verifyValue(TestVals)); +} +} // namespace diff --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt index dafd73518aedb..848ccba696e76 100644 --- a/llvm/unittests/ADT/CMakeLists.txt +++ b/llvm/unittests/ADT/CMakeLists.txt @@ -12,6 +12,7 @@ add_llvm_unittest(ADTTests BitFieldsTest.cpp BitmaskEnumTest.cpp BitTest.cpp + BitsetTest.cpp BitVectorTest.cpp BreadthFirstIteratorTest.cpp BumpPtrListTest.cpp From 93b0df66a345f08fb2633874b8286ab98e86a83c Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Fri, 10 Oct 2025 11:16:27 +0900 Subject: [PATCH 2/3] Use if constexpr --- llvm/include/llvm/ADT/Bitset.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/include/llvm/ADT/Bitset.h b/llvm/include/llvm/ADT/Bitset.h index b64648f9bccc4..851ae19d12def 100644 --- a/llvm/include/llvm/ADT/Bitset.h +++ b/llvm/include/llvm/ADT/Bitset.h @@ -46,7 +46,7 @@ class Bitset { protected: constexpr Bitset(const std::array &B) { - if (sizeof(BitWord) == sizeof(uint64_t)) { + if constexpr (sizeof(BitWord) == sizeof(uint64_t)) { for (size_t I = 0; I != B.size(); ++I) Bits[I] = B[I]; } else { From af2ebac24519d4e049918419dc126c48125a1dc7 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Fri, 10 Oct 2025 11:17:21 +0900 Subject: [PATCH 3/3] Make StorageType private --- llvm/include/llvm/ADT/Bitset.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/llvm/include/llvm/ADT/Bitset.h b/llvm/include/llvm/ADT/Bitset.h index 851ae19d12def..0dfeb2088dfa0 100644 --- a/llvm/include/llvm/ADT/Bitset.h +++ b/llvm/include/llvm/ADT/Bitset.h @@ -38,10 +38,7 @@ class Bitset { static constexpr unsigned NumWords = (NumBits + BitwordBits - 1) / BitwordBits; -protected: using StorageType = std::array; - -private: StorageType Bits{}; protected: