mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-21 16:26:39 +00: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>
114 lines
5.1 KiB
C++
114 lines
5.1 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 <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "common/ostream.h"
|
|
#include "common/raw_string_ostream.h"
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
#include "llvm/Support/VirtualFileSystem.h"
|
|
#include "testing/base/global_exe_path.h"
|
|
#include "toolchain/driver/driver.h"
|
|
#include "toolchain/testing/yaml_test_helpers.h"
|
|
|
|
namespace Carbon::SemIR {
|
|
namespace {
|
|
|
|
using ::testing::_;
|
|
using ::testing::AllOf;
|
|
using ::testing::Contains;
|
|
using ::testing::Each;
|
|
using ::testing::ElementsAre;
|
|
using ::testing::IsEmpty;
|
|
using ::testing::MatchesRegex;
|
|
using ::testing::Pair;
|
|
using ::testing::SizeIs;
|
|
|
|
namespace Yaml = ::Carbon::Testing::Yaml;
|
|
|
|
TEST(SemIRTest, Yaml) {
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> fs =
|
|
new llvm::vfs::InMemoryFileSystem;
|
|
CARBON_CHECK(fs->addFile(
|
|
"test.carbon", /*ModificationTime=*/0,
|
|
llvm::MemoryBuffer::getMemBuffer("fn F() { let x: () = (); return; }")));
|
|
const auto install_paths =
|
|
InstallPaths::MakeForBazelRunfiles(Testing::GetExePath());
|
|
RawStringOstream print_stream;
|
|
Driver driver(fs, &install_paths, /*input_stream=*/nullptr, &print_stream,
|
|
&llvm::errs());
|
|
auto run_result =
|
|
driver.RunCommand({"compile", "--no-prelude-import", "--phase=check",
|
|
"--dump-raw-sem-ir", "test.carbon"});
|
|
EXPECT_TRUE(run_result.success);
|
|
|
|
// Matches the ID of an instruction. Instruction counts may change as various
|
|
// support changes, so this code is only doing loose structural checks.
|
|
auto inst_block_id = Yaml::Scalar(MatchesRegex(R"(inst_block(\d+|_empty))"));
|
|
auto inst_id = Yaml::Scalar(MatchesRegex(R"((ir\d+\.)?inst\d+)"));
|
|
auto constant_id =
|
|
Yaml::Scalar(MatchesRegex(R"(concrete_constant\((ir\d+\.)?inst\d+\))"));
|
|
auto inst_builtin = Yaml::Scalar(MatchesRegex(R"(inst\(\w+\))"));
|
|
auto type_id = Yaml::Scalar(
|
|
MatchesRegex(R"(type\((\w+|inst\(\w+\)|(ir\d+\.)?inst\d+)\))"));
|
|
auto type_builtin = Pair(type_id, Yaml::Mapping(_));
|
|
|
|
auto file = Yaml::Mapping(ElementsAre(
|
|
Pair("import_irs", Yaml::Mapping(SizeIs(2))),
|
|
Pair("import_ir_insts", Yaml::Mapping(SizeIs(0))),
|
|
Pair("clang_decls", Yaml::Mapping(SizeIs(0))),
|
|
Pair("name_scopes", Yaml::Mapping(SizeIs(1))),
|
|
Pair("entity_names", Yaml::Mapping(SizeIs(1))),
|
|
Pair("functions", Yaml::Mapping(SizeIs(1))),
|
|
Pair("classes", Yaml::Mapping(SizeIs(0))),
|
|
Pair("generics", Yaml::Mapping(SizeIs(0))),
|
|
Pair("specifics", Yaml::Mapping(SizeIs(0))),
|
|
Pair("struct_type_fields", Yaml::Mapping(SizeIs(1))),
|
|
Pair("types", Yaml::Mapping(Each(type_builtin))),
|
|
Pair("insts", Yaml::Mapping(AllOf(
|
|
Each(Key(inst_id)),
|
|
// kind is required, other parts are optional.
|
|
Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
|
|
// A 0-arg instruction.
|
|
Contains(Pair(_, Yaml::Mapping(ElementsAre(
|
|
Pair("kind", "Return"))))),
|
|
// A 1-arg instruction.
|
|
Contains(Pair(_, Yaml::Mapping(ElementsAre(
|
|
Pair("kind", "TupleType"),
|
|
Pair("arg0", inst_block_id),
|
|
Pair("type", "type(TypeType)"))))),
|
|
// A 2-arg instruction.
|
|
Contains(Pair(_, Yaml::Mapping(ElementsAre(
|
|
|
|
Pair("kind", "FunctionDecl"),
|
|
Pair("arg0", "function0"),
|
|
Pair("arg1", "inst_block_empty"),
|
|
Pair("type", type_id)))))))),
|
|
Pair("constant_values",
|
|
Yaml::Mapping(ElementsAre(
|
|
Pair("values",
|
|
Yaml::Mapping(AllOf(Each(Pair(inst_id, constant_id))))),
|
|
Pair("symbolic_constants", Yaml::Mapping(SizeIs(0)))))),
|
|
Pair("inst_blocks",
|
|
Yaml::Mapping(ElementsAre(
|
|
Pair("inst_block_empty", Yaml::Mapping(IsEmpty())),
|
|
Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
|
|
Pair("imports", Yaml::Mapping(IsEmpty())),
|
|
Pair("global_init", Yaml::Mapping(IsEmpty())),
|
|
Pair("inst_block4", Yaml::Mapping(Each(Pair(_, inst_id)))),
|
|
Pair("inst_block5", Yaml::Mapping(Each(Pair(_, inst_id)))),
|
|
Pair("inst_block6", Yaml::Mapping(Each(Pair(_, inst_id)))),
|
|
Pair("inst_block7", Yaml::Mapping(Each(Pair(_, inst_id)))),
|
|
Pair("inst_block8", Yaml::Mapping(Each(Pair(_, inst_id)))))))));
|
|
|
|
auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
|
|
ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
|
|
|
|
EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), IsYaml(root));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::SemIR
|