diff options
author | Brad King <brad.king@kitware.com> | 2023-03-14 13:34:42 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2023-03-14 13:35:00 (GMT) |
commit | db4f4ad24e9a0ec8e0cb22b6b0204173d06e1cf8 (patch) | |
tree | 371c95db87ee1b9956ad84fd9f889196d027dc60 /Source | |
parent | 9b68949a27e6962423fb75296b06f4d6d1e6cf73 (diff) | |
parent | 2def6a874b52ef70157f101cbca9ee9b92a5a7f5 (diff) | |
download | CMake-db4f4ad24e9a0ec8e0cb22b6b0204173d06e1cf8.zip CMake-db4f4ad24e9a0ec8e0cb22b6b0204173d06e1cf8.tar.gz CMake-db4f4ad24e9a0ec8e0cb22b6b0204173d06e1cf8.tar.bz2 |
Merge topic 'support_cubin_fatbin_optix_cuda_output'
2def6a874b CUDA: Add support for CUBIN, FATBIN, and OPTIXIR compilation
Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: buildbot <buildbot@kitware.com>
Merge-request: !8259
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmGeneratorTarget.cxx | 28 | ||||
-rw-r--r-- | Source/cmMakefileTargetGenerator.cxx | 23 | ||||
-rw-r--r-- | Source/cmNinjaTargetGenerator.cxx | 43 | ||||
-rw-r--r-- | Source/cmTarget.cxx | 43 | ||||
-rw-r--r-- | Source/cmVisualStudio10TargetGenerator.cxx | 25 |
5 files changed, 128 insertions, 34 deletions
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 54cd8ba..4cfa1d7 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -3,6 +3,7 @@ #include "cmGeneratorTarget.h" #include <algorithm> +#include <array> #include <cassert> #include <cerrno> #include <cstddef> @@ -1011,12 +1012,27 @@ const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file) const char* cmGeneratorTarget::GetCustomObjectExtension() const { - static std::string extension; - const bool has_ptx_extension = - this->GetPropertyAsBool("CUDA_PTX_COMPILATION"); - if (has_ptx_extension) { - extension = ".ptx"; - return extension.c_str(); + struct compiler_mode + { + std::string variable; + std::string extension; + }; + static std::array<compiler_mode, 4> const modes{ + { { "CUDA_PTX_COMPILATION", ".ptx" }, + { "CUDA_CUBIN_COMPILATION", ".cubin" }, + { "CUDA_FATBIN_COMPILATION", ".fatbin" }, + { "CUDA_OPTIX_COMPILATION", ".optixir" } } + }; + + std::string const& compiler = + this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID"); + if (!compiler.empty()) { + for (const auto& m : modes) { + const bool has_extension = this->GetPropertyAsBool(m.variable); + if (has_extension) { + return m.extension.c_str(); + } + } } return nullptr; } diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index e217dd9..2ead739 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -3,6 +3,7 @@ #include "cmMakefileTargetGenerator.h" #include <algorithm> +#include <array> #include <cassert> #include <cstdio> #include <iterator> @@ -977,11 +978,23 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles( this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG"); cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, " "); } - if (this->GeneratorTarget->GetPropertyAsBool("CUDA_PTX_COMPILATION")) { - const std::string& ptxFlag = - this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_PTX_FLAG"); - cudaCompileMode = cmStrCat(cudaCompileMode, ptxFlag); - } else { + + static std::array<cm::string_view, 4> const compileModes{ + { "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s } + }; + bool useNormalCompileMode = true; + for (cm::string_view mode : compileModes) { + auto propName = cmStrCat("CUDA_", mode, "_COMPILATION"); + auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG"); + if (this->GeneratorTarget->GetPropertyAsBool(propName)) { + const std::string& flag = + this->Makefile->GetRequiredDefinition(defName); + cudaCompileMode = cmStrCat(cudaCompileMode, flag); + useNormalCompileMode = false; + break; + } + } + if (useNormalCompileMode) { const std::string& wholeFlag = this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG"); cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag); diff --git a/Source/cmNinjaTargetGenerator.cxx b/Source/cmNinjaTargetGenerator.cxx index 8663f46..dc56142 100644 --- a/Source/cmNinjaTargetGenerator.cxx +++ b/Source/cmNinjaTargetGenerator.cxx @@ -3,6 +3,7 @@ #include "cmNinjaTargetGenerator.h" #include <algorithm> +#include <array> #include <cassert> #include <functional> #include <iterator> @@ -859,11 +860,22 @@ void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang, this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG"); cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, " "); } - if (this->GeneratorTarget->GetPropertyAsBool("CUDA_PTX_COMPILATION")) { - const std::string& ptxFlag = - this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_PTX_FLAG"); - cudaCompileMode = cmStrCat(cudaCompileMode, ptxFlag); - } else { + static std::array<cm::string_view, 4> const compileModes{ + { "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s } + }; + bool useNormalCompileMode = true; + for (cm::string_view mode : compileModes) { + auto propName = cmStrCat("CUDA_", mode, "_COMPILATION"); + auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG"); + if (this->GeneratorTarget->GetPropertyAsBool(propName)) { + const std::string& flag = + this->Makefile->GetRequiredDefinition(defName); + cudaCompileMode = cmStrCat(cudaCompileMode, flag); + useNormalCompileMode = false; + break; + } + } + if (useNormalCompileMode) { const std::string& wholeFlag = this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG"); cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag); @@ -1789,11 +1801,22 @@ void cmNinjaTargetGenerator::ExportObjectCompileCommand( this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG"); cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, " "); } - if (this->GeneratorTarget->GetPropertyAsBool("CUDA_PTX_COMPILATION")) { - const std::string& ptxFlag = - this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_PTX_FLAG"); - cudaCompileMode = cmStrCat(cudaCompileMode, ptxFlag); - } else { + static std::array<cm::string_view, 4> const compileModes{ + { "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s } + }; + bool useNormalCompileMode = true; + for (cm::string_view mode : compileModes) { + auto propName = cmStrCat("CUDA_", mode, "_COMPILATION"); + auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG"); + if (this->GeneratorTarget->GetPropertyAsBool(propName)) { + const std::string& flag = + this->Makefile->GetRequiredDefinition(defName); + cudaCompileMode = cmStrCat(cudaCompileMode, flag); + useNormalCompileMode = false; + break; + } + } + if (useNormalCompileMode) { const std::string& wholeFlag = this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG"); cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag); diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index ec87271..f06e26b 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -1775,6 +1775,9 @@ MAKE_PROP(COMPILE_FEATURES); MAKE_PROP(COMPILE_OPTIONS); MAKE_PROP(PRECOMPILE_HEADERS); MAKE_PROP(PRECOMPILE_HEADERS_REUSE_FROM); +MAKE_PROP(CUDA_CUBIN_COMPILATION); +MAKE_PROP(CUDA_FATBIN_COMPILATION); +MAKE_PROP(CUDA_OPTIX_COMPILATION); MAKE_PROP(CUDA_PTX_COMPILATION); MAKE_PROP(EXPORT_NAME); MAKE_PROP(IMPORTED); @@ -1910,14 +1913,38 @@ void cmTarget::StoreProperty(const std::string& prop, ValueType value) value ? value : std::string{})) { // NOLINT(bugprone-branch-clone) /* error was reported by check method */ - } else if (prop == propCUDA_PTX_COMPILATION && - this->GetType() != cmStateEnums::OBJECT_LIBRARY) { - std::ostringstream e; - e << "CUDA_PTX_COMPILATION property can only be applied to OBJECT " - "targets (\"" - << this->impl->Name << "\")\n"; - this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str()); - return; + } else if (prop == propCUDA_CUBIN_COMPILATION || + prop == propCUDA_FATBIN_COMPILATION || + prop == propCUDA_OPTIX_COMPILATION || + prop == propCUDA_PTX_COMPILATION) { + auto const& compiler = + this->impl->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID"); + auto const& compilerVersion = + this->impl->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_VERSION"); + if (this->GetType() != cmStateEnums::OBJECT_LIBRARY) { + auto e = + cmStrCat(prop, " property can only be applied to OBJECT targets(", + this->impl->Name, ")\n"); + this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e); + return; + } + const bool flag_found = + (prop == propCUDA_PTX_COMPILATION && + this->impl->Makefile->GetDefinition("_CMAKE_CUDA_PTX_FLAG")) || + (prop == propCUDA_CUBIN_COMPILATION && + this->impl->Makefile->GetDefinition("_CMAKE_CUDA_CUBIN_FLAG")) || + (prop == propCUDA_FATBIN_COMPILATION && + this->impl->Makefile->GetDefinition("_CMAKE_CUDA_FATBIN_FLAG")) || + (prop == propCUDA_OPTIX_COMPILATION && + this->impl->Makefile->GetDefinition("_CMAKE_CUDA_OPTIX_FLAG")); + if (flag_found) { + this->impl->Properties.SetProperty(prop, value); + } else { + auto e = cmStrCat(prop, " property is not supported by ", compiler, + " compiler version ", compilerVersion, "."); + this->impl->Makefile->IssueMessage(MessageType::FATAL_ERROR, e); + return; + } } else if (prop == propPRECOMPILE_HEADERS_REUSE_FROM) { if (this->GetProperty("PRECOMPILE_HEADERS")) { std::ostringstream e; diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx index ef0fcf3..8926f9e 100644 --- a/Source/cmVisualStudio10TargetGenerator.cxx +++ b/Source/cmVisualStudio10TargetGenerator.cxx @@ -3597,13 +3597,13 @@ bool cmVisualStudio10TargetGenerator::ComputeCudaOptions( if (this->GeneratorTarget->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) { cudaOptions.AddFlag("GenerateRelocatableDeviceCode", "true"); } - bool notPtx = true; + bool notPtxLike = true; if (this->GeneratorTarget->GetPropertyAsBool("CUDA_PTX_COMPILATION")) { cudaOptions.AddFlag("NvccCompilation", "ptx"); // We drop the %(Extension) component as CMake expects all PTX files // to not have the source file extension at all cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).ptx"); - notPtx = false; + notPtxLike = false; if (cmSystemTools::VersionCompare(cmSystemTools::OP_GREATER_EQUAL, cudaVersion, "9.0") && @@ -3618,9 +3618,24 @@ bool cmVisualStudio10TargetGenerator::ComputeCudaOptions( "%(BaseCommandLineTemplate) [CompileOut] [FastMath] " "[Defines] \"%(FullPath)\""); } - } - - if (notPtx && + } else if (this->GeneratorTarget->GetPropertyAsBool( + "CUDA_CUBIN_COMPILATION")) { + cudaOptions.AddFlag("NvccCompilation", "cubin"); + cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).cubin"); + notPtxLike = false; + } else if (this->GeneratorTarget->GetPropertyAsBool( + "CUDA_FATBIN_COMPILATION")) { + cudaOptions.AddFlag("NvccCompilation", "fatbin"); + cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).fatbin"); + notPtxLike = false; + } else if (this->GeneratorTarget->GetPropertyAsBool( + "CUDA_OPTIX_COMPILATION")) { + cudaOptions.AddFlag("NvccCompilation", "optix-ir"); + cudaOptions.AddFlag("CompileOut", "$(IntDir)%(Filename).optixir"); + notPtxLike = false; + } + + if (notPtxLike && cmSystemTools::VersionCompareGreaterEq( "8.0", this->GlobalGenerator->GetPlatformToolsetCudaString())) { // Explicitly state that we want this file to be treated as a |