From ebe8ddefc48478307d5f206cd3e40c41d28a36e3 Mon Sep 17 00:00:00 2001 From: Yong He Date: Tue, 26 Sep 2023 23:56:06 -0700 Subject: Various SPIRV fixes. (#3231) * Various SPIRV fixes. - Geometry shader support (WIP). - Fix texture get dimension and load. - Fold global GetElement(MakeArray/MakeVector) insts. - Call spvopt to inline all functions. - Translate OpImageSubscript. - Emit struct member names and global variable names. - Fix lowering of OpBitNot -> OpNot, instead of OpBitReverse. * Fix test. * Fix geometry shader. * Fix geometry shader emit. * Add atomic Image access test. * Fix tests. * don't fail if spirv-opt fails. * Update comments. * Fix test. * Cleanups. * indentation --------- Co-authored-by: Yong He Co-authored-by: Ellie Hermaszewska --- source/slang/slang-spirv-opt.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 source/slang/slang-spirv-opt.cpp (limited to 'source/slang/slang-spirv-opt.cpp') diff --git a/source/slang/slang-spirv-opt.cpp b/source/slang/slang-spirv-opt.cpp new file mode 100644 index 000000000..34c87db9f --- /dev/null +++ b/source/slang/slang-spirv-opt.cpp @@ -0,0 +1,68 @@ +#include "slang-spirv-opt.h" +#include "slang-spirv-val.h" + +namespace Slang +{ + +struct RemoveFileRAII +{ + String fileName; + + RemoveFileRAII(String inFileName) + :fileName(inFileName) + {} + + ~RemoveFileRAII() + { + File::remove(fileName); + } +}; + +SlangResult optimizeSPIRV(const List& spirv, String& outErr, List& outSpv) +{ + // Set up our process + CommandLine commandLine; + commandLine.m_executableLocation.setName("spirv-opt"); + commandLine.addArg("--merge-return"); + commandLine.addArg("--inline-entry-points-exhaustive"); + commandLine.addArg("--eliminate-dead-functions"); + commandLine.addArg("--eliminate-local-single-block"); + commandLine.addArg("--eliminate-local-single-store"); + commandLine.addArg("--eliminate-dead-code-aggressive"); + + commandLine.addArg("-o"); + String outFileName; + File::generateTemporary(UnownedStringSlice("out_spv"), outFileName); + RemoveFileRAII removeFile(outFileName); + + commandLine.addArg(outFileName); + + RefPtr p; + + // If we failed to even start the process, then spirv-opt isn't available + SLANG_RETURN_ON_FAIL(Process::create(commandLine, 0, p)); + const auto in = p->getStream(StdStreamType::In); + const auto out = p->getStream(StdStreamType::Out); + const auto err = p->getStream(StdStreamType::ErrorOut); + + List outErrData; + SLANG_RETURN_ON_FAIL(StreamUtil::readAndWrite(in, spirv.getArrayView(), out, outSpv, err, outErrData)); + + outSpv.clear(); + File::readAllBytes(outFileName, outSpv); + + SLANG_RETURN_ON_FAIL(p->waitForTermination(3600000)); + + outErr = String( + reinterpret_cast(outErrData.begin()), + reinterpret_cast(outErrData.end()) + ); + + const auto ret = p->getReturnValue(); + if (ret != 0) + return SLANG_FAIL; + + return debugValidateSPIRV(outSpv); +} + +} -- cgit v1.2.3