ClangLinkerWrapper.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. //===-- clang-linker-wrapper/ClangLinkerWrapper.cpp - wrapper over linker-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===---------------------------------------------------------------------===//
  8. //
  9. // This tool works as a wrapper over a linking job. This tool is used to create
  10. // linked device images for offloading. It scans the linker's input for embedded
  11. // device offloading data stored in sections `.llvm.offloading.<triple>.<arch>`
  12. // and extracts it as a temporary file. The extracted device files will then be
  13. // passed to a device linking job to create a final device image.
  14. //
  15. //===---------------------------------------------------------------------===//
  16. #include "OffloadWrapper.h"
  17. #include "clang/Basic/Version.h"
  18. #include "llvm/BinaryFormat/Magic.h"
  19. #include "llvm/Bitcode/BitcodeWriter.h"
  20. #include "llvm/CodeGen/CommandFlags.h"
  21. #include "llvm/IR/Constants.h"
  22. #include "llvm/IR/DiagnosticPrinter.h"
  23. #include "llvm/IR/Module.h"
  24. #include "llvm/IRReader/IRReader.h"
  25. #include "llvm/LTO/LTO.h"
  26. #include "llvm/MC/TargetRegistry.h"
  27. #include "llvm/Object/Archive.h"
  28. #include "llvm/Object/ArchiveWriter.h"
  29. #include "llvm/Object/Binary.h"
  30. #include "llvm/Object/ObjectFile.h"
  31. #include "llvm/Support/CommandLine.h"
  32. #include "llvm/Support/Errc.h"
  33. #include "llvm/Support/FileOutputBuffer.h"
  34. #include "llvm/Support/FileSystem.h"
  35. #include "llvm/Support/Host.h"
  36. #include "llvm/Support/InitLLVM.h"
  37. #include "llvm/Support/MemoryBuffer.h"
  38. #include "llvm/Support/Path.h"
  39. #include "llvm/Support/Program.h"
  40. #include "llvm/Support/Signals.h"
  41. #include "llvm/Support/SourceMgr.h"
  42. #include "llvm/Support/StringSaver.h"
  43. #include "llvm/Support/TargetSelect.h"
  44. #include "llvm/Support/WithColor.h"
  45. #include "llvm/Support/raw_ostream.h"
  46. #include "llvm/Target/TargetMachine.h"
  47. using namespace llvm;
  48. using namespace llvm::object;
  49. static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden);
  50. enum DebugKind {
  51. NoDebugInfo,
  52. DirectivesOnly,
  53. FullDebugInfo,
  54. };
  55. // Mark all our options with this category, everything else (except for -help)
  56. // will be hidden.
  57. static cl::OptionCategory
  58. ClangLinkerWrapperCategory("clang-linker-wrapper options");
  59. static cl::opt<bool> StripSections(
  60. "strip-sections", cl::ZeroOrMore,
  61. cl::desc("Strip offloading sections from the host object file."),
  62. cl::init(true), cl::cat(ClangLinkerWrapperCategory));
  63. static cl::opt<std::string> LinkerUserPath("linker-path", cl::Required,
  64. cl::desc("Path of linker binary"),
  65. cl::cat(ClangLinkerWrapperCategory));
  66. static cl::opt<std::string>
  67. TargetFeatures("target-feature", cl::ZeroOrMore,
  68. cl::desc("Target features for triple"),
  69. cl::cat(ClangLinkerWrapperCategory));
  70. static cl::opt<std::string> OptLevel("opt-level", cl::ZeroOrMore,
  71. cl::desc("Optimization level for LTO"),
  72. cl::init("O2"),
  73. cl::cat(ClangLinkerWrapperCategory));
  74. static cl::list<std::string>
  75. BitcodeLibraries("target-library", cl::ZeroOrMore,
  76. cl::desc("Path for the target bitcode library"),
  77. cl::cat(ClangLinkerWrapperCategory));
  78. static cl::opt<bool> EmbedBitcode(
  79. "target-embed-bc", cl::ZeroOrMore,
  80. cl::desc("Embed linked bitcode instead of an executable device image"),
  81. cl::init(false), cl::cat(ClangLinkerWrapperCategory));
  82. static cl::opt<std::string>
  83. HostTriple("host-triple", cl::ZeroOrMore,
  84. cl::desc("Triple to use for the host compilation"),
  85. cl::init(sys::getDefaultTargetTriple()),
  86. cl::cat(ClangLinkerWrapperCategory));
  87. static cl::list<std::string>
  88. PtxasArgs("ptxas-args", cl::ZeroOrMore,
  89. cl::desc("Argument to pass to the ptxas invocation"),
  90. cl::cat(ClangLinkerWrapperCategory));
  91. static cl::opt<bool> Verbose("v", cl::ZeroOrMore,
  92. cl::desc("Verbose output from tools"),
  93. cl::init(false),
  94. cl::cat(ClangLinkerWrapperCategory));
  95. static cl::opt<DebugKind> DebugInfo(
  96. cl::desc("Choose debugging level:"), cl::init(NoDebugInfo),
  97. cl::values(clEnumValN(NoDebugInfo, "g0", "No debug information"),
  98. clEnumValN(DirectivesOnly, "gline-directives-only",
  99. "Direction information"),
  100. clEnumValN(FullDebugInfo, "g", "Full debugging support")));
  101. static cl::opt<bool> SaveTemps("save-temps", cl::ZeroOrMore,
  102. cl::desc("Save intermediary results."),
  103. cl::cat(ClangLinkerWrapperCategory));
  104. static cl::opt<std::string> CudaPath("cuda-path", cl::ZeroOrMore,
  105. cl::desc("Save intermediary results."),
  106. cl::cat(ClangLinkerWrapperCategory));
  107. // Do not parse linker options.
  108. static cl::list<std::string>
  109. HostLinkerArgs(cl::Positional,
  110. cl::desc("<options to be passed to linker>..."));
  111. /// Path of the current binary.
  112. static const char *LinkerExecutable;
  113. /// Filename of the executable being created.
  114. static StringRef ExecutableName;
  115. /// Binary path for the CUDA installation.
  116. static std::string CudaBinaryPath;
  117. /// Temporary files created by the linker wrapper.
  118. static SmallVector<std::string, 16> TempFiles;
  119. /// Codegen flags for LTO backend.
  120. static codegen::RegisterCodeGenFlags CodeGenFlags;
  121. /// Magic section string that marks the existence of offloading data. The
  122. /// section string will be formatted as `.llvm.offloading.<triple>.<arch>`.
  123. #define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
  124. /// Information for a device offloading file extracted from the host.
  125. struct DeviceFile {
  126. DeviceFile(StringRef TheTriple, StringRef Arch, StringRef Filename)
  127. : TheTriple(TheTriple), Arch(Arch), Filename(Filename) {}
  128. const std::string TheTriple;
  129. const std::string Arch;
  130. const std::string Filename;
  131. operator std::string() const { return TheTriple + "-" + Arch; }
  132. };
  133. namespace {
  134. Expected<Optional<std::string>>
  135. extractFromBuffer(std::unique_ptr<MemoryBuffer> Buffer,
  136. SmallVectorImpl<DeviceFile> &DeviceFiles);
  137. static StringRef getDeviceFileExtension(StringRef DeviceTriple,
  138. bool IsBitcode = false) {
  139. Triple TheTriple(DeviceTriple);
  140. if (TheTriple.isAMDGPU() || IsBitcode)
  141. return "bc";
  142. if (TheTriple.isNVPTX())
  143. return "cubin";
  144. return "o";
  145. }
  146. std::string getMainExecutable(const char *Name) {
  147. void *Ptr = (void *)(intptr_t)&getMainExecutable;
  148. auto COWPath = sys::fs::getMainExecutable(Name, Ptr);
  149. return sys::path::parent_path(COWPath).str();
  150. }
  151. /// Extract the device file from the string '<triple>-<arch>=<library>.bc'.
  152. DeviceFile getBitcodeLibrary(StringRef LibraryStr) {
  153. auto DeviceAndPath = StringRef(LibraryStr).split('=');
  154. auto TripleAndArch = DeviceAndPath.first.rsplit('-');
  155. return DeviceFile(TripleAndArch.first, TripleAndArch.second,
  156. DeviceAndPath.second);
  157. }
  158. /// Get a temporary filename suitable for output.
  159. Error createOutputFile(const Twine &Prefix, StringRef Extension,
  160. SmallString<128> &NewFilename) {
  161. if (!SaveTemps) {
  162. if (std::error_code EC =
  163. sys::fs::createTemporaryFile(Prefix, Extension, NewFilename))
  164. return createFileError(NewFilename, EC);
  165. TempFiles.push_back(static_cast<std::string>(NewFilename));
  166. } else {
  167. const Twine &Filename = Prefix + "." + Extension;
  168. Filename.toNullTerminatedStringRef(NewFilename);
  169. }
  170. return Error::success();
  171. }
  172. Error runLinker(std::string &LinkerPath, SmallVectorImpl<std::string> &Args) {
  173. std::vector<StringRef> LinkerArgs;
  174. LinkerArgs.push_back(LinkerPath);
  175. for (auto &Arg : Args)
  176. LinkerArgs.push_back(Arg);
  177. if (sys::ExecuteAndWait(LinkerPath, LinkerArgs))
  178. return createStringError(inconvertibleErrorCode(), "'linker' failed");
  179. return Error::success();
  180. }
  181. void PrintVersion(raw_ostream &OS) {
  182. OS << clang::getClangToolFullVersion("clang-linker-wrapper") << '\n';
  183. }
  184. void removeFromCompilerUsed(Module &M, GlobalValue &Value) {
  185. GlobalVariable *GV = M.getGlobalVariable("llvm.compiler.used");
  186. Type *Int8PtrTy = Type::getInt8PtrTy(M.getContext());
  187. Constant *ValueToRemove =
  188. ConstantExpr::getPointerBitCastOrAddrSpaceCast(&Value, Int8PtrTy);
  189. SmallPtrSet<Constant *, 16> InitAsSet;
  190. SmallVector<Constant *, 16> Init;
  191. if (GV) {
  192. if (GV->hasInitializer()) {
  193. auto *CA = cast<ConstantArray>(GV->getInitializer());
  194. for (auto &Op : CA->operands()) {
  195. Constant *C = cast_or_null<Constant>(Op);
  196. if (C != ValueToRemove && InitAsSet.insert(C).second)
  197. Init.push_back(C);
  198. }
  199. }
  200. GV->eraseFromParent();
  201. }
  202. if (Init.empty())
  203. return;
  204. ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
  205. GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
  206. ConstantArray::get(ATy, Init),
  207. "llvm.compiler.used");
  208. GV->setSection("llvm.metadata");
  209. }
  210. Expected<Optional<std::string>>
  211. extractFromBinary(const ObjectFile &Obj,
  212. SmallVectorImpl<DeviceFile> &DeviceFiles) {
  213. StringRef Extension = sys::path::extension(Obj.getFileName()).drop_front();
  214. StringRef Prefix = sys::path::stem(Obj.getFileName());
  215. SmallVector<StringRef, 4> ToBeStripped;
  216. // Extract data from sections of the form `.llvm.offloading.<triple>.<arch>`.
  217. for (const SectionRef &Sec : Obj.sections()) {
  218. Expected<StringRef> Name = Sec.getName();
  219. if (!Name || !Name->startswith(OFFLOAD_SECTION_MAGIC_STR))
  220. continue;
  221. SmallVector<StringRef, 4> SectionFields;
  222. Name->split(SectionFields, '.');
  223. StringRef DeviceTriple = SectionFields[3];
  224. StringRef Arch = SectionFields[4];
  225. if (Expected<StringRef> Contents = Sec.getContents()) {
  226. SmallString<128> TempFile;
  227. StringRef DeviceExtension = getDeviceFileExtension(
  228. DeviceTriple, identify_magic(*Contents) == file_magic::bitcode);
  229. if (Error Err =
  230. createOutputFile(Prefix + "-device-" + DeviceTriple + "-" + Arch,
  231. DeviceExtension, TempFile))
  232. return std::move(Err);
  233. Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
  234. FileOutputBuffer::create(TempFile, Sec.getSize());
  235. if (!OutputOrErr)
  236. return OutputOrErr.takeError();
  237. std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
  238. std::copy(Contents->begin(), Contents->end(), Output->getBufferStart());
  239. if (Error E = Output->commit())
  240. return std::move(E);
  241. DeviceFiles.emplace_back(DeviceTriple, Arch, TempFile);
  242. ToBeStripped.push_back(*Name);
  243. }
  244. }
  245. if (ToBeStripped.empty() || !StripSections)
  246. return None;
  247. // If the object file to strip doesn't exist we need to write it so we can
  248. // pass it to llvm-strip.
  249. SmallString<128> StripFile = Obj.getFileName();
  250. if (!sys::fs::exists(StripFile)) {
  251. SmallString<128> TempFile;
  252. if (Error Err = createOutputFile(
  253. sys::path::stem(StripFile),
  254. sys::path::extension(StripFile).drop_front(), TempFile))
  255. return std::move(Err);
  256. auto Contents = Obj.getMemoryBufferRef().getBuffer();
  257. Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
  258. FileOutputBuffer::create(TempFile, Contents.size());
  259. if (!OutputOrErr)
  260. return OutputOrErr.takeError();
  261. std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
  262. std::copy(Contents.begin(), Contents.end(), Output->getBufferStart());
  263. if (Error E = Output->commit())
  264. return std::move(E);
  265. StripFile = TempFile;
  266. }
  267. // We will use llvm-strip to remove the now unneeded section containing the
  268. // offloading code.
  269. ErrorOr<std::string> StripPath =
  270. sys::findProgramByName("llvm-strip", {getMainExecutable("llvm-strip")});
  271. if (!StripPath)
  272. StripPath = sys::findProgramByName("llvm-strip");
  273. if (!StripPath)
  274. return None;
  275. SmallString<128> TempFile;
  276. if (Error Err = createOutputFile(Prefix + "-host", Extension, TempFile))
  277. return std::move(Err);
  278. SmallVector<StringRef, 8> StripArgs;
  279. StripArgs.push_back(*StripPath);
  280. StripArgs.push_back("--no-strip-all");
  281. StripArgs.push_back(StripFile);
  282. for (auto &Section : ToBeStripped) {
  283. StripArgs.push_back("--remove-section");
  284. StripArgs.push_back(Section);
  285. }
  286. StripArgs.push_back("-o");
  287. StripArgs.push_back(TempFile);
  288. if (sys::ExecuteAndWait(*StripPath, StripArgs))
  289. return createStringError(inconvertibleErrorCode(), "'llvm-strip' failed");
  290. return static_cast<std::string>(TempFile);
  291. }
  292. Expected<Optional<std::string>>
  293. extractFromBitcode(std::unique_ptr<MemoryBuffer> Buffer,
  294. SmallVectorImpl<DeviceFile> &DeviceFiles) {
  295. LLVMContext Context;
  296. SMDiagnostic Err;
  297. std::unique_ptr<Module> M = getLazyIRModule(std::move(Buffer), Err, Context);
  298. if (!M)
  299. return createStringError(inconvertibleErrorCode(),
  300. "Failed to create module");
  301. StringRef Extension = sys::path::extension(M->getName()).drop_front();
  302. StringRef Prefix =
  303. sys::path::stem(M->getName()).take_until([](char C) { return C == '-'; });
  304. SmallVector<GlobalVariable *, 4> ToBeDeleted;
  305. // Extract data from the global string containing a section of the form
  306. // `.llvm.offloading.<triple>.<arch>`.
  307. for (GlobalVariable &GV : M->globals()) {
  308. if (!GV.hasSection() ||
  309. !GV.getSection().startswith(OFFLOAD_SECTION_MAGIC_STR))
  310. continue;
  311. auto *CDS = dyn_cast<ConstantDataSequential>(GV.getInitializer());
  312. if (!CDS)
  313. continue;
  314. SmallVector<StringRef, 4> SectionFields;
  315. GV.getSection().split(SectionFields, '.');
  316. StringRef DeviceTriple = SectionFields[3];
  317. StringRef Arch = SectionFields[4];
  318. StringRef Contents = CDS->getAsString();
  319. SmallString<128> TempFile;
  320. StringRef DeviceExtension = getDeviceFileExtension(
  321. DeviceTriple, identify_magic(Contents) == file_magic::bitcode);
  322. if (Error Err =
  323. createOutputFile(Prefix + "-device-" + DeviceTriple + "-" + Arch,
  324. DeviceExtension, TempFile))
  325. return std::move(Err);
  326. Expected<std::unique_ptr<FileOutputBuffer>> OutputOrErr =
  327. FileOutputBuffer::create(TempFile, Contents.size());
  328. if (!OutputOrErr)
  329. return OutputOrErr.takeError();
  330. std::unique_ptr<FileOutputBuffer> Output = std::move(*OutputOrErr);
  331. std::copy(Contents.begin(), Contents.end(), Output->getBufferStart());
  332. if (Error E = Output->commit())
  333. return std::move(E);
  334. DeviceFiles.emplace_back(DeviceTriple, Arch, TempFile);
  335. ToBeDeleted.push_back(&GV);
  336. }
  337. if (ToBeDeleted.empty() || !StripSections)
  338. return None;
  339. // We need to materialize the lazy module before we make any changes.
  340. if (Error Err = M->materializeAll())
  341. return std::move(Err);
  342. // Remove the global from the module and write it to a new file.
  343. for (GlobalVariable *GV : ToBeDeleted) {
  344. removeFromCompilerUsed(*M, *GV);
  345. GV->eraseFromParent();
  346. }
  347. SmallString<128> TempFile;
  348. if (Error Err = createOutputFile(Prefix + "-host", Extension, TempFile))
  349. return std::move(Err);
  350. std::error_code EC;
  351. raw_fd_ostream HostOutput(TempFile, EC, sys::fs::OF_None);
  352. if (EC)
  353. return createFileError(TempFile, EC);
  354. WriteBitcodeToFile(*M, HostOutput);
  355. return static_cast<std::string>(TempFile);
  356. }
  357. Expected<Optional<std::string>>
  358. extractFromArchive(const Archive &Library,
  359. SmallVectorImpl<DeviceFile> &DeviceFiles) {
  360. bool NewMembers = false;
  361. SmallVector<NewArchiveMember, 8> Members;
  362. // Try to extract device code from each file stored in the static archive.
  363. // Save the stripped archive members to create a new host archive with the
  364. // offloading code removed.
  365. Error Err = Error::success();
  366. for (auto Child : Library.children(Err)) {
  367. auto ChildBufferRefOrErr = Child.getMemoryBufferRef();
  368. if (!ChildBufferRefOrErr)
  369. return ChildBufferRefOrErr.takeError();
  370. std::unique_ptr<MemoryBuffer> ChildBuffer =
  371. MemoryBuffer::getMemBuffer(*ChildBufferRefOrErr, false);
  372. auto FileOrErr = extractFromBuffer(std::move(ChildBuffer), DeviceFiles);
  373. if (!FileOrErr)
  374. return FileOrErr.takeError();
  375. // If we created a new stripped host file, use it to create a new archive
  376. // member, otherwise use the old member.
  377. if (!FileOrErr->hasValue()) {
  378. Expected<NewArchiveMember> NewMember =
  379. NewArchiveMember::getOldMember(Child, true);
  380. if (!NewMember)
  381. return NewMember.takeError();
  382. Members.push_back(std::move(*NewMember));
  383. } else {
  384. Expected<NewArchiveMember> NewMember =
  385. NewArchiveMember::getFile(**FileOrErr, true);
  386. if (!NewMember)
  387. return NewMember.takeError();
  388. Members.push_back(std::move(*NewMember));
  389. NewMembers = true;
  390. // We no longer need the stripped file, remove it.
  391. if (std::error_code EC = sys::fs::remove(**FileOrErr))
  392. return createFileError(**FileOrErr, EC);
  393. }
  394. }
  395. if (Err)
  396. return std::move(Err);
  397. if (!NewMembers || !StripSections)
  398. return None;
  399. // Create a new static library using the stripped host files.
  400. SmallString<128> TempFile;
  401. StringRef Prefix = sys::path::stem(Library.getFileName());
  402. if (Error Err = createOutputFile(Prefix + "-host", "a", TempFile))
  403. return std::move(Err);
  404. std::unique_ptr<MemoryBuffer> Buffer =
  405. MemoryBuffer::getMemBuffer(Library.getMemoryBufferRef(), false);
  406. if (Error Err = writeArchive(TempFile, Members, true, Library.kind(), true,
  407. Library.isThin(), std::move(Buffer)))
  408. return std::move(Err);
  409. return static_cast<std::string>(TempFile);
  410. }
  411. /// Extracts embedded device offloading code from a memory \p Buffer to a list
  412. /// of \p DeviceFiles. If device code was extracted a new file with the embedded
  413. /// device code stripped from the buffer will be returned.
  414. Expected<Optional<std::string>>
  415. extractFromBuffer(std::unique_ptr<MemoryBuffer> Buffer,
  416. SmallVectorImpl<DeviceFile> &DeviceFiles) {
  417. file_magic Type = identify_magic(Buffer->getBuffer());
  418. switch (Type) {
  419. case file_magic::bitcode:
  420. return extractFromBitcode(std::move(Buffer), DeviceFiles);
  421. case file_magic::elf_relocatable:
  422. case file_magic::macho_object:
  423. case file_magic::coff_object: {
  424. Expected<std::unique_ptr<ObjectFile>> ObjFile =
  425. ObjectFile::createObjectFile(*Buffer, Type);
  426. if (!ObjFile)
  427. return ObjFile.takeError();
  428. return extractFromBinary(*ObjFile->get(), DeviceFiles);
  429. }
  430. case file_magic::archive: {
  431. Expected<std::unique_ptr<llvm::object::Archive>> LibFile =
  432. object::Archive::create(*Buffer);
  433. if (!LibFile)
  434. return LibFile.takeError();
  435. return extractFromArchive(*LibFile->get(), DeviceFiles);
  436. }
  437. default:
  438. return errorCodeToError(object_error::invalid_file_type);
  439. }
  440. }
  441. // TODO: Move these to a separate file.
  442. namespace nvptx {
  443. Expected<std::string> assemble(StringRef InputFile, Triple TheTriple,
  444. StringRef Arch) {
  445. // NVPTX uses the ptxas binary to create device object files.
  446. ErrorOr<std::string> PtxasPath =
  447. sys::findProgramByName("ptxas", {CudaBinaryPath});
  448. if (!PtxasPath)
  449. PtxasPath = sys::findProgramByName("ptxas");
  450. if (!PtxasPath)
  451. return createStringError(PtxasPath.getError(),
  452. "Unable to find 'ptxas' in path");
  453. // Create a new file to write the linked device image to.
  454. SmallString<128> TempFile;
  455. if (Error Err =
  456. createOutputFile(sys::path::filename(ExecutableName) + "-device-" +
  457. TheTriple.getArchName() + "-" + Arch,
  458. "cubin", TempFile))
  459. return std::move(Err);
  460. SmallVector<StringRef, 16> CmdArgs;
  461. std::string Opt = "-" + OptLevel;
  462. CmdArgs.push_back(*PtxasPath);
  463. CmdArgs.push_back(TheTriple.isArch64Bit() ? "-m64" : "-m32");
  464. if (Verbose)
  465. CmdArgs.push_back("-v");
  466. if (DebugInfo == DirectivesOnly && OptLevel[1] == '0')
  467. CmdArgs.push_back("-lineinfo");
  468. else if (DebugInfo == FullDebugInfo && OptLevel[1] == '0')
  469. CmdArgs.push_back("-g");
  470. for (auto &Arg : PtxasArgs)
  471. CmdArgs.push_back(Arg);
  472. CmdArgs.push_back("-o");
  473. CmdArgs.push_back(TempFile);
  474. CmdArgs.push_back(Opt);
  475. CmdArgs.push_back("--gpu-name");
  476. CmdArgs.push_back(Arch);
  477. CmdArgs.push_back("-c");
  478. CmdArgs.push_back(InputFile);
  479. if (sys::ExecuteAndWait(*PtxasPath, CmdArgs))
  480. return createStringError(inconvertibleErrorCode(), "'ptxas' failed");
  481. return static_cast<std::string>(TempFile);
  482. }
  483. Expected<std::string> link(ArrayRef<std::string> InputFiles, Triple TheTriple,
  484. StringRef Arch) {
  485. // NVPTX uses the nvlink binary to link device object files.
  486. ErrorOr<std::string> NvlinkPath =
  487. sys::findProgramByName("nvlink", {CudaBinaryPath});
  488. if (!NvlinkPath)
  489. NvlinkPath = sys::findProgramByName("nvlink");
  490. if (!NvlinkPath)
  491. return createStringError(NvlinkPath.getError(),
  492. "Unable to find 'nvlink' in path");
  493. // Create a new file to write the linked device image to.
  494. SmallString<128> TempFile;
  495. if (Error Err =
  496. createOutputFile(sys::path::filename(ExecutableName) + "-device-" +
  497. TheTriple.getArchName() + "-" + Arch,
  498. "out", TempFile))
  499. return std::move(Err);
  500. SmallVector<StringRef, 16> CmdArgs;
  501. CmdArgs.push_back(*NvlinkPath);
  502. CmdArgs.push_back(TheTriple.isArch64Bit() ? "-m64" : "-m32");
  503. if (Verbose)
  504. CmdArgs.push_back("-v");
  505. if (DebugInfo != NoDebugInfo)
  506. CmdArgs.push_back("-g");
  507. CmdArgs.push_back("-o");
  508. CmdArgs.push_back(TempFile);
  509. CmdArgs.push_back("-arch");
  510. CmdArgs.push_back(Arch);
  511. // Add extracted input files.
  512. for (StringRef Input : InputFiles)
  513. CmdArgs.push_back(Input);
  514. if (sys::ExecuteAndWait(*NvlinkPath, CmdArgs))
  515. return createStringError(inconvertibleErrorCode(), "'nvlink' failed");
  516. return static_cast<std::string>(TempFile);
  517. }
  518. } // namespace nvptx
  519. namespace amdgcn {
  520. Expected<std::string> link(ArrayRef<std::string> InputFiles, Triple TheTriple,
  521. StringRef Arch) {
  522. // AMDGPU uses lld to link device object files.
  523. ErrorOr<std::string> LLDPath =
  524. sys::findProgramByName("lld", {getMainExecutable("lld")});
  525. if (!LLDPath)
  526. LLDPath = sys::findProgramByName("lld");
  527. if (!LLDPath)
  528. return createStringError(LLDPath.getError(),
  529. "Unable to find 'lld' in path");
  530. // Create a new file to write the linked device image to.
  531. SmallString<128> TempFile;
  532. if (Error Err = createOutputFile(sys::path::filename(ExecutableName) + "-" +
  533. TheTriple.getArchName() + "-" + Arch,
  534. "out", TempFile))
  535. return std::move(Err);
  536. SmallVector<StringRef, 16> CmdArgs;
  537. CmdArgs.push_back(*LLDPath);
  538. CmdArgs.push_back("-flavor");
  539. CmdArgs.push_back("gnu");
  540. CmdArgs.push_back("--no-undefined");
  541. CmdArgs.push_back("-shared");
  542. CmdArgs.push_back("-o");
  543. CmdArgs.push_back(TempFile);
  544. // Add extracted input files.
  545. for (StringRef Input : InputFiles)
  546. CmdArgs.push_back(Input);
  547. if (sys::ExecuteAndWait(*LLDPath, CmdArgs))
  548. return createStringError(inconvertibleErrorCode(), "'lld' failed");
  549. return static_cast<std::string>(TempFile);
  550. }
  551. } // namespace amdgcn
  552. Expected<std::string> linkDevice(ArrayRef<std::string> InputFiles,
  553. Triple TheTriple, StringRef Arch) {
  554. switch (TheTriple.getArch()) {
  555. case Triple::nvptx:
  556. case Triple::nvptx64:
  557. return nvptx::link(InputFiles, TheTriple, Arch);
  558. case Triple::amdgcn:
  559. return amdgcn::link(InputFiles, TheTriple, Arch);
  560. case Triple::x86:
  561. case Triple::x86_64:
  562. // TODO: x86 linking support.
  563. default:
  564. return createStringError(inconvertibleErrorCode(),
  565. TheTriple.getArchName() +
  566. " linking is not supported");
  567. }
  568. }
  569. void diagnosticHandler(const DiagnosticInfo &DI) {
  570. std::string ErrStorage;
  571. raw_string_ostream OS(ErrStorage);
  572. DiagnosticPrinterRawOStream DP(OS);
  573. DI.print(DP);
  574. switch (DI.getSeverity()) {
  575. case DS_Error:
  576. WithColor::error(errs(), LinkerExecutable) << ErrStorage << "\n";
  577. break;
  578. case DS_Warning:
  579. WithColor::warning(errs(), LinkerExecutable) << ErrStorage << "\n";
  580. break;
  581. case DS_Note:
  582. WithColor::note(errs(), LinkerExecutable) << ErrStorage << "\n";
  583. break;
  584. case DS_Remark:
  585. WithColor::remark(errs()) << ErrStorage << "\n";
  586. break;
  587. }
  588. }
  589. // Get the target features passed in from the driver as <triple>=<features>.
  590. std::vector<std::string> getTargetFeatures(const Triple &TheTriple) {
  591. std::vector<std::string> Features;
  592. auto TargetAndFeatures = StringRef(TargetFeatures).split('=');
  593. if (TargetAndFeatures.first != TheTriple.getTriple())
  594. return Features;
  595. for (auto Feature : llvm::split(TargetAndFeatures.second, ','))
  596. Features.push_back(Feature.str());
  597. return Features;
  598. }
  599. CodeGenOpt::Level getCGOptLevel(unsigned OptLevel) {
  600. switch (OptLevel) {
  601. case 0:
  602. return CodeGenOpt::None;
  603. case 1:
  604. return CodeGenOpt::Less;
  605. case 2:
  606. return CodeGenOpt::Default;
  607. case 3:
  608. return CodeGenOpt::Aggressive;
  609. }
  610. llvm_unreachable("Invalid optimization level");
  611. }
  612. template <typename ModuleHook = function_ref<bool(size_t, const Module &)>>
  613. std::unique_ptr<lto::LTO> createLTO(
  614. const Triple &TheTriple, StringRef Arch, bool WholeProgram,
  615. ModuleHook Hook = [](size_t, const Module &) { return true; }) {
  616. lto::Config Conf;
  617. lto::ThinBackend Backend;
  618. // TODO: Handle index-only thin-LTO
  619. Backend = lto::createInProcessThinBackend(
  620. llvm::heavyweight_hardware_concurrency(1));
  621. Conf.CPU = Arch.str();
  622. Conf.Options = codegen::InitTargetOptionsFromCodeGenFlags(TheTriple);
  623. Conf.MAttrs = getTargetFeatures(TheTriple);
  624. Conf.CGOptLevel = getCGOptLevel(OptLevel[1] - '0');
  625. Conf.OptLevel = OptLevel[1] - '0';
  626. Conf.DefaultTriple = TheTriple.getTriple();
  627. Conf.DiagHandler = diagnosticHandler;
  628. Conf.PTO.LoopVectorization = Conf.OptLevel > 1;
  629. Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
  630. if (SaveTemps) {
  631. auto HandleError = [&](Error Err) {
  632. logAllUnhandledErrors(std::move(Err),
  633. WithColor::error(errs(), LinkerExecutable));
  634. exit(1);
  635. };
  636. Conf.PostInternalizeModuleHook = [&](size_t, const Module &M) {
  637. SmallString<128> TempFile;
  638. if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
  639. "-device-" + TheTriple.getTriple(),
  640. "bc", TempFile))
  641. HandleError(std::move(Err));
  642. std::error_code EC;
  643. raw_fd_ostream LinkedBitcode(TempFile, EC, sys::fs::OF_None);
  644. if (EC)
  645. HandleError(errorCodeToError(EC));
  646. WriteBitcodeToFile(M, LinkedBitcode);
  647. return true;
  648. };
  649. }
  650. Conf.PostOptModuleHook = Hook;
  651. if (TheTriple.isNVPTX())
  652. Conf.CGFileType = CGFT_AssemblyFile;
  653. else
  654. Conf.CGFileType = CGFT_ObjectFile;
  655. // TODO: Handle remark files
  656. Conf.HasWholeProgramVisibility = WholeProgram;
  657. return std::make_unique<lto::LTO>(std::move(Conf), Backend);
  658. }
  659. // Returns true if \p S is valid as a C language identifier and will be given
  660. // `__start_` and `__stop_` symbols.
  661. bool isValidCIdentifier(StringRef S) {
  662. return !S.empty() && (isAlpha(S[0]) || S[0] == '_') &&
  663. std::all_of(S.begin() + 1, S.end(),
  664. [](char C) { return C == '_' || isAlnum(C); });
  665. }
  666. Error linkBitcodeFiles(SmallVectorImpl<std::string> &InputFiles,
  667. const Triple &TheTriple, StringRef Arch) {
  668. SmallVector<std::unique_ptr<MemoryBuffer>, 4> SavedBuffers;
  669. SmallVector<std::unique_ptr<lto::InputFile>, 4> BitcodeFiles;
  670. SmallVector<std::string, 4> NewInputFiles;
  671. StringMap<bool> UsedInRegularObj;
  672. StringMap<bool> UsedInSharedLib;
  673. // Search for bitcode files in the input and create an LTO input file. If it
  674. // is not a bitcode file, scan its symbol table for symbols we need to
  675. // save.
  676. for (StringRef File : InputFiles) {
  677. ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
  678. MemoryBuffer::getFileOrSTDIN(File);
  679. if (std::error_code EC = BufferOrErr.getError())
  680. return createFileError(File, EC);
  681. file_magic Type = identify_magic((*BufferOrErr)->getBuffer());
  682. if (Type != file_magic::bitcode) {
  683. Expected<std::unique_ptr<ObjectFile>> ObjFile =
  684. ObjectFile::createObjectFile(**BufferOrErr, Type);
  685. if (!ObjFile)
  686. return ObjFile.takeError();
  687. NewInputFiles.push_back(File.str());
  688. for (auto &Sym : (*ObjFile)->symbols()) {
  689. Expected<StringRef> Name = Sym.getName();
  690. if (!Name)
  691. return Name.takeError();
  692. // Record if we've seen these symbols in any object or shared libraries.
  693. if ((*ObjFile)->isRelocatableObject())
  694. UsedInRegularObj[*Name] = true;
  695. else
  696. UsedInSharedLib[*Name] = true;
  697. }
  698. } else {
  699. Expected<std::unique_ptr<lto::InputFile>> InputFileOrErr =
  700. llvm::lto::InputFile::create(**BufferOrErr);
  701. if (!InputFileOrErr)
  702. return InputFileOrErr.takeError();
  703. // Save the input file and the buffer associated with its memory.
  704. BitcodeFiles.push_back(std::move(*InputFileOrErr));
  705. SavedBuffers.push_back(std::move(*BufferOrErr));
  706. }
  707. }
  708. if (BitcodeFiles.empty())
  709. return Error::success();
  710. auto HandleError = [&](Error Err) {
  711. logAllUnhandledErrors(std::move(Err),
  712. WithColor::error(errs(), LinkerExecutable));
  713. exit(1);
  714. };
  715. // LTO Module hook to output bitcode without running the backend.
  716. auto OutputBitcode = [&](size_t Task, const Module &M) {
  717. SmallString<128> TempFile;
  718. if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
  719. "-jit-" + TheTriple.getTriple(),
  720. "bc", TempFile))
  721. HandleError(std::move(Err));
  722. std::error_code EC;
  723. raw_fd_ostream LinkedBitcode(TempFile, EC, sys::fs::OF_None);
  724. if (EC)
  725. HandleError(errorCodeToError(EC));
  726. WriteBitcodeToFile(M, LinkedBitcode);
  727. NewInputFiles.push_back(static_cast<std::string>(TempFile));
  728. return false;
  729. };
  730. // We assume visibility of the whole program if every input file was bitcode.
  731. bool WholeProgram = BitcodeFiles.size() == InputFiles.size();
  732. auto LTOBackend =
  733. (EmbedBitcode) ? createLTO(TheTriple, Arch, WholeProgram, OutputBitcode)
  734. : createLTO(TheTriple, Arch, WholeProgram);
  735. // We need to resolve the symbols so the LTO backend knows which symbols need
  736. // to be kept or can be internalized. This is a simplified symbol resolution
  737. // scheme to approximate the full resolution a linker would do.
  738. DenseSet<StringRef> PrevailingSymbols;
  739. for (auto &BitcodeFile : BitcodeFiles) {
  740. const auto Symbols = BitcodeFile->symbols();
  741. SmallVector<lto::SymbolResolution, 16> Resolutions(Symbols.size());
  742. size_t Idx = 0;
  743. for (auto &Sym : Symbols) {
  744. lto::SymbolResolution &Res = Resolutions[Idx++];
  745. // We will use this as the prevailing symbol definition in LTO unless
  746. // it is undefined or another definition has already been used.
  747. Res.Prevailing =
  748. !Sym.isUndefined() && PrevailingSymbols.insert(Sym.getName()).second;
  749. // We need LTO to preseve the following global symbols:
  750. // 1) Symbols used in regular objects.
  751. // 2) Sections that will be given a __start/__stop symbol.
  752. // 3) Prevailing symbols that are needed visibile to external libraries.
  753. Res.VisibleToRegularObj =
  754. UsedInRegularObj[Sym.getName()] ||
  755. isValidCIdentifier(Sym.getSectionName()) ||
  756. (Res.Prevailing &&
  757. (Sym.getVisibility() != GlobalValue::HiddenVisibility &&
  758. !Sym.canBeOmittedFromSymbolTable()));
  759. // Identify symbols that must be exported dynamically and can be
  760. // referenced by other files.
  761. Res.ExportDynamic =
  762. Sym.getVisibility() != GlobalValue::HiddenVisibility &&
  763. (UsedInSharedLib[Sym.getName()] ||
  764. !Sym.canBeOmittedFromSymbolTable());
  765. // The final definition will reside in this linkage unit if the symbol is
  766. // defined and local to the module. This only checks for bitcode files,
  767. // full assertion will require complete symbol resolution.
  768. Res.FinalDefinitionInLinkageUnit =
  769. Sym.getVisibility() != GlobalValue::DefaultVisibility &&
  770. (!Sym.isUndefined() && !Sym.isCommon());
  771. // We do not support linker redefined symbols (e.g. --wrap) for device
  772. // image linking, so the symbols will not be changed after LTO.
  773. Res.LinkerRedefined = false;
  774. }
  775. // Add the bitcode file with its resolved symbols to the LTO job.
  776. if (Error Err = LTOBackend->add(std::move(BitcodeFile), Resolutions))
  777. return Err;
  778. }
  779. // Run the LTO job to compile the bitcode.
  780. size_t MaxTasks = LTOBackend->getMaxTasks();
  781. std::vector<SmallString<128>> Files(MaxTasks);
  782. auto AddStream = [&](size_t Task) -> std::unique_ptr<CachedFileStream> {
  783. int FD = -1;
  784. auto &TempFile = Files[Task];
  785. StringRef Extension = (TheTriple.isNVPTX()) ? "s" : "o";
  786. if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
  787. "-device-" + TheTriple.getTriple(),
  788. Extension, TempFile))
  789. HandleError(std::move(Err));
  790. if (std::error_code EC = sys::fs::openFileForWrite(TempFile, FD))
  791. HandleError(errorCodeToError(EC));
  792. return std::make_unique<CachedFileStream>(
  793. std::make_unique<llvm::raw_fd_ostream>(FD, true));
  794. };
  795. if (Error Err = LTOBackend->run(AddStream))
  796. return Err;
  797. // Is we are compiling for NVPTX we need to run the assembler first.
  798. if (TheTriple.isNVPTX() && !EmbedBitcode) {
  799. for (auto &File : Files) {
  800. auto FileOrErr = nvptx::assemble(File, TheTriple, Arch);
  801. if (!FileOrErr)
  802. return FileOrErr.takeError();
  803. File = *FileOrErr;
  804. }
  805. }
  806. // Append the new inputs to the device linker input.
  807. for (auto &File : Files)
  808. NewInputFiles.push_back(static_cast<std::string>(File));
  809. InputFiles = NewInputFiles;
  810. return Error::success();
  811. }
  812. /// Runs the appropriate linking action on all the device files specified in \p
  813. /// DeviceFiles. The linked device images are returned in \p LinkedImages.
  814. Error linkDeviceFiles(ArrayRef<DeviceFile> DeviceFiles,
  815. SmallVectorImpl<std::string> &LinkedImages) {
  816. // Get the list of inputs for a specific device.
  817. StringMap<SmallVector<std::string, 4>> LinkerInputMap;
  818. for (auto &File : DeviceFiles)
  819. LinkerInputMap[StringRef(File)].push_back(File.Filename);
  820. // Try to link each device toolchain.
  821. for (auto &LinkerInput : LinkerInputMap) {
  822. auto TargetFeatures = LinkerInput.getKey().rsplit('-');
  823. Triple TheTriple(TargetFeatures.first);
  824. StringRef Arch(TargetFeatures.second);
  825. // Run LTO on any bitcode files and replace the input with the result.
  826. if (Error Err = linkBitcodeFiles(LinkerInput.getValue(), TheTriple, Arch))
  827. return Err;
  828. // If we are embedding bitcode for JIT, skip the final device linking.
  829. if (EmbedBitcode) {
  830. assert(!LinkerInput.getValue().empty() && "No bitcode image to embed");
  831. LinkedImages.push_back(LinkerInput.getValue().front());
  832. continue;
  833. }
  834. auto ImageOrErr = linkDevice(LinkerInput.getValue(), TheTriple, Arch);
  835. if (!ImageOrErr)
  836. return ImageOrErr.takeError();
  837. LinkedImages.push_back(*ImageOrErr);
  838. }
  839. return Error::success();
  840. }
  841. // Compile the module to an object file using the appropriate target machine for
  842. // the host triple.
  843. Expected<std::string> compileModule(Module &M) {
  844. std::string Msg;
  845. const Target *T = TargetRegistry::lookupTarget(M.getTargetTriple(), Msg);
  846. if (!T)
  847. return createStringError(inconvertibleErrorCode(), Msg);
  848. auto Options =
  849. codegen::InitTargetOptionsFromCodeGenFlags(Triple(M.getTargetTriple()));
  850. StringRef CPU = "";
  851. StringRef Features = "";
  852. std::unique_ptr<TargetMachine> TM(T->createTargetMachine(
  853. HostTriple, CPU, Features, Options, Reloc::PIC_, M.getCodeModel()));
  854. if (M.getDataLayout().isDefault())
  855. M.setDataLayout(TM->createDataLayout());
  856. SmallString<128> ObjectFile;
  857. int FD = -1;
  858. if (Error Err = createOutputFile(sys::path::filename(ExecutableName) +
  859. "offload-wrapper",
  860. "o", ObjectFile))
  861. return std::move(Err);
  862. if (std::error_code EC = sys::fs::openFileForWrite(ObjectFile, FD))
  863. return errorCodeToError(EC);
  864. auto OS = std::make_unique<llvm::raw_fd_ostream>(FD, true);
  865. legacy::PassManager CodeGenPasses;
  866. TargetLibraryInfoImpl TLII(Triple(M.getTargetTriple()));
  867. CodeGenPasses.add(new TargetLibraryInfoWrapperPass(TLII));
  868. if (TM->addPassesToEmitFile(CodeGenPasses, *OS, nullptr, CGFT_ObjectFile))
  869. return createStringError(inconvertibleErrorCode(),
  870. "Failed to execute host backend");
  871. CodeGenPasses.run(M);
  872. return static_cast<std::string>(ObjectFile);
  873. }
  874. /// Creates the object file containing the device image and runtime registration
  875. /// code from the device images stored in \p Images.
  876. Expected<std::string> wrapDeviceImages(ArrayRef<std::string> Images) {
  877. SmallVector<std::unique_ptr<MemoryBuffer>, 4> SavedBuffers;
  878. SmallVector<ArrayRef<char>, 4> ImagesToWrap;
  879. for (StringRef ImageFilename : Images) {
  880. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ImageOrError =
  881. llvm::MemoryBuffer::getFileOrSTDIN(ImageFilename);
  882. if (std::error_code EC = ImageOrError.getError())
  883. return createFileError(ImageFilename, EC);
  884. ImagesToWrap.emplace_back((*ImageOrError)->getBufferStart(),
  885. (*ImageOrError)->getBufferSize());
  886. SavedBuffers.emplace_back(std::move(*ImageOrError));
  887. }
  888. LLVMContext Context;
  889. Module M("offload.wrapper.module", Context);
  890. M.setTargetTriple(HostTriple);
  891. if (Error Err = wrapBinaries(M, ImagesToWrap))
  892. return std::move(Err);
  893. return compileModule(M);
  894. }
  895. Optional<std::string> findFile(StringRef Dir, const Twine &Name) {
  896. SmallString<128> Path;
  897. // TODO: Parse `--sysroot` somewhere and use it here.
  898. sys::path::append(Path, Dir, Name);
  899. if (sys::fs::exists(Path))
  900. return static_cast<std::string>(Path);
  901. return None;
  902. }
  903. Optional<std::string> findFromSearchPaths(StringRef Name,
  904. ArrayRef<StringRef> SearchPaths) {
  905. for (StringRef Dir : SearchPaths)
  906. if (Optional<std::string> File = findFile(Dir, Name))
  907. return File;
  908. return None;
  909. }
  910. Optional<std::string> searchLibraryBaseName(StringRef Name,
  911. ArrayRef<StringRef> SearchPaths) {
  912. for (StringRef Dir : SearchPaths) {
  913. if (Optional<std::string> File = findFile(Dir, "lib" + Name + ".so"))
  914. return None;
  915. if (Optional<std::string> File = findFile(Dir, "lib" + Name + ".a"))
  916. return File;
  917. }
  918. return None;
  919. }
  920. /// Search for static libraries in the linker's library path given input like
  921. /// `-lfoo` or `-l:libfoo.a`.
  922. Optional<std::string> searchLibrary(StringRef Input,
  923. ArrayRef<StringRef> SearchPaths) {
  924. if (!Input.startswith("-l"))
  925. return None;
  926. StringRef Name = Input.drop_front(2);
  927. if (Name.startswith(":"))
  928. return findFromSearchPaths(Name.drop_front(), SearchPaths);
  929. return searchLibraryBaseName(Name, SearchPaths);
  930. }
  931. } // namespace
  932. int main(int argc, const char **argv) {
  933. InitLLVM X(argc, argv);
  934. InitializeAllTargetInfos();
  935. InitializeAllTargets();
  936. InitializeAllTargetMCs();
  937. InitializeAllAsmParsers();
  938. InitializeAllAsmPrinters();
  939. LinkerExecutable = argv[0];
  940. sys::PrintStackTraceOnErrorSignal(argv[0]);
  941. cl::SetVersionPrinter(PrintVersion);
  942. cl::HideUnrelatedOptions(ClangLinkerWrapperCategory);
  943. cl::ParseCommandLineOptions(
  944. argc, argv,
  945. "A wrapper utility over the host linker. It scans the input files for\n"
  946. "sections that require additional processing prior to linking. The tool\n"
  947. "will then transparently pass all arguments and input to the specified\n"
  948. "host linker to create the final binary.\n");
  949. if (Help) {
  950. cl::PrintHelpMessage();
  951. return EXIT_SUCCESS;
  952. }
  953. auto reportError = [argv](Error E) {
  954. logAllUnhandledErrors(std::move(E), WithColor::error(errs(), argv[0]));
  955. return EXIT_FAILURE;
  956. };
  957. if (!CudaPath.empty())
  958. CudaBinaryPath = CudaPath + "/bin";
  959. ExecutableName = *(llvm::find(HostLinkerArgs, "-o") + 1);
  960. SmallVector<std::string, 16> LinkerArgs;
  961. for (const std::string &Arg : HostLinkerArgs)
  962. LinkerArgs.push_back(Arg);
  963. SmallVector<StringRef, 16> LibraryPaths;
  964. for (StringRef Arg : LinkerArgs) {
  965. if (Arg.startswith("-L"))
  966. LibraryPaths.push_back(Arg.drop_front(2));
  967. }
  968. // Try to extract device code from the linker input and replace the linker
  969. // input with a new file that has the device section stripped.
  970. SmallVector<DeviceFile, 4> DeviceFiles;
  971. for (std::string &Arg : LinkerArgs) {
  972. if (Arg == ExecutableName)
  973. continue;
  974. // Search for static libraries in the library link path.
  975. std::string Filename = Arg;
  976. if (Optional<std::string> Library = searchLibrary(Arg, LibraryPaths))
  977. Filename = *Library;
  978. if ((sys::path::extension(Filename) == ".o" ||
  979. sys::path::extension(Filename) == ".a")) {
  980. ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
  981. MemoryBuffer::getFileOrSTDIN(Filename);
  982. if (std::error_code EC = BufferOrErr.getError())
  983. return reportError(createFileError(Filename, EC));
  984. auto NewFileOrErr =
  985. extractFromBuffer(std::move(*BufferOrErr), DeviceFiles);
  986. if (!NewFileOrErr)
  987. return reportError(NewFileOrErr.takeError());
  988. if (NewFileOrErr->hasValue())
  989. Arg = **NewFileOrErr;
  990. }
  991. }
  992. // Add the device bitcode libraries to the device files if any were passed in.
  993. for (StringRef LibraryStr : BitcodeLibraries)
  994. DeviceFiles.push_back(getBitcodeLibrary(LibraryStr));
  995. // Link the device images extracted from the linker input.
  996. SmallVector<std::string, 16> LinkedImages;
  997. if (Error Err = linkDeviceFiles(DeviceFiles, LinkedImages))
  998. return reportError(std::move(Err));
  999. // Wrap each linked device image into a linkable host binary and add it to the
  1000. // link job's inputs.
  1001. auto FileOrErr = wrapDeviceImages(LinkedImages);
  1002. if (!FileOrErr)
  1003. return reportError(FileOrErr.takeError());
  1004. LinkerArgs.push_back(*FileOrErr);
  1005. // Run the host linking job.
  1006. if (Error Err = runLinker(LinkerUserPath, LinkerArgs))
  1007. return reportError(std::move(Err));
  1008. // Remove the temporary files created.
  1009. for (const auto &TempFile : TempFiles)
  1010. if (std::error_code EC = sys::fs::remove(TempFile))
  1011. reportError(createFileError(TempFile, EC));
  1012. return EXIT_SUCCESS;
  1013. }