Move generators to unique pointers.

This Cl will setup the code to allow a Reset method to be added to the
generators.

Bug: tint:211
Change-Id: I41c3aaf0daf832729aea9c76500e297ae32d7f5e
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/28042
Commit-Queue: David Neto <dneto@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
dan sinclair 2020-09-03 00:58:46 +00:00 committed by Commit Bot service account
parent bab2a4dd96
commit 836027e327
13 changed files with 45 additions and 36 deletions

View File

@ -21,14 +21,15 @@ namespace writer {
namespace hlsl { namespace hlsl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), impl_(&module_) {} : Text(std::move(module)),
impl_(std::make_unique<GeneratorImpl>(&module_)) {}
Generator::~Generator() = default; Generator::~Generator() = default;
bool Generator::Generate() { bool Generator::Generate() {
auto ret = impl_.Generate(out_); auto ret = impl_->Generate(out_);
if (!ret) { if (!ret) {
error_ = impl_.error(); error_ = impl_->error();
} }
return ret; return ret;
} }
@ -38,7 +39,7 @@ std::string Generator::result() const {
} }
std::string Generator::error() const { std::string Generator::error() const {
return impl_.error(); return impl_->error();
} }
} // namespace hlsl } // namespace hlsl

View File

@ -15,6 +15,7 @@
#ifndef SRC_WRITER_HLSL_GENERATOR_H_ #ifndef SRC_WRITER_HLSL_GENERATOR_H_
#define SRC_WRITER_HLSL_GENERATOR_H_ #define SRC_WRITER_HLSL_GENERATOR_H_
#include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
@ -45,7 +46,7 @@ class Generator : public Text {
private: private:
std::ostringstream out_; std::ostringstream out_;
GeneratorImpl impl_; std::unique_ptr<GeneratorImpl> impl_;
}; };
} // namespace hlsl } // namespace hlsl

View File

@ -21,24 +21,25 @@ namespace writer {
namespace msl { namespace msl {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: Text(std::move(module)), impl_(&module_) {} : Text(std::move(module)),
impl_(std::make_unique<GeneratorImpl>(&module_)) {}
Generator::~Generator() = default; Generator::~Generator() = default;
bool Generator::Generate() { bool Generator::Generate() {
auto ret = impl_.Generate(); auto ret = impl_->Generate();
if (!ret) { if (!ret) {
error_ = impl_.error(); error_ = impl_->error();
} }
return ret; return ret;
} }
std::string Generator::result() const { std::string Generator::result() const {
return impl_.result(); return impl_->result();
} }
std::string Generator::error() const { std::string Generator::error() const {
return impl_.error(); return impl_->error();
} }
} // namespace msl } // namespace msl

View File

@ -15,6 +15,7 @@
#ifndef SRC_WRITER_MSL_GENERATOR_H_ #ifndef SRC_WRITER_MSL_GENERATOR_H_
#define SRC_WRITER_MSL_GENERATOR_H_ #define SRC_WRITER_MSL_GENERATOR_H_
#include <memory>
#include <string> #include <string>
#include "src/writer/msl/generator_impl.h" #include "src/writer/msl/generator_impl.h"
@ -43,7 +44,7 @@ class Generator : public Text {
std::string error() const; std::string error() const;
private: private:
GeneratorImpl impl_; std::unique_ptr<GeneratorImpl> impl_;
}; };
} // namespace msl } // namespace msl

View File

@ -29,9 +29,9 @@ BinaryWriter::BinaryWriter() = default;
BinaryWriter::~BinaryWriter() = default; BinaryWriter::~BinaryWriter() = default;
void BinaryWriter::WriteBuilder(const Builder& builder) { void BinaryWriter::WriteBuilder(Builder* builder) {
out_.reserve(builder.total_size()); out_.reserve(builder->total_size());
builder.iterate( builder->iterate(
[this](const Instruction& inst) { this->process_instruction(inst); }); [this](const Instruction& inst) { this->process_instruction(inst); });
} }

View File

