main: Print the WGSL on reader/spirv error diagnostics

Just having a "error: blah" emitted without any additional context is not helpful without seeing the parsed SPIR-V as WGSL.

Change-Id: I6bf665f7fd3ce2637ec1ab020b2215f7fd5ee482
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/71603
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: James Price <jrprice@google.com>
This commit is contained in:
Ben Clayton 2021-12-03 16:19:53 +00:00 committed by Tint LUCI CQ
parent 3298625760
commit 2ec1393f31
1 changed files with 84 additions and 43 deletions

View File

@ -959,10 +959,36 @@ int main(int argc, const char** argv) {
std::unique_ptr<tint::Program> program; std::unique_ptr<tint::Program> program;
std::unique_ptr<tint::Source::File> source_file; std::unique_ptr<tint::Source::File> source_file;
#if TINT_BUILD_WGSL_READER
enum class InputFormat {
kUnknown,
kWgsl,
kSpirvBin,
kSpirvAsm,
};
auto input_format = InputFormat::kUnknown;
if (options.input_filename.size() > 5 && if (options.input_filename.size() > 5 &&
options.input_filename.substr(options.input_filename.size() - 5) == options.input_filename.substr(options.input_filename.size() - 5) ==
".wgsl") { ".wgsl") {
input_format = InputFormat::kWgsl;
} else if (options.input_filename.size() > 4 &&
options.input_filename.substr(options.input_filename.size() - 4) ==
".spv") {
input_format = InputFormat::kSpirvBin;
} else if (options.input_filename.size() > 7 &&
options.input_filename.substr(options.input_filename.size() - 7) ==
".spvasm") {
input_format = InputFormat::kSpirvAsm;
}
switch (input_format) {
case InputFormat::kUnknown: {
std::cerr << "Unknown input format" << std::endl;
return 1;
}
case InputFormat::kWgsl: {
#if TINT_BUILD_WGSL_READER
std::vector<uint8_t> data; std::vector<uint8_t> data;
if (!ReadFile<uint8_t>(options.input_filename, &data)) { if (!ReadFile<uint8_t>(options.input_filename, &data)) {
return 1; return 1;
@ -971,24 +997,28 @@ int main(int argc, const char** argv) {
options.input_filename, std::string(data.begin(), data.end())); options.input_filename, std::string(data.begin(), data.end()));
program = std::make_unique<tint::Program>( program = std::make_unique<tint::Program>(
tint::reader::wgsl::Parse(source_file.get())); tint::reader::wgsl::Parse(source_file.get()));
} break;
#else
std::cerr << "Tint not built with the WGSL reader enabled" << std::endl;
return 1;
#endif // TINT_BUILD_WGSL_READER #endif // TINT_BUILD_WGSL_READER
}
case InputFormat::kSpirvBin: {
#if TINT_BUILD_SPV_READER #if TINT_BUILD_SPV_READER
// Handle SPIR-V binary input, in files ending with .spv
if (options.input_filename.size() > 4 &&
options.input_filename.substr(options.input_filename.size() - 4) ==
".spv") {
std::vector<uint32_t> data; std::vector<uint32_t> data;
if (!ReadFile<uint32_t>(options.input_filename, &data)) { if (!ReadFile<uint32_t>(options.input_filename, &data)) {
return 1; return 1;
} }
program = std::make_unique<tint::Program>(tint::reader::spirv::Parse(data)); program =
std::make_unique<tint::Program>(tint::reader::spirv::Parse(data));
break;
#else
std::cerr << "Tint not built with the SPIR-V reader enabled" << std::endl;
return 1;
#endif // TINT_BUILD_SPV_READER
} }
// Handle SPIR-V assembly input, in files ending with .spvasm case InputFormat::kSpirvAsm: {
if (options.input_filename.size() > 7 && #if TINT_BUILD_SPV_READER
options.input_filename.substr(options.input_filename.size() - 7) ==
".spvasm") {
std::vector<char> text; std::vector<char> text;
if (!ReadFile<char>(options.input_filename, &text)) { if (!ReadFile<char>(options.input_filename, &text)) {
return 1; return 1;
@ -1005,16 +1035,27 @@ int main(int argc, const char** argv) {
SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS)) { SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS)) {
return 1; return 1;
} }
program = std::make_unique<tint::Program>(tint::reader::spirv::Parse(data)); program =
} std::make_unique<tint::Program>(tint::reader::spirv::Parse(data));
break;
#else
std::cerr << "Tint not built with the SPIR-V reader enabled" << std::endl;
return 1;
#endif // TINT_BUILD_SPV_READER #endif // TINT_BUILD_SPV_READER
}
}
if (!program) { if (!program) {
std::cerr << "Failed to create reader for input file: " std::cerr << "Failed to parse input file: " << options.input_filename
<< options.input_filename << std::endl; << std::endl;
return 1; return 1;
} }
if (program->Diagnostics().count() > 0) { if (program->Diagnostics().count() > 0) {
if (!program->IsValid() && input_format != InputFormat::kWgsl) {
// Invalid program from a non-wgsl source. Print the WGSL, to help
// understand the diagnostics.
PrintWGSL(std::cout, *program);
}
diag_formatter.format(program->Diagnostics(), diag_printer.get()); diag_formatter.format(program->Diagnostics(), diag_printer.get());
} }