Add SPIR-V parser impl

The parser impl saves a copy of the SPIR-V binary.

Bug: tint:3
Change-Id: I5d61c87123c0bcb417d0c7004e0ef4e3c8fbb027
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16642
Reviewed-by: dan sinclair <dsinclair@google.com>
This commit is contained in:
David Neto 2020-03-13 20:14:33 +00:00
parent 306a2f8381
commit 1ae7c7dba9
6 changed files with 135 additions and 8 deletions

View File

@ -189,8 +189,11 @@ set(TINT_LIB_SRCS
if(TINT_BUILD_SPV_PARSER) if(TINT_BUILD_SPV_PARSER)
list(APPEND TINT_LIB_SRCS list(APPEND TINT_LIB_SRCS
reader/spv/fail_stream.h
reader/spv/parser.cc reader/spv/parser.cc
reader/spv/parser.h reader/spv/parser.h
reader/spv/parser_impl.cc
reader/spv/parser_impl.h
) )
endif() endif()

View File

@ -39,11 +39,11 @@ class FailStream {
/// Converts to a boolean status. A true result indicates success, /// Converts to a boolean status. A true result indicates success,
/// and a false result indicates failure. /// and a false result indicates failure.
/// @returns status /// @returns the status
operator bool() const { return *status_ptr_; } operator bool() const { return *status_ptr_; }
/// Returns the current status value. This can be more readable /// Returns the current status value. This can be more readable
/// the conversion operator. /// the conversion operator.
/// @returns status /// @returns the status
bool status() const { return *status_ptr_; } bool status() const { return *status_ptr_; }
/// Records failure. /// Records failure.

View File

@ -14,23 +14,25 @@
#include "src/reader/spv/parser.h" #include "src/reader/spv/parser.h"
#include <utility> #include "src/reader/spv/parser_impl.h"
namespace tint { namespace tint {
namespace reader { namespace reader {
namespace spv { namespace spv {
Parser::Parser(const std::vector<uint32_t>&) : Reader() {} Parser::Parser(const std::vector<uint32_t>& spv_binary)
: Reader(), impl_(std::make_unique<ParserImpl>(spv_binary)) {}
Parser::~Parser() = default; Parser::~Parser() = default;
bool Parser::Parse() { bool Parser::Parse() {
set_error("SPIR-V parsing is not supported yet"); const auto result = impl_->Parse();
return false; set_error(impl_->error());
return result;
} }
ast::Module Parser::module() { ast::Module Parser::module() {
return std::move(module_); return impl_->module();
} }
} // namespace spv } // namespace spv

View File

@ -15,6 +15,8 @@
#ifndef SRC_READER_SPV_PARSER_H_ #ifndef SRC_READER_SPV_PARSER_H_
#define SRC_READER_SPV_PARSER_H_ #define SRC_READER_SPV_PARSER_H_
#include <cstdint>
#include <memory>
#include <vector> #include <vector>
#include "src/reader/reader.h" #include "src/reader/reader.h"
@ -42,7 +44,7 @@ class Parser : public Reader {
ast::Module module() override; ast::Module module() override;
private: private:
ast::Module module_; std::unique_ptr<ParserImpl> impl_;
}; };
} // namespace spv } // namespace spv

View File

@ -0,0 +1,45 @@
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstring>
#include "src/reader/spv/parser_impl.h"
namespace tint {
namespace reader {
namespace spv {
ParserImpl::ParserImpl(const std::vector<uint32_t>& spv_binary)
: Reader(), spv_binary_(spv_binary), fail_stream_(&success_, &errors_) {}
ParserImpl::~ParserImpl() = default;
bool ParserImpl::Parse() {
// Exit early if we've already failed.
if (success_) {
Fail() << "SPIR-V parsing is not supported yet";
}
return success_;
}
ast::Module ParserImpl::module() {
// TODO(dneto): Should we clear out spv_binary_ here, to reduce
// memory usage?
return std::move(ast_module_);
}
} // namespace spv
} // namespace reader
} // namespace tint

View File

@ -0,0 +1,75 @@
// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef SRC_READER_SPV_PARSER_IMPL_H_
#define SRC_READER_SPV_PARSER_IMPL_H_
#include <cstdint>
#include <memory>
#include <sstream>
#include <vector>
#include "src/reader/reader.h"
#include "src/reader/spv/fail_stream.h"
namespace tint {
namespace reader {
namespace spv {
/// Parser implementation for SPIR-V.
class ParserImpl : Reader {
public:
/// Creates a new parser
/// @param input the input data to parse
explicit ParserImpl(const std::vector<uint32_t>& input);
~ParserImpl() override;
/// Run the parser
/// @returns true if the parse was successful, false otherwise.
bool Parse() override;
/// @returns the module. The module in the parser will be reset after this.
ast::Module module() override;
/// Logs failure, ands return a failure stream to accumulate diagnostic
/// messages. By convention, a failure should only be logged along with
/// a non-empty string diagnostic.
/// @returns the failure stream
FailStream& Fail() {
success_ = false;
return fail_stream_;
}
/// @returns the accumulated error string
const std::string error() { return errors_.str(); }
private:
// The SPIR-V binary we're parsing
std::vector<uint32_t> spv_binary_;
// The resulting module in Tint AST form.
ast::Module ast_module_;
// Is the parse successful?
bool success_ = true;
// Collector for diagnostic messages.
std::stringstream errors_;
FailStream fail_stream_;
};
} // namespace spv
} // namespace reader
} // namespace tint
#endif // SRC_READER_SPV_PARSER_IMPL_H_