summaryrefslogtreecommitdiff
path: root/source/compiler-core/slang-artifact-desc-util.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2022-08-16 16:12:45 -0400
committerGitHub <noreply@github.com>2022-08-16 13:12:45 -0700
commit42de00db3ffe07599fff6d47d0d7228181ee3082 (patch)
tree84367b359cd2701212214379d4d604488c4fac91 /source/compiler-core/slang-artifact-desc-util.cpp
parentac71724c03392b429e44641a3641b2bcf7cc55fc (diff)
Move metadata/diagnostics to associated types (#2358)
* #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. * Split out CastableList and related types, and placed in core. * Small fixes around IArtifact. Improve IArtifact docs. First impl of getChildren for IArtifact. * Documentation improvements for Artifact related types. * Fix typo. * Special case adding a ICastableList to a LazyCastableList. * Small simplification of LazyCastableList, by adding State member. * Removed the ILockFile interface because IFileArtifactRepresentation can be used. * Implement DiagnosticsArtifactRepresentation. * Added PostEmitMetadataArtifactRepresentation * Add searching by predicate. Added handling of accessing Artifact as ISharedLibrary * Fix typo. * Add find to IArtifacgtList. Fix some missing SLANG_NO_THROW. * Small improvements around ArtifactDesc types. * Another small change around ArtifactKind. * Some more shuffling of ArtifactDesc. * Make IArtifact castable Remove IArtifactList Made IArtifactContainer derive from IArtifact Made ModuleLibrary atomic ref counted/given IModuleLibrary interface. * Must call _requireChildren before any children access. * Fix missing SLANG_MCALL on castAs. * Fix missing SLANG_OVERRIDE. * Added IArtifactHandler * Use ICastable for basis of scope/lookup. * WIP first attempt to remove CompileResult. * Fix support for for downstream compiler shared library adapter. * Fix issues found when replacing CompileResult. * Fix typo. * Fix getting items form 'significant' member of an Artifact. * Split out ArtifactUtil & ArtifactHandler. * Work around for problem on Visual studio. * Improve searching. * Add missing files. * Split out Artifact associated types. Don't produce a container by default - use associated for 'metadata'. * Remove no longer used ArtifactPayload type. * Generalized converting representations. Small improvements to artifacts. * Fix intermediate dumping issue. * Removed #if 0 out CompileResult. Remove DownstreamCompileResult maybeDumpIntermediate. * Pull out functionality for dumping artifact output into ArtifactOutputUtil Fixed a bug in naming files based on ArtifactDesc. * std::atomic issue. * Fix outputting as text bug. Some small improvements. * Add fix around prefix for dumping. Improved how handling for extensions work form ArtifactDesc. * Dump assembly if available.
Diffstat (limited to 'source/compiler-core/slang-artifact-desc-util.cpp')
-rw-r--r--source/compiler-core/slang-artifact-desc-util.cpp220
1 files changed, 127 insertions, 93 deletions
diff --git a/source/compiler-core/slang-artifact-desc-util.cpp b/source/compiler-core/slang-artifact-desc-util.cpp
index 2c2b177c2..490020a27 100644
--- a/source/compiler-core/slang-artifact-desc-util.cpp
+++ b/source/compiler-core/slang-artifact-desc-util.cpp
@@ -215,7 +215,6 @@ SLANG_HIERARCHICAL_ENUM(ArtifactKind, SLANG_ARTIFACT_KIND, SLANG_ARTIFACT_KIND_E
x(SlangAST, AST) \
x(CompileResults, Base) \
x(MetaData, Base) \
- x(PostEmitMetadata, MetaData) \
x(DebugInfo, MetaData) \
x(Diagnostics, MetaData)
@@ -466,66 +465,28 @@ static const KindExtension g_cpuKindExts[] =
return getDescFromExtension(extension);
}
-/* static*/ UnownedStringSlice ArtifactDescUtil::getCpuExtensionForKind(Kind kind)
+/* static*/ SlangResult ArtifactDescUtil::appendCpuExtensionForKind(Kind kind, StringBuilder& out)
{
for (const auto& kindExt : g_cpuKindExts)
{
if (kind == kindExt.kind)
{
- return kindExt.ext;
+ out << kindExt.ext;
+ return SLANG_OK;
}
}
- return UnownedStringSlice();
+ return SLANG_E_NOT_FOUND;
}
-UnownedStringSlice ArtifactDescUtil::getAssemblyExtensionForPayload(ArtifactPayload payload)
+static UnownedStringSlice _getPayloadExtension(ArtifactPayload payload)
{
+ typedef ArtifactPayload Payload;
switch (payload)
{
- case ArtifactPayload::DXIL: return toSlice("dxil-asm");
- case ArtifactPayload::DXBC: return toSlice("dxbc-asm");
- case ArtifactPayload::SPIRV: return toSlice("spv-asm");
- case ArtifactPayload::PTX: return toSlice("ptx");
-
- // TODO(JS):
- // Not sure what to do for metal - does it have an assembly name?
-
- default: break;
- }
-
- // We'll just use asm for all CPU assembly type
- if (isDerivedFrom(payload, ArtifactPayload::CPULike))
- {
- return toSlice("asm");
- }
-
- if (isDerivedFrom(payload, ArtifactPayload::GeneralIR))
- {
- switch (payload)
- {
- case ArtifactPayload::SlangIR: return toSlice("slang-ir-asm");
- case ArtifactPayload::LLVMIR: return toSlice("llvm-ir-asm");
- break;
- }
- }
-
- return UnownedStringSlice();
-}
+ /* Misc */
+ case Payload::Unknown: return toSlice("unknown");
-UnownedStringSlice ArtifactDescUtil::getDefaultExtension(const ArtifactDesc& desc)
-{
- switch (desc.kind)
- {
- case ArtifactKind::Zip: return toSlice("zip");
- case ArtifactKind::Riff: return toSlice("riff");
- case ArtifactKind::Assembly:
- {
- return getAssemblyExtensionForPayload(desc.payload);
- }
- case ArtifactKind::Source:
- {
- switch (desc.payload)
- {
+ /* Source types */
case Payload::HLSL: return toSlice("hlsl");
case Payload::GLSL: return toSlice("glsl");
@@ -537,24 +498,8 @@ UnownedStringSlice ArtifactDescUtil::getDefaultExtension(const ArtifactDesc& des
case Payload::CUDA: return toSlice("cu");
case Payload::Slang: return toSlice("slang");
- default: break;
- }
- }
- default: break;
- }
-
- if (ArtifactDescUtil::isCpuLikeTarget(desc))
- {
- return getCpuExtensionForKind(desc.kind);
- }
-
- if (isDerivedFrom(desc.kind, ArtifactKind::BinaryLike))
- {
- switch (desc.payload)
- {
- case Payload::None: return UnownedStringSlice();
- case Payload::Unknown: return toSlice("unknown");
+ /* Binary types */
case Payload::DXIL: return toSlice("dxil");
case Payload::DXBC: return toSlice("dxbc");
case Payload::SPIRV: return toSlice("spv");
@@ -563,20 +508,90 @@ UnownedStringSlice ArtifactDescUtil::getDefaultExtension(const ArtifactDesc& des
case Payload::LLVMIR: return toSlice("llvm-ir");
- case Payload::SlangIR:
+ case Payload::SlangIR: return toSlice("slang-ir");
+
+ case Payload::MetalAIR: return toSlice("air");
+
+ default: break;
+ }
+ return UnownedStringSlice();
+}
+
+SlangResult ArtifactDescUtil::appendDefaultExtension(const ArtifactDesc& desc, StringBuilder& out)
+{
+ switch (desc.kind)
+ {
+ case ArtifactKind::Library:
{
- return (desc.kind == ArtifactKind::Library) ? toSlice("slang-module") : toSlice("slang-ir");
+ // Special cases
+ if (desc.payload == Payload::SlangIR)
+ {
+ out << toSlice("slang-module");
+ return SLANG_OK;
+ }
+ else if (desc.payload == Payload::MetalAIR)
+ {
+ // https://developer.apple.com/documentation/metal/shader_libraries/building_a_library_with_metal_s_command-line_tools
+ out << toSlice("metallib");
+ return SLANG_OK;
+ }
+
+ break;
}
- case Payload::MetalAIR:
+ case ArtifactKind::Zip:
{
- // https://developer.apple.com/documentation/metal/shader_libraries/building_a_library_with_metal_s_command-line_tools
- return (desc.kind == ArtifactKind::Library) ? toSlice("metallib") : toSlice("air");
+ out << toSlice("zip");
+ return SLANG_OK;
+ }
+ case ArtifactKind::Riff:
+ {
+ out << toSlice("riff");
+ return SLANG_OK;
+ }
+ case ArtifactKind::Assembly:
+ {
+ // Special case PTX, because it is assembly
+ if (desc.payload == Payload::PTX)
+ {
+ out << _getPayloadExtension(desc.payload);
+ return SLANG_OK;
+ }
+
+ // We'll just use asm for all CPU assembly type
+ if (isDerivedFrom(desc.payload, ArtifactPayload::CPULike))
+ {
+ out << toSlice("asm");
+ return SLANG_OK;
+ }
+
+ // Use the payload extension "-asm"
+ out << _getPayloadExtension(desc.payload);
+ out << toSlice("-asm");
+ return SLANG_OK;
+ }
+ case ArtifactKind::Source:
+ {
+ out << _getPayloadExtension(desc.payload);
+ return SLANG_OK;
}
default: break;
+ }
+
+ if (ArtifactDescUtil::isCpuLikeTarget(desc) && !isDerivedFrom(desc.payload, ArtifactPayload::Source))
+ {
+ return appendCpuExtensionForKind(desc.kind, out);
+ }
+ else
+ {
+ auto slice = _getPayloadExtension(desc.payload);
+ if (slice.getLength())
+ {
+ out << slice;
+ return SLANG_OK;
}
}
- return UnownedStringSlice();
+ return SLANG_E_NOT_FOUND;
}
/* static */String ArtifactDescUtil::getBaseNameFromPath(const ArtifactDesc& desc, const UnownedStringSlice& path)
@@ -602,7 +617,10 @@ UnownedStringSlice ArtifactDescUtil::getDefaultExtension(const ArtifactDesc& des
// Strip any extension
{
- auto descExt = getDefaultExtension(desc);
+ StringBuilder descExt;
+
+ appendDefaultExtension(desc, descExt);
+
// Strip the extension if it's a match
if (descExt.getLength() &&
Path::getPathExt(name) == descExt)
@@ -620,27 +638,17 @@ UnownedStringSlice ArtifactDescUtil::getDefaultExtension(const ArtifactDesc& des
return getBaseNameFromPath(desc, path);
}
-/* static */SlangResult ArtifactDescUtil::calcPathForDesc(const ArtifactDesc& desc, const UnownedStringSlice& basePath, StringBuilder& outPath)
+/* static */SlangResult ArtifactDescUtil::calcNameForDesc(const ArtifactDesc& desc, const UnownedStringSlice& inBaseName, StringBuilder& outName)
{
- outPath.Clear();
-
- UnownedStringSlice baseName;
-
- // Append the directory
- Index pos = Path::findLastSeparatorIndex(basePath);
- if (pos >= 0)
- {
- // Keep the stem including the delimiter
- outPath.append(basePath.head(pos + 1));
- // Get the baseName
- baseName = basePath.tail(pos + 1);
- }
+ UnownedStringSlice baseName(inBaseName);
+ // If there is no basename, set one
if (baseName.getLength() == 0)
{
baseName = toSlice("unknown");
}
+ // Prefix
if (isCpuBinary(desc) &&
(desc.kind == ArtifactKind::SharedLibrary ||
desc.kind == ArtifactKind::Library))
@@ -648,21 +656,47 @@ UnownedStringSlice ArtifactDescUtil::getDefaultExtension(const ArtifactDesc& des
const bool isSharedLibraryPrefixPlatform = SLANG_LINUX_FAMILY || SLANG_APPLE_FAMILY;
if (isSharedLibraryPrefixPlatform)
{
- outPath << "lib";
- outPath << baseName;
+ outName << "lib";
}
}
- // If there is an extension append it
- const UnownedStringSlice ext = getDefaultExtension(desc);
+ // Output the basename
+ outName << baseName;
- if (ext.getLength())
+ // If there is an extension append it
+ StringBuilder ext;
+ if (SLANG_SUCCEEDED(appendDefaultExtension(desc, ext)) && ext.getLength() > 0)
{
- outPath.appendChar('.');
- outPath.append(ext);
+ outName.appendChar('.');
+ outName.append(ext);
}
return SLANG_OK;
}
+/* static */SlangResult ArtifactDescUtil::calcPathForDesc(const ArtifactDesc& desc, const UnownedStringSlice& basePath, StringBuilder& outPath)
+{
+ outPath.Clear();
+
+ // Append the directory
+ Index pos = Path::findLastSeparatorIndex(basePath);
+ if (pos >= 0)
+ {
+ // Keep the stem including the delimiter
+ outPath.append(basePath.head(pos + 1));
+
+ StringBuilder buf;
+ const auto baseName = basePath.tail(pos + 1);
+
+ SLANG_RETURN_ON_FAIL(calcNameForDesc(desc, baseName, buf));
+ outPath.append(buf);
+
+ return SLANG_OK;
+ }
+ else
+ {
+ return calcNameForDesc(desc, basePath, outPath);
+ }
+}
+
} // namespace Slang