summaryrefslogtreecommitdiff
path: root/source/slang/slang-artifact-output-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/slang/slang-artifact-output-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/slang/slang-artifact-output-util.cpp')
-rw-r--r--source/slang/slang-artifact-output-util.cpp237
1 files changed, 237 insertions, 0 deletions
diff --git a/source/slang/slang-artifact-output-util.cpp b/source/slang/slang-artifact-output-util.cpp
new file mode 100644
index 000000000..2694fcb10
--- /dev/null
+++ b/source/slang/slang-artifact-output-util.cpp
@@ -0,0 +1,237 @@
+#include "slang-artifact-output-util.h"
+
+#include "../core/slang-platform.h"
+
+#include "../core/slang-io.h"
+#include "../core/slang-string-util.h"
+#include "../core/slang-hex-dump-util.h"
+
+#include "../core/slang-type-text-util.h"
+
+// Artifact
+#include "../compiler-core/slang-artifact-desc-util.h"
+#include "../compiler-core/slang-artifact-util.h"
+
+#include "slang-compiler.h"
+
+namespace Slang
+{
+
+// Given a desc returns a codegen target if it can be used for disassembly
+// Returns Unknown if cannot be used for generating disassembly
+//
+// NOTE! This returns the code gen target for the input binary, *not* the dissassembly output
+static CodeGenTarget _getDisassemblyCodeGenTarget(const ArtifactDesc& desc)
+{
+ switch (desc.payload)
+ {
+ case ArtifactPayload::DXIL: return CodeGenTarget::DXIL;
+ case ArtifactPayload::DXBC: return CodeGenTarget::DXBytecode;
+ case ArtifactPayload::SPIRV: return CodeGenTarget::SPIRV;
+ default: break;
+ }
+ return CodeGenTarget::Unknown;
+}
+
+/* static */SlangResult ArtifactOutputUtil::dissassembleWithDownstream(Session* session, IArtifact* artifact, DiagnosticSink* sink, IArtifact** outArtifact)
+{
+ auto desc = artifact->getDesc();
+
+ // Get the downstream compiler that can be used for this target
+ // TODO(JS):
+ // This could perhaps be performed in some other manner if there was more than one way to produce
+ // disassembly from a binary.
+
+ const CodeGenTarget target = _getDisassemblyCodeGenTarget(desc);
+ if (target == CodeGenTarget::Unknown)
+ {
+ return SLANG_FAIL;
+ }
+
+ auto downstreamCompiler = getDownstreamCompilerRequiredForTarget(target);
+
+ // Get the required downstream compiler
+ DownstreamCompiler* compiler = session->getOrLoadDownstreamCompiler(downstreamCompiler, sink);
+
+ if (!compiler)
+ {
+ if (sink)
+ {
+ auto compilerName = TypeTextUtil::getPassThroughAsHumanText((SlangPassThrough)downstreamCompiler);
+ sink->diagnose(SourceLoc(), Diagnostics::passThroughCompilerNotFound, compilerName);
+ }
+ return SLANG_FAIL;
+ }
+
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(artifact->loadBlob(ArtifactKeep::No, blob.writeRef()));
+
+ const auto data = blob->getBufferPointer();
+ const auto dataSizeInBytes = blob->getBufferSize();
+
+ ComPtr<ISlangBlob> dissassemblyBlob;
+ SLANG_RETURN_ON_FAIL(compiler->disassemble(SlangCompileTarget(target), data, dataSizeInBytes, dissassemblyBlob.writeRef()));
+
+ ArtifactDesc disassemblyDesc(desc);
+ disassemblyDesc.kind = ArtifactKind::Assembly;
+
+ auto disassemblyArtifact = ArtifactUtil::createArtifact(disassemblyDesc);
+ disassemblyArtifact->addRepresentationUnknown(dissassemblyBlob);
+
+ *outArtifact = disassemblyArtifact.detach();
+ return SLANG_OK;
+}
+
+SlangResult ArtifactOutputUtil::maybeDisassemble(Session* session, IArtifact* artifact, DiagnosticSink* sink, ComPtr<IArtifact>& outArtifact)
+{
+ const auto desc = artifact->getDesc();
+ if (ArtifactDescUtil::isText(artifact->getDesc()))
+ {
+ // Nothing to convert
+ return SLANG_OK;
+ }
+
+ if (_getDisassemblyCodeGenTarget(desc) != CodeGenTarget::Unknown)
+ {
+ // Get the blob
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(artifact->loadBlob(ArtifactKeep::No, blob.writeRef()));
+
+ ComPtr<IArtifact> disassemblyArtifact;
+
+ if (SLANG_SUCCEEDED(dissassembleWithDownstream(session, artifact, sink, disassemblyArtifact.writeRef())))
+ {
+ // Check it is now text
+ SLANG_ASSERT(ArtifactDescUtil::isText(disassemblyArtifact->getDesc()));
+
+ outArtifact.swap(disassemblyArtifact);
+ return SLANG_OK;
+ }
+ }
+
+ return SLANG_OK;
+}
+
+/* static */SlangResult ArtifactOutputUtil::write(const ArtifactDesc& desc, ISlangBlob* blob, ISlangWriter* writer)
+{
+ // If is text, we can just output
+ if (ArtifactDescUtil::isText(desc))
+ {
+ return writer->write((const char*)blob->getBufferPointer(), blob->getBufferSize());
+ }
+ else
+ {
+ if (writer->isConsole())
+ {
+ // Else just dump as text
+ return HexDumpUtil::dumpWithMarkers((const uint8_t*)blob->getBufferPointer(), blob->getBufferSize(), 24, writer);
+ }
+ else
+ {
+ // Redirecting stdout to a file, so do the usual thing
+ writer->setMode(SLANG_WRITER_MODE_BINARY);
+ return writer->write((const char*)blob->getBufferPointer(), blob->getBufferSize());
+ }
+ }
+}
+
+/* static */SlangResult ArtifactOutputUtil::write(IArtifact* artifact, ISlangWriter* writer)
+{
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(artifact->loadBlob(ArtifactKeep::No, blob.writeRef()));
+ return write(artifact->getDesc(), blob, writer);
+}
+
+static SlangResult _requireBlob(IArtifact* artifact, DiagnosticSink* sink, ComPtr<ISlangBlob>& outBlob)
+{
+ const auto res = artifact->loadBlob(ArtifactKeep::No, outBlob.writeRef());
+ if (SLANG_FAILED(res))
+ {
+ sink->diagnose(SourceLoc(), Diagnostics::cannotAccessAsBlob);
+ return res;
+ }
+ return SLANG_OK;
+}
+
+/* static */SlangResult ArtifactOutputUtil::write(IArtifact* artifact, DiagnosticSink* sink, const UnownedStringSlice& writerName, ISlangWriter* writer)
+{
+ if (sink == nullptr)
+ {
+ return write(artifact, writer);
+ }
+
+ // Make sure we can access as a blob
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(_requireBlob(artifact, sink, blob));
+
+ const auto res = write(artifact->getDesc(), blob, writer);
+ if (SLANG_FAILED(res))
+ {
+ sink->diagnose(SourceLoc(), Diagnostics::cannotWriteOutputFile, writerName);
+ }
+ return res;
+}
+
+/* static */SlangResult ArtifactOutputUtil::maybeConvertAndWrite(Session* session, IArtifact* artifact, DiagnosticSink* sink, const UnownedStringSlice& writerName, ISlangWriter* writer)
+{
+ // If the output is console we will try and turn into disassembly
+ if (writer->isConsole())
+ {
+ ComPtr<IArtifact> disassemblyArtifact;
+ maybeDisassemble(session, artifact, sink, disassemblyArtifact);
+
+ if (disassemblyArtifact)
+ {
+ return write(disassemblyArtifact, sink, writerName, writer);
+ }
+ }
+
+ return write(artifact, sink, writerName, writer);
+}
+
+/* static */SlangResult ArtifactOutputUtil::writeToFile(const ArtifactDesc& desc, const void* data, size_t size, const String& path)
+{
+ if (ArtifactDescUtil::isText(desc))
+ {
+ return File::writeNativeText(path, data, size);
+ }
+ else
+ {
+ return File::writeAllBytes(path, data, size);
+ }
+}
+
+/* static */SlangResult ArtifactOutputUtil::writeToFile(const ArtifactDesc& desc, ISlangBlob* blob, const String& path)
+{
+ SLANG_RETURN_ON_FAIL(writeToFile(desc, blob->getBufferPointer(), blob->getBufferSize(), path));
+ return SLANG_OK;
+}
+
+/* static */SlangResult ArtifactOutputUtil::writeToFile(IArtifact* artifact, const String& path)
+{
+ // Get the blob
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(artifact->loadBlob(ArtifactKeep::No, blob.writeRef()));
+ return writeToFile(artifact->getDesc(), blob, path);
+}
+
+/* static */SlangResult ArtifactOutputUtil::writeToFile(IArtifact* artifact, DiagnosticSink* sink, const String& path)
+{
+ if (!sink)
+ {
+ return writeToFile(artifact, path);
+ }
+
+ ComPtr<ISlangBlob> blob;
+ SLANG_RETURN_ON_FAIL(_requireBlob(artifact, sink, blob));
+
+ const auto res = writeToFile(artifact, path);
+ if (SLANG_FAILED(res) && sink)
+ {
+ sink->diagnose(SourceLoc(), Diagnostics::cannotWriteOutputFile, path);
+ }
+
+ return res;
+}
+
+}