Convert SPIR-V reader to utils::StringStream.

This CL updates the SPIR-V reader to use utils::StringStream internally.

Bug: tint:1686
Change-Id: I619c2aa56b85e6c24b933ffa2a1a409d63846f5e
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/122000
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Dan Sinclair <dsinclair@chromium.org>
This commit is contained in:
dan sinclair 2023-02-28 17:36:43 +00:00 committed by Dawn LUCI CQ
parent b23cda4bc2
commit 0c184c2856
19 changed files with 74 additions and 54 deletions

View File

@ -19,6 +19,7 @@
#include <sstream>
#include <string>
#include "src/tint/utils/string_stream.h"
#include "src/tint/utils/vector.h"
namespace tint::reader::spirv {
@ -173,7 +174,7 @@ inline std::string ToString(Construct::Kind kind) {
/// @returns a short summary string
inline std::string ToStringBrief(const Construct* c) {
if (c) {
std::stringstream ss;
utils::StringStream ss;
ss << ToString(c->kind) << "@" << c->begin_id;
return ss.str();
}
@ -184,7 +185,7 @@ inline std::string ToStringBrief(const Construct* c) {
/// @param o the stream
/// @param c the structured construct
/// @returns the stream
inline std::ostream& operator<<(std::ostream& o, const Construct& c) {
inline utils::StringStream& operator<<(utils::StringStream& o, const Construct& c) {
o << "Construct{ " << ToString(c.kind) << " [" << c.begin_pos << "," << c.end_pos << ")"
<< " begin_id:" << c.begin_id << " end_id:" << c.end_id << " depth:" << c.depth;
@ -215,7 +216,8 @@ inline std::ostream& operator<<(std::ostream& o, const Construct& c) {
/// @param o the stream
/// @param c the structured construct
/// @returns the stream
inline std::ostream& operator<<(std::ostream& o, const std::unique_ptr<Construct>& c) {
inline utils::StringStream& operator<<(utils::StringStream& o,
const std::unique_ptr<Construct>& c) {
return o << *(c.get());
}
@ -223,7 +225,7 @@ inline std::ostream& operator<<(std::ostream& o, const std::unique_ptr<Construct
/// @param c the construct
/// @returns the string representation
inline std::string ToString(const Construct& c) {
std::stringstream ss;
utils::StringStream ss;
ss << c;
return ss.str();
}
@ -246,7 +248,7 @@ inline std::string ToString(const std::unique_ptr<Construct>& c) {
/// @param o the stream
/// @param cl the construct list
/// @returns the stream
inline std::ostream& operator<<(std::ostream& o, const ConstructList& cl) {
inline utils::StringStream& operator<<(utils::StringStream& o, const ConstructList& cl) {
o << "ConstructList{\n";
for (const auto& c : cl) {
o << " " << c << "\n";
@ -259,7 +261,7 @@ inline std::ostream& operator<<(std::ostream& o, const ConstructList& cl) {
/// @param cl the construct list
/// @returns the string representation
inline std::string ToString(const ConstructList& cl) {
std::stringstream ss;
utils::StringStream ss;
ss << cl;
return ss.str();
}

View File

@ -18,6 +18,7 @@
#include "gmock/gmock.h"
#include "src/tint/type/texture_dimension.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -45,7 +46,7 @@ class SpvPipelineStageTest : public testing::TestWithParam<PipelineStageCase> {
protected:
bool success_ = true;
std::stringstream errors_;
utils::StringStream errors_;
FailStream fail_stream_;
EnumConverter converter_;
};
@ -103,7 +104,7 @@ class SpvStorageClassTest : public testing::TestWithParam<StorageClassCase> {
protected:
bool success_ = true;
std::stringstream errors_;
utils::StringStream errors_;
FailStream fail_stream_;
EnumConverter converter_;
};
@ -164,7 +165,7 @@ class SpvBuiltinTest : public testing::TestWithParam<BuiltinCase> {
protected:
bool success_ = true;
std::stringstream errors_;
utils::StringStream errors_;
FailStream fail_stream_;
EnumConverter converter_;
};
@ -239,7 +240,7 @@ class SpvDimTest : public testing::TestWithParam<DimCase> {
protected:
bool success_ = true;
std::stringstream errors_;
utils::StringStream errors_;
FailStream fail_stream_;
EnumConverter converter_;
};
@ -311,7 +312,7 @@ class SpvImageFormatTest : public testing::TestWithParam<TexelFormatCase> {
protected:
bool success_ = true;
std::stringstream errors_;
utils::StringStream errors_;
FailStream fail_stream_;
EnumConverter converter_;
};

View File

@ -15,7 +15,7 @@
#ifndef SRC_TINT_READER_SPIRV_FAIL_STREAM_H_
#define SRC_TINT_READER_SPIRV_FAIL_STREAM_H_
#include <ostream>
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
@ -29,7 +29,7 @@ class FailStream {
/// to be a valid pointer to bool.
/// @param out output stream where a message should be written to explain
/// the failure
FailStream(bool* status_ptr, std::ostream* out) : status_ptr_(status_ptr), out_(out) {}
FailStream(bool* status_ptr, utils::StringStream* out) : status_ptr_(status_ptr), out_(out) {}
/// Copy constructor
/// @param other the fail stream to clone
FailStream(const FailStream& other) = default;
@ -61,7 +61,7 @@ class FailStream {
private:
bool* status_ptr_;
std::ostream* out_;
utils::StringStream* out_;
};
} // namespace tint::reader::spirv

View File

@ -15,6 +15,7 @@
#include "src/tint/reader/spirv/fail_stream.h"
#include "gmock/gmock.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -56,7 +57,7 @@ TEST_F(FailStreamTest, FailMethodReturnsSelf) {
TEST_F(FailStreamTest, ShiftOperatorAccumulatesValues) {
bool flag = true;
std::stringstream ss;
utils::StringStream ss;
FailStream fs(&flag, &ss);
ss << "prefix ";

View File

@ -16,6 +16,7 @@
#include "src/tint/reader/spirv/function.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -307,7 +308,7 @@ TEST_P(SpvBinaryArithTest, EmitExpression) {
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error() << "\n" << assembly;
auto fe = p->function_emitter(100);
EXPECT_TRUE(fe.EmitBody()) << p->error();
std::ostringstream ss;
utils::StringStream ss;
ss << "let x_1 : " << GetParam().ast_type << " = (" << GetParam().ast_lhs << " "
<< GetParam().ast_op << " " << GetParam().ast_rhs << ");";
auto ast_body = fe.ast_body();
@ -346,7 +347,7 @@ TEST_P(SpvBinaryArithGeneralTest, EmitExpression) {
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error() << "\n" << assembly;
auto fe = p->function_emitter(100);
EXPECT_TRUE(fe.EmitBody()) << p->error();
std::ostringstream ss;
utils::StringStream ss;
ss << "let x_1 : " << GetParam().wgsl_type << " = " << GetParam().expected << ";";
auto ast_body = fe.ast_body();
auto got = test::ToString(p->program(), ast_body);

View File

@ -15,6 +15,7 @@
#include "src/tint/reader/spirv/function.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -125,7 +126,7 @@ TEST_P(SpvBinaryBitTest, EmitExpression) {
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error() << "\n" << assembly;
auto fe = p->function_emitter(100);
EXPECT_TRUE(fe.EmitBody()) << p->error();
std::ostringstream ss;
utils::StringStream ss;
ss << "let x_1 : " << GetParam().ast_type << " = (" << GetParam().ast_lhs << " "
<< GetParam().ast_op << " " << GetParam().ast_rhs << ");";
auto ast_body = fe.ast_body();
@ -163,7 +164,7 @@ TEST_P(SpvBinaryBitGeneralTest, EmitExpression) {
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error() << "\n" << assembly;
auto fe = p->function_emitter(100);
EXPECT_TRUE(fe.EmitBody()) << p->error() << assembly;
std::ostringstream ss;
utils::StringStream ss;
ss << "let x_1 : " << GetParam().wgsl_type << " = " << GetParam().expected << ";\nreturn;\n";
auto ast_body = fe.ast_body();
auto got = test::ToString(p->program(), ast_body);

View File

@ -16,6 +16,7 @@
#include "src/tint/reader/spirv/function.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -27,7 +28,7 @@ using ::testing::HasSubstr;
using SpvParserCFGTest = SpvParserTest;
std::string Dump(const std::vector<uint32_t>& v) {
std::ostringstream o;
utils::StringStream o;
o << "{";
for (auto a : v) {
o << a << " ";

View File

@ -16,6 +16,7 @@
#include "src/tint/reader/spirv/function.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -34,7 +35,7 @@ std::string Preamble() {
/// @returns a SPIR-V assembly segment which assigns debug names
/// to particular IDs.
std::string Names(std::vector<std::string> ids) {
std::ostringstream outs;
utils::StringStream outs;
for (auto& id : ids) {
outs << " OpName %" << id << " \"" << id << "\"\n";
}

View File

@ -16,6 +16,7 @@
#include "src/tint/reader/spirv/function.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -188,7 +189,7 @@ TEST_P(SpvBinaryLogicalTest, EmitExpression) {
ASSERT_TRUE(p->BuildAndParseInternalModuleExceptFunctions()) << p->error() << "\n" << assembly;
auto fe = p->function_emitter(100);
EXPECT_TRUE(fe.EmitBody()) << p->error();
std::ostringstream ss;
utils::StringStream ss;
ss << "let x_1 : " << GetParam().ast_type << " = (" << GetParam().ast_lhs << " "
<< GetParam().ast_op << " " << GetParam().ast_rhs << ");";
auto ast_body = fe.ast_body();

View File

@ -16,6 +16,7 @@
#include "src/tint/reader/spirv/function.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -26,7 +27,7 @@ using ::testing::HasSubstr;
/// @returns a SPIR-V assembly segment which assigns debug names
/// to particular IDs.
std::string Names(std::vector<std::string> ids) {
std::ostringstream outs;
utils::StringStream outs;
for (auto& id : ids) {
outs << " OpName %" << id << " \"" << id << "\"\n";
}

View File

@ -19,6 +19,7 @@
#include <unordered_set>
#include "src/tint/debug.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
@ -219,7 +220,7 @@ std::string Namer::FindUnusedDerivedName(const std::string& base_name) {
std::string derived_name;
uint32_t& i = next_unusued_derived_name_id_[base_name];
while (i != 0xffffffff) {
std::stringstream new_name_stream;
utils::StringStream new_name_stream;
new_name_stream << base_name;
if (i > 0) {
new_name_stream << "_" << i;
@ -305,7 +306,7 @@ void Namer::ResolveMemberNamesForStruct(uint32_t struct_id, uint32_t num_members
uint32_t i = 1;
std::string new_name;
do {
std::stringstream new_name_stream;
utils::StringStream new_name_stream;
new_name_stream << suggestion << "_" << i;
new_name = new_name_stream.str();
++i;
@ -331,7 +332,7 @@ void Namer::ResolveMemberNamesForStruct(uint32_t struct_id, uint32_t num_members
uint32_t index = 0;
for (auto& name : name_vector) {
if (name.empty()) {
std::stringstream suggestion;
utils::StringStream suggestion;
suggestion << "field" << index;
// Again, modify the name-vector in-place.
name = disambiguate_name(suggestion.str());

View File

@ -15,6 +15,7 @@
#include "src/tint/reader/spirv/namer.h"
#include "gmock/gmock.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -29,7 +30,7 @@ class SpvNamerTest : public testing::Test {
std::string error() { return errors_.str(); }
protected:
std::stringstream errors_;
utils::StringStream errors_;
bool success_ = true;
FailStream fail_stream_;
};
@ -351,7 +352,7 @@ using SpvNamerReservedWordTest = ::testing::TestWithParam<std::string>;
TEST_P(SpvNamerReservedWordTest, ReservedWordsAreUsed) {
bool success;
std::stringstream errors;
utils::StringStream errors;
FailStream fail_stream(&success, &errors);
Namer namer(fail_stream);
const std::string reserved = GetParam();

View File

@ -24,6 +24,7 @@
#include "src/tint/utils/compiler_macros.h"
#include "src/tint/utils/hashmap.h"
#include "src/tint/utils/string_stream.h"
TINT_BEGIN_DISABLE_WARNING(NEWLINE_EOF);
TINT_BEGIN_DISABLE_WARNING(OLD_STYLE_CAST);
@ -819,7 +820,7 @@ class ParserImpl : Reader {
// Is the parse successful?
bool success_ = true;
// Collector for diagnostic messages.
std::stringstream errors_;
utils::StringStream errors_;
FailStream fail_stream_;
spvtools::MessageConsumer message_consumer_;

View File

@ -15,6 +15,7 @@
#include "gmock/gmock.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -47,7 +48,7 @@ std::string MainBody() {
/// @returns a SPIR-V assembly segment which assigns debug names
/// to particular IDs.
std::string Names(std::vector<std::string> ids) {
std::ostringstream outs;
utils::StringStream outs;
for (auto& id : ids) {
outs << " OpName %" << id << " \"" << id << "\"\n";
}

View File

@ -18,6 +18,7 @@
#include "src/tint/reader/spirv/function.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/reader/spirv/spirv_tools_helpers_test.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -235,7 +236,7 @@ std::string CommonTypes() {
}
std::string Bindings(std::vector<uint32_t> ids) {
std::ostringstream os;
utils::StringStream os;
int binding = 0;
for (auto id : ids) {
os << " OpDecorate %" << id << " DescriptorSet 0\n"

View File

@ -23,6 +23,7 @@
#include "src/tint/utils/hash.h"
#include "src/tint/utils/map.h"
#include "src/tint/utils/string.h"
#include "src/tint/utils/string_stream.h"
#include "src/tint/utils/unique_allocator.h"
TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Type);
@ -557,31 +558,31 @@ std::string I32::String() const {
}
std::string Pointer::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "ptr<" << utils::ToString(address_space) << ", " << type->String() + ">";
return ss.str();
}
std::string Reference::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "ref<" + utils::ToString(address_space) << ", " << type->String() << ">";
return ss.str();
}
std::string Vector::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "vec" << size << "<" << type->String() << ">";
return ss.str();
}
std::string Matrix::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "mat" << columns << "x" << rows << "<" << type->String() << ">";
return ss.str();
}
std::string Array::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "array<" << type->String() << ", " << size << ", " << stride << ">";
return ss.str();
}
@ -597,31 +598,31 @@ std::string Sampler::String() const {
}
std::string DepthTexture::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "depth_" << dims;
return ss.str();
}
std::string DepthMultisampledTexture::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "depth_multisampled_" << dims;
return ss.str();
}
std::string MultisampledTexture::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "texture_multisampled_" << dims << "<" << type << ">";
return ss.str();
}
std::string SampledTexture::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "texture_" << dims << "<" << type << ">";
return ss.str();
}
std::string StorageTexture::String() const {
std::stringstream ss;
utils::StringStream ss;
ss << "texture_storage_" << dims << "<" << format << ", " << access << ">";
return ss.str();
}

View File

@ -16,6 +16,7 @@
#include "gtest/gtest.h"
#include "spirv-tools/libspirv.hpp"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv::test {
@ -24,7 +25,7 @@ std::vector<uint32_t> Assemble(const std::string& spirv_assembly) {
// (The target environment doesn't affect assembly.
spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_0);
std::stringstream errors;
utils::StringStream errors;
std::vector<uint32_t> result;
tools.SetMessageConsumer([&errors](spv_message_level_t, const char*,
const spv_position_t& position, const char* message) {
@ -40,7 +41,7 @@ std::vector<uint32_t> Assemble(const std::string& spirv_assembly) {
std::string Disassemble(const std::vector<uint32_t>& spirv_module) {
spvtools::SpirvTools tools(SPV_ENV_UNIVERSAL_1_0);
std::stringstream errors;
utils::StringStream errors;
tools.SetMessageConsumer([&errors](spv_message_level_t, const char*,
const spv_position_t& position, const char* message) {
errors << "disassmbly error:" << position.line << ":" << position.column << ": " << message;

View File

@ -16,6 +16,8 @@
#include <sstream>
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
Usage::Usage() {}
@ -179,7 +181,7 @@ void Usage::AddDepthTexture() {
}
std::string Usage::to_str() const {
std::ostringstream ss;
utils::StringStream ss;
ss << *this;
return ss.str();
}

View File

@ -17,6 +17,7 @@
#include "gmock/gmock.h"
#include "src/tint/reader/spirv/parser_impl_test_helper.h"
#include "src/tint/utils/string_stream.h"
namespace tint::reader::spirv {
namespace {
@ -38,7 +39,7 @@ TEST_F(SpvParserTest, Usage_Trivial_Properties) {
}
TEST_F(SpvParserTest, Usage_Trivial_Output) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
ss << u;
EXPECT_THAT(ss.str(), Eq("Usage()"));
@ -89,13 +90,13 @@ TEST_F(SpvParserTest, Usage_Add) {
EXPECT_TRUE(a.IsStorageReadTexture());
EXPECT_FALSE(a.IsStorageWriteTexture());
std::ostringstream ss;
utils::StringStream ss;
ss << a;
EXPECT_THAT(ss.str(), Eq("Usage(Sampler( comparison )Texture( read ))"));
}
TEST_F(SpvParserTest, Usage_AddSampler) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddSampler();
@ -120,7 +121,7 @@ TEST_F(SpvParserTest, Usage_AddSampler) {
}
TEST_F(SpvParserTest, Usage_AddComparisonSampler) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddComparisonSampler();
@ -144,7 +145,7 @@ TEST_F(SpvParserTest, Usage_AddComparisonSampler) {
}
TEST_F(SpvParserTest, Usage_AddTexture) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddTexture();
@ -168,7 +169,7 @@ TEST_F(SpvParserTest, Usage_AddTexture) {
}
TEST_F(SpvParserTest, Usage_AddSampledTexture) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddSampledTexture();
@ -192,7 +193,7 @@ TEST_F(SpvParserTest, Usage_AddSampledTexture) {
}
TEST_F(SpvParserTest, Usage_AddMultisampledTexture) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddMultisampledTexture();
@ -216,7 +217,7 @@ TEST_F(SpvParserTest, Usage_AddMultisampledTexture) {
}
TEST_F(SpvParserTest, Usage_AddDepthTexture) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddDepthTexture();
@ -240,7 +241,7 @@ TEST_F(SpvParserTest, Usage_AddDepthTexture) {
}
TEST_F(SpvParserTest, Usage_AddStorageReadTexture) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddStorageReadTexture();
@ -264,7 +265,7 @@ TEST_F(SpvParserTest, Usage_AddStorageReadTexture) {
}
TEST_F(SpvParserTest, Usage_AddStorageWriteTexture) {
std::ostringstream ss;
utils::StringStream ss;
Usage u;
u.AddStorageWriteTexture();