cmLinkLineDeviceComputer.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmLinkLineDeviceComputer.h"
  4. #include <algorithm>
  5. #include <set>
  6. #include <utility>
  7. #include <vector>
  8. #include <cmext/algorithm>
  9. #include "cmComputeLinkInformation.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmLinkItem.h"
  13. #include "cmListFileCache.h"
  14. #include "cmLocalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmStateDirectory.h"
  17. #include "cmStateSnapshot.h"
  18. #include "cmStateTypes.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmValue.h"
  21. class cmOutputConverter;
  22. cmLinkLineDeviceComputer::cmLinkLineDeviceComputer(
  23. cmOutputConverter* outputConverter, cmStateDirectory const& stateDir)
  24. : cmLinkLineComputer(outputConverter, stateDir)
  25. {
  26. }
  27. cmLinkLineDeviceComputer::~cmLinkLineDeviceComputer() = default;
  28. static bool cmLinkItemValidForDevice(std::string const& item)
  29. {
  30. // Valid items are:
  31. // * Non-flags (does not start in '-')
  32. // * Specific flags --library, --library-path, -l, -L
  33. // For example:
  34. // * 'cublas_device' => pass-along
  35. // * '--library pthread' => pass-along
  36. // * '-lpthread' => pass-along
  37. // * '-pthread' => drop
  38. // * '-a' => drop
  39. // * '-framework Name' (as one string) => drop
  40. return (!cmHasPrefix(item, '-') || //
  41. cmHasLiteralPrefix(item, "-l") || //
  42. cmHasLiteralPrefix(item, "-L") || //
  43. cmHasLiteralPrefix(item, "--library"));
  44. }
  45. bool cmLinkLineDeviceComputer::ComputeRequiresDeviceLinking(
  46. cmComputeLinkInformation& cli)
  47. {
  48. // Determine if this item might requires device linking.
  49. // For this we only consider targets
  50. using ItemVector = cmComputeLinkInformation::ItemVector;
  51. ItemVector const& items = cli.GetItems();
  52. return std::any_of(
  53. items.begin(), items.end(),
  54. [](cmComputeLinkInformation::Item const& item) -> bool {
  55. return item.Target &&
  56. item.Target->GetType() == cmStateEnums::STATIC_LIBRARY &&
  57. // this dependency requires us to device link it
  58. !item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS") &&
  59. item.Target->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION");
  60. });
  61. }
  62. bool cmLinkLineDeviceComputer::ComputeRequiresDeviceLinkingIPOFlag(
  63. cmComputeLinkInformation& cli)
  64. {
  65. // Determine if this item might requires device linking.
  66. // For this we only consider targets
  67. using ItemVector = cmComputeLinkInformation::ItemVector;
  68. ItemVector const& items = cli.GetItems();
  69. std::string config = cli.GetConfig();
  70. return std::any_of(
  71. items.begin(), items.end(),
  72. [config](cmComputeLinkInformation::Item const& item) -> bool {
  73. return item.Target &&
  74. item.Target->GetType() == cmStateEnums::STATIC_LIBRARY &&
  75. // this dependency requires us to device link it
  76. !item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS") &&
  77. item.Target->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION") &&
  78. item.Target->IsIPOEnabled("CUDA", config);
  79. });
  80. }
  81. void cmLinkLineDeviceComputer::ComputeLinkLibraries(
  82. cmComputeLinkInformation& cli, std::string const& stdLibString,
  83. std::vector<BT<std::string>>& linkLibraries)
  84. {
  85. // Generate the unique set of link items when device linking.
  86. // The nvcc device linker is designed so that each static library
  87. // with device symbols only needs to be listed once as it doesn't
  88. // care about link order.
  89. std::set<std::string> emitted;
  90. using ItemVector = cmComputeLinkInformation::ItemVector;
  91. ItemVector const& items = cli.GetItems();
  92. std::string config = cli.GetConfig();
  93. bool skipItemAfterFramework = false;
  94. for (auto const& item : items) {
  95. if (skipItemAfterFramework) {
  96. skipItemAfterFramework = false;
  97. continue;
  98. }
  99. if (item.Target) {
  100. bool skip = false;
  101. switch (item.Target->GetType()) {
  102. case cmStateEnums::SHARED_LIBRARY:
  103. case cmStateEnums::MODULE_LIBRARY:
  104. case cmStateEnums::OBJECT_LIBRARY:
  105. case cmStateEnums::INTERFACE_LIBRARY:
  106. skip = true;
  107. break;
  108. case cmStateEnums::STATIC_LIBRARY:
  109. skip = item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
  110. break;
  111. default:
  112. break;
  113. }
  114. if (skip) {
  115. continue;
  116. }
  117. }
  118. BT<std::string> linkLib;
  119. if (item.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
  120. // nvcc understands absolute paths to libraries ending in '.o', .a', or
  121. // '.lib'. These should be passed to nvlink. Other extensions need to be
  122. // left out because nvlink may not understand or need them. Even though
  123. // it can tolerate '.so' or '.dylib' it cannot tolerate '.so.1'.
  124. if (cmHasLiteralSuffix(item.Value.Value, ".o") ||
  125. cmHasLiteralSuffix(item.Value.Value, ".obj") ||
  126. cmHasLiteralSuffix(item.Value.Value, ".a") ||
  127. cmHasLiteralSuffix(item.Value.Value, ".lib")) {
  128. linkLib.Value = item
  129. .GetFormattedItem(this->ConvertToOutputFormat(
  130. this->ConvertToLinkReference(item.Value.Value)))
  131. .Value;
  132. }
  133. } else if (item.Value == "-framework") {
  134. // This is the first part of '-framework Name' where the framework
  135. // name is specified as a following item. Ignore both.
  136. skipItemAfterFramework = true;
  137. continue;
  138. } else if (cmLinkItemValidForDevice(item.Value.Value)) {
  139. linkLib.Value = item.Value.Value;
  140. }
  141. if (emitted.insert(linkLib.Value).second) {
  142. linkLib.Value += " ";
  143. cmLinkImplementation const* linkImpl =
  144. cli.GetTarget()->GetLinkImplementation(cli.GetConfig(),
  145. cmGeneratorTarget::UseTo::Link);
  146. for (cmLinkItem const& iter : linkImpl->Libraries) {
  147. if (iter.Target &&
  148. iter.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  149. std::string libPath = iter.Target->GetLocation(cli.GetConfig());
  150. if (item.Value == libPath) {
  151. linkLib.Backtrace = iter.Backtrace;
  152. break;
  153. }
  154. }
  155. }
  156. linkLibraries.emplace_back(linkLib);
  157. }
  158. }
  159. if (!stdLibString.empty()) {
  160. linkLibraries.emplace_back(cmStrCat(stdLibString, ' '));
  161. }
  162. }
  163. std::string cmLinkLineDeviceComputer::GetLinkerLanguage(cmGeneratorTarget*,
  164. std::string const&)
  165. {
  166. return "CUDA";
  167. }
  168. bool requireDeviceLinking(cmGeneratorTarget& target, cmLocalGenerator& lg,
  169. std::string const& config)
  170. {
  171. if (!target.GetGlobalGenerator()->GetLanguageEnabled("CUDA")) {
  172. return false;
  173. }
  174. if (target.GetType() == cmStateEnums::OBJECT_LIBRARY) {
  175. return false;
  176. }
  177. if (!lg.GetMakefile()->IsOn("CMAKE_CUDA_COMPILER_HAS_DEVICE_LINK_PHASE")) {
  178. return false;
  179. }
  180. if (cmValue resolveDeviceSymbols =
  181. target.GetProperty("CUDA_RESOLVE_DEVICE_SYMBOLS")) {
  182. // If CUDA_RESOLVE_DEVICE_SYMBOLS has been explicitly set we need
  183. // to honor the value no matter what it is.
  184. return resolveDeviceSymbols.IsOn();
  185. }
  186. // Determine if we have any dependencies that require
  187. // us to do a device link step
  188. cmGeneratorTarget::LinkClosure const* closure =
  189. target.GetLinkClosure(config);
  190. if (cm::contains(closure->Languages, "CUDA")) {
  191. if (target.GetProperty("CUDA_SEPARABLE_COMPILATION").IsOn()) {
  192. bool doDeviceLinking = false;
  193. switch (target.GetType()) {
  194. case cmStateEnums::SHARED_LIBRARY:
  195. case cmStateEnums::MODULE_LIBRARY:
  196. case cmStateEnums::EXECUTABLE:
  197. doDeviceLinking = true;
  198. break;
  199. default:
  200. break;
  201. }
  202. return doDeviceLinking;
  203. }
  204. cmComputeLinkInformation* pcli = target.GetLinkInformation(config);
  205. if (pcli) {
  206. cmLinkLineDeviceComputer deviceLinkComputer(
  207. &lg, lg.GetStateSnapshot().GetDirectory());
  208. return deviceLinkComputer.ComputeRequiresDeviceLinking(*pcli);
  209. }
  210. return true;
  211. }
  212. return false;
  213. }