file.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_.array_ref()) {
  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(
  89. "sem_ir", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  90. map.Add("import_irs", import_irs_.OutputYaml());
  91. map.Add("import_ir_insts", import_ir_insts_.OutputYaml());
  92. map.Add("name_scopes", name_scopes_.OutputYaml());
  93. map.Add("entity_names", entity_names_.OutputYaml());
  94. map.Add("functions", functions_.OutputYaml());
  95. map.Add("classes", classes_.OutputYaml());
  96. map.Add("generics", generics_.OutputYaml());
  97. map.Add("specifics", specifics_.OutputYaml());
  98. map.Add("struct_type_fields", struct_type_fields_.OutputYaml());
  99. map.Add("types", types_.OutputYaml());
  100. map.Add("insts",
  101. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  102. for (auto [id, inst] : insts_.enumerate()) {
  103. if (!include_singletons && IsSingletonInstId(id)) {
  104. continue;
  105. }
  106. map.Add(PrintToString(id), Yaml::OutputScalar(inst));
  107. }
  108. }));
  109. map.Add("constant_values",
  110. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  111. for (auto [id, _] : insts_.enumerate()) {
  112. if (!include_singletons && IsSingletonInstId(id)) {
  113. continue;
  114. }
  115. auto value = constant_values_.Get(id);
  116. if (!value.has_value() || value.is_constant()) {
  117. map.Add(PrintToString(id), Yaml::OutputScalar(value));
  118. }
  119. }
  120. }));
  121. map.Add(
  122. "symbolic_constants",
  123. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  124. for (const auto& [i, symbolic] :
  125. llvm::enumerate(constant_values().symbolic_constants())) {
  126. map.Add(
  127. PrintToString(ConstantId::ForSymbolicConstantIndex(i)),
  128. Yaml::OutputScalar(symbolic));
  129. }
  130. }));
  131. map.Add("inst_blocks", inst_blocks_.OutputYaml());
  132. }));
  133. });
  134. }
  135. auto File::CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  136. -> void {
  137. mem_usage.Collect(MemUsage::ConcatLabel(label, "allocator_"), allocator_);
  138. mem_usage.Collect(MemUsage::ConcatLabel(label, "entity_names_"),
  139. entity_names_);
  140. mem_usage.Collect(MemUsage::ConcatLabel(label, "functions_"), functions_);
  141. mem_usage.Collect(MemUsage::ConcatLabel(label, "classes_"), classes_);
  142. mem_usage.Collect(MemUsage::ConcatLabel(label, "interfaces_"), interfaces_);
  143. mem_usage.Collect(MemUsage::ConcatLabel(label, "impls_"), impls_);
  144. mem_usage.Collect(MemUsage::ConcatLabel(label, "generics_"), generics_);
  145. mem_usage.Collect(MemUsage::ConcatLabel(label, "specifics_"), specifics_);
  146. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_irs_"), import_irs_);
  147. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_ir_insts_"),
  148. import_ir_insts_);
  149. mem_usage.Collect(MemUsage::ConcatLabel(label, "struct_type_fields_"),
  150. struct_type_fields_);
  151. mem_usage.Collect(MemUsage::ConcatLabel(label, "insts_"), insts_);
  152. mem_usage.Collect(MemUsage::ConcatLabel(label, "name_scopes_"), name_scopes_);
  153. mem_usage.Collect(MemUsage::ConcatLabel(label, "constant_values_"),
  154. constant_values_);
  155. mem_usage.Collect(MemUsage::ConcatLabel(label, "inst_blocks_"), inst_blocks_);
  156. mem_usage.Collect(MemUsage::ConcatLabel(label, "constants_"), constants_);
  157. mem_usage.Collect(MemUsage::ConcatLabel(label, "types_"), types_);
  158. }
  159. } // namespace Carbon::SemIR