Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canonicalize away bit width and embed small integers into IntIds #4487

Merged
merged 14 commits into from
Nov 13, 2024
32 changes: 32 additions & 0 deletions toolchain/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ cc_library(
hdrs = ["value_ids.h"],
deps = [
":index_base",
"//common:check",
"//common:ostream",
"@llvm-project//llvm:Support",
],
Expand Down Expand Up @@ -80,10 +81,41 @@ cc_test(
],
)

cc_library(
name = "int_store",
srcs = ["int_store.cpp"],
hdrs = ["int_store.h"],
deps = [
":index_base",
":mem_usage",
":value_store",
":yaml",
"//common:check",
"//common:hashtable_key_context",
"//common:ostream",
"//common:set",
"@llvm-project//llvm:Support",
],
)

cc_test(
name = "int_store_test",
size = "small",
srcs = ["int_store_test.cpp"],
deps = [
":int_store",
"//testing/base:gtest_main",
"//testing/base:test_raw_ostream",
"//toolchain/testing:yaml_test_helpers",
"@googletest//:gtest",
],
)

cc_library(
name = "shared_value_stores",
hdrs = ["shared_value_stores.h"],
deps = [
":int_store",
":mem_usage",
":value_ids",
":value_store",
Expand Down
66 changes: 66 additions & 0 deletions toolchain/base/int_store.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include "toolchain/base/int_store.h"

namespace Carbon {

auto IntStore::CanonicalBitWidth(int significant_bits) -> int {
// For larger integers, we store them in as a signed APInt with a canonical
// width that is the smallest multiple of the word type's bits, but no
// smaller than a minimum of 64 bits to avoid spurious resizing of the most
// common cases (<= 64 bits).
static constexpr int WordWidth = llvm::APInt::APINT_BITS_PER_WORD;

return std::max<int>(
MinAPWidth, ((significant_bits + WordWidth - 1) / WordWidth) * WordWidth);
}

auto IntStore::CanonicalizeSigned(llvm::APInt value) -> llvm::APInt {
return value.sextOrTrunc(CanonicalBitWidth(value.getSignificantBits()));
}

auto IntStore::CanonicalizeUnsigned(llvm::APInt value) -> llvm::APInt {
// We need the width to include a zero sign bit as we canonicalize to a
// signed representation.
return value.zextOrTrunc(CanonicalBitWidth(value.getActiveBits() + 1));
}

auto IntStore::AddLarge(int64_t value) -> IntId {
auto ap_id =
values_.Add(llvm::APInt(CanonicalBitWidth(64), value, /*isSigned=*/true));
return MakeIndexOrInvalid(ap_id.index);
}

auto IntStore::AddSignedLarge(llvm::APInt value) -> IntId {
auto ap_id = values_.Add(CanonicalizeSigned(value));
return MakeIndexOrInvalid(ap_id.index);
}

auto IntStore::AddUnsignedLarge(llvm::APInt value) -> IntId {
auto ap_id = values_.Add(CanonicalizeUnsigned(value));
return MakeIndexOrInvalid(ap_id.index);
}

auto IntStore::LookupLarge(int64_t value) const -> IntId {
auto ap_id = values_.Lookup(
llvm::APInt(CanonicalBitWidth(64), value, /*isSigned=*/true));
return MakeIndexOrInvalid(ap_id.index);
}

auto IntStore::LookupSignedLarge(llvm::APInt value) const -> IntId {
auto ap_id = values_.Lookup(CanonicalizeSigned(value));
return MakeIndexOrInvalid(ap_id.index);
}

auto IntStore::OutputYaml() const -> Yaml::OutputMapping {
return values_.OutputYaml();
}

auto IntStore::CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
-> void {
mem_usage.Collect(std::string(label), values_);
}

} // namespace Carbon
Loading
Loading