file.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/sem_ir/file.h"
  5. #include <optional>
  6. #include <string>
  7. #include <utility>
  8. #include "common/check.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "toolchain/base/kind_switch.h"
  12. #include "toolchain/base/shared_value_stores.h"
  13. #include "toolchain/base/yaml.h"
  14. #include "toolchain/parse/node_ids.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/inst.h"
  17. #include "toolchain/sem_ir/inst_kind.h"
  18. #include "toolchain/sem_ir/typed_insts.h"
  19. namespace Carbon::SemIR {
  20. File::File(const Parse::Tree* parse_tree, CheckIRId check_ir_id,
  21. const std::optional<Parse::Tree::PackagingDecl>& packaging_decl,
  22. SharedValueStores& value_stores, std::string filename)
  23. : parse_tree_(parse_tree),
  24. check_ir_id_(check_ir_id),
  25. package_id_(packaging_decl ? packaging_decl->names.package_id
  26. : PackageNameId::None),
  27. library_id_(packaging_decl ? LibraryNameId::ForStringLiteralValueId(
  28. packaging_decl->names.library_id)
  29. : LibraryNameId::Default),
  30. value_stores_(&value_stores),
  31. filename_(std::move(filename)),
  32. impls_(*this),
  33. constant_values_(ConstantId::NotConstant),
  34. inst_blocks_(allocator_),
  35. constants_(this) {
  36. // `type` and the error type are both complete & concrete types.
  37. types_.SetComplete(
  38. TypeType::TypeId,
  39. {.value_repr = {.kind = ValueRepr::Copy, .type_id = TypeType::TypeId}});
  40. types_.SetComplete(
  41. ErrorInst::TypeId,
  42. {.value_repr = {.kind = ValueRepr::Copy, .type_id = ErrorInst::TypeId}});
  43. insts_.Reserve(SingletonInstKinds.size());
  44. for (auto kind : SingletonInstKinds) {
  45. auto inst_id =
  46. insts_.AddInNoBlock(LocIdAndInst::NoLoc(Inst::MakeSingleton(kind)));
  47. constant_values_.Set(inst_id, ConstantId::ForConcreteConstant(inst_id));
  48. }
  49. }
  50. auto File::Verify() const -> ErrorOr<Success> {
  51. // Invariants don't necessarily hold for invalid IR.
  52. if (has_errors_) {
  53. return Success();
  54. }
  55. // Check that every code block has a terminator sequence that appears at the
  56. // end of the block.
  57. for (const Function& function : functions_.values()) {
  58. for (InstBlockId block_id : function.body_block_ids) {
  59. TerminatorKind prior_kind = TerminatorKind::NotTerminator;
  60. for (InstId inst_id : inst_blocks().Get(block_id)) {
  61. TerminatorKind inst_kind =
  62. insts().Get(inst_id).kind().terminator_kind();
  63. if (prior_kind == TerminatorKind::Terminator) {
  64. return Error(llvm::formatv("Inst {0} in block {1} follows terminator",
  65. inst_id, block_id));
  66. }
  67. if (prior_kind > inst_kind) {
  68. return Error(
  69. llvm::formatv("Non-terminator inst {0} in block {1} follows "
  70. "terminator sequence",
  71. inst_id, block_id));
  72. }
  73. prior_kind = inst_kind;
  74. }
  75. if (prior_kind != TerminatorKind::Terminator) {
  76. return Error(llvm::formatv("No terminator in block {0}", block_id));
  77. }
  78. }
  79. }
  80. // TODO: Check that an instruction only references other instructions that are
  81. // either global or that dominate it.
  82. return Success();
  83. }
  84. auto File::OutputYaml(bool include_singletons) const -> Yaml::OutputMapping {
  85. return Yaml::OutputMapping([this, include_singletons](
  86. Yaml::OutputMapping::Map map) {
  87. map.Add("filename", filename_);
  88. map.Add("sem_ir", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  89. map.Add("import_irs", import_irs_.OutputYaml());
  90. map.Add("import_ir_insts", import_ir_insts_.OutputYaml());
  91. map.Add("name_scopes", name_scopes_.OutputYaml());
  92. map.Add("entity_names", entity_names_.OutputYaml());
  93. map.Add("functions", functions_.OutputYaml());
  94. map.Add("classes", classes_.OutputYaml());
  95. map.Add("generics", generics_.OutputYaml());
  96. map.Add("specifics", specifics_.OutputYaml());
  97. map.Add("struct_type_fields", struct_type_fields_.OutputYaml());
  98. map.Add("types", types_.OutputYaml());
  99. map.Add("insts",
  100. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  101. for (auto [id, inst] : insts_.enumerate()) {
  102. if (!include_singletons && IsSingletonInstId(id)) {
  103. continue;
  104. }
  105. map.Add(PrintToString(id), Yaml::OutputScalar(inst));
  106. }
  107. }));
  108. map.Add("constant_values",
  109. constant_values_.OutputYaml(include_singletons));
  110. map.Add("inst_blocks", inst_blocks_.OutputYaml());
  111. }));
  112. });
  113. }
  114. auto File::CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  115. -> void {
  116. mem_usage.Collect(MemUsage::ConcatLabel(label, "allocator_"), allocator_);
  117. mem_usage.Collect(MemUsage::ConcatLabel(label, "entity_names_"),
  118. entity_names_);
  119. mem_usage.Collect(MemUsage::ConcatLabel(label, "functions_"), functions_);
  120. mem_usage.Collect(MemUsage::ConcatLabel(label, "classes_"), classes_);
  121. mem_usage.Collect(MemUsage::ConcatLabel(label, "interfaces_"), interfaces_);
  122. mem_usage.Collect(MemUsage::ConcatLabel(label, "impls_"), impls_);
  123. mem_usage.Collect(MemUsage::ConcatLabel(label, "generics_"), generics_);
  124. mem_usage.Collect(MemUsage::ConcatLabel(label, "specifics_"), specifics_);
  125. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_irs_"), import_irs_);
  126. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_ir_insts_"),
  127. import_ir_insts_);
  128. mem_usage.Collect(MemUsage::ConcatLabel(label, "struct_type_fields_"),
  129. struct_type_fields_);
  130. mem_usage.Collect(MemUsage::ConcatLabel(label, "insts_"), insts_);
  131. mem_usage.Collect(MemUsage::ConcatLabel(label, "name_scopes_"), name_scopes_);
  132. mem_usage.Collect(MemUsage::ConcatLabel(label, "constant_values_"),
  133. constant_values_);
  134. mem_usage.Collect(MemUsage::ConcatLabel(label, "inst_blocks_"), inst_blocks_);
  135. mem_usage.Collect(MemUsage::ConcatLabel(label, "constants_"), constants_);
  136. mem_usage.Collect(MemUsage::ConcatLabel(label, "types_"), types_);
  137. }
  138. } // namespace Carbon::SemIR