@ -38,7 +38,7 @@ class BinaryWriter {
/// the SPIR-V header. You |must| call |WriteHeader| before |WriteBuilder| /// the SPIR-V header. You |must| call |WriteHeader| before |WriteBuilder|
/// if you want the SPIR-V to be emitted. /// if you want the SPIR-V to be emitted.
/// @param builder the builder to assemble from /// @param builder the builder to assemble from
void WriteBuilder(const Builder& builder); void WriteBuilder(Builder* builder);
/// Writes the given instruction into the binary. /// Writes the given instruction into the binary.
/// @param inst the instruction to assemble /// @param inst the instruction to assemble

View File

@ -47,7 +47,7 @@ TEST_F(BinaryWriterTest, Float) {
Builder b(&mod); Builder b(&mod);
b.push_preamble(spv::Op::OpKill, {Operand::Float(2.4f)}); b.push_preamble(spv::Op::OpKill, {Operand::Float(2.4f)});
BinaryWriter bw; BinaryWriter bw;
bw.WriteBuilder(b); bw.WriteBuilder(&b);
auto res = bw.result(); auto res = bw.result();
ASSERT_EQ(res.size(), 2u); ASSERT_EQ(res.size(), 2u);
@ -61,7 +61,7 @@ TEST_F(BinaryWriterTest, Int) {
Builder b(&mod); Builder b(&mod);
b.push_preamble(spv::Op::OpKill, {Operand::Int(2)}); b.push_preamble(spv::Op::OpKill, {Operand::Int(2)});
BinaryWriter bw; BinaryWriter bw;
bw.WriteBuilder(b); bw.WriteBuilder(&b);
auto res = bw.result(); auto res = bw.result();
ASSERT_EQ(res.size(), 2u); ASSERT_EQ(res.size(), 2u);
@ -73,7 +73,7 @@ TEST_F(BinaryWriterTest, String) {
Builder b(&mod); Builder b(&mod);
b.push_preamble(spv::Op::OpKill, {Operand::String("my_string")}); b.push_preamble(spv::Op::OpKill, {Operand::String("my_string")});
BinaryWriter bw; BinaryWriter bw;
bw.WriteBuilder(b); bw.WriteBuilder(&b);
auto res = bw.result(); auto res = bw.result();
ASSERT_EQ(res.size(), 4u); ASSERT_EQ(res.size(), 4u);
@ -98,7 +98,7 @@ TEST_F(BinaryWriterTest, String_Multiple4Length) {
Builder b(&mod); Builder b(&mod);
b.push_preamble(spv::Op::OpKill, {Operand::String("mystring")}); b.push_preamble(spv::Op::OpKill, {Operand::String("mystring")});
BinaryWriter bw; BinaryWriter bw;
bw.WriteBuilder(b); bw.WriteBuilder(&b);
auto res = bw.result(); auto res = bw.result();
ASSERT_EQ(res.size(), 4u); ASSERT_EQ(res.size(), 4u);

View File

@ -21,18 +21,20 @@ namespace writer {
namespace spirv { namespace spirv {
Generator::Generator(ast::Module module) Generator::Generator(ast::Module module)
: writer::Writer(std::move(module)), builder_(&module_) {} : writer::Writer(std::move(module)),
builder_(std::make_unique<Builder>(&module_)),
writer_(std::make_unique<BinaryWriter>()) {}
Generator::~Generator() = default; Generator::~Generator() = default;
bool Generator::Generate() { bool Generator::Generate() {
if (!builder_.Build()) { if (!builder_->Build()) {
set_error(builder_.error()); set_error(builder_->error());
return false; return false;
} }
writer_.WriteHeader(builder_.id_bound()); writer_->WriteHeader(builder_->id_bound());
writer_.WriteBuilder(builder_); writer_->WriteBuilder(builder_.get());
return true; return true;
} }

View File

@ -15,6 +15,7 @@
#ifndef SRC_WRITER_SPIRV_GENERATOR_H_ #ifndef SRC_WRITER_SPIRV_GENERATOR_H_
#define SRC_WRITER_SPIRV_GENERATOR_H_ #define SRC_WRITER_SPIRV_GENERATOR_H_
#include <memory>
#include <vector> #include <vector>
#include "src/ast/module.h" #include "src/ast/module.h"
@ -39,11 +40,11 @@ class Generator : public writer::Writer {
bool Generate() override; bool Generate() override;
/// @returns the result data /// @returns the result data
const std::vector<uint32_t>& result() const { return writer_.result(); } const std::vector<uint32_t>& result() const { return writer_->result(); }
private: private:
Builder builder_; std::unique_ptr<Builder> builder_;
BinaryWriter writer_; std::unique_ptr<BinaryWriter> writer_;
}; };
} // namespace spirv } // namespace spirv

View File

@ -61,10 +61,10 @@ std::string Disassemble(const std::vector<uint32_t>& data) {
} // namespace } // namespace
std::string DumpBuilder(const Builder& builder) { std::string DumpBuilder(Builder& builder) {
BinaryWriter writer; BinaryWriter writer;
writer.WriteHeader(builder.id_bound()); writer.WriteHeader(builder.id_bound());
writer.WriteBuilder(builder); writer.WriteBuilder(&builder);
return Disassemble(writer.result()); return Disassemble(writer.result());
} }

View File

@ -28,7 +28,7 @@ namespace spirv {
/// Dumps the given builder to a SPIR-V disassembly string /// Dumps the given builder to a SPIR-V disassembly string
/// @param builder the builder to convert /// @param builder the builder to convert
/// @returns the builder as a SPIR-V disassembly string /// @returns the builder as a SPIR-V disassembly string
std::string DumpBuilder(const Builder& builder); std::string DumpBuilder(Builder& builder);
/// Dumps the given instruction to a SPIR-V disassembly string /// Dumps the given instruction to a SPIR-V disassembly string
/// @param inst the instruction to dump /// @param inst the instruction to dump

View File

@ -20,24 +20,25 @@ namespace tint {
namespace writer { namespace writer {
namespace wgsl { namespace wgsl {
Generator::Generator(ast::Module module) : Text(std::move(module)) {} Generator::Generator(ast::Module module)
: Text(std::move(module)), impl_(std::make_unique<GeneratorImpl>()) {}
Generator::~Generator() = default; Generator::~Generator() = default;
bool Generator::Generate() { bool Generator::Generate() {
auto ret = impl_.Generate(module_); auto ret = impl_->Generate(module_);
if (!ret) { if (!ret) {
error_ = impl_.error(); error_ = impl_->error();
} }
return ret; return ret;
} }
std::string Generator::result() const { std::string Generator::result() const {
return impl_.result(); return impl_->result();
} }
std::string Generator::error() const { std::string Generator::error() const {
return impl_.error(); return impl_->error();
} }
} // namespace wgsl } // namespace wgsl

View File

@ -15,6 +15,7 @@
#ifndef SRC_WRITER_WGSL_GENERATOR_H_ #ifndef SRC_WRITER_WGSL_GENERATOR_H_
#define SRC_WRITER_WGSL_GENERATOR_H_ #define SRC_WRITER_WGSL_GENERATOR_H_
#include <memory>
#include <string> #include <string>
#include "src/writer/text.h" #include "src/writer/text.h"
@ -43,7 +44,7 @@ class Generator : public Text {
std::string error() const; std::string error() const;
private: private:
GeneratorImpl impl_; std::unique_ptr<GeneratorImpl> impl_;
}; };
} // namespace wgsl } // namespace wgsl