summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-spirv-val.cpp
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2023-08-08 06:02:53 +0800
committerGitHub <noreply@github.com>2023-08-07 15:02:53 -0700
commit3a9624040d3a3395d855caaa4358a9ff1fbcca3b (patch)
tree97a7f9de5b7c7718714f566a28c147d93cadd086 /source/slang/slang-spirv-val.cpp
parent0e28cd02710e6a8e989e89fd1f9b4ebf85892f99 (diff)
Validate generated SPIR-V on output (#3061)
* Validate generated SPIR-V * regenerate vs projects * Do not fail compiles if spirv-val is not available --------- Co-authored-by: Yong He <yonghe@outlook.com>
Diffstat (limited to 'source/slang/slang-spirv-val.cpp')
-rw-r--r--source/slang/slang-spirv-val.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/source/slang/slang-spirv-val.cpp b/source/slang/slang-spirv-val.cpp
new file mode 100644
index 000000000..990ccd909
--- /dev/null
+++ b/source/slang/slang-spirv-val.cpp
@@ -0,0 +1,40 @@
+#include "slang-spirv-val.h"
+
+namespace Slang
+{
+
+SlangResult debugValidateSPIRV(const List<uint8_t>& spirv)
+{
+ // Set up our process
+ CommandLine commandLine;
+ commandLine.m_executableLocation.setName("spirv-val");
+ RefPtr<Process> p;
+ const auto createResult = Process::create(commandLine, 0, p);
+ // If we failed to even start the process, then validation isn't available
+ if(SLANG_FAILED(createResult))
+ return SLANG_E_NOT_AVAILABLE;
+ const auto in = p->getStream(StdStreamType::In);
+ const auto out = p->getStream(StdStreamType::Out);
+ const auto err = p->getStream(StdStreamType::ErrorOut);
+
+ // Write the assembly
+ SLANG_RETURN_ON_FAIL(in->write(spirv.getBuffer(), spirv.getCount()));
+ in->close();
+
+ // Wait for it to finish
+ if(!p->waitForTermination(1000))
+ return SLANG_FAIL;
+
+ // TODO: allow inheriting stderr in Process
+ List<Byte> outData;
+ SLANG_RETURN_ON_FAIL(StreamUtil::readAll(out, 0, outData));
+ fwrite(outData.getBuffer(), outData.getCount(), 1, stderr);
+ outData.clear();
+ SLANG_RETURN_ON_FAIL(StreamUtil::readAll(err, 0, outData));
+ fwrite(outData.getBuffer(), outData.getCount(), 1, stderr);
+
+ const auto ret = p->getReturnValue();
+ return ret == 0 ? SLANG_OK : SLANG_FAIL;
+}
+
+}