From 6e6a876a6b5ad3d2ef402757d2e20641f5a2b49b Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Thu, 12 Dec 2019 11:39:19 -0500 Subject: Slang compiles CUDA source via NVRTC (#1151) * CPPCompiler -> DownstreamCompiler * Added DownstreamCompileResult to start abstraction such that we don't need files. * * Split out slang-blob.cpp * Made CompileResult hold a DownstreamCompileResult - for access to binary or ISlangSharedLibrary * Keep temporary files in scope. * Add a hash to the hex dump stream. * Move all file tracking into DownstreamCompiler. * WIP support for nvrtc. * WIP: Adding support for nvrtc compiler. Adding enum types, wiring up the nvrtc into slang. * Fix remaining CPPCompiler references. * Fix order issue on target string matching. * Use ISlangSharedLibrary for nvrtc. * Use DownstreamCompiler for nvrtc. * WIP first pass at compilation win nvrtc. * Added testing if file is on file system into CommandLineDownstreamCompiler. Added sourceContentsPath. * Make test cuda-compile.cu work by just compiling not comparing output. * Fix warning on clang. --- source/core/slang-downstream-compiler.cpp | 74 ++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 7 deletions(-) (limited to 'source/core/slang-downstream-compiler.cpp') diff --git a/source/core/slang-downstream-compiler.cpp b/source/core/slang-downstream-compiler.cpp index 52ec2fcd7..9532cf17f 100644 --- a/source/core/slang-downstream-compiler.cpp +++ b/source/core/slang-downstream-compiler.cpp @@ -16,6 +16,7 @@ #include "slang-visual-studio-compiler-util.h" #include "slang-gcc-compiler-util.h" +#include "slang-nvrtc-compiler.h" namespace Slang { @@ -57,6 +58,7 @@ void DownstreamCompiler::Desc::appendAsText(StringBuilder& out) const case CompilerType::Clang: return UnownedStringSlice::fromLiteral("Clang"); case CompilerType::SNC: return UnownedStringSlice::fromLiteral("SNC"); case CompilerType::GHS: return UnownedStringSlice::fromLiteral("GHS"); + case CompilerType::NVRTC: return UnownedStringSlice::fromLiteral("NVRTC"); } } @@ -212,6 +214,38 @@ SlangResult CommandLineDownstreamCompileResult::getBinary(ComPtr& ou /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! CommandLineDownstreamCompiler !!!!!!!!!!!!!!!!!!!!!!*/ +static bool _isContentsInFile(const DownstreamCompiler::CompileOptions& options) +{ + if (options.sourceContentsPath.getLength() <= 0) + { + return false; + } + + // We can see if we can load it + if (File::exists(options.sourceContentsPath)) + { + // Here we look for the file on the regular file system (as opposed to using the + // ISlangFileSystem. This is unfortunate but necessary - because when we call out + // to the compiler all it is able to (currently) see are files on the file system. + // + // Note that it could be coincidence that the filesystem has a file that's identical in + // contents/name. That being the case though, any includes wouldn't work for a generated + // file either from some specialized ISlangFileSystem, so this is probably as good as it gets + // until we can integrate directly to a C/C++ compiler through say a shared library where we can control + // file system access. + try + { + String readContents = File::readAllText(options.sourceContentsPath); + // We should see if they are the same + return options.sourceContents == readContents.getUnownedSlice(); + } + catch (const Slang::IOException&) + { + } + } + return false; +} + SlangResult CommandLineDownstreamCompiler::compile(const CompileOptions& inOptions, RefPtr& out) { // Copy the command line options @@ -232,8 +266,12 @@ SlangResult CommandLineDownstreamCompiler::compile(const CompileOptions& inOptio SLANG_RETURN_ON_FAIL(File::generateTemporary(UnownedStringSlice::fromLiteral("slang-generated"), modulePath)); options.modulePath = modulePath; } - - if (options.sourceContents.getLength() != 0) + + if (_isContentsInFile(options)) + { + options.sourceFiles.add(options.sourceContentsPath); + } + else { String compileSourcePath = modulePath; @@ -264,10 +302,11 @@ SlangResult CommandLineDownstreamCompiler::compile(const CompileOptions& inOptio // Add it as a source file options.sourceFiles.add(compileSourcePath); - - // There is no source contents - options.sourceContents = String(); } + + // There is no source contents + options.sourceContents = String(); + options.sourceContentsPath = String(); } // Append command line args to the end of cmdLine using the target specific function for the specified options @@ -488,8 +527,29 @@ static void _addGCCFamilyCompiler(const String& path, const String& inExeName, D _addGCCFamilyCompiler(desc.getPath(CompilerType::Clang), "clang", set); _addGCCFamilyCompiler(desc.getPath(CompilerType::GCC), "g++", set); - // Set the default to the compiler closest to how this source was compiled - set->setDefaultCompiler(findClosestCompiler(set, getCompiledWithDesc())); + { + DownstreamCompiler* cppCompiler = findClosestCompiler(set, getCompiledWithDesc()); + + // Set the default to the compiler closest to how this source was compiled + set->setDefaultCompiler(DownstreamCompiler::SourceType::CPP, cppCompiler); + set->setDefaultCompiler(DownstreamCompiler::SourceType::C, cppCompiler); + } + + // Lets see if we have NVRTC. + { + ISlangSharedLibrary* sharedLibrary = desc.sharedLibraries[int(CompilerType::NVRTC)]; + if (sharedLibrary) + { + RefPtr compiler; + if (SLANG_SUCCEEDED(NVRTCDownstreamCompilerUtil::createCompiler(sharedLibrary, compiler))) + { + set->addCompiler(compiler); + + set->setDefaultCompiler(DownstreamCompiler::SourceType::CUDA, compiler); + } + } + } + return SLANG_OK; } -- cgit v1.2.3