SPIRV-Tools/test/util/bit_vector_test.cpp
Steven Perron d42f65e7c1 Use a bit vector in ADCE
The unordered_set in ADCE that holds all of the live instructions takes
a very long time to be destroyed.  In some shaders, it takes over 40% of
the time.

If we look at the unique ids of the live instructions, I believe they
are dense enough make a simple bit vector a good choice for to hold that
data.  When I check the density of the bit vector for larger shaders, we
are usually using less than 4 bytes per element in the vector, and
almost always less than 16.

So, in this commit, I introduce a simple bit vector class, and
use it in ADCE.

This help improve the compile time for some shaders on windows by the
40% mentioned above.

Contributes to https://github.com/KhronosGroup/SPIRV-Tools/issues/1328.
2018-04-13 16:38:02 -04:00

113 lines
2.7 KiB
C++

// Copyright (c) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <vector>
#include "gmock/gmock.h"
#include "util/bit_vector.h"
namespace {
using spvtools::utils::BitVector;
using BitVectorTest = ::testing::Test;
TEST(BitVectorTest, Initialize) {
BitVector bvec;
// Checks that all values are 0. Also tests checking a bit past the end of
// the vector containing the bits.
for (int i = 1; i < 10000; i *= 2) {
EXPECT_FALSE(bvec.Get(i));
}
}
TEST(BitVectorTest, Set) {
BitVector bvec;
// Since 10,000 is larger than the initial size, this tests the resizing
// code.
for (int i = 3; i < 10000; i *= 2) {
bvec.Set(i);
}
// Check that bits that were not set are 0.
for (int i = 1; i < 10000; i *= 2) {
EXPECT_FALSE(bvec.Get(i));
}
// Check that bits that were set are 1.
for (int i = 3; i < 10000; i *= 2) {
EXPECT_TRUE(bvec.Get(i));
}
}
TEST(BitVectorTest, SetReturnValue) {
BitVector bvec;
// Make sure |Set| returns false when the bit was not set.
for (int i = 3; i < 10000; i *= 2) {
EXPECT_FALSE(bvec.Set(i));
}
// Make sure |Set| returns true when the bit was already set.
for (int i = 3; i < 10000; i *= 2) {
EXPECT_TRUE(bvec.Set(i));
}
}
TEST(BitVectorTest, Clear) {
BitVector bvec;
for (int i = 3; i < 10000; i *= 2) {
bvec.Set(i);
}
// Check that the bits were properly set.
for (int i = 3; i < 10000; i *= 2) {
EXPECT_TRUE(bvec.Get(i));
}
// Clear all of the bits except for bit 3.
for (int i = 6; i < 10000; i *= 2) {
bvec.Clear(i);
}
// Make sure bit 3 was not cleared.
EXPECT_TRUE(bvec.Get(3));
// Make sure all of the other bits that were set have been cleared.
for (int i = 6; i < 10000; i *= 2) {
EXPECT_FALSE(bvec.Get(i));
}
}
TEST(BitVectorTest, ClearReturnValue) {
BitVector bvec;
for (int i = 3; i < 10000; i *= 2) {
bvec.Set(i);
}
// Make sure |Clear| returns true if the bit was set.
for (int i = 3; i < 10000; i *= 2) {
EXPECT_TRUE(bvec.Clear(i));
}
// Make sure |Clear| returns false if the bit was not set.
for (int i = 3; i < 10000; i *= 2) {
EXPECT_FALSE(bvec.Clear(i));
}
}
} // namespace