mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-03 22:02:51 +01:00
Use the `CheckIRId` as a unique identifier for the scope of an `InstId` - if an `InstId` is created within the scope of one `CheckIRId` it must not be used in the scope of a different `CheckIRId`. This is achieved without extra storage, but with false negatives for large inputs. When an `InstId` is created, the original index of the `Inst` is XORed with a tag derived from the `CheckIRId` to produce the final `InstId`. When the `InstId` is used, the expected tag is XORed with the `InstId` to get back to the original index - if the tags don't match, the resulting index will be corrupted, likely too large - resulting in an out of bounds index CHECK-failure. (the tag value is derived as such: * take the CheckIRId * left shift one bit (padding zero) * left shift another bit (padding 1 - used to signify that the resulting `InstId` has a tag combined into it) * reverse the bits In this way, the tag is unlikely to overlap with the index for small test cases - making it possible to separate out the `CheckIRId` from the index in these cases to provide more meaningful debugging/CHECK messages, and more informative `SemIR` textual dumping that can now include the `CheckIRId` along with the `Inst`'s index in the name of an `inst`) The test churn here is improved printing as tagged `InstId`s can now, with best effort (more likely for small test cases where the `CheckIRId` and the `Inst` index aren't at risk of overlapping from the high and low bits), render the `CheckIRId` as part of the inst's name. Going from `instNN` to `irMM.instNN`. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
66 lines
1.9 KiB
C++
66 lines
1.9 KiB
C++
// 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/sem_ir/inst.h"
|
|
|
|
#include <utility>
|
|
|
|
#include "toolchain/sem_ir/file.h"
|
|
|
|
namespace Carbon::SemIR {
|
|
|
|
auto Inst::Print(llvm::raw_ostream& out) const -> void {
|
|
out << "{kind: " << kind();
|
|
|
|
auto print_args = [&](auto info) {
|
|
using Info = decltype(info);
|
|
if constexpr (Info::NumArgs > 0) {
|
|
out << ", arg0: " << FromRaw<typename Info::template ArgType<0>>(arg0_);
|
|
}
|
|
if constexpr (Info::NumArgs > 1) {
|
|
out << ", arg1: " << FromRaw<typename Info::template ArgType<1>>(arg1_);
|
|
}
|
|
};
|
|
|
|
switch (kind()) {
|
|
#define CARBON_SEM_IR_INST_KIND(Name) \
|
|
case Name::Kind: \
|
|
print_args(Internal::InstLikeTypeInfo<Name>()); \
|
|
break;
|
|
#include "toolchain/sem_ir/inst_kind.def"
|
|
}
|
|
if (type_id_.has_value()) {
|
|
out << ", type: " << type_id_;
|
|
}
|
|
out << "}";
|
|
}
|
|
|
|
// Returns the IdKind of an instruction's argument, or None if there is no
|
|
// argument with that index.
|
|
template <typename InstKind, int ArgIndex>
|
|
static constexpr auto IdKindFor() -> IdKind {
|
|
using Info = Internal::InstLikeTypeInfo<InstKind>;
|
|
if constexpr (ArgIndex < Info::NumArgs) {
|
|
return IdKind::For<typename Info::template ArgType<ArgIndex>>;
|
|
} else {
|
|
return IdKind::None;
|
|
}
|
|
}
|
|
|
|
const std::pair<IdKind, IdKind> Inst::ArgKindTable[] = {
|
|
#define CARBON_SEM_IR_INST_KIND(Name) \
|
|
{IdKindFor<Name, 0>(), IdKindFor<Name, 1>()},
|
|
#include "toolchain/sem_ir/inst_kind.def"
|
|
};
|
|
|
|
InstStore::InstStore(File* file, int32_t reserved_inst_ids)
|
|
: file_(file),
|
|
values_(IdTag(file->check_ir_id().index, reserved_inst_ids)) {}
|
|
|
|
auto InstStore::GetUnattachedType(TypeId type_id) const -> TypeId {
|
|
return file_->types().GetUnattachedType(type_id);
|
|
}
|
|
|
|
} // namespace Carbon::SemIR
|