|
| 1 | +#include "slang-metal-compiler.h" |
| 2 | +#include "slang-gcc-compiler-util.h" |
| 3 | +#include "slang-artifact-desc-util.h" |
| 4 | +#include "slang-artifact-util.h" |
| 5 | +#include "slang-artifact-representation.h" |
| 6 | + |
| 7 | +namespace Slang |
| 8 | +{ |
| 9 | + |
| 10 | + class MetalDownstreamCompiler : public DownstreamCompilerBase |
| 11 | + { |
| 12 | + public: |
| 13 | + // Because the metal compiler shares the same commandline interface with clang, |
| 14 | + // we will use GccDownstreamCompilerUtil, which implements both gcc and clang, |
| 15 | + // to create the inner compiler and wrap it here. |
| 16 | + // |
| 17 | + ComPtr<IDownstreamCompiler> cppCompiler; |
| 18 | + String executablePath; |
| 19 | + |
| 20 | + MetalDownstreamCompiler(ComPtr<IDownstreamCompiler>& innerCompiler, String path) |
| 21 | + : DownstreamCompilerBase(innerCompiler->getDesc()) |
| 22 | + , cppCompiler(innerCompiler) |
| 23 | + , executablePath(path) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + virtual SLANG_NO_THROW bool SLANG_MCALL isFileBased() override { return true; } |
| 28 | + |
| 29 | + virtual SLANG_NO_THROW SlangResult SLANG_MCALL compile(const CompileOptions& options, IArtifact** outArtifact) |
| 30 | + { |
| 31 | + // All compile requests should be routed directly to the inner compiler. |
| 32 | + return cppCompiler->compile(options, outArtifact); |
| 33 | + } |
| 34 | + |
| 35 | + virtual SLANG_NO_THROW bool SLANG_MCALL canConvert(const ArtifactDesc& from, const ArtifactDesc& to) override |
| 36 | + { |
| 37 | + // Report that we can convert Metal IR to disassembly. |
| 38 | + return ArtifactDescUtil::isDisassembly(from, to) && from.payload == ArtifactPayload::MetalAIR; |
| 39 | + } |
| 40 | + |
| 41 | + virtual SLANG_NO_THROW SlangResult SLANG_MCALL convert(IArtifact* from, const ArtifactDesc& to, IArtifact** outArtifact) override |
| 42 | + { |
| 43 | + // Use metal-objdump to disassemble the Metal IR. |
| 44 | + |
| 45 | + ExecutableLocation exeLocation(executablePath, "metal-objdump"); |
| 46 | + CommandLine cmdLine; |
| 47 | + cmdLine.setExecutableLocation(exeLocation); |
| 48 | + cmdLine.addArg("--disassemble"); |
| 49 | + ComPtr<IOSFileArtifactRepresentation> srcFile; |
| 50 | + SLANG_RETURN_ON_FAIL(from->requireFile(IArtifact::Keep::No, srcFile.writeRef())); |
| 51 | + cmdLine.addArg(String(srcFile->getPath())); |
| 52 | + |
| 53 | + ExecuteResult exeRes; |
| 54 | + SLANG_RETURN_ON_FAIL(ProcessUtil::execute(cmdLine, exeRes)); |
| 55 | + auto artifact = ArtifactUtil::createArtifact(to); |
| 56 | + artifact->addRepresentationUnknown(StringBlob::create(exeRes.standardOutput)); |
| 57 | + *outArtifact = artifact.detach(); |
| 58 | + return SLANG_OK; |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + static SlangResult locateMetalCompiler(const String& path, DownstreamCompilerSet* set) |
| 63 | + { |
| 64 | + ComPtr<IDownstreamCompiler> innerCppCompiler; |
| 65 | + ExecutableLocation exeLocation(path, "metal"); |
| 66 | + SLANG_RETURN_ON_FAIL(GCCDownstreamCompilerUtil::createCompiler(exeLocation, innerCppCompiler)); |
| 67 | + |
| 68 | + ComPtr<IDownstreamCompiler> compiler = ComPtr<IDownstreamCompiler>( |
| 69 | + new MetalDownstreamCompiler(innerCppCompiler, path)); |
| 70 | + set->addCompiler(compiler); |
| 71 | + return SLANG_OK; |
| 72 | + } |
| 73 | + |
| 74 | + SlangResult MetalDownstreamCompilerUtil::locateCompilers(const String& path, ISlangSharedLibraryLoader* loader, DownstreamCompilerSet* set) |
| 75 | + { |
| 76 | + SLANG_UNUSED(loader); |
| 77 | + return locateMetalCompiler(path, set); |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments