220 changes: 156 additions & 64 deletions llvm/lib/Transforms/IPO/LowerBitSets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ using namespace llvm;

#define DEBUG_TYPE "lowerbitsets"

STATISTIC(NumBitSetsCreated, "Number of bitsets created");
STATISTIC(ByteArraySizeBits, "Byte array size in bits");
STATISTIC(ByteArraySizeBytes, "Byte array size in bytes");
STATISTIC(NumByteArraysCreated, "Number of byte arrays created");
STATISTIC(NumBitSetCallsLowered, "Number of bitset calls lowered");
STATISTIC(NumBitSetDisjointSets, "Number of disjoint sets of bitsets");

Expand All @@ -46,7 +48,7 @@ bool BitSetInfo::containsGlobalOffset(uint64_t Offset) const {
if (BitOffset >= BitSize)
return false;

return (Bits[BitOffset / 8] >> (BitOffset % 8)) & 1;
return Bits.count(BitOffset);
}

bool BitSetInfo::containsValue(
Expand Down Expand Up @@ -101,18 +103,15 @@ BitSetInfo BitSetBuilder::build() {
BSI.ByteOffset = Min;

BSI.AlignLog2 = 0;
// FIXME: Can probably do something smarter if all offsets are 0.
if (Mask != 0)
BSI.AlignLog2 = countTrailingZeros(Mask, ZB_Undefined);

// Build the compressed bitset while normalizing the offsets against the
// computed alignment.
BSI.BitSize = ((Max - Min) >> BSI.AlignLog2) + 1;
uint64_t ByteSize = (BSI.BitSize + 7) / 8;
BSI.Bits.resize(ByteSize);
for (uint64_t Offset : Offsets) {
Offset >>= BSI.AlignLog2;
BSI.Bits[Offset / 8] |= 1 << (Offset % 8);
BSI.Bits.insert(Offset);
}

return BSI;
Expand Down Expand Up @@ -147,14 +146,46 @@ void GlobalLayoutBuilder::addFragment(const std::set<uint64_t> &F) {
FragmentMap[ObjIndex] = FragmentIndex;
}

void ByteArrayBuilder::allocate(const std::set<uint64_t> &Bits,
uint64_t BitSize, uint64_t &AllocByteOffset,
uint8_t &AllocMask) {
// Find the smallest current allocation.
unsigned Bit = 0;
for (unsigned I = 1; I != BitsPerByte; ++I)
if (BitAllocs[I] < BitAllocs[Bit])
Bit = I;

AllocByteOffset = BitAllocs[Bit];

// Add our size to it.
unsigned ReqSize = AllocByteOffset + BitSize;
BitAllocs[Bit] = ReqSize;
if (Bytes.size() < ReqSize)
Bytes.resize(ReqSize);

// Set our bits.
AllocMask = 1 << Bit;
for (uint64_t B : Bits)
Bytes[AllocByteOffset + B] |= AllocMask;
}

namespace {

struct ByteArrayInfo {
std::set<uint64_t> Bits;
uint64_t BitSize;
GlobalVariable *ByteArray;
Constant *Mask;
};

struct LowerBitSets : public ModulePass {
static char ID;
LowerBitSets() : ModulePass(ID) {
initializeLowerBitSetsPass(*PassRegistry::getPassRegistry());
}

Module *M;

const DataLayout *DL;
IntegerType *Int1Ty;
IntegerType *Int8Ty;
Expand All @@ -169,20 +200,23 @@ struct LowerBitSets : public ModulePass {
// Mapping from bitset mdstrings to the call sites that test them.
DenseMap<MDString *, std::vector<CallInst *>> BitSetTestCallSites;

std::vector<ByteArrayInfo> ByteArrayInfos;

BitSetInfo
buildBitSet(MDString *BitSet,
const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout);
Value *createBitSetTest(IRBuilder<> &B, const BitSetInfo &BSI,
GlobalVariable *BitSetGlobal, Value *BitOffset);
ByteArrayInfo *createByteArray(BitSetInfo &BSI);
void allocateByteArrays();
Value *createBitSetTest(IRBuilder<> &B, BitSetInfo &BSI, ByteArrayInfo *&BAI,
Value *BitOffset);
Value *
lowerBitSetCall(CallInst *CI, const BitSetInfo &BSI,
GlobalVariable *BitSetGlobal, GlobalVariable *CombinedGlobal,
lowerBitSetCall(CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI,
GlobalVariable *CombinedGlobal,
const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout);
void buildBitSetsFromGlobals(Module &M,
const std::vector<MDString *> &BitSets,
void buildBitSetsFromGlobals(const std::vector<MDString *> &BitSets,
const std::vector<GlobalVariable *> &Globals);
bool buildBitSets(Module &M);
bool eraseBitSetMetadata(Module &M);
bool buildBitSets();
bool eraseBitSetMetadata();

bool doInitialization(Module &M) override;
bool runOnModule(Module &M) override;
Expand All @@ -198,19 +232,21 @@ char LowerBitSets::ID = 0;

ModulePass *llvm::createLowerBitSetsPass() { return new LowerBitSets; }

bool LowerBitSets::doInitialization(Module &M) {
DL = M.getDataLayout();
bool LowerBitSets::doInitialization(Module &Mod) {
M = &Mod;

DL = M->getDataLayout();
if (!DL)
report_fatal_error("Data layout required");

Int1Ty = Type::getInt1Ty(M.getContext());
Int8Ty = Type::getInt8Ty(M.getContext());
Int32Ty = Type::getInt32Ty(M.getContext());
Int1Ty = Type::getInt1Ty(M->getContext());
Int8Ty = Type::getInt8Ty(M->getContext());
Int32Ty = Type::getInt32Ty(M->getContext());
Int32PtrTy = PointerType::getUnqual(Int32Ty);
Int64Ty = Type::getInt64Ty(M.getContext());
IntPtrTy = DL->getIntPtrType(M.getContext(), 0);
Int64Ty = Type::getInt64Ty(M->getContext());
IntPtrTy = DL->getIntPtrType(M->getContext(), 0);

BitSetNM = M.getNamedMetadata("llvm.bitsets");
BitSetNM = M->getNamedMetadata("llvm.bitsets");

BitSetTestCallSites.clear();

Expand Down Expand Up @@ -259,52 +295,113 @@ static Value *createMaskedBitTest(IRBuilder<> &B, Value *Bits,
return B.CreateICmpNE(MaskedBits, ConstantInt::get(BitsType, 0));
}

ByteArrayInfo *LowerBitSets::createByteArray(BitSetInfo &BSI) {
// Create globals to stand in for byte arrays and masks. These never actually
// get initialized, we RAUW and erase them later in allocateByteArrays() once
// we know the offset and mask to use.
auto ByteArrayGlobal = new GlobalVariable(
*M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr);
auto MaskGlobal = new GlobalVariable(
*M, Int8Ty, /*isConstant=*/true, GlobalValue::PrivateLinkage, nullptr);

ByteArrayInfos.emplace_back();
ByteArrayInfo *BAI = &ByteArrayInfos.back();

BAI->Bits = BSI.Bits;
BAI->BitSize = BSI.BitSize;
BAI->ByteArray = ByteArrayGlobal;
BAI->Mask = ConstantExpr::getPtrToInt(MaskGlobal, Int8Ty);
return BAI;
}

void LowerBitSets::allocateByteArrays() {
std::stable_sort(ByteArrayInfos.begin(), ByteArrayInfos.end(),
[](const ByteArrayInfo &BAI1, const ByteArrayInfo &BAI2) {
return BAI1.BitSize > BAI2.BitSize;
});

std::vector<uint64_t> ByteArrayOffsets(ByteArrayInfos.size());

ByteArrayBuilder BAB;
for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
ByteArrayInfo *BAI = &ByteArrayInfos[I];

uint8_t Mask;
BAB.allocate(BAI->Bits, BAI->BitSize, ByteArrayOffsets[I], Mask);

BAI->Mask->replaceAllUsesWith(ConstantInt::get(Int8Ty, Mask));
cast<GlobalVariable>(BAI->Mask->getOperand(0))->eraseFromParent();
}

Constant *ByteArrayConst = ConstantDataArray::get(M->getContext(), BAB.Bytes);
auto ByteArray =
new GlobalVariable(*M, ByteArrayConst->getType(), /*isConstant=*/true,
GlobalValue::PrivateLinkage, ByteArrayConst);

for (unsigned I = 0; I != ByteArrayInfos.size(); ++I) {
ByteArrayInfo *BAI = &ByteArrayInfos[I];

Constant *Idxs[] = {ConstantInt::get(IntPtrTy, 0),
ConstantInt::get(IntPtrTy, ByteArrayOffsets[I])};
Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(ByteArray, Idxs);

// Create an alias instead of RAUW'ing the gep directly. On x86 this ensures
// that the pc-relative displacement is folded into the lea instead of the
// test instruction getting another displacement.
GlobalAlias *Alias = GlobalAlias::create(
Int8Ty, 0, GlobalValue::PrivateLinkage, "bits", GEP, M);
BAI->ByteArray->replaceAllUsesWith(Alias);
BAI->ByteArray->eraseFromParent();
}

ByteArraySizeBits = BAB.BitAllocs[0] + BAB.BitAllocs[1] + BAB.BitAllocs[2] +
BAB.BitAllocs[3] + BAB.BitAllocs[4] + BAB.BitAllocs[5] +
BAB.BitAllocs[6] + BAB.BitAllocs[7];
ByteArraySizeBytes = BAB.Bytes.size();
}

/// Build a test that bit BitOffset is set in BSI, where
/// BitSetGlobal is a global containing the bits in BSI.
Value *LowerBitSets::createBitSetTest(IRBuilder<> &B, const BitSetInfo &BSI,
GlobalVariable *BitSetGlobal,
Value *BitOffset) {
if (BSI.Bits.size() <= 8) {
Value *LowerBitSets::createBitSetTest(IRBuilder<> &B, BitSetInfo &BSI,
ByteArrayInfo *&BAI, Value *BitOffset) {
if (BSI.BitSize <= 64) {
// If the bit set is sufficiently small, we can avoid a load by bit testing
// a constant.
IntegerType *BitsTy;
if (BSI.Bits.size() <= 4)
if (BSI.BitSize <= 32)
BitsTy = Int32Ty;
else
BitsTy = Int64Ty;

uint64_t Bits = 0;
for (auto I = BSI.Bits.rbegin(), E = BSI.Bits.rend(); I != E; ++I) {
Bits <<= 8;
Bits |= *I;
}
for (auto Bit : BSI.Bits)
Bits |= uint64_t(1) << Bit;
Constant *BitsConst = ConstantInt::get(BitsTy, Bits);
return createMaskedBitTest(B, BitsConst, BitOffset);
} else {
// TODO: We might want to use the memory variant of the bt instruction
// with the previously computed bit offset at -Os. This instruction does
// exactly what we want but has been benchmarked as being slower than open
// coding the load+bt.
Value *BitSetGlobalOffset =
B.CreateLShr(BitOffset, ConstantInt::get(IntPtrTy, 5));
Value *BitSetEntryAddr = B.CreateGEP(
ConstantExpr::getBitCast(BitSetGlobal, Int32PtrTy), BitSetGlobalOffset);
Value *BitSetEntry = B.CreateLoad(BitSetEntryAddr);

return createMaskedBitTest(B, BitSetEntry, BitOffset);
if (!BAI) {
++NumByteArraysCreated;
BAI = createByteArray(BSI);
}

Value *ByteAddr = B.CreateGEP(BAI->ByteArray, BitOffset);
Value *Byte = B.CreateLoad(ByteAddr);

Value *ByteAndMask = B.CreateAnd(Byte, BAI->Mask);
return B.CreateICmpNE(ByteAndMask, ConstantInt::get(Int8Ty, 0));
}
}

/// Lower a llvm.bitset.test call to its implementation. Returns the value to
/// replace the call with.
Value *LowerBitSets::lowerBitSetCall(
CallInst *CI, const BitSetInfo &BSI, GlobalVariable *BitSetGlobal,
CallInst *CI, BitSetInfo &BSI, ByteArrayInfo *&BAI,
GlobalVariable *CombinedGlobal,
const DenseMap<GlobalVariable *, uint64_t> &GlobalLayout) {
Value *Ptr = CI->getArgOperand(0);

if (BSI.containsValue(DL, GlobalLayout, Ptr))
return ConstantInt::getTrue(BitSetGlobal->getParent()->getContext());
return ConstantInt::getTrue(CombinedGlobal->getParent()->getContext());

Constant *GlobalAsInt = ConstantExpr::getPtrToInt(CombinedGlobal, IntPtrTy);
Constant *OffsetedGlobalAsInt = ConstantExpr::getAdd(
Expand Down Expand Up @@ -353,7 +450,7 @@ Value *LowerBitSets::lowerBitSetCall(

// Now that we know that the offset is in range and aligned, load the
// appropriate bit from the bitset.
Value *Bit = createBitSetTest(ThenB, BSI, BitSetGlobal, BitOffset);
Value *Bit = createBitSetTest(ThenB, BSI, BAI, BitOffset);

// The value we want is 0 if we came directly from the initial block
// (having failed the range or alignment checks), or the loaded bit if
Expand All @@ -368,7 +465,6 @@ Value *LowerBitSets::lowerBitSetCall(
/// Given a disjoint set of bitsets and globals, layout the globals, build the
/// bit sets and lower the llvm.bitset.test calls.
void LowerBitSets::buildBitSetsFromGlobals(
Module &M,
const std::vector<MDString *> &BitSets,
const std::vector<GlobalVariable *> &Globals) {
// Build a new global with the combined contents of the referenced globals.
Expand All @@ -391,9 +487,9 @@ void LowerBitSets::buildBitSetsFromGlobals(
}
if (!GlobalInits.empty())
GlobalInits.pop_back();
Constant *NewInit = ConstantStruct::getAnon(M.getContext(), GlobalInits);
Constant *NewInit = ConstantStruct::getAnon(M->getContext(), GlobalInits);
auto CombinedGlobal =
new GlobalVariable(M, NewInit->getType(), /*isConstant=*/true,
new GlobalVariable(*M, NewInit->getType(), /*isConstant=*/true,
GlobalValue::PrivateLinkage, NewInit);

const StructLayout *CombinedGlobalLayout =
Expand All @@ -410,18 +506,12 @@ void LowerBitSets::buildBitSetsFromGlobals(
// Build the bitset.
BitSetInfo BSI = buildBitSet(BS, GlobalLayout);

// Create a global in which to store it.
++NumBitSetsCreated;
Constant *BitsConst = ConstantDataArray::get(M.getContext(), BSI.Bits);
auto BitSetGlobal = new GlobalVariable(
M, BitsConst->getType(), /*isConstant=*/true,
GlobalValue::PrivateLinkage, BitsConst, BS->getString() + ".bits");
ByteArrayInfo *BAI = 0;

// Lower each call to llvm.bitset.test for this bitset.
for (CallInst *CI : BitSetTestCallSites[BS]) {
++NumBitSetCallsLowered;
Value *Lowered =
lowerBitSetCall(CI, BSI, BitSetGlobal, CombinedGlobal, GlobalLayout);
Value *Lowered = lowerBitSetCall(CI, BSI, BAI, CombinedGlobal, GlobalLayout);
CI->replaceAllUsesWith(Lowered);
CI->eraseFromParent();
}
Expand All @@ -439,17 +529,17 @@ void LowerBitSets::buildBitSetsFromGlobals(
GlobalAlias *GAlias = GlobalAlias::create(
Globals[I]->getType()->getElementType(),
Globals[I]->getType()->getAddressSpace(), Globals[I]->getLinkage(),
"", CombinedGlobalElemPtr, &M);
"", CombinedGlobalElemPtr, M);
GAlias->takeName(Globals[I]);
Globals[I]->replaceAllUsesWith(GAlias);
Globals[I]->eraseFromParent();
}
}

/// Lower all bit sets in this module.
bool LowerBitSets::buildBitSets(Module &M) {
bool LowerBitSets::buildBitSets() {
Function *BitSetTestFunc =
M.getFunction(Intrinsic::getName(Intrinsic::bitset_test));
M->getFunction(Intrinsic::getName(Intrinsic::bitset_test));
if (!BitSetTestFunc)
return false;

Expand Down Expand Up @@ -591,22 +681,24 @@ bool LowerBitSets::buildBitSets(Module &M) {
});

// Build the bitsets from this disjoint set.
buildBitSetsFromGlobals(M, BitSets, OrderedGlobals);
buildBitSetsFromGlobals(BitSets, OrderedGlobals);
}

allocateByteArrays();

return true;
}

bool LowerBitSets::eraseBitSetMetadata(Module &M) {
bool LowerBitSets::eraseBitSetMetadata() {
if (!BitSetNM)
return false;

M.eraseNamedMetadata(BitSetNM);
M->eraseNamedMetadata(BitSetNM);
return true;
}

bool LowerBitSets::runOnModule(Module &M) {
bool Changed = buildBitSets(M);
Changed |= eraseBitSetMetadata(M);
bool Changed = buildBitSets();
Changed |= eraseBitSetMetadata();
return Changed;
}
34 changes: 15 additions & 19 deletions llvm/test/Transforms/LowerBitSets/simple.ll
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ target datalayout = "e-p:32:32"
@c = constant i32 3
@d = constant [2 x i32] [i32 4, i32 5]

; CHECK: [[BA:@[^ ]*]] = private constant [68 x i8] c"\03\01\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\02\00\01"

; Offset 0, 4 byte alignment
; CHECK: @bitset1.bits = private constant [9 x i8] c"\03\00\00\00\00\00\00\00\08"
!0 = !{!"bitset1", i32* @a, i32 0}
; CHECK-NODISCARD-DAG: !{!"bitset1", i32* @a, i32 0}
!1 = !{!"bitset1", [63 x i32]* @b, i32 0}
Expand All @@ -19,14 +20,12 @@ target datalayout = "e-p:32:32"
; CHECK-NODISCARD-DAG: !{!"bitset1", [2 x i32]* @d, i32 4}

; Offset 4, 256 byte alignment
; CHECK: @bitset2.bits = private constant [1 x i8] c"\03"
!3 = !{!"bitset2", [63 x i32]* @b, i32 0}
; CHECK-NODISCARD-DAG: !{!"bitset2", [63 x i32]* @b, i32 0}
!4 = !{!"bitset2", i32* @c, i32 0}
; CHECK-NODISCARD-DAG: !{!"bitset2", i32* @c, i32 0}

; Offset 0, 4 byte alignment
; CHECK: @bitset3.bits = private constant [9 x i8] c"\01\00\00\00\00\00\00\00\02"
!5 = !{!"bitset3", i32* @a, i32 0}
; CHECK-NODISCARD-DAG: !{!"bitset3", i32* @a, i32 0}
!6 = !{!"bitset3", i32* @c, i32 0}
Expand All @@ -43,6 +42,9 @@ target datalayout = "e-p:32:32"
; CHECK: @c = alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 4)
; CHECK: @d = alias getelementptr inbounds ({ i32, [0 x i8], [63 x i32], [4 x i8], i32, [0 x i8], [2 x i32] }* [[G]], i32 0, i32 6)

; CHECK: @bits = private alias getelementptr inbounds ([68 x i8]* [[BA]], i32 0, i32 0)
; CHECK: @bits1 = private alias getelementptr inbounds ([68 x i8]* [[BA]], i32 0, i32 0)

declare i1 @llvm.bitset.test(i8* %ptr, metadata %bitset) nounwind readnone

; CHECK: @foo(i32* [[A0:%[^ ]*]])
Expand All @@ -59,15 +61,12 @@ define i1 @foo(i32* %p) {
; CHECK: [[R6:%[^ ]*]] = icmp ult i32 [[R5]], 68
; CHECK: br i1 [[R6]]

; CHECK: [[R8:%[^ ]*]] = lshr i32 [[R5]], 5
; CHECK: [[R9:%[^ ]*]] = getelementptr i32, i32* bitcast ([9 x i8]* @bitset1.bits to i32*), i32 [[R8]]
; CHECK: [[R10:%[^ ]*]] = load i32, i32* [[R9]]
; CHECK: [[R11:%[^ ]*]] = and i32 [[R5]], 31
; CHECK: [[R12:%[^ ]*]] = shl i32 1, [[R11]]
; CHECK: [[R13:%[^ ]*]] = and i32 [[R10]], [[R12]]
; CHECK: [[R14:%[^ ]*]] = icmp ne i32 [[R13]], 0
; CHECK: [[R8:%[^ ]*]] = getelementptr i8, i8* @bits, i32 [[R5]]
; CHECK: [[R9:%[^ ]*]] = load i8, i8* [[R8]]
; CHECK: [[R10:%[^ ]*]] = and i8 [[R9]], 1
; CHECK: [[R11:%[^ ]*]] = icmp ne i8 [[R10]], 0

; CHECK: [[R16:%[^ ]*]] = phi i1 [ false, {{%[^ ]*}} ], [ [[R14]], {{%[^ ]*}} ]
; CHECK: [[R16:%[^ ]*]] = phi i1 [ false, {{%[^ ]*}} ], [ [[R11]], {{%[^ ]*}} ]
%x = call i1 @llvm.bitset.test(i8* %pi8, metadata !"bitset1")

; CHECK-NOT: llvm.bitset.test
Expand Down Expand Up @@ -105,15 +104,12 @@ define i1 @baz(i32* %p) {
; CHECK: [[T6:%[^ ]*]] = icmp ult i32 [[T5]], 66
; CHECK: br i1 [[T6]]

; CHECK: [[T8:%[^ ]*]] = lshr i32 [[T5]], 5
; CHECK: [[T9:%[^ ]*]] = getelementptr i32, i32* bitcast ([9 x i8]* @bitset3.bits to i32*), i32 [[T8]]
; CHECK: [[T10:%[^ ]*]] = load i32, i32* [[T9]]
; CHECK: [[T11:%[^ ]*]] = and i32 [[T5]], 31
; CHECK: [[T12:%[^ ]*]] = shl i32 1, [[T11]]
; CHECK: [[T13:%[^ ]*]] = and i32 [[T10]], [[T12]]
; CHECK: [[T14:%[^ ]*]] = icmp ne i32 [[T13]], 0
; CHECK: [[T8:%[^ ]*]] = getelementptr i8, i8* @bits1, i32 [[T5]]
; CHECK: [[T9:%[^ ]*]] = load i8, i8* [[T8]]
; CHECK: [[T10:%[^ ]*]] = and i8 [[T9]], 2
; CHECK: [[T11:%[^ ]*]] = icmp ne i8 [[T10]], 0

; CHECK: [[T16:%[^ ]*]] = phi i1 [ false, {{%[^ ]*}} ], [ [[T14]], {{%[^ ]*}} ]
; CHECK: [[T16:%[^ ]*]] = phi i1 [ false, {{%[^ ]*}} ], [ [[T11]], {{%[^ ]*}} ]
%x = call i1 @llvm.bitset.test(i8* %pi8, metadata !"bitset3")
; CHECK: ret i1 [[T16]]
ret i1 %x
Expand Down
90 changes: 75 additions & 15 deletions llvm/unittests/Transforms/IPO/LowerBitSets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,39 @@ using namespace llvm;
TEST(LowerBitSets, BitSetBuilder) {
struct {
std::vector<uint64_t> Offsets;
std::vector<uint8_t> Bits;
std::set<uint64_t> Bits;
uint64_t ByteOffset;
uint64_t BitSize;
unsigned AlignLog2;
bool IsSingleOffset;
bool IsAllOnes;
} BSBTests[] = {
{{}, {0}, 0, 1, 0, false, false},
{{0}, {1}, 0, 1, 0, true, true},
{{4}, {1}, 4, 1, 0, true, true},
{{37}, {1}, 37, 1, 0, true, true},
{{0, 1}, {3}, 0, 2, 0, false, true},
{{0, 4}, {3}, 0, 2, 2, false, true},
{{0, uint64_t(1) << 33}, {3}, 0, 2, 33, false, true},
{{3, 7}, {3}, 3, 2, 2, false, true},
{{0, 1, 7}, {131}, 0, 8, 0, false, false},
{{0, 2, 14}, {131}, 0, 8, 1, false, false},
{{0, 1, 8}, {3, 1}, 0, 9, 0, false, false},
{{0, 2, 16}, {3, 1}, 0, 9, 1, false, false},
{{0, 1, 2, 3, 4, 5, 6, 7}, {255}, 0, 8, 0, false, true},
{{0, 1, 2, 3, 4, 5, 6, 7, 8}, {255, 1}, 0, 9, 0, false, true},
{{}, {}, 0, 1, 0, false, false},
{{0}, {0}, 0, 1, 0, true, true},
{{4}, {0}, 4, 1, 0, true, true},
{{37}, {0}, 37, 1, 0, true, true},
{{0, 1}, {0, 1}, 0, 2, 0, false, true},
{{0, 4}, {0, 1}, 0, 2, 2, false, true},
{{0, uint64_t(1) << 33}, {0, 1}, 0, 2, 33, false, true},
{{3, 7}, {0, 1}, 3, 2, 2, false, true},
{{0, 1, 7}, {0, 1, 7}, 0, 8, 0, false, false},
{{0, 2, 14}, {0, 1, 7}, 0, 8, 1, false, false},
{{0, 1, 8}, {0, 1, 8}, 0, 9, 0, false, false},
{{0, 2, 16}, {0, 1, 8}, 0, 9, 1, false, false},
{{0, 1, 2, 3, 4, 5, 6, 7},
{0, 1, 2, 3, 4, 5, 6, 7},
0,
8,
0,
false,
true},
{{0, 1, 2, 3, 4, 5, 6, 7, 8},
{0, 1, 2, 3, 4, 5, 6, 7, 8},
0,
9,
0,
false,
true},
};

for (auto &&T : BSBTests) {
Expand Down Expand Up @@ -93,3 +105,51 @@ TEST(LowerBitSets, GlobalLayoutBuilder) {
EXPECT_EQ(T.WantLayout, ComputedLayout);
}
}

TEST(LowerBitSets, ByteArrayBuilder) {
struct BABAlloc {
std::set<uint64_t> Bits;
uint64_t BitSize;
uint64_t WantByteOffset;
uint8_t WantMask;
};

struct {
std::vector<BABAlloc> Allocs;
std::vector<uint8_t> WantBytes;
} BABTests[] = {
{{{{0}, 1, 0, 1}, {{0}, 1, 0, 2}}, {3}},
{{{{0}, 16, 0, 1},
{{1}, 15, 0, 2},
{{2}, 14, 0, 4},
{{3}, 13, 0, 8},
{{4}, 12, 0, 0x10},
{{5}, 11, 0, 0x20},
{{6}, 10, 0, 0x40},
{{7}, 9, 0, 0x80},
{{0}, 7, 9, 0x80},
{{0}, 6, 10, 0x40},
{{0}, 5, 11, 0x20},
{{0}, 4, 12, 0x10},
{{0}, 3, 13, 8},
{{0}, 2, 14, 4},
{{0}, 1, 15, 2}},
{1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80, 0, 0x80, 0x40, 0x20, 0x10, 8, 4,
2}},
};

for (auto &&T : BABTests) {
ByteArrayBuilder BABuilder;

for (auto &&A : T.Allocs) {
uint64_t GotByteOffset;
uint8_t GotMask;

BABuilder.allocate(A.Bits, A.BitSize, GotByteOffset, GotMask);
EXPECT_EQ(A.WantByteOffset, GotByteOffset);
EXPECT_EQ(A.WantMask, GotMask);
}

EXPECT_EQ(T.WantBytes, BABuilder.Bytes);
}
}