summaryrefslogtreecommitdiffstats
path: root/source/compiler-core
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-10-09 14:03:43 -0700
committerGitHub <noreply@github.com>2023-10-09 14:03:43 -0700
commit67e186f0169591c48d24bd8ff7e4e4e715e8fa45 (patch)
tree9b46dc35145d18d5b25d9b3b16759c9b7343615c /source/compiler-core
parent17c7163c2ae8fc290e70b43d8700b68ef18b1ee1 (diff)
Run curated spirv-opt passes through slang-glslang. (#3266)
* Run curated spirv-opt passes through slang-glslang. * Cleanup. * Replace spirv-dis downstream compiler with glslang. * delete slang-spirv-opt.cpp. --------- Co-authored-by: Yong He <yhe@nvidia.com>
Diffstat (limited to 'source/compiler-core')
-rw-r--r--source/compiler-core/slang-downstream-compiler-util.cpp5
-rw-r--r--source/compiler-core/slang-downstream-compiler-util.h1
-rw-r--r--source/compiler-core/slang-glslang-compiler.cpp43
-rw-r--r--source/compiler-core/slang-glslang-compiler.h11
-rw-r--r--source/compiler-core/slang-spirv-dis-compiler.cpp105
-rw-r--r--source/compiler-core/slang-spirv-dis-compiler.h26
6 files changed, 51 insertions, 140 deletions
diff --git a/source/compiler-core/slang-downstream-compiler-util.cpp b/source/compiler-core/slang-downstream-compiler-util.cpp
index 44d750be8..52bf03acc 100644
--- a/source/compiler-core/slang-downstream-compiler-util.cpp
+++ b/source/compiler-core/slang-downstream-compiler-util.cpp
@@ -23,7 +23,6 @@
#include "slang-dxc-compiler.h"
#include "slang-glslang-compiler.h"
#include "slang-llvm-compiler.h"
-#include "slang-spirv-dis-compiler.h"
namespace Slang
{
@@ -55,6 +54,7 @@ struct DownstreamCompilerInfos
infos.infos[int(SLANG_PASS_THROUGH_DXC)] = Info(SourceLanguageFlag::HLSL);
infos.infos[int(SLANG_PASS_THROUGH_FXC)] = Info(SourceLanguageFlag::HLSL);
infos.infos[int(SLANG_PASS_THROUGH_GLSLANG)] = Info(SourceLanguageFlag::GLSL);
+ infos.infos[int(SLANG_PASS_THROUGH_SPIRV_OPT)] = Info(SourceLanguageFlag::SPIRV);
return infos;
}
@@ -327,8 +327,9 @@ DownstreamCompilerMatchVersion DownstreamCompilerUtil::getCompiledVersion()
outFuncs[int(SLANG_PASS_THROUGH_DXC)] = &DXCDownstreamCompilerUtil::locateCompilers;
outFuncs[int(SLANG_PASS_THROUGH_FXC)] = &FXCDownstreamCompilerUtil::locateCompilers;
outFuncs[int(SLANG_PASS_THROUGH_GLSLANG)] = &GlslangDownstreamCompilerUtil::locateCompilers;
+ outFuncs[int(SLANG_PASS_THROUGH_SPIRV_OPT)] = &SpirvOptDownstreamCompilerUtil::locateCompilers;
outFuncs[int(SLANG_PASS_THROUGH_LLVM)] = &LLVMDownstreamCompilerUtil::locateCompilers;
- outFuncs[int(SLANG_PASS_THROUGH_SPIRV_DIS)] = &SPIRVDisDownstreamCompilerUtil::locateCompilers;
+ outFuncs[int(SLANG_PASS_THROUGH_SPIRV_DIS)] = &SpirvDisDownstreamCompilerUtil::locateCompilers;
}
static String _getParentPath(const String& path)
diff --git a/source/compiler-core/slang-downstream-compiler-util.h b/source/compiler-core/slang-downstream-compiler-util.h
index 6aad1ca90..84b8edcca 100644
--- a/source/compiler-core/slang-downstream-compiler-util.h
+++ b/source/compiler-core/slang-downstream-compiler-util.h
@@ -24,6 +24,7 @@ struct DownstreamCompilerInfo
C = SourceLanguageFlags(1) << SLANG_SOURCE_LANGUAGE_C,
CPP = SourceLanguageFlags(1) << SLANG_SOURCE_LANGUAGE_CPP,
CUDA = SourceLanguageFlags(1) << SLANG_SOURCE_LANGUAGE_CUDA,
+ SPIRV = SourceLanguageFlags(1) << SLANG_SOURCE_LANGUAGE_SPIRV,
};
};
diff --git a/source/compiler-core/slang-glslang-compiler.cpp b/source/compiler-core/slang-glslang-compiler.cpp
index 4c0bc74d7..0335c33b4 100644
--- a/source/compiler-core/slang-glslang-compiler.cpp
+++ b/source/compiler-core/slang-glslang-compiler.cpp
@@ -52,7 +52,7 @@ public:
/// Must be called before use
SlangResult init(ISlangSharedLibrary* library);
- GlslangDownstreamCompiler() {}
+ GlslangDownstreamCompiler(SlangPassThrough compilerType) : m_compilerType(compilerType) {}
protected:
@@ -63,6 +63,8 @@ protected:
glslang_CompileFunc_1_2 m_compile_1_2 = nullptr;
ComPtr<ISlangSharedLibrary> m_sharedLibrary;
+
+ SlangPassThrough m_compilerType;
};
SlangResult GlslangDownstreamCompiler::init(ISlangSharedLibrary* library)
@@ -80,7 +82,7 @@ SlangResult GlslangDownstreamCompiler::init(ISlangSharedLibrary* library)
m_sharedLibrary = library;
// It's not clear how to query for a version, but we can get a version number from the header
- m_desc = Desc(SLANG_PASS_THROUGH_GLSLANG);
+ m_desc = Desc(m_compilerType);
Slang::String filename;
if (m_compile_1_2)
@@ -173,9 +175,9 @@ SlangResult GlslangDownstreamCompiler::compile(const CompileOptions& inOptions,
IArtifact* sourceArtifact = options.sourceArtifacts[0];
- if (options.sourceLanguage != SLANG_SOURCE_LANGUAGE_GLSL || options.targetType != SLANG_SPIRV)
+ if (options.targetType != SLANG_SPIRV)
{
- SLANG_ASSERT(!"Can only compile GLSL to SPIR-V");
+ SLANG_ASSERT(!"Can only compile to SPIR-V");
return SLANG_FAIL;
}
@@ -199,7 +201,19 @@ SlangResult GlslangDownstreamCompiler::compile(const CompileOptions& inOptions,
memset(&request, 0, sizeof(request));
request.sizeInBytes = sizeof(request);
- request.action = GLSLANG_ACTION_COMPILE_GLSL_TO_SPIRV;
+ switch (options.sourceLanguage)
+ {
+ case SLANG_SOURCE_LANGUAGE_GLSL:
+ request.action = GLSLANG_ACTION_COMPILE_GLSL_TO_SPIRV;
+ break;
+ case SLANG_SOURCE_LANGUAGE_SPIRV:
+ request.action = GLSLANG_ACTION_OPTIMIZE_SPIRV;
+ break;
+ default:
+ SLANG_ASSERT(!"Can only handle GLSL or SPIR-V as input.");
+ return SLANG_FAIL;
+ }
+
request.sourcePath = sourcePath.getBuffer();
request.slangStage = options.stage;
@@ -340,7 +354,7 @@ SlangResult GlslangDownstreamCompiler::getVersionString(slang::IBlob** outVersio
return SLANG_OK;
}
-/* static */SlangResult GlslangDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
+static SlangResult locateGlslangSpirvDownstreamCompiler(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set, SlangPassThrough compilerType)
{
ComPtr<ISlangSharedLibrary> library;
@@ -367,7 +381,7 @@ SlangResult GlslangDownstreamCompiler::getVersionString(slang::IBlob** outVersio
return SLANG_FAIL;
}
- auto compiler = new GlslangDownstreamCompiler;
+ auto compiler = new GlslangDownstreamCompiler(compilerType);
ComPtr<IDownstreamCompiler> compilerIntf(compiler);
SLANG_RETURN_ON_FAIL(compiler->init(library));
@@ -375,6 +389,21 @@ SlangResult GlslangDownstreamCompiler::getVersionString(slang::IBlob** outVersio
return SLANG_OK;
}
+SlangResult GlslangDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
+{
+ return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_GLSLANG);
+}
+
+SlangResult SpirvOptDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
+{
+ return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_SPIRV_OPT);
+}
+
+SlangResult SpirvDisDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
+{
+ return locateGlslangSpirvDownstreamCompiler(path, loader, set, SLANG_PASS_THROUGH_SPIRV_DIS);
+}
+
#else // SLANG_ENABLE_GLSLANG_SUPPORT
/* static */SlangResult GlslangDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set)
diff --git a/source/compiler-core/slang-glslang-compiler.h b/source/compiler-core/slang-glslang-compiler.h
index ea4352a0b..577bcaacc 100644
--- a/source/compiler-core/slang-glslang-compiler.h
+++ b/source/compiler-core/slang-glslang-compiler.h
@@ -13,6 +13,17 @@ struct GlslangDownstreamCompilerUtil
static SlangResult locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set);
};
+
+struct SpirvOptDownstreamCompilerUtil
+{
+ static SlangResult locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set);
+};
+
+struct SpirvDisDownstreamCompilerUtil
+{
+ static SlangResult locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set);
+};
+
}
#endif
diff --git a/source/compiler-core/slang-spirv-dis-compiler.cpp b/source/compiler-core/slang-spirv-dis-compiler.cpp
deleted file mode 100644
index 9b40254ce..000000000
--- a/source/compiler-core/slang-spirv-dis-compiler.cpp
+++ /dev/null
@@ -1,105 +0,0 @@
-#include "slang-spirv-dis-compiler.h"
-#include "../core/slang-common.h"
-#include "../core/slang-string-util.h"
-#include "../core/slang-string.h"
-#include "slang-artifact-desc-util.h"
-#include "slang-artifact-representation.h"
-#include "slang-artifact-util.h"
-#include "slang-artifact-representation-impl.h"
-namespace Slang
-{
-
-SlangResult SPIRVDisDownstreamCompilerUtil::locateCompilers(
- const String&,
- ISlangSharedLibraryLoader*,
- DownstreamCompilerSet* set)
-{
- // TODO: We could check that the compiler is actually present in PATH (or
- // explicitly given)
- ComPtr<IDownstreamCompiler> com(
- new SPIRVDisDownstreamCompiler(DownstreamCompilerDesc(SLANG_PASS_THROUGH_SPIRV_DIS)));
- set->addCompiler(com);
-
- return SLANG_OK;
-}
-
-SlangResult SLANG_MCALL SPIRVDisDownstreamCompiler::convert(
- IArtifact* from,
- const ArtifactDesc& to,
- IArtifact** outArtifact) noexcept
-{
- const auto& fromDesc = from->getDesc();
- if(to.kind != ArtifactKind::Assembly ||
- to.payload != ArtifactPayload::SPIRV ||
- to.flags != 0 ||
- fromDesc.kind != ArtifactKind::Executable ||
- fromDesc.payload != ArtifactPayload::SPIRV ||
- fromDesc.flags != 0)
- return SLANG_E_INVALID_ARG;
-
- ISlangBlob* fromBlob;
- SLANG_RETURN_ON_FAIL(from->loadBlob(ArtifactKeep::No, &fromBlob));
-
- // Set up our process
- CommandLine commandLine;
- commandLine.m_executableLocation.setName("spirv-dis");
- commandLine.addArg("--comment");
-
- RefPtr<Process> p;
- SLANG_RETURN_ON_FAIL(Process::create(commandLine, 0, p));
-
- auto inputStream = p->getStream(StdStreamType::In);
- if (!inputStream)
- return SLANG_FAIL;
-
- auto outputStream = p->getStream(StdStreamType::Out);
- List<uint8_t> outBytes;
- const auto err = p->getStream(StdStreamType::ErrorOut);
- List<Byte> errData;
-
- SLANG_RETURN_ON_FAIL(StreamUtil::readAndWrite(
- inputStream,
- ArrayView<Byte>((Byte*)fromBlob->getBufferPointer(), (Index)fromBlob->getBufferSize()),
- outputStream,
- outBytes,
- err,
- errData));
-
- // Wait for it to finish
- if(!p->waitForTermination(1000))
- return SLANG_FAIL;
-
- if (errData.getCount())
- fwrite(errData.getBuffer(), 1, (size_t)errData.getCount(), stderr);
-
- StringBuilder sbOutput;
- List<UnownedStringSlice> lines;
- StringUtil::calcLines(StringUtil::trimEndOfLine(UnownedStringSlice((const char*)outBytes.getBuffer())), lines);
- for (auto line : lines)
- {
- sbOutput << StringUtil::trimEndOfLine(line) << "\n";
- }
- // If spirv-dis failed, we fail
- const auto ret = p->getReturnValue();
- if(ret != 0)
- return SLANG_FAIL;
-
- // Return as a file artifact
-
- auto disassemblyBlob = StringBlob::moveCreate(sbOutput);
-
- auto artifact = ArtifactUtil::createArtifact(to);
- artifact->addRepresentationUnknown(disassemblyBlob);
-
- *outArtifact = artifact.detach();
- return SLANG_OK;
-}
-
-SlangResult SLANG_MCALL SPIRVDisDownstreamCompiler::compile(
- const CompileOptions&,
- IArtifact**) noexcept
-{
- SLANG_UNIMPLEMENTED_X(__func__);
-}
-
-}
diff --git a/source/compiler-core/slang-spirv-dis-compiler.h b/source/compiler-core/slang-spirv-dis-compiler.h
deleted file mode 100644
index fb01627cd..000000000
--- a/source/compiler-core/slang-spirv-dis-compiler.h
+++ /dev/null
@@ -1,26 +0,0 @@
-#pragma once
-
-#include "slang-downstream-compiler-util.h"
-
-#include "../core/slang-platform.h"
-
-namespace Slang
-{
-
-struct SPIRVDisDownstreamCompilerUtil
-{
- static SlangResult locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set);
-};
-
-class SPIRVDisDownstreamCompiler : public DownstreamCompilerBase
-{
-public:
- SPIRVDisDownstreamCompiler(const Desc& desc) : DownstreamCompilerBase(desc) {}
-
- virtual SlangResult SLANG_MCALL convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) noexcept override;
-
- virtual bool SLANG_MCALL isFileBased() noexcept override { return true; }
- virtual SlangResult SLANG_MCALL compile(const CompileOptions& options, IArtifact** outArtifact) noexcept override;
-};
-
-}