From 1378fffd9da094beb41b2db89b96f556c23ab6cb Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 10 Aug 2022 10:04:06 -0400 Subject: Artifact and ICastable (#2351) * #include an absolute path didn't work - because paths were taken to always be relative. * WIP with hierarchical enums. * Some small fixes and improvements around artifact desc related types. * Improvements around hierarchical enum. * Fixes to get Artifact types refactor to be able to execute tests. * Attempt to better categorize PTX. * Work around for potentially unused function warning. * Typo fix. * Simplify Artifact header. * Small improvements around Artifact kind/payload/style. * Added IDestroyable/ICastable * Add IArtifactList. * First impl of IArtifactUtil. * Use the ICastable interface for IArtifactRepresentation. * Added IArtifactRepresentation & IArtifactAssociated. * Add SLANG_OVERRIDE to avoid gcc/clang warning. * Fix calling convention issue on win32. * Fix missing SLANG_OVERRIDE. * First attempt at file abstraction around Artifact. * Added creation of lock file. * Move functionality for determining file paths to the IArtifactUtil. Add casting to ICastable. * Added some casting/finding mechanisms. * Simplify IArtifact interface, and use Items for file reps. * Fix problem with libraries on DXIL. * Split out ArtifactRepresentation. * Move ArtifactDesc functionality to ArtifactDescUtil. ArtifactInfoUtil becomes ArtifactDescUtil. * Split implementations from the interfaces for Artifact. * Use TypeTextUtil for target name outputting. * Add artifact impls. * Add ICastableList * Added UnknownCastableAdapter * Make ISlangSharedLibrary derive from ICastable, and remain backwards compatible with slang-llvm. * Refactor Representation on Artifact. * Make our ISlangBlobs also derive from ICastable. Make ISlangBlob atomic ref counted. * Fix typo. --- source/slang/slang-compiler.cpp | 76 +++++++++++++++++++++++++++++--- source/slang/slang-ir-link.cpp | 3 +- source/slang/slang-module-library.cpp | 4 +- source/slang/slang-options.cpp | 2 +- source/slang/slang-repro.cpp | 27 ++++++------ source/slang/slang-workspace-version.cpp | 8 ++-- source/slang/slang.cpp | 11 +++-- 7 files changed, 96 insertions(+), 35 deletions(-) (limited to 'source/slang') diff --git a/source/slang/slang-compiler.cpp b/source/slang/slang-compiler.cpp index 361f92525..17785bdc6 100644 --- a/source/slang/slang-compiler.cpp +++ b/source/slang/slang-compiler.cpp @@ -55,6 +55,52 @@ namespace Slang { + // A temporary class that adapts `ISlangSharedLibrary_Dep1` to ISlangSharedLibrary + + class SharedLibraryDep1Adapter : 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); } + + SharedLibraryDep1Adapter(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* SharedLibraryDep1Adapter::castAs(const SlangUUID& guid) + { + if (auto intf = getInterface(guid)) + { + return intf; + } + return getObject(guid); + } // !!!!!!!!!!!!!!!!!!!!!! free functions for DiagnosicSink !!!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -81,7 +127,24 @@ namespace Slang { if (downstreamResult) { - return downstreamResult->getHostCallableSharedLibrary(outSharedLibrary); + // TODO(JS): Work around for not knowing actual interface this is returning, + // and needing to support deps interface + + 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 SharedLibraryDep1Adapter(libDep1); + return SLANG_OK; + } } return SLANG_FAIL; } @@ -1304,8 +1367,9 @@ namespace Slang // Set up the library artifact ComPtr artifact(new Artifact(ArtifactDesc::make(ArtifactKind::Library, Artifact::Payload::HostCPU), "slang-rt")); + ComPtr fileRep(new FileArtifactRepresentation(IFileArtifactRepresentation::Kind::NameOnly, "slang-rt", nullptr, nullptr)); - artifact->addItem(fileRep); + artifact->addRepresentation(fileRep); options.libraries.add(artifact); } @@ -2122,10 +2186,10 @@ namespace Slang } // Need to turn into a blob - RefPtr blob(new ListBlob); - // Swap the streams contents into the blob - stream.swapContents(blob->m_data); - m_containerBlob = blob; + List blobData; + stream.swapContents(blobData); + + m_containerBlob = ListBlob::moveCreate(blobData); return res; } diff --git a/source/slang/slang-ir-link.cpp b/source/slang/slang-ir-link.cpp index 636a32a4d..6667a6d9d 100644 --- a/source/slang/slang-ir-link.cpp +++ b/source/slang/slang-ir-link.cpp @@ -1411,8 +1411,7 @@ LinkedIR linkIR( }); for (IArtifact* artifact : linkage->m_libModules) { - ModuleLibrary* library = (ModuleLibrary*)artifact->findItemObject(ModuleLibrary::getTypeGuid()); - if (library) + if (auto library = findRepresentation(artifact)) { irModules.addRange(library->m_modules.getBuffer()->readRef(), library->m_modules.getCount()); } diff --git a/source/slang/slang-module-library.cpp b/source/slang/slang-module-library.cpp index b60555766..75450d7b3 100644 --- a/source/slang/slang-module-library.cpp +++ b/source/slang/slang-module-library.cpp @@ -91,7 +91,7 @@ SlangResult loadModuleLibrary(const Byte* inBytes, size_t bytesCount, EndToEndCo SlangResult loadModuleLibrary(ArtifactKeep keep, IArtifact* artifact, EndToEndCompileRequest* req, RefPtr& outLibrary) { - if (auto foundLibrary = (ModuleLibrary*)artifact->findItemObject(ModuleLibrary::getTypeGuid())) + if (auto foundLibrary = findRepresentation(artifact)) { outLibrary = foundLibrary; return SLANG_OK; @@ -107,7 +107,7 @@ SlangResult loadModuleLibrary(ArtifactKeep keep, IArtifact* artifact, EndToEndCo if (canKeep(keep)) { - artifact->addItem(library); + artifact->addRepresentation(library); } outLibrary = library; diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 5b14ca24f..6351ffbd4 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -1506,7 +1506,7 @@ struct OptionsParser return SLANG_FAIL; } } - artifact->addItem(fileRep); + artifact->addRepresentation(fileRep); SLANG_RETURN_ON_FAIL(_addLibraryReference(requestImpl, artifact)); } diff --git a/source/slang/slang-repro.cpp b/source/slang/slang-repro.cpp index ad275ee7b..ad1bc25fc 100644 --- a/source/slang/slang-repro.cpp +++ b/source/slang/slang-repro.cpp @@ -249,15 +249,15 @@ struct StoreContext base[fileState]->contents = offsetContents; } - if (srcPathInfo->m_canonicalPath && base[fileState]->canonicalPath.isNull()) + if (srcPathInfo->m_canonicalPath.getLength() && base[fileState]->canonicalPath.isNull()) { - auto offsetCanonicalPath = fromString(srcPathInfo->m_canonicalPath->getString()); + auto offsetCanonicalPath = fromString(srcPathInfo->m_canonicalPath); base[fileState]->canonicalPath = offsetCanonicalPath; } - if (srcPathInfo->m_uniqueIdentity && base[fileState]->uniqueIdentity.isNull()) + if (srcPathInfo->m_uniqueIdentity.getLength() && base[fileState]->uniqueIdentity.isNull()) { - auto offsetUniqueIdentity = fromString(srcPathInfo->m_uniqueIdentity->getString()); + auto offsetUniqueIdentity = fromString(srcPathInfo->m_uniqueIdentity); base[fileState]->uniqueIdentity = offsetUniqueIdentity; } } @@ -668,20 +668,19 @@ struct LoadContext // If wasn't loaded, and has contents, use that if (!blob && file->contents) { - blob = new StringBlob(m_base->asRaw(file->contents)->getSlice()); + blob = StringBlob::create(m_base->asRaw(file->contents)->getSlice()); } dstInfo = new CacheFileSystem::PathInfo(String()); if (file->uniqueIdentity) { - String uniqueIdentity = m_base->asRaw(file->uniqueIdentity)->getSlice(); - dstInfo->m_uniqueIdentity = new StringBlob(uniqueIdentity); + dstInfo->m_uniqueIdentity = m_base->asRaw(file->uniqueIdentity)->getSlice(); } if (file->canonicalPath) { - dstInfo->m_canonicalPath = new StringBlob(m_base->asRaw(file->canonicalPath)->getSlice()); + dstInfo->m_canonicalPath = m_base->asRaw(file->canonicalPath)->getSlice(); } if (blob) @@ -865,13 +864,13 @@ struct LoadContext for (const auto& pair : context.m_fileToPathInfoMap) { CacheFileSystem::PathInfo* pathInfo = pair.Value; - SLANG_ASSERT(pathInfo->m_uniqueIdentity); - dstUniqueMap.Add(pathInfo->m_uniqueIdentity->getString(), pathInfo); + SLANG_ASSERT(pathInfo->m_uniqueIdentity.getLength()); + dstUniqueMap.Add(pathInfo->m_uniqueIdentity, pathInfo); // Add canonical paths too.. - if (pathInfo->m_canonicalPath) + if (pathInfo->m_canonicalPath.getLength()) { - String canonicalPath = pathInfo->m_canonicalPath->getString(); + String canonicalPath = pathInfo->m_canonicalPath; dstPathMap.AddIfNotExists(canonicalPath, pathInfo); } @@ -1050,8 +1049,8 @@ struct LoadContext for (const auto& pair : context.m_fileToPathInfoMap) { CacheFileSystem::PathInfo* pathInfo = pair.Value; - SLANG_ASSERT(pathInfo->m_uniqueIdentity); - dstUniqueMap.Add(pathInfo->m_uniqueIdentity->getString(), pathInfo); + SLANG_ASSERT(pathInfo->m_uniqueIdentity.getLength()); + dstUniqueMap.Add(pathInfo->m_uniqueIdentity, pathInfo); } } diff --git a/source/slang/slang-workspace-version.cpp b/source/slang/slang-workspace-version.cpp index 2451290f5..ebfe9218a 100644 --- a/source/slang/slang-workspace-version.cpp +++ b/source/slang/slang-workspace-version.cpp @@ -330,8 +330,7 @@ SlangResult Workspace::loadFile(const char* path, ISlangBlob** outBlob) RefPtr doc; if (openedDocuments.TryGetValue(canonnicalPath, doc)) { - RefPtr stringBlob = new StringBlob(doc->getText()); - *outBlob = stringBlob.detach(); + *outBlob = StringBlob::create(doc->getText()).detach(); return SLANG_OK; } return Slang::OSFileSystem::getExtSingleton()->loadFile(path, outBlob); @@ -479,7 +478,8 @@ Module* WorkspaceVersion::getOrLoadModule(String path) if (!doc) return nullptr; ComPtr diagnosticBlob; - RefPtr sourceBlob = new StringBlob((*doc)->getText()); + auto sourceBlob = StringBlob::create((*doc)->getText()); + auto moduleName = getMangledNameFromNameString(path.getUnownedSlice()); linkage->contentAssistInfo.primaryModuleName = linkage->getNamePool()->getName(moduleName); linkage->contentAssistInfo.primaryModulePath = path; @@ -494,7 +494,7 @@ Module* WorkspaceVersion::getOrLoadModule(String path) auto parsedModule = linkage->loadModuleFromSource( moduleName.getBuffer(), path.getBuffer(), - sourceBlob.Ptr(), + sourceBlob, diagnosticBlob.writeRef()); if (parsedModule) { diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index f74a4a5d6..684b0660d 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -4515,8 +4515,7 @@ SlangResult EndToEndCompileRequest::addLibraryReference(const void* libData, siz // Create an artifact without any name (as one is not provided) ComPtr artifact(new Artifact(desc, String())); - - artifact->addItem(library); + artifact->addRepresentation(library); return _addLibraryReference(this, artifact); } @@ -4929,12 +4928,12 @@ SlangResult EndToEndCompileRequest::saveRepro(ISlangBlob** outBlob) SLANG_RETURN_ON_FAIL(ReproUtil::saveState(this, &stream)); - RefPtr listBlob(new ListBlob); - // Put the content of the stream in the blob - stream.swapContents(listBlob->m_data); - *outBlob = listBlob.detach(); + List data; + stream.swapContents(data); + + *outBlob = ListBlob::moveCreate(data).detach(); return SLANG_OK; } -- cgit v1.2.3