From 843dd40f22228627f11e7c72e66e75dc77b03310 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Fri, 20 Oct 2023 14:31:50 -0700 Subject: [PATCH] Adding ValueStore printing and --dump-shared-values (#3320) This replaces the printing that was removed from SemIR's raw dump. It's separate because (for example) lexing generates shared values, and so reviewing them is not specific to any particular phase. --- toolchain/base/value_store.h | 33 +++++++++++++++++-- toolchain/driver/driver.cpp | 16 ++++++++- .../driver/testdata/dump_shared_values.carbon | 20 +++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 toolchain/driver/testdata/dump_shared_values.carbon diff --git a/toolchain/base/value_store.h b/toolchain/base/value_store.h index 5bba06526216..6476b05a66ce 100644 --- a/toolchain/base/value_store.h +++ b/toolchain/base/value_store.h @@ -10,6 +10,7 @@ #include "llvm/ADT/APInt.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/Support/YAMLParser.h" #include "toolchain/base/index_base.h" namespace Carbon { @@ -80,7 +81,7 @@ constexpr StringId StringId::Invalid(StringId::InvalidIndex); // A simple wrapper for accumulating values, providing IDs to later retrieve the // value. This does not do deduplication. template -class ValueStore { +class ValueStore : public Printable> { public: // Stores the value and returns an ID to reference it. auto Add(typename IdT::IndexedType value) -> IdT { @@ -96,6 +97,14 @@ class ValueStore { return values_[id.index]; } + auto Print(llvm::raw_ostream& out) const -> void { Print(out, 0); } + auto Print(llvm::raw_ostream& out, int indent) const -> void { + for (const auto& value : values_) { + out.indent(indent); + out << "- " << value << "\n"; + } + } + private: llvm::SmallVector values_; }; @@ -103,7 +112,7 @@ class ValueStore { // Storage for StringRefs. The caller is responsible for ensuring storage is // allocated. template <> -class ValueStore { +class ValueStore : public Printable> { public: // Returns an ID to reference the value. May return an existing ID if the // string was previously added. @@ -122,6 +131,14 @@ class ValueStore { return values_[id.index]; } + auto Print(llvm::raw_ostream& out) const -> void { Print(out, 0); } + auto Print(llvm::raw_ostream& out, int indent) const -> void { + for (auto value : values_) { + out.indent(indent); + out << "- \"" << llvm::yaml::escape(value) << "\"\n"; + } + } + private: llvm::DenseMap map_; llvm::SmallVector values_; @@ -129,7 +146,7 @@ class ValueStore { // Stores that will be used across compiler steps. This is provided mainly so // that they don't need to be passed separately. -class SharedValueStores { +class SharedValueStores : public Printable { public: auto integers() -> ValueStore& { return integers_; } auto integers() const -> const ValueStore& { return integers_; } @@ -138,6 +155,16 @@ class SharedValueStores { auto strings() -> ValueStore& { return strings_; } auto strings() const -> const ValueStore& { return strings_; } + auto Print(llvm::raw_ostream& out) const -> void { + out << "shared_values:\n" + << " - integers:\n"; + integers_.Print(out, 6); + out << " - reals:\n"; + reals_.Print(out, 6); + out << " - strings:\n"; + strings_.Print(out, 6); + } + private: ValueStore integers_; ValueStore reals_; diff --git a/toolchain/driver/driver.cpp b/toolchain/driver/driver.cpp index 3bbae4daa60f..5a0b6a8bac6f 100644 --- a/toolchain/driver/driver.cpp +++ b/toolchain/driver/driver.cpp @@ -180,6 +180,14 @@ and displaying them in source order. }, [&](auto& arg_b) { arg_b.Set(&stream_errors); }); + b.AddFlag( + { + .name = "dump-shared-values", + .help = R"""( +Dumps shared values. These aren't owned by any particular file or phase. +)""", + }, + [&](auto& arg_b) { arg_b.Set(&dump_shared_values); }); b.AddFlag( { .name = "dump-tokens", @@ -257,6 +265,7 @@ Dump the generated assembly to stdout after codegen. bool asm_output = false; bool force_obj_output = false; + bool dump_shared_values = false; bool dump_tokens = false; bool dump_parse_tree = false; bool dump_raw_sem_ir = false; @@ -599,7 +608,6 @@ auto Driver::Compile(const CompileOptions& options) -> bool { return false; } - SharedValueStores value_stores; llvm::SmallVector> units; auto flush = llvm::make_scope_exit([&]() { // The diagnostics consumer must be flushed before compilation artifacts are @@ -609,6 +617,12 @@ auto Driver::Compile(const CompileOptions& options) -> bool { unit->Flush(); } }); + SharedValueStores value_stores; + auto dump_shared_values = llvm::make_scope_exit([&]() { + if (options.dump_shared_values) { + output_stream_ << value_stores; + } + }); for (const auto& input_file_name : options.input_file_names) { units.push_back(std::make_unique( this, &value_stores, options, input_file_name)); diff --git a/toolchain/driver/testdata/dump_shared_values.carbon b/toolchain/driver/testdata/dump_shared_values.carbon new file mode 100644 index 000000000000..035830ac1c15 --- /dev/null +++ b/toolchain/driver/testdata/dump_shared_values.carbon @@ -0,0 +1,20 @@ +// 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 +// +// ARGS: compile --phase=lex --dump-shared-values %s +// +// AUTOUPDATE + +var a: f64 = 1.0; +var b: String = "ab\"c"; + +// CHECK:STDOUT: shared_values: +// CHECK:STDOUT: - integers: +// CHECK:STDOUT: - 64 +// CHECK:STDOUT: - reals: +// CHECK:STDOUT: - 10*10^-1 +// CHECK:STDOUT: - strings: +// CHECK:STDOUT: - "a" +// CHECK:STDOUT: - "b" +// CHECK:STDOUT: - "ab\"c"