diff options
author | Raul Tambre <raul@tambre.ee> | 2020-03-12 11:31:47 (GMT) |
---|---|---|
committer | Raul Tambre <raul@tambre.ee> | 2020-04-15 14:55:41 (GMT) |
commit | e98588aabad578a9b33aae05e77f6ec5b3ff2e46 (patch) | |
tree | 34c2ee6ab9b11721b5287a6a9a2b01afb04a5943 /Source | |
parent | 71a06093795efc55c4ef3575b5c4177d38894870 (diff) | |
download | CMake-e98588aabad578a9b33aae05e77f6ec5b3ff2e46.zip CMake-e98588aabad578a9b33aae05e77f6ec5b3ff2e46.tar.gz CMake-e98588aabad578a9b33aae05e77f6ec5b3ff2e46.tar.bz2 |
CUDA: Add CUDA_ARCHITECTURES target property
Simplifies CUDA target architecture handling.
Required for Clang support as Clang doesn't automatically select a supported architecture.
We detect a supported architecture during compiler identification and set CMAKE_CUDA_ARCHITECTURES to it.
Introduces CMP0104 for backwards compatibility with manually setting code generation flags with NVCC.
Implements #17963.
Diffstat (limited to 'Source')
-rw-r--r-- | Source/cmCoreTryCompile.cxx | 3 | ||||
-rw-r--r-- | Source/cmGeneratorTarget.cxx | 89 | ||||
-rw-r--r-- | Source/cmGeneratorTarget.h | 2 | ||||
-rw-r--r-- | Source/cmLocalGenerator.cxx | 2 | ||||
-rw-r--r-- | Source/cmPolicies.h | 7 | ||||
-rw-r--r-- | Source/cmTarget.cxx | 1 |
6 files changed, 103 insertions, 1 deletions
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 9175dac..dc2df14 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -40,6 +40,8 @@ static std::string const kCMAKE_CXX_LINK_NO_PIE_SUPPORTED = "CMAKE_CXX_LINK_NO_PIE_SUPPORTED"; static std::string const kCMAKE_CXX_LINK_PIE_SUPPORTED = "CMAKE_CXX_LINK_PIE_SUPPORTED"; +static std::string const kCMAKE_CUDA_ARCHITECTURES = + "CMAKE_CUDA_ARCHITECTURES"; static std::string const kCMAKE_CUDA_COMPILER_TARGET = "CMAKE_CUDA_COMPILER_TARGET"; static std::string const kCMAKE_ENABLE_EXPORTS = "CMAKE_ENABLE_EXPORTS"; @@ -713,6 +715,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv, vars.insert(kCMAKE_C_COMPILER_TARGET); vars.insert(kCMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN); vars.insert(kCMAKE_CXX_COMPILER_TARGET); + vars.insert(kCMAKE_CUDA_ARCHITECTURES); vars.insert(kCMAKE_CUDA_COMPILER_TARGET); vars.insert(kCMAKE_ENABLE_EXPORTS); vars.insert(kCMAKE_LINK_SEARCH_END_STATIC); diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 93eb8fb..8ec4825 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -3085,6 +3085,95 @@ void cmGeneratorTarget::GetAppleArchs(const std::string& config, } } +void cmGeneratorTarget::AddCUDAArchitectureFlags(std::string& flags) const +{ + struct CudaArchitecture + { + std::string name; + bool real{ true }; + bool virtual_{ true }; + }; + std::vector<CudaArchitecture> architectures; + + { + std::vector<std::string> options; + cmExpandList(this->GetSafeProperty("CUDA_ARCHITECTURES"), options); + + if (options.empty()) { + switch (this->GetPolicyStatusCMP0104()) { + case cmPolicies::WARN: + if (!this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile()) { + this->Makefile->IssueMessage( + MessageType::AUTHOR_WARNING, + cmPolicies::GetPolicyWarning(cmPolicies::CMP0104) + + "\nCUDA_ARCHITECTURES is empty for target \"" + + this->GetName() + "\"."); + } + CM_FALLTHROUGH; + case cmPolicies::OLD: + break; + default: + this->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + "CUDA_ARCHITECTURES is empty for target \"" + this->GetName() + + "\"."); + } + } + + for (std::string& option : options) { + CudaArchitecture architecture; + + // Architecture name is up to the first specifier. + std::size_t pos = option.find_first_of('-'); + architecture.name = option.substr(0, pos); + + if (pos != std::string::npos) { + cm::string_view specifier{ option.c_str() + pos + 1, + option.length() - pos - 1 }; + + if (specifier == "real") { + architecture.real = true; + architecture.virtual_ = false; + } else if (specifier == "virtual") { + architecture.real = false; + architecture.virtual_ = true; + } else { + this->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + "Uknown CUDA architecture specifier \"" + std::string(specifier) + + "\"."); + } + } + + architectures.emplace_back(architecture); + } + } + + std::string const& compiler = + this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID"); + + if (compiler == "NVIDIA") { + for (CudaArchitecture& architecture : architectures) { + flags += + " --generate-code=arch=compute_" + architecture.name + ",code=["; + + if (architecture.virtual_) { + flags += "compute_" + architecture.name; + + if (architecture.real) { + flags += ","; + } + } + + if (architecture.real) { + flags += "sm_" + architecture.name; + } + + flags += "]"; + } + } +} + //---------------------------------------------------------------------------- std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable( std::string const& var, std::string const& lang, diff --git a/Source/cmGeneratorTarget.h b/Source/cmGeneratorTarget.h index 9136928..7cd253d 100644 --- a/Source/cmGeneratorTarget.h +++ b/Source/cmGeneratorTarget.h @@ -421,6 +421,8 @@ public: void GetAppleArchs(const std::string& config, std::vector<std::string>& archVec) const; + void AddCUDAArchitectureFlags(std::string& flags) const; + std::string GetFeatureSpecificLinkRuleVariable( std::string const& var, std::string const& lang, std::string const& config) const; diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index e6083d3..83f8eda 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -1944,6 +1944,8 @@ void cmLocalGenerator::AddLanguageFlags(std::string& flags, this->AppendFlags(flags, "-swift-version " + std::string(v)); } } + } else if (lang == "CUDA") { + target->AddCUDAArchitectureFlags(flags); } // Add MSVC runtime library flags. This is activated by the presence diff --git a/Source/cmPolicies.h b/Source/cmPolicies.h index d3daad8..0eb42bf 100644 --- a/Source/cmPolicies.h +++ b/Source/cmPolicies.h @@ -308,6 +308,10 @@ class cmMakefile; 3, 17, 0, cmPolicies::WARN) \ SELECT(POLICY, CMP0103, \ "multiple export() with same FILE without APPEND is not allowed.", \ + 3, 18, 0, cmPolicies::WARN) \ + SELECT(POLICY, CMP0104, \ + "CMAKE_CUDA_ARCHITECTURES now detected for NVCC, empty " \ + "CUDA_ARCHITECTURES not allowed.", \ 3, 18, 0, cmPolicies::WARN) #define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1) @@ -338,7 +342,8 @@ class cmMakefile; F(CMP0081) \ F(CMP0083) \ F(CMP0095) \ - F(CMP0099) + F(CMP0099) \ + F(CMP0104) /** \class cmPolicies * \brief Handles changes in CMake behavior and policies diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 063576c..9010c3b 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -360,6 +360,7 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type, initProp("CUDA_SEPARABLE_COMPILATION"); initProp("CUDA_RESOLVE_DEVICE_SYMBOLS"); initProp("CUDA_RUNTIME_LIBRARY"); + initProp("CUDA_ARCHITECTURES"); initProp("LINK_SEARCH_START_STATIC"); initProp("LINK_SEARCH_END_STATIC"); initProp("Swift_LANGUAGE_VERSION"); |