compile_subcommand.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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/driver/compile_subcommand.h"
  5. #include <functional>
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <system_error>
  10. #include <utility>
  11. #include "common/vlog.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/ScopeExit.h"
  14. #include "toolchain/base/pretty_stack_trace_function.h"
  15. #include "toolchain/base/timings.h"
  16. #include "toolchain/check/check.h"
  17. #include "toolchain/codegen/codegen.h"
  18. #include "toolchain/diagnostics/diagnostic_emitter.h"
  19. #include "toolchain/diagnostics/sorting_diagnostic_consumer.h"
  20. #include "toolchain/lex/lex.h"
  21. #include "toolchain/lower/lower.h"
  22. #include "toolchain/parse/parse.h"
  23. #include "toolchain/parse/tree_and_subtrees.h"
  24. #include "toolchain/sem_ir/formatter.h"
  25. #include "toolchain/sem_ir/inst_namer.h"
  26. #include "toolchain/source/source_buffer.h"
  27. namespace Carbon {
  28. auto CompileOptions::Build(CommandLine::CommandBuilder& b) -> void {
  29. b.AddStringPositionalArg(
  30. {
  31. .name = "FILE",
  32. .help = R"""(
  33. The input Carbon source file to compile.
  34. )""",
  35. },
  36. [&](auto& arg_b) {
  37. arg_b.Required(true);
  38. arg_b.Append(&input_filenames);
  39. });
  40. b.AddOneOfOption(
  41. {
  42. .name = "phase",
  43. .help = R"""(
  44. Selects the compilation phase to run. These phases are always run in sequence,
  45. so every phase before the one selected will also be run. The default is to
  46. compile to machine code.
  47. )""",
  48. },
  49. [&](auto& arg_b) {
  50. arg_b.SetOneOf(
  51. {
  52. arg_b.OneOfValue("lex", Phase::Lex),
  53. arg_b.OneOfValue("parse", Phase::Parse),
  54. arg_b.OneOfValue("check", Phase::Check),
  55. arg_b.OneOfValue("lower", Phase::Lower),
  56. arg_b.OneOfValue("codegen", Phase::CodeGen).Default(true),
  57. },
  58. &phase);
  59. });
  60. // TODO: Rearrange the code setting this option and two related ones to
  61. // allow them to reference each other instead of hard-coding their names.
  62. b.AddStringOption(
  63. {
  64. .name = "output",
  65. .value_name = "FILE",
  66. .help = R"""(
  67. The output filename for codegen.
  68. When this is a file name, either textual assembly or a binary object will be
  69. written to it based on the flag `--asm-output`. The default is to write a binary
  70. object file.
  71. Passing `--output=-` will write the output to stdout. In that case, the flag
  72. `--asm-output` is ignored and the output defaults to textual assembly. Binary
  73. object output can be forced by enabling `--force-obj-output`.
  74. )""",
  75. },
  76. [&](auto& arg_b) { arg_b.Set(&output_filename); });
  77. // Include the common code generation options at this point to render it
  78. // after the more common options above, but before the more unusual options
  79. // below.
  80. codegen_options.Build(b);
  81. b.AddFlag(
  82. {
  83. .name = "asm-output",
  84. .help = R"""(
  85. Write textual assembly rather than a binary object file to the code generation
  86. output.
  87. This flag only applies when writing to a file. When writing to stdout, the
  88. default is textual assembly and this flag is ignored.
  89. )""",
  90. },
  91. [&](auto& arg_b) { arg_b.Set(&asm_output); });
  92. b.AddFlag(
  93. {
  94. .name = "force-obj-output",
  95. .help = R"""(
  96. Force binary object output, even with `--output=-`.
  97. When `--output=-` is set, the default is textual assembly; this forces printing
  98. of a binary object file instead. Ignored for other `--output` values.
  99. )""",
  100. },
  101. [&](auto& arg_b) { arg_b.Set(&force_obj_output); });
  102. b.AddFlag(
  103. {
  104. .name = "stream-errors",
  105. .help = R"""(
  106. Stream error messages to stderr as they are generated rather than sorting them
  107. and displaying them in source order.
  108. )""",
  109. },
  110. [&](auto& arg_b) { arg_b.Set(&stream_errors); });
  111. b.AddFlag(
  112. {
  113. .name = "dump-shared-values",
  114. .help = R"""(
  115. Dumps shared values. These aren't owned by any particular file or phase.
  116. )""",
  117. },
  118. [&](auto& arg_b) { arg_b.Set(&dump_shared_values); });
  119. b.AddFlag(
  120. {
  121. .name = "dump-tokens",
  122. .help = R"""(
  123. Dump the tokens to stdout when lexed.
  124. )""",
  125. },
  126. [&](auto& arg_b) { arg_b.Set(&dump_tokens); });
  127. b.AddFlag(
  128. {
  129. .name = "omit-file-boundary-tokens",
  130. .help = R"""(
  131. For `--dump-tokens`, omit file start and end boundary tokens.
  132. )""",
  133. },
  134. [&](auto& arg_b) { arg_b.Set(&omit_file_boundary_tokens); });
  135. b.AddFlag(
  136. {
  137. .name = "dump-parse-tree",
  138. .help = R"""(
  139. Dump the parse tree to stdout when parsed.
  140. )""",
  141. },
  142. [&](auto& arg_b) { arg_b.Set(&dump_parse_tree); });
  143. b.AddFlag(
  144. {
  145. .name = "preorder-parse-tree",
  146. .help = R"""(
  147. When dumping the parse tree, reorder it so that it is in preorder rather than
  148. postorder.
  149. )""",
  150. },
  151. [&](auto& arg_b) { arg_b.Set(&preorder_parse_tree); });
  152. b.AddFlag(
  153. {
  154. .name = "dump-raw-sem-ir",
  155. .help = R"""(
  156. Dump the raw JSON structure of SemIR to stdout when built.
  157. )""",
  158. },
  159. [&](auto& arg_b) { arg_b.Set(&dump_raw_sem_ir); });
  160. b.AddFlag(
  161. {
  162. .name = "dump-sem-ir",
  163. .help = R"""(
  164. Dump the SemIR to stdout when built.
  165. )""",
  166. },
  167. [&](auto& arg_b) { arg_b.Set(&dump_sem_ir); });
  168. b.AddFlag(
  169. {
  170. .name = "builtin-sem-ir",
  171. .help = R"""(
  172. Include the SemIR for builtins when dumping it.
  173. )""",
  174. },
  175. [&](auto& arg_b) { arg_b.Set(&builtin_sem_ir); });
  176. b.AddFlag(
  177. {
  178. .name = "dump-llvm-ir",
  179. .help = R"""(
  180. Dump the LLVM IR to stdout after lowering.
  181. )""",
  182. },
  183. [&](auto& arg_b) { arg_b.Set(&dump_llvm_ir); });
  184. b.AddFlag(
  185. {
  186. .name = "dump-asm",
  187. .help = R"""(
  188. Dump the generated assembly to stdout after codegen.
  189. )""",
  190. },
  191. [&](auto& arg_b) { arg_b.Set(&dump_asm); });
  192. b.AddFlag(
  193. {
  194. .name = "dump-mem-usage",
  195. .help = R"""(
  196. Dumps the amount of memory used.
  197. )""",
  198. },
  199. [&](auto& arg_b) { arg_b.Set(&dump_mem_usage); });
  200. b.AddFlag(
  201. {
  202. .name = "dump-timings",
  203. .help = R"""(
  204. Dumps the duration of each phase for each compilation unit.
  205. )""",
  206. },
  207. [&](auto& arg_b) { arg_b.Set(&dump_timings); });
  208. b.AddFlag(
  209. {
  210. .name = "prelude-import",
  211. .help = R"""(
  212. Whether to use the implicit prelude import. Enabled by default.
  213. )""",
  214. },
  215. [&](auto& arg_b) {
  216. arg_b.Default(true);
  217. arg_b.Set(&prelude_import);
  218. });
  219. b.AddFlag(
  220. {
  221. .name = "custom-core",
  222. .value_name = "CUSTOM_CORE",
  223. .help = R"""(
  224. Whether to use a custom Core package, the files for which must all be included
  225. in the compile command line.
  226. The prelude library in the Core package is imported automatically. By default,
  227. the Core package shipped with the toolchain is used, and its files do not need
  228. to be specified in the compile command line.
  229. )""",
  230. },
  231. [&](auto& arg_b) {
  232. arg_b.Default(false);
  233. arg_b.Set(&custom_core);
  234. });
  235. b.AddStringOption(
  236. {
  237. .name = "exclude-dump-file-prefix",
  238. .value_name = "PREFIX",
  239. .help = R"""(
  240. Excludes files with the given prefix from dumps.
  241. )""",
  242. },
  243. [&](auto& arg_b) { arg_b.Set(&exclude_dump_file_prefix); });
  244. b.AddFlag(
  245. {
  246. .name = "debug-info",
  247. .help = R"""(
  248. Whether to emit DWARF debug information.
  249. )""",
  250. },
  251. [&](auto& arg_b) {
  252. arg_b.Default(true);
  253. arg_b.Set(&include_debug_info);
  254. });
  255. }
  256. static constexpr CommandLine::CommandInfo SubcommandInfo = {
  257. .name = "compile",
  258. .help = R"""(
  259. Compile Carbon source code.
  260. This subcommand runs the Carbon compiler over input source code, checking it for
  261. errors and producing the requested output.
  262. Error messages are written to the standard error stream.
  263. Different phases of the compiler can be selected to run, and intermediate state
  264. can be written to standard output as these phases progress.
  265. )""",
  266. };
  267. CompileSubcommand::CompileSubcommand() : DriverSubcommand(SubcommandInfo) {}
  268. // Returns a string for printing the phase in a diagnostic.
  269. static auto PhaseToString(CompileOptions::Phase phase) -> std::string {
  270. switch (phase) {
  271. case CompileOptions::Phase::Lex:
  272. return "lex";
  273. case CompileOptions::Phase::Parse:
  274. return "parse";
  275. case CompileOptions::Phase::Check:
  276. return "check";
  277. case CompileOptions::Phase::Lower:
  278. return "lower";
  279. case CompileOptions::Phase::CodeGen:
  280. return "codegen";
  281. }
  282. }
  283. auto CompileSubcommand::ValidateOptions(
  284. Diagnostics::NoLocEmitter& emitter) const -> bool {
  285. CARBON_DIAGNOSTIC(
  286. CompilePhaseFlagConflict, Error,
  287. "requested dumping {0} but compile phase is limited to `{1}`",
  288. std::string, std::string);
  289. using Phase = CompileOptions::Phase;
  290. switch (options_.phase) {
  291. case Phase::Lex:
  292. if (options_.dump_parse_tree) {
  293. emitter.Emit(CompilePhaseFlagConflict, "parse tree",
  294. PhaseToString(options_.phase));
  295. return false;
  296. }
  297. [[fallthrough]];
  298. case Phase::Parse:
  299. if (options_.dump_sem_ir) {
  300. emitter.Emit(CompilePhaseFlagConflict, "SemIR",
  301. PhaseToString(options_.phase));
  302. return false;
  303. }
  304. [[fallthrough]];
  305. case Phase::Check:
  306. if (options_.dump_llvm_ir) {
  307. emitter.Emit(CompilePhaseFlagConflict, "LLVM IR",
  308. PhaseToString(options_.phase));
  309. return false;
  310. }
  311. [[fallthrough]];
  312. case Phase::Lower:
  313. case Phase::CodeGen:
  314. // Everything can be dumped in these phases.
  315. break;
  316. }
  317. return true;
  318. }
  319. namespace {
  320. // Ties together information for a file being compiled.
  321. class CompilationUnit {
  322. public:
  323. explicit CompilationUnit(DriverEnv& driver_env, const CompileOptions& options,
  324. Diagnostics::Consumer* consumer,
  325. llvm::StringRef input_filename);
  326. // Loads source and lexes it. Returns true on success.
  327. auto RunLex() -> void;
  328. // Parses tokens. Returns true on success.
  329. auto RunParse() -> void;
  330. // Returns information needed to check this unit.
  331. auto GetCheckUnit(SemIR::CheckIRId check_ir_id) -> Check::Unit;
  332. // Runs post-check logic. Returns true if checking succeeded for the IR.
  333. auto PostCheck() -> void;
  334. // Lower SemIR to LLVM IR.
  335. auto RunLower(std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  336. tree_and_subtrees_getters_for_debug_info) -> void;
  337. auto RunCodeGen() -> void;
  338. // Runs post-compile logic. This is always called, and called after all other
  339. // actions on the CompilationUnit.
  340. auto PostCompile() -> void;
  341. // Flushes diagnostics, specifically as part of generating stack trace
  342. // information.
  343. auto FlushForStackTrace() -> void { consumer_->Flush(); }
  344. auto input_filename() -> llvm::StringRef { return input_filename_; }
  345. auto success() -> bool { return success_; }
  346. auto has_source() -> bool { return source_.has_value(); }
  347. auto get_trees_and_subtrees() -> Parse::GetTreeAndSubtreesFn {
  348. return *tree_and_subtrees_getter_;
  349. }
  350. private:
  351. // Do codegen. Returns true on success.
  352. auto RunCodeGenHelper() -> bool;
  353. // The TreeAndSubtrees is mainly used for debugging and diagnostics, and has
  354. // significant overhead. Avoid constructing it when unused.
  355. auto GetParseTreeAndSubtrees() -> const Parse::TreeAndSubtrees&;
  356. // Wraps a call with log statements to indicate start and end. Typically logs
  357. // with the actual function name, but marks timings with the appropriate
  358. // phase.
  359. auto LogCall(llvm::StringLiteral logging_label,
  360. llvm::StringLiteral timing_label,
  361. llvm::function_ref<auto()->void> fn) -> void;
  362. // Returns true if the current input file can be dumped.
  363. auto IncludeInDumps() const -> bool;
  364. // Returns true if the specified input file can be dumped.
  365. auto IncludeInDumps(llvm::StringRef filename) const -> bool;
  366. DriverEnv* driver_env_;
  367. SharedValueStores value_stores_;
  368. const CompileOptions& options_;
  369. // The input filename from the command line. For most diagnostics, we
  370. // typically use `source_->filename()`, which includes a `-` -> `<stdin>`
  371. // translation. However, logging and some diagnostics use the command line
  372. // argument.
  373. std::string input_filename_;
  374. // Copied from driver_ for CARBON_VLOG.
  375. llvm::raw_pwrite_stream* vlog_stream_;
  376. // Diagnostics are sent to consumer_, with optional sorting.
  377. std::optional<Diagnostics::SortingConsumer> sorting_consumer_;
  378. Diagnostics::Consumer* consumer_;
  379. bool success_ = true;
  380. // Tracks memory usage of the compile.
  381. std::optional<MemUsage> mem_usage_;
  382. // Tracks timings of the compile.
  383. std::optional<Timings> timings_;
  384. // These are initialized as steps are run.
  385. std::optional<SourceBuffer> source_;
  386. std::optional<Lex::TokenizedBuffer> tokens_;
  387. std::optional<Parse::Tree> parse_tree_;
  388. std::optional<Parse::TreeAndSubtrees> parse_tree_and_subtrees_;
  389. std::optional<std::function<auto()->const Parse::TreeAndSubtrees&>>
  390. tree_and_subtrees_getter_;
  391. std::optional<SemIR::File> sem_ir_;
  392. std::unique_ptr<clang::ASTUnit> cpp_ast_;
  393. std::unique_ptr<llvm::LLVMContext> llvm_context_;
  394. std::unique_ptr<llvm::Module> module_;
  395. };
  396. CompilationUnit::CompilationUnit(DriverEnv& driver_env,
  397. const CompileOptions& options,
  398. Diagnostics::Consumer* consumer,
  399. llvm::StringRef input_filename)
  400. : driver_env_(&driver_env),
  401. options_(options),
  402. input_filename_(input_filename),
  403. vlog_stream_(driver_env_->vlog_stream) {
  404. if (vlog_stream_ != nullptr || options_.stream_errors) {
  405. consumer_ = consumer;
  406. } else {
  407. sorting_consumer_ = Diagnostics::SortingConsumer(*consumer);
  408. consumer_ = &*sorting_consumer_;
  409. }
  410. if (options_.dump_mem_usage && IncludeInDumps()) {
  411. mem_usage_ = MemUsage();
  412. }
  413. if (options_.dump_timings && IncludeInDumps()) {
  414. timings_ = Timings();
  415. }
  416. }
  417. auto CompilationUnit::RunLex() -> void {
  418. CARBON_CHECK(!tokens_, "Called RunLex twice");
  419. LogCall("SourceBuffer::MakeFromFileOrStdin", "source", [&] {
  420. source_ = SourceBuffer::MakeFromFileOrStdin(*driver_env_->fs,
  421. input_filename_, *consumer_);
  422. });
  423. if (!source_) {
  424. success_ = false;
  425. return;
  426. }
  427. if (mem_usage_) {
  428. mem_usage_->Add("source_", source_->text().size(), source_->text().size());
  429. }
  430. CARBON_VLOG("*** SourceBuffer ***\n```\n{0}\n```\n", source_->text());
  431. LogCall("Lex::Lex", "lex",
  432. [&] { tokens_ = Lex::Lex(value_stores_, *source_, *consumer_); });
  433. if (options_.dump_tokens && IncludeInDumps()) {
  434. consumer_->Flush();
  435. tokens_->Print(*driver_env_->output_stream,
  436. options_.omit_file_boundary_tokens);
  437. }
  438. if (mem_usage_) {
  439. mem_usage_->Collect("tokens_", *tokens_);
  440. }
  441. CARBON_VLOG("*** Lex::TokenizedBuffer ***\n{0}", tokens_);
  442. if (tokens_->has_errors()) {
  443. success_ = false;
  444. }
  445. }
  446. auto CompilationUnit::RunParse() -> void {
  447. LogCall("Parse::Parse", "parse", [&] {
  448. parse_tree_ = Parse::Parse(*tokens_, *consumer_, vlog_stream_);
  449. });
  450. if (options_.dump_parse_tree && IncludeInDumps()) {
  451. consumer_->Flush();
  452. const auto& tree_and_subtrees = GetParseTreeAndSubtrees();
  453. if (options_.preorder_parse_tree) {
  454. tree_and_subtrees.PrintPreorder(*driver_env_->output_stream);
  455. } else {
  456. tree_and_subtrees.Print(*driver_env_->output_stream);
  457. }
  458. }
  459. if (mem_usage_) {
  460. mem_usage_->Collect("parse_tree_", *parse_tree_);
  461. }
  462. CARBON_VLOG("*** Parse::Tree ***\n{0}", parse_tree_);
  463. if (parse_tree_->has_errors()) {
  464. success_ = false;
  465. }
  466. }
  467. auto CompilationUnit::GetCheckUnit(SemIR::CheckIRId check_ir_id)
  468. -> Check::Unit {
  469. CARBON_CHECK(parse_tree_, "Must call RunParse first");
  470. CARBON_CHECK(!sem_ir_, "Called GetCheckUnit twice");
  471. tree_and_subtrees_getter_ = [this]() -> const Parse::TreeAndSubtrees& {
  472. return this->GetParseTreeAndSubtrees();
  473. };
  474. sem_ir_.emplace(&*parse_tree_, check_ir_id, parse_tree_->packaging_decl(),
  475. value_stores_, input_filename_);
  476. return {.consumer = consumer_,
  477. .value_stores = &value_stores_,
  478. .timings = timings_ ? &*timings_ : nullptr,
  479. .tree_and_subtrees_getter = *tree_and_subtrees_getter_,
  480. .sem_ir = &*sem_ir_,
  481. .cpp_ast = &cpp_ast_};
  482. }
  483. auto CompilationUnit::PostCheck() -> void {
  484. CARBON_CHECK(sem_ir_, "Must call GetCheckUnit first");
  485. // We've finished all steps that can produce diagnostics. Emit the
  486. // diagnostics now, so that the developer sees them sooner and doesn't need
  487. // to wait for code generation.
  488. consumer_->Flush();
  489. if (mem_usage_) {
  490. mem_usage_->Collect("sem_ir_", *sem_ir_);
  491. }
  492. if (options_.dump_raw_sem_ir && IncludeInDumps()) {
  493. CARBON_VLOG("*** Raw SemIR::File ***\n{0}\n", *sem_ir_);
  494. sem_ir_->Print(*driver_env_->output_stream, options_.builtin_sem_ir);
  495. if (options_.dump_sem_ir) {
  496. *driver_env_->output_stream << "\n";
  497. }
  498. }
  499. bool print = options_.dump_sem_ir && IncludeInDumps();
  500. if (vlog_stream_ || print) {
  501. // Omit entities imported from files that we are not dumping.
  502. auto should_format_entity = [&](SemIR::InstId entity_inst_id) -> bool {
  503. // TODO: Reuse `GetCanonicalImportIRInst`. Currently it depends on
  504. // `Check::Context`, which we don't have access to here.
  505. const SemIR::File* file = &*sem_ir_;
  506. while (true) {
  507. auto loc_id = file->insts().GetLocId(entity_inst_id);
  508. if (loc_id.kind() != SemIR::LocId::Kind::ImportIRInstId) {
  509. return true;
  510. }
  511. auto import_ir_inst =
  512. file->import_ir_insts().Get(loc_id.import_ir_inst_id());
  513. const auto* import_file =
  514. file->import_irs().Get(import_ir_inst.ir_id).sem_ir;
  515. CARBON_CHECK(import_file);
  516. if (!IncludeInDumps(import_file->filename())) {
  517. return false;
  518. }
  519. file = import_file;
  520. entity_inst_id = import_ir_inst.inst_id;
  521. }
  522. };
  523. SemIR::Formatter formatter(&*sem_ir_, should_format_entity);
  524. if (vlog_stream_) {
  525. CARBON_VLOG("*** SemIR::File ***\n");
  526. formatter.Print(*vlog_stream_);
  527. }
  528. if (print) {
  529. formatter.Print(*driver_env_->output_stream);
  530. }
  531. }
  532. if (sem_ir_->has_errors()) {
  533. success_ = false;
  534. }
  535. }
  536. auto CompilationUnit::RunLower(
  537. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  538. tree_and_subtrees_getters_for_debug_info) -> void {
  539. LogCall("Lower::LowerToLLVM", "lower", [&] {
  540. llvm_context_ = std::make_unique<llvm::LLVMContext>();
  541. // TODO: Consider disabling instruction naming by default if we're not
  542. // producing textual LLVM IR.
  543. SemIR::InstNamer inst_namer(&*sem_ir_);
  544. module_ = Lower::LowerToLLVM(*llvm_context_,
  545. tree_and_subtrees_getters_for_debug_info,
  546. input_filename_, *sem_ir_, sem_ir_->cpp_ast(),
  547. &inst_namer, vlog_stream_);
  548. });
  549. if (vlog_stream_) {
  550. CARBON_VLOG("*** llvm::Module ***\n");
  551. module_->print(*vlog_stream_, /*AAW=*/nullptr,
  552. /*ShouldPreserveUseListOrder=*/false,
  553. /*IsForDebug=*/true);
  554. }
  555. if (options_.dump_llvm_ir && IncludeInDumps()) {
  556. module_->print(*driver_env_->output_stream, /*AAW=*/nullptr,
  557. /*ShouldPreserveUseListOrder=*/true);
  558. }
  559. }
  560. auto CompilationUnit::RunCodeGen() -> void {
  561. CARBON_CHECK(module_, "Must call RunLower first");
  562. LogCall("CodeGen", "codegen", [&] { success_ = RunCodeGenHelper(); });
  563. }
  564. auto CompilationUnit::PostCompile() -> void {
  565. if (options_.dump_shared_values && IncludeInDumps()) {
  566. Yaml::Print(*driver_env_->output_stream,
  567. value_stores_.OutputYaml(input_filename_));
  568. }
  569. if (mem_usage_) {
  570. mem_usage_->Collect("value_stores_", value_stores_);
  571. Yaml::Print(*driver_env_->output_stream,
  572. mem_usage_->OutputYaml(input_filename_));
  573. }
  574. if (timings_) {
  575. Yaml::Print(*driver_env_->output_stream,
  576. timings_->OutputYaml(input_filename_));
  577. }
  578. // The diagnostics consumer must be flushed before compilation artifacts are
  579. // destructed, because diagnostics can refer to their state.
  580. consumer_->Flush();
  581. }
  582. auto CompilationUnit::RunCodeGenHelper() -> bool {
  583. std::optional<CodeGen> codegen =
  584. CodeGen::Make(module_.get(), options_.codegen_options.target,
  585. driver_env_->error_stream);
  586. if (!codegen) {
  587. return false;
  588. }
  589. if (vlog_stream_) {
  590. CARBON_VLOG("*** Assembly ***\n");
  591. codegen->EmitAssembly(*vlog_stream_);
  592. }
  593. if (options_.output_filename == "-") {
  594. // TODO: The output file name, forcing object output, and requesting
  595. // textual assembly output are all somewhat linked flags. We should add
  596. // some validation that they are used correctly.
  597. if (options_.force_obj_output) {
  598. if (!codegen->EmitObject(*driver_env_->output_stream)) {
  599. return false;
  600. }
  601. } else {
  602. if (!codegen->EmitAssembly(*driver_env_->output_stream)) {
  603. return false;
  604. }
  605. }
  606. } else {
  607. llvm::SmallString<256> output_filename = options_.output_filename;
  608. if (output_filename.empty()) {
  609. if (!source_->is_regular_file()) {
  610. // Don't invent file names like `-.o` or `/dev/stdin.o`.
  611. // TODO: Consider rephrasing the diagnostic to use the file as the
  612. // `Emit` location.
  613. CARBON_DIAGNOSTIC(CompileInputNotRegularFile, Error,
  614. "output file name must be specified for input `{0}` "
  615. "that is not a regular file",
  616. std::string);
  617. driver_env_->emitter.Emit(CompileInputNotRegularFile, input_filename_);
  618. return false;
  619. }
  620. output_filename = input_filename_;
  621. llvm::sys::path::replace_extension(output_filename,
  622. options_.asm_output ? ".s" : ".o");
  623. } else {
  624. // TODO: Handle the case where multiple input files were specified
  625. // along with an output file name. That should either be an error or
  626. // should produce a single LLVM IR module containing all inputs.
  627. // Currently each unit overwrites the output from the previous one in
  628. // this case.
  629. }
  630. CARBON_VLOG("Writing output to: {0}\n", output_filename);
  631. std::error_code ec;
  632. llvm::raw_fd_ostream output_file(output_filename, ec,
  633. llvm::sys::fs::OF_None);
  634. if (ec) {
  635. // TODO: Consider rephrasing the diagnostic to use the file as the `Emit`
  636. // location.
  637. CARBON_DIAGNOSTIC(CompileOutputFileOpenError, Error,
  638. "could not open output file `{0}`: {1}", std::string,
  639. std::string);
  640. driver_env_->emitter.Emit(CompileOutputFileOpenError,
  641. output_filename.str().str(), ec.message());
  642. return false;
  643. }
  644. if (options_.asm_output) {
  645. if (!codegen->EmitAssembly(output_file)) {
  646. return false;
  647. }
  648. } else {
  649. if (!codegen->EmitObject(output_file)) {
  650. return false;
  651. }
  652. }
  653. }
  654. return true;
  655. }
  656. auto CompilationUnit::GetParseTreeAndSubtrees()
  657. -> const Parse::TreeAndSubtrees& {
  658. if (!parse_tree_and_subtrees_) {
  659. parse_tree_and_subtrees_ = Parse::TreeAndSubtrees(*tokens_, *parse_tree_);
  660. if (mem_usage_) {
  661. mem_usage_->Collect("parse_tree_and_subtrees_",
  662. *parse_tree_and_subtrees_);
  663. }
  664. }
  665. return *parse_tree_and_subtrees_;
  666. }
  667. auto CompilationUnit::LogCall(llvm::StringLiteral logging_label,
  668. llvm::StringLiteral timing_label,
  669. llvm::function_ref<auto()->void> fn) -> void {
  670. CARBON_VLOG("*** {0}: {1} ***\n", logging_label, input_filename_);
  671. Timings::ScopedTiming timing(timings_ ? &*timings_ : nullptr, timing_label);
  672. fn();
  673. CARBON_VLOG("*** {0} done ***\n", logging_label);
  674. }
  675. auto CompilationUnit::IncludeInDumps() const -> bool {
  676. return IncludeInDumps(input_filename_);
  677. }
  678. auto CompilationUnit::IncludeInDumps(llvm::StringRef filename) const -> bool {
  679. return options_.exclude_dump_file_prefix.empty() ||
  680. !filename.starts_with(options_.exclude_dump_file_prefix);
  681. }
  682. } // namespace
  683. auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult {
  684. if (!ValidateOptions(driver_env.emitter)) {
  685. return {.success = false};
  686. }
  687. // Find the files comprising the prelude if we are importing it.
  688. // TODO: Replace this with a search for library api files in a
  689. // package-specific search path based on the library name.
  690. llvm::SmallVector<std::string> prelude;
  691. if (options_.prelude_import && !options_.custom_core &&
  692. options_.phase >= CompileOptions::Phase::Check) {
  693. if (auto find = driver_env.installation->ReadPreludeManifest(); find.ok()) {
  694. prelude = std::move(*find);
  695. } else {
  696. // TODO: Change ReadPreludeManifest to produce diagnostics.
  697. CARBON_DIAGNOSTIC(CompilePreludeManifestError, Error, "{0}", std::string);
  698. driver_env.emitter.Emit(CompilePreludeManifestError,
  699. PrintToString(find.error()));
  700. return {.success = false};
  701. }
  702. }
  703. // Prepare CompilationUnits before building scope exit handlers.
  704. llvm::SmallVector<std::unique_ptr<CompilationUnit>> units;
  705. units.reserve(prelude.size() + options_.input_filenames.size());
  706. // Add the prelude files.
  707. for (const auto& input_filename : prelude) {
  708. units.push_back(std::make_unique<CompilationUnit>(
  709. driver_env, options_, &driver_env.consumer, input_filename));
  710. }
  711. // Add the input source files.
  712. for (const auto& input_filename : options_.input_filenames) {
  713. units.push_back(std::make_unique<CompilationUnit>(
  714. driver_env, options_, &driver_env.consumer, input_filename));
  715. }
  716. auto on_exit = llvm::make_scope_exit([&]() {
  717. // Finish compilation units. This flushes their diagnostics in the order in
  718. // which they were specified on the command line.
  719. for (auto& unit : units) {
  720. unit->PostCompile();
  721. }
  722. driver_env.consumer.Flush();
  723. });
  724. PrettyStackTraceFunction flush_on_crash([&](llvm::raw_ostream& out) {
  725. // When crashing, flush diagnostics. If sorting diagnostics, they can be
  726. // redirected to the crash stream; if streaming, the original stream is
  727. // flushed.
  728. // TODO: Eventually we'll want to limit the count.
  729. if (options_.stream_errors) {
  730. out << "Flushing diagnostics\n";
  731. } else {
  732. out << "Pending diagnostics:\n";
  733. driver_env.consumer.set_stream(&out);
  734. }
  735. for (auto& unit : units) {
  736. unit->FlushForStackTrace();
  737. }
  738. driver_env.consumer.Flush();
  739. driver_env.consumer.set_stream(driver_env.error_stream);
  740. });
  741. // Returns a DriverResult object. Called whenever Compile returns.
  742. auto make_result = [&]() {
  743. DriverResult result = {.success = true};
  744. for (const auto& unit : units) {
  745. result.success &= unit->success();
  746. result.per_file_success.push_back(
  747. {unit->input_filename().str(), unit->success()});
  748. }
  749. return result;
  750. };
  751. // Lex.
  752. for (auto& unit : units) {
  753. unit->RunLex();
  754. }
  755. if (options_.phase == CompileOptions::Phase::Lex) {
  756. return make_result();
  757. }
  758. // Parse and check phases examine `has_source` because they want to proceed if
  759. // lex failed, but not if source doesn't exist. Later steps are skipped if
  760. // anything failed, so don't need this.
  761. // Parse.
  762. for (auto& unit : units) {
  763. if (unit->has_source()) {
  764. unit->RunParse();
  765. }
  766. }
  767. if (options_.phase == CompileOptions::Phase::Parse) {
  768. return make_result();
  769. }
  770. // Gather Check::Units.
  771. llvm::SmallVector<Check::Unit> check_units;
  772. check_units.reserve(units.size());
  773. for (auto& unit : units) {
  774. if (unit->has_source()) {
  775. SemIR::CheckIRId check_ir_id(check_units.size());
  776. check_units.push_back(unit->GetCheckUnit(check_ir_id));
  777. }
  778. }
  779. // Execute the actual checking.
  780. CARBON_VLOG_TO(driver_env.vlog_stream, "*** Check::CheckParseTrees ***\n");
  781. Check::CheckParseTrees(check_units, options_.prelude_import, driver_env.fs,
  782. driver_env.vlog_stream, driver_env.fuzzing);
  783. CARBON_VLOG_TO(driver_env.vlog_stream,
  784. "*** Check::CheckParseTrees done ***\n");
  785. for (auto& unit : units) {
  786. if (unit->has_source()) {
  787. unit->PostCheck();
  788. }
  789. }
  790. if (options_.phase == CompileOptions::Phase::Check) {
  791. return make_result();
  792. }
  793. // Unlike previous steps, errors block further progress.
  794. if (llvm::any_of(units, [&](const auto& unit) { return !unit->success(); })) {
  795. CARBON_VLOG_TO(driver_env.vlog_stream,
  796. "*** Stopping before lowering due to errors ***\n");
  797. return make_result();
  798. }
  799. // Lower.
  800. llvm::SmallVector<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters;
  801. std::optional<llvm::ArrayRef<Parse::GetTreeAndSubtreesFn>>
  802. tree_and_subtrees_getters_for_debug_info;
  803. if (options_.include_debug_info) {
  804. // This size may not match due to units that are missing source, but that's
  805. // an error case and not worth extra work.
  806. tree_and_subtrees_getters.reserve(units.size());
  807. for (auto& unit : units) {
  808. if (unit->has_source()) {
  809. tree_and_subtrees_getters.push_back(unit->get_trees_and_subtrees());
  810. }
  811. }
  812. tree_and_subtrees_getters_for_debug_info = {};
  813. tree_and_subtrees_getters_for_debug_info = tree_and_subtrees_getters;
  814. }
  815. for (const auto& unit : units) {
  816. unit->RunLower(tree_and_subtrees_getters_for_debug_info);
  817. }
  818. if (options_.phase == CompileOptions::Phase::Lower) {
  819. return make_result();
  820. }
  821. CARBON_CHECK(options_.phase == CompileOptions::Phase::CodeGen,
  822. "CodeGen should be the last stage");
  823. // Codegen.
  824. for (auto& unit : units) {
  825. unit->RunCodeGen();
  826. }
  827. return make_result();
  828. }
  829. } // namespace Carbon