file.cpp 7.4 KB

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