From 2a869c105dcc23ede8f5e6e16b08261f45aa5aad Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 1 Sep 2022 17:41:26 -0400 Subject: Upgrade slang-llvm (#2390) * #include an absolute path didn't work - because paths were taken to always be relative. * Use IDownstreamCompiler inteface directly with updated slang-llvm. * Small tidy around includes in slang-llvm-compiler.cpp --- source/compiler-core/slang-downstream-dep1.cpp | 313 ------------------------- source/compiler-core/slang-downstream-dep1.h | 200 ---------------- source/compiler-core/slang-llvm-compiler.cpp | 28 +-- 3 files changed, 4 insertions(+), 537 deletions(-) delete mode 100644 source/compiler-core/slang-downstream-dep1.cpp delete mode 100644 source/compiler-core/slang-downstream-dep1.h (limited to 'source') diff --git a/source/compiler-core/slang-downstream-dep1.cpp b/source/compiler-core/slang-downstream-dep1.cpp deleted file mode 100644 index c62fc77fc..000000000 --- a/source/compiler-core/slang-downstream-dep1.cpp +++ /dev/null @@ -1,313 +0,0 @@ -// slang-downstream-dep1.cpp -#include "slang-downstream-dep1.h" - -#include "slang-artifact-util.h" -#include "slang-artifact-associated-impl.h" -#include "slang-artifact-desc-util.h" - -#include "../core/slang-castable-util.h" -#include "../core/slang-string-util.h" - -#include "slang-slice-allocator.h" - -namespace Slang -{ - -/* !!!!!!!!!!!!!!!!!!!!!!!!! DownstreamArtifactRepresentation_Dep1 !!!!!!!!!!!!!!!!!!!!!!!! */ - -class DownstreamResultArtifactRepresentationAdapater_Dep1 : public ComBaseObject, public IArtifactRepresentation -{ -public: - SLANG_COM_BASE_IUNKNOWN_ALL - - // ICastable - virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE; - - // IArtifactRepresentation - virtual SLANG_NO_THROW SlangResult SLANG_MCALL createRepresentation(const Guid& typeGuid, ICastable** outCastable) SLANG_OVERRIDE; - virtual SLANG_NO_THROW bool SLANG_MCALL exists() SLANG_OVERRIDE { return true; } - - DownstreamResultArtifactRepresentationAdapater_Dep1(DownstreamCompileResult_Dep1* result): - m_result(result) - { - } - - void* getInterface(const Guid& guid); - void* getObject(const Guid& guid); - - RefPtr m_result; -}; - -void* DownstreamResultArtifactRepresentationAdapater_Dep1::castAs(const SlangUUID& guid) -{ - if (auto ptr = getInterface(guid)) - { - return ptr; - } - return getObject(guid); -} - -void* DownstreamResultArtifactRepresentationAdapater_Dep1::getInterface(const Guid& guid) -{ - if (guid == ISlangUnknown::getTypeGuid() || - guid == ICastable::getTypeGuid() || - guid == IArtifactRepresentation::getTypeGuid()) - { - IArtifactRepresentation* rep = this; - return rep; - } - return nullptr; -} - -void* DownstreamResultArtifactRepresentationAdapater_Dep1::getObject(const Guid& guid) -{ - SLANG_UNUSED(guid); - return nullptr; -} - -SlangResult DownstreamResultArtifactRepresentationAdapater_Dep1::createRepresentation(const Guid& typeGuid, ICastable** outCastable) -{ - if (typeGuid == ISlangSharedLibrary::getTypeGuid()) - { - ComPtr lib; - SLANG_RETURN_ON_FAIL(DownstreamUtil_Dep1::getDownstreamSharedLibrary(m_result, lib)); - - *outCastable = lib.detach(); - return SLANG_OK; - } - else if (typeGuid == ISlangBlob::getTypeGuid()) - { - ComPtr blob; - SLANG_RETURN_ON_FAIL(m_result->getBinary(blob)); - - *outCastable = CastableUtil::getCastable(blob).detach(); - return SLANG_OK; - } - - return SLANG_E_NOT_AVAILABLE; -} - -/* !!!!!!!!!!!!!!!!!!!!!!!!! DownstreamCompilerAdapter_Dep1 !!!!!!!!!!!!!!!!!!!!!!!! */ - -DownstreamCompilerAdapter_Dep1::DownstreamCompilerAdapter_Dep1(DownstreamCompiler_Dep1* dep, ArtifactPayload disassemblyPayload) : - m_dep(dep), - m_disassemblyPayload(disassemblyPayload) -{ - auto desc = dep->getDesc(); - m_desc = DownstreamCompilerDesc(desc.type, desc.majorVersion, desc.minorVersion); -} -SlangResult DownstreamCompilerAdapter_Dep1::compile(const CompileOptions& inOptions, IArtifact** outArtifact) -{ - // Currently this only for llvm, so we'll just ignore other scenarios - if (inOptions.sourceArtifacts.count != 1) - { - return SLANG_FAIL; - } - - IArtifact* sourceArtifact = inOptions.sourceArtifacts[0]; - - typedef DownstreamCompileOptions_Dep1::SomeEnum SomeEnum; - - // Convert to the Deps1 compile options - - DownstreamCompileOptions_Dep1 options; - - options.optimizationLevel = SomeEnum(inOptions.optimizationLevel); - options.debugInfoType = SomeEnum(inOptions.debugInfoType); - options.targetType = inOptions.targetType; - options.sourceLanguage = inOptions.sourceLanguage; - options.floatingPointMode = SomeEnum(inOptions.floatingPointMode); - options.pipelineType = SomeEnum(inOptions.pipelineType); - options.matrixLayout = inOptions.matrixLayout; - - options.flags = inOptions.flags; - options.platform = SomeEnum(inOptions.platform); - - options.modulePath = asString(inOptions.modulePath); - - for (auto& src : inOptions.defines) - { - DownstreamCompileOptions_Dep1::Define dst; - - dst.nameWithSig = asStringSlice(src.nameWithSig); - dst.value = asStringSlice(src.value); - - options.defines.add(dst); - } - - ComPtr blob; - SLANG_RETURN_ON_FAIL(sourceArtifact->loadBlob(ArtifactKeep::Yes, blob.writeRef())); - - options.sourceContents = StringUtil::getString(blob); - options.sourceContentsPath = ArtifactUtil::findPath(sourceArtifact); - - options.includePaths = SliceUtil::toList(inOptions.includePaths); - options.libraryPaths = SliceUtil::toList(inOptions.libraryPaths); - - options.libraries = SliceUtil::toComPtrList(inOptions.libraries); - - for (auto& src : inOptions.requiredCapabilityVersions) - { - DownstreamCompileOptions_Dep1::CapabilityVersion capVer; - capVer.kind = SomeEnum(src.kind); - - auto& srcVer = src.version; - - capVer.version.m_major = srcVer.m_major; - capVer.version.m_minor = srcVer.m_minor; - capVer.version.m_patch = uint16_t(srcVer.m_patch); - - options.requiredCapabilityVersions.add(capVer); - } - - options.entryPointName = asStringSlice(inOptions.entryPointName); - options.profileName = asStringSlice(inOptions.profileName); - - options.stage = inOptions.stage; - - options.compilerSpecificArguments = SliceUtil::toList(inOptions.compilerSpecificArguments); - - options.fileSystemExt = inOptions.fileSystemExt; - options.sourceManager = inOptions.sourceManager; - - RefPtr result; - SLANG_RETURN_ON_FAIL(m_dep->compile(options, result)); - - ComPtr artifact = ArtifactUtil::createArtifactForCompileTarget(options.targetType); - - // Convert the diagnostics - - auto dstDiagnostics = ArtifactDiagnostics::create(); - const DownstreamDiagnostics_Dep1* srcDiagnostics = &result->getDiagnostics(); - - dstDiagnostics->setResult(srcDiagnostics->result); - dstDiagnostics->setRaw(SliceUtil::asCharSlice(srcDiagnostics->rawDiagnostics)); - - for (const auto& srcDiagnostic : srcDiagnostics->diagnostics) - { - IArtifactDiagnostics::Diagnostic dstDiagnostic; - - dstDiagnostic.severity = ArtifactDiagnostic::Severity(srcDiagnostic.severity); - dstDiagnostic.stage = ArtifactDiagnostic::Stage(srcDiagnostic.stage); - - dstDiagnostic.code = SliceUtil::asTerminatedCharSlice(srcDiagnostic.code); - dstDiagnostic.text = SliceUtil::asTerminatedCharSlice(srcDiagnostic.text); - dstDiagnostic.filePath = SliceUtil::asTerminatedCharSlice(srcDiagnostic.filePath); - - dstDiagnostic.location.line = srcDiagnostic.fileLine; - - // Add the diagnostic - dstDiagnostics->add(dstDiagnostic); - } - - artifact->addAssociated(dstDiagnostics); - - // We need to add a representation that can produce shared libraries/blobs on demand - - auto rep = new DownstreamResultArtifactRepresentationAdapater_Dep1(result); - artifact->addRepresentation(rep); - - *outArtifact = artifact.detach(); - return SLANG_OK; -} - -bool DownstreamCompilerAdapter_Dep1::canConvert(const ArtifactDesc& from, const ArtifactDesc& to) -{ - // Can only disassemble blobs that are DXBC - return ArtifactDescUtil::isDissassembly(from, to) && from.payload == m_disassemblyPayload; -} - -SlangResult DownstreamCompilerAdapter_Dep1::convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) -{ - if (!canConvert(from->getDesc(), to)) - { - return SLANG_FAIL; - } - - ComPtr fromBlob; - SLANG_RETURN_ON_FAIL(from->loadBlob(ArtifactKeep::No, fromBlob.writeRef())); - - const auto compileTarget = ArtifactDescUtil::getCompileTargetFromDesc(from->getDesc()); - - // Do the disassembly - ComPtr dstBlob; - SLANG_RETURN_ON_FAIL(m_dep->disassemble(compileTarget, fromBlob->getBufferPointer(), fromBlob->getBufferSize(), dstBlob.writeRef())); - - auto artifact = ArtifactUtil::createArtifact(to); - - artifact->addRepresentationUnknown(dstBlob); - - *outArtifact = artifact.detach(); - return SLANG_OK; -} - -/* !!!!!!!!!!!!!!!!!!!!!!!!! SharedLibraryDep1Adapter !!!!!!!!!!!!!!!!!!!!!!!! */ - -// A temporary class that adapts `ISlangSharedLibrary_Dep1` to ISlangSharedLibrary -class SharedLibraryAdapter_Dep1 : public ComBaseObject, public ISlangSharedLibrary -{ -public: - SLANG_COM_BASE_IUNKNOWN_ALL - - // ICastable - virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE; - - // ISlangSharedLibrary - virtual SLANG_NO_THROW void* SLANG_MCALL findSymbolAddressByName(char const* name) SLANG_OVERRIDE { return m_contained->findSymbolAddressByName(name); } - - SharedLibraryAdapter_Dep1(ISlangSharedLibrary_Dep1* dep1) : - m_contained(dep1) - { - } - -protected: - void* getInterface(const Guid& guid) - { - if (guid == ISlangUnknown::getTypeGuid() || - guid == ICastable::getTypeGuid() || - guid == ISlangSharedLibrary::getTypeGuid()) - { - return static_cast(this); - } - return nullptr; - } - void* getObject(const Guid& guid) - { - SLANG_UNUSED(guid); - return nullptr; - } - - ComPtr m_contained; -}; - -void* SharedLibraryAdapter_Dep1::castAs(const SlangUUID& guid) -{ - if (auto intf = getInterface(guid)) - { - return intf; - } - return getObject(guid); -} - -/* Hack to take into account downstream compilers shared library interface might need an adapter */ -/* static */SlangResult DownstreamUtil_Dep1::getDownstreamSharedLibrary(DownstreamCompileResult_Dep1* downstreamResult, ComPtr& outSharedLibrary) -{ - ComPtr lib; - SLANG_RETURN_ON_FAIL(downstreamResult->getHostCallableSharedLibrary(lib)); - - if (SLANG_SUCCEEDED(lib->queryInterface(ISlangSharedLibrary::getTypeGuid(), (void**)outSharedLibrary.writeRef()))) - { - return SLANG_OK; - } - - ComPtr libDep1; - if (SLANG_SUCCEEDED(lib->queryInterface(ISlangSharedLibrary_Dep1::getTypeGuid(), (void**)libDep1.writeRef()))) - { - // Okay, we need to adapt for now - outSharedLibrary = new SharedLibraryAdapter_Dep1(libDep1); - return SLANG_OK; - } - return SLANG_E_NOT_FOUND; -} - -} diff --git a/source/compiler-core/slang-downstream-dep1.h b/source/compiler-core/slang-downstream-dep1.h deleted file mode 100644 index 94d3d73b8..000000000 --- a/source/compiler-core/slang-downstream-dep1.h +++ /dev/null @@ -1,200 +0,0 @@ -#ifndef SLANG_DOWNSTREAM_DEP1_H -#define SLANG_DOWNSTREAM_DEP1_H - - -#include "slang-downstream-compiler.h" - -namespace Slang -{ - -// (DEPRECIATED) - - -struct SemanticVersion_Dep1 -{ - uint32_t m_major; - uint16_t m_minor; - uint16_t m_patch; -}; - -struct DownstreamCompileOptions_Dep1 -{ - typedef uint32_t Flags; - enum class SomeEnum { First, B, C }; - - struct Define - { - String nameWithSig; ///< If macro takes parameters include in brackets - String value; - }; - - struct CapabilityVersion - { - SomeEnum kind; - SemanticVersion_Dep1 version; - }; - - SomeEnum optimizationLevel = SomeEnum::First; - SomeEnum debugInfoType = SomeEnum::First; - SlangCompileTarget targetType = SLANG_HOST_EXECUTABLE; - SlangSourceLanguage sourceLanguage = SLANG_SOURCE_LANGUAGE_CPP; - SomeEnum floatingPointMode = SomeEnum::First; - SomeEnum pipelineType = SomeEnum::First; - SlangMatrixLayoutMode matrixLayout = SLANG_MATRIX_LAYOUT_MODE_UNKNOWN; - - Flags flags = 0; - - SomeEnum platform = SomeEnum::First; - - /// The path/name of the output module. Should not have the extension, as that will be added for each of the target types. - /// If not set a module path will be internally generated internally on a command line based compiler - String modulePath; - - List defines; - - /// The contents of the source to compile. This can be empty is sourceFiles is set. - /// If the compiler is a commandLine file this source will be written to a temporary file. - String sourceContents; - /// 'Path' that the contents originated from. NOTE! This is for reporting only and doesn't have to exist on file system - String sourceContentsPath; - - /// The names/paths of source to compile. This can be empty if sourceContents is set. - List sourceFiles; - - List includePaths; - List libraryPaths; - - /// Libraries to link against. - List> libraries; - - List requiredCapabilityVersions; - - /// For compilers/compiles that require an entry point name, else can be empty - String entryPointName; - /// Profile name to use, only required for compiles that need to compile against a a specific profiles. - /// Profile names are tied to compilers and targets. - String profileName; - - /// The stage being compiled for - SlangStage stage = SLANG_STAGE_NONE; - - /// Arguments that are specific to a particular compiler implementation. - List compilerSpecificArguments; - - /// NOTE! Not all downstream compilers can use the fileSystemExt/sourceManager. This option will be ignored in those scenarios. - ISlangFileSystemExt* fileSystemExt = nullptr; - SourceManager* sourceManager = nullptr; -}; - -// Compiler description -struct DownstreamCompilerDesc_Dep1 -{ - SlangPassThrough type; ///< The type of the compiler - - /// TODO(JS): Would probably be better if changed to SemanticVersion, but not trivial to change - // because this type is part of the DownstreamCompiler interface, which is used with `slang-llvm`. - Int majorVersion; ///< Major version (interpretation is type specific) - Int minorVersion; ///< Minor version (interpretation is type specific) -}; - -struct DownstreamDiagnostic_Dep1 -{ - enum class Severity - { - Unknown, - Info, - Warning, - Error, - CountOf, - }; - enum class Stage - { - Compile, - Link, - }; - - Severity severity = Severity::Unknown; ///< The severity of error - Stage stage = Stage::Compile; ///< The stage the error came from - String text; ///< The text of the error - String code; ///< The compiler specific error code - String filePath; ///< The path the error originated from - Int fileLine = 0; ///< The line number the error came from -}; - -struct DownstreamDiagnostics_Dep1 -{ - typedef DownstreamDiagnostic_Dep1 Diagnostic; - - String rawDiagnostics; - - SlangResult result = SLANG_OK; - List diagnostics; -}; - -class DownstreamCompileResult_Dep1 : public RefObject -{ -public: - SLANG_CLASS_GUID(0xdfc5d318, 0x8675, 0x40ef, { 0xbd, 0x7b, 0x4, 0xa4, 0xff, 0x66, 0x11, 0x30 }) - - virtual SlangResult getHostCallableSharedLibrary(ComPtr& outLibrary) { SLANG_UNUSED(outLibrary); return SLANG_FAIL; } - virtual SlangResult getBinary(ComPtr& outBlob) { SLANG_UNUSED(outBlob); return SLANG_FAIL; } - - const DownstreamDiagnostics_Dep1& getDiagnostics() const { return m_diagnostics; } - - /// Ctor - DownstreamCompileResult_Dep1(const DownstreamDiagnostics_Dep1& diagnostics) : - m_diagnostics(diagnostics) - {} - -protected: - DownstreamDiagnostics_Dep1 m_diagnostics; -}; - -class DownstreamCompiler_Dep1: public RefObject -{ -public: - typedef RefObject Super; - - /// Get the desc of this compiler - const DownstreamCompilerDesc_Dep1& getDesc() const { return m_desc; } - /// Compile using the specified options. The result is in resOut - virtual SlangResult compile(const DownstreamCompileOptions_Dep1& options, RefPtr& outResult) = 0; - /// Some compilers have support converting a binary blob into disassembly. Output disassembly is held in the output blob - virtual SlangResult disassemble(SlangCompileTarget sourceBlobTarget, const void* blob, size_t blobSize, ISlangBlob** out); - - /// True if underlying compiler uses file system to communicate source - virtual bool isFileBased() = 0; - -protected: - - DownstreamCompilerDesc_Dep1 m_desc; -}; - -class DownstreamCompilerAdapter_Dep1 : public DownstreamCompilerBase -{ -public: - // IDownstreamCompiler - virtual SLANG_NO_THROW const Desc& SLANG_MCALL getDesc() SLANG_OVERRIDE { return m_desc; } - virtual SLANG_NO_THROW SlangResult SLANG_MCALL compile(const CompileOptions& options, IArtifact** outArtifact) SLANG_OVERRIDE; - virtual SLANG_NO_THROW bool SLANG_MCALL canConvert(const ArtifactDesc& from, const ArtifactDesc& to) SLANG_OVERRIDE; - virtual SLANG_NO_THROW SlangResult SLANG_MCALL convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) SLANG_OVERRIDE; - virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() SLANG_OVERRIDE { return m_dep->isFileBased(); } - - DownstreamCompilerAdapter_Dep1(DownstreamCompiler_Dep1* dep, ArtifactPayload disassemblyPayload); - -protected: - - DownstreamCompilerDesc m_desc; - - ArtifactPayload m_disassemblyPayload; - RefPtr m_dep; -}; - -struct DownstreamUtil_Dep1 -{ - static SlangResult getDownstreamSharedLibrary(DownstreamCompileResult_Dep1* downstreamResult, ComPtr& outSharedLibrary); -}; - -} - -#endif diff --git a/source/compiler-core/slang-llvm-compiler.cpp b/source/compiler-core/slang-llvm-compiler.cpp index 4cb3b7260..382dd0842 100644 --- a/source/compiler-core/slang-llvm-compiler.cpp +++ b/source/compiler-core/slang-llvm-compiler.cpp @@ -1,25 +1,7 @@ // slang-llvm-compiler.cpp #include "slang-llvm-compiler.h" -#include "slang-downstream-dep1.h" - #include "../core/slang-common.h" -#include "../../slang-com-helper.h" - -#include "../core/slang-blob.h" - -#include "../core/slang-string-util.h" -#include "../core/slang-string-slice-pool.h" - -#include "../core/slang-io.h" -#include "../core/slang-shared-library.h" -#include "../core/slang-semantic-version.h" -#include "../core/slang-char-util.h" - -#include "slang-include-system.h" -#include "slang-source-loc.h" - -#include "../core/slang-shared-library.h" namespace Slang { @@ -36,19 +18,17 @@ namespace Slang return SLANG_FAIL; } - typedef SlangResult(*CreateDownstreamCompilerFunc_Dep1)(RefPtr& out); + typedef SlangResult(*CreateDownstreamCompilerFunc)(const Guid& intf, IDownstreamCompiler** outCompiler); - auto fn = (CreateDownstreamCompilerFunc_Dep1)library->findFuncByName("createLLVMDownstreamCompiler"); + auto fn = (CreateDownstreamCompilerFunc)library->findFuncByName("createLLVMDownstreamCompiler_V2"); if (!fn) { return SLANG_FAIL; } - RefPtr downstreamCompilerDep1; - - SLANG_RETURN_ON_FAIL(fn(downstreamCompilerDep1)); + ComPtr downstreamCompiler; - ComPtr downstreamCompiler(new DownstreamCompilerAdapter_Dep1(downstreamCompilerDep1, ArtifactPayload::None)); + SLANG_RETURN_ON_FAIL(fn(IDownstreamCompiler::getTypeGuid(), downstreamCompiler.writeRef())); set->addSharedLibrary(library); set->addCompiler(downstreamCompiler); -- cgit v1.2.3