Skip to content

Commit 7f8f9ac

Browse files
authored
[dxil2spv] Print missing runtime errors (#4481)
- Emit a diagnostic when input cannot be parsed. - Add exception handling code, for exceptions that can originate from DXC utility code. This cannot be easily tested with FileCheck because we would have to negate the dxil2spv return code.
1 parent 9395376 commit 7f8f9ac

2 files changed

Lines changed: 37 additions & 17 deletions

File tree

tools/clang/tools/dxil2spv/dxil2spvmain.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "dxc/Support/HLSLOptions.h"
3535
#include "dxc/Support/WinAdapter.h"
3636
#include "dxc/Support/dxcapi.use.h"
37+
#include "dxc/Support/exception.h"
3738
#include "dxc/dxcapi.h"
3839
#include "lib/dxil2spv.h"
3940
#include "clang/Frontend/CompilerInstance.h"
@@ -43,6 +44,8 @@
4344
#include "llvm/Support/FileSystem.h"
4445
#include "llvm/Support/MSFileSystem.h"
4546
#include "llvm/Support/raw_ostream.h"
47+
#include <cstdlib>
48+
#include <exception>
4649
#include <string>
4750

4851
// Command line options for dxil2spv. The general approach is to mirror the
@@ -82,24 +85,37 @@ int main(int argc, const char **argv_) {
8285
return EXIT_SUCCESS;
8386
}
8487

85-
// Setup a compiler instance with diagnostics.
86-
clang::CompilerInstance instance;
87-
auto *diagnosticPrinter = new clang::TextDiagnosticPrinter(
88-
llvm::errs(), new clang::DiagnosticOptions());
89-
instance.createDiagnostics(diagnosticPrinter, false);
90-
instance.setOutStream(&llvm::outs());
88+
try {
89+
// Setup a compiler instance with diagnostics.
90+
clang::CompilerInstance instance;
91+
auto *diagnosticPrinter = new clang::TextDiagnosticPrinter(
92+
llvm::errs(), new clang::DiagnosticOptions());
93+
instance.createDiagnostics(diagnosticPrinter, false);
94+
instance.setOutStream(&llvm::outs());
9195

92-
// TODO: Allow configuration of targetEnv via options.
93-
instance.getCodeGenOpts().SpirvOptions.targetEnv = "vulkan1.0";
96+
// TODO: Allow configuration of targetEnv via options.
97+
instance.getCodeGenOpts().SpirvOptions.targetEnv = "vulkan1.0";
9498

95-
// Set input and ouptut filenames.
96-
instance.getCodeGenOpts().MainFileName = optInputFilename;
97-
instance.getFrontendOpts().OutputFile = optOutputFilename;
99+
// Set input and ouptut filenames.
100+
instance.getCodeGenOpts().MainFileName = optInputFilename;
101+
instance.getFrontendOpts().OutputFile = optOutputFilename;
98102

99-
// Run translator.
100-
clang::dxil2spv::Translator translator(instance);
101-
translator.Run();
103+
// Run translator.
104+
clang::dxil2spv::Translator translator(instance);
105+
translator.Run();
102106

103-
return instance.getDiagnosticClient().getNumErrors() > 0 ? EXIT_FAILURE
104-
: EXIT_SUCCESS;
107+
if (instance.getDiagnosticClient().getNumErrors() > 0)
108+
return EXIT_FAILURE;
109+
} catch (const hlsl::Exception& ex) {
110+
llvm::errs() << "Exception: " << ex.msg << "(HRESULT: " << ex.hr << ")\n";
111+
return EXIT_FAILURE;
112+
} catch (const std::exception &ex) {
113+
llvm::errs() << "Exception: " << ex.what() << "\n";
114+
return EXIT_FAILURE;
115+
} catch (...) {
116+
llvm::errs() << "Unknown exception\n";
117+
return EXIT_FAILURE;
118+
}
119+
120+
return EXIT_SUCCESS;
105121
}

tools/clang/tools/dxil2spv/lib/dxil2spv.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ void Translator::Run() {
7171
// Get DXIL program from container.
7272
const hlsl::DxilPartHeader *partHeader =
7373
hlsl::GetDxilPartByType(blobHeader, hlsl::DxilFourCC::DFCC_DXIL);
74-
IFTBOOL(partHeader != nullptr, DXC_E_MISSING_PART);
74+
if (partHeader == nullptr) {
75+
emitError("Could not process input. Missing DXContainer part header.");
76+
return;
77+
}
78+
7579
const hlsl::DxilProgramHeader *programHeader =
7680
reinterpret_cast<const hlsl::DxilProgramHeader *>(
7781
GetDxilPartData(partHeader));

0 commit comments

Comments
 (0)