cmLinkLineDeviceComputer.cxx 7.9 KB

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