Instantiate a stubbed SPIR-V parser if file ends in .spv

- spv parser just errors out for now
- link against SPIRV-Tools-opt
- Fixe CMake variable TINT_ENABLE_SPV_PARSER --> TINT_BUILD_SPV_PARSER

Bug: tint:3
Change-Id: Ie4ef9b03e001fca3cc11f65a425612755857feac
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16600
Reviewed-by: dan sinclair <dsinclair@google.com>
This commit is contained in:
David Neto 2020-03-10 22:54:12 +00:00 committed by dan sinclair
parent 49faa4c8d7
commit bbd8987e71
5 changed files with 57 additions and 19 deletions

View File

@ -69,6 +69,9 @@ endif()
function(tint_default_compile_options TARGET) function(tint_default_compile_options TARGET)
include_directories("${PROJECT_SOURCE_DIR}") include_directories("${PROJECT_SOURCE_DIR}")
if (${TINT_BUILD_SPV_PARSER})
target_compile_definitions(${TARGET} PRIVATE -DTINT_BUILD_SPV_PARSER=1)
endif()
if (${COMPILER_IS_LIKE_GNU}) if (${COMPILER_IS_LIKE_GNU})
target_compile_options(${TARGET} PRIVATE target_compile_options(${TARGET} PRIVATE
-std=c++14 -std=c++14

View File

@ -26,6 +26,10 @@
#include "src/writer/wgsl/generator.h" #include "src/writer/wgsl/generator.h"
#include "src/writer/writer.h" #include "src/writer/writer.h"
#if TINT_BUILD_SPV_PARSER
#include "src/reader/spv/parser.h"
#endif
namespace { namespace {
enum class Format { enum class Format {
@ -110,7 +114,18 @@ bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
return true; return true;
} }
std::vector<uint8_t> ReadFile(const std::string& input_file) { /// Copies the content from the file named |filename| to |buffer|,
/// assuming each element in the file is of type |T|. If any error occurs,
/// writes error messages to the standard error stream and returns false.
/// Assumes the size of a |T| object is divisible by its required alignment.
/// @returns true if we successfully read the file.
template <typename T>
bool ReadFile(const std::string& input_file, std::vector<T>* buffer) {
if (!buffer) {
std::cerr << "The buffer pointer was null" << std::endl;
return false;
}
FILE* file = nullptr; FILE* file = nullptr;
#if defined(_MSC_VER) #if defined(_MSC_VER)
fopen_s(&file, input_file.c_str(), "rb"); fopen_s(&file, input_file.c_str(), "rb");
@ -119,7 +134,7 @@ std::vector<uint8_t> ReadFile(const std::string& input_file) {
#endif #endif
if (!file) { if (!file) {
std::cerr << "Failed to open " << input_file << std::endl; std::cerr << "Failed to open " << input_file << std::endl;
return {}; return false;
} }
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
@ -129,21 +144,28 @@ std::vector<uint8_t> ReadFile(const std::string& input_file) {
fclose(file); fclose(file);
return {}; return {};
} }
const auto file_size = static_cast<size_t>(tell_file_size);
if (0 != (file_size % sizeof(T))) {
std::cerr << "File " << input_file
<< " does not contain an integral number of objects: "
<< file_size << " bytes in the file, require " << sizeof(T)
<< " bytes per object" << std::endl;
fclose(file);
return false;
}
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
size_t file_size = static_cast<size_t>(tell_file_size); buffer->clear();
buffer->resize(file_size / sizeof(T));
std::vector<uint8_t> data; size_t bytes_read = fread(buffer->data(), 1, file_size, file);
data.resize(file_size);
size_t bytes_read = fread(data.data(), sizeof(uint8_t), file_size, file);
fclose(file); fclose(file);
if (bytes_read != file_size) { if (bytes_read != file_size) {
std::cerr << "Failed to read " << input_file << std::endl; std::cerr << "Failed to read " << input_file << std::endl;
return {}; return false;
} }
return data; return true;
} }
std::string Disassemble(const std::vector<uint32_t>& data) { std::string Disassemble(const std::vector<uint32_t>& data) {
@ -204,18 +226,28 @@ int main(int argc, const char** argv) {
return 1; return 1;
} }
auto data = ReadFile(options.input_filename);
if (data.size() == 0)
return 1;
std::unique_ptr<tint::reader::Reader> reader; std::unique_ptr<tint::reader::Reader> reader;
std::string ext = "wgsl"; if (options.input_filename.size() > 5 &&
if (options.input_filename.size() > 4 && options.input_filename.substr(options.input_filename.size() - 5) ==
options.input_filename.substr(options.input_filename.size() - 4) == ".wgsl") {
"wgsl") { std::vector<uint8_t> data;
if (!ReadFile<uint8_t>(options.input_filename, &data)) {
return 1;
}
reader = std::make_unique<tint::reader::wgsl::Parser>( reader = std::make_unique<tint::reader::wgsl::Parser>(
std::string(data.begin(), data.end())); std::string(data.begin(), data.end()));
} }
#if TINT_BUILD_SPV_PARSER
if (options.input_filename.size() > 4 &&
options.input_filename.substr(options.input_filename.size() - 4) ==
".spv") {
std::vector<uint32_t> data;
if (!ReadFile<uint32_t>(options.input_filename, &data)) {
return 1;
}
reader = std::make_unique<tint::reader::spv::Parser>(data);
}
#endif
if (!reader) { if (!reader) {
std::cerr << "Failed to create reader for input file: " std::cerr << "Failed to create reader for input file: "
<< options.input_filename << std::endl; << options.input_filename << std::endl;

View File

@ -305,8 +305,9 @@ add_library(libtint ${TINT_LIB_SRCS})
tint_default_compile_options(libtint) tint_default_compile_options(libtint)
set_target_properties(libtint PROPERTIES OUTPUT_NAME "tint") set_target_properties(libtint PROPERTIES OUTPUT_NAME "tint")
if(${TINT_ENABLE_SPV_PARSER}) if(${TINT_BUILD_SPV_PARSER})
target_link_libraries(libtint SPIRV-Tools) # We'll use the optimizer for its nice SPIR-V in-memory representation
target_link_libraries(libtint SPIRV-Tools-opt SPIRV-Tools)
endif() endif()
add_executable(tint_unittests ${TINT_TEST_SRCS}) add_executable(tint_unittests ${TINT_TEST_SRCS})

View File

@ -25,6 +25,7 @@ Parser::Parser(const std::vector<uint32_t>&) : Reader() {}
Parser::~Parser() = default; Parser::~Parser() = default;
bool Parser::Parse() { bool Parser::Parse() {
set_error("SPIR-V parsing is not supported yet");
return false; return false;
} }

View File

@ -31,6 +31,7 @@ class Parser : public Reader {
/// Creates a new parser /// Creates a new parser
/// @param input the input data to parse /// @param input the input data to parse
explicit Parser(const std::vector<uint32_t>& input); explicit Parser(const std::vector<uint32_t>& input);
/// Destructor
~Parser() override; ~Parser() override;
/// Run the parser /// Run the parser