xref: /llvm-project/lld/unittests/AsLibELF/ROCm.cpp (revision 6f2e92c10cebca51f9bc98b99fa3b5583ff396c4)
1*6f2e92c1SAlexandre Ganea //===- ROCm.cpp -------------------------------------------------*- C++ -*-===//
2*6f2e92c1SAlexandre Ganea //
3*6f2e92c1SAlexandre Ganea // This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4*6f2e92c1SAlexandre Ganea // See https://llvm.org/LICENSE.txt for license information.
5*6f2e92c1SAlexandre Ganea // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*6f2e92c1SAlexandre Ganea //
7*6f2e92c1SAlexandre Ganea //===----------------------------------------------------------------------===//
8*6f2e92c1SAlexandre Ganea // The purpose of this test is to showcase a more singular usage of LLD as a
9*6f2e92c1SAlexandre Ganea // library, where only one LLD driver is being used (and linked in the target
10*6f2e92c1SAlexandre Ganea // application). We also expect that linking twice the same object files
11*6f2e92c1SAlexandre Ganea // would yield a successfull result. When used as library, LLD always cleans its
12*6f2e92c1SAlexandre Ganea // internal memory context after each linker call.
13*6f2e92c1SAlexandre Ganea //===----------------------------------------------------------------------===//
14*6f2e92c1SAlexandre Ganea 
15*6f2e92c1SAlexandre Ganea // When this flag is on, we actually need the MinGW driver library, not the
16*6f2e92c1SAlexandre Ganea // ELF one. Here our test only covers the case where the ELF driver is linked
17*6f2e92c1SAlexandre Ganea // into the unit test binary.
18*6f2e92c1SAlexandre Ganea #ifndef LLD_DEFAULT_LD_LLD_IS_MINGW
19*6f2e92c1SAlexandre Ganea 
20*6f2e92c1SAlexandre Ganea #include "lld/Common/Driver.h"
21*6f2e92c1SAlexandre Ganea #include "llvm/ADT/SmallString.h"
22*6f2e92c1SAlexandre Ganea #include "llvm/Support/FileSystem.h"
23*6f2e92c1SAlexandre Ganea #include "llvm/Support/FileUtilities.h"
24*6f2e92c1SAlexandre Ganea #include "llvm/Support/Path.h"
25*6f2e92c1SAlexandre Ganea #include "gmock/gmock.h"
26*6f2e92c1SAlexandre Ganea #include <algorithm>
27*6f2e92c1SAlexandre Ganea 
expand(const char * path)28*6f2e92c1SAlexandre Ganea static std::string expand(const char *path) {
29*6f2e92c1SAlexandre Ganea   if (!llvm::StringRef(path).contains("%"))
30*6f2e92c1SAlexandre Ganea     return std::string(path);
31*6f2e92c1SAlexandre Ganea 
32*6f2e92c1SAlexandre Ganea   llvm::SmallString<256> thisPath;
33*6f2e92c1SAlexandre Ganea   thisPath.append(getenv("LLD_SRC_DIR"));
34*6f2e92c1SAlexandre Ganea   llvm::sys::path::append(thisPath, "unittests", "AsLibELF");
35*6f2e92c1SAlexandre Ganea 
36*6f2e92c1SAlexandre Ganea   std::string expanded(path);
37*6f2e92c1SAlexandre Ganea   expanded.replace(expanded.find("%S"), 2, thisPath.data(), thisPath.size());
38*6f2e92c1SAlexandre Ganea   return expanded;
39*6f2e92c1SAlexandre Ganea }
40*6f2e92c1SAlexandre Ganea 
LLD_HAS_DRIVER(elf)41*6f2e92c1SAlexandre Ganea LLD_HAS_DRIVER(elf)
42*6f2e92c1SAlexandre Ganea 
43*6f2e92c1SAlexandre Ganea static bool lldInvoke(const char *inPath, const char *outPath) {
44*6f2e92c1SAlexandre Ganea   std::vector<const char *> args{"ld.lld", "-shared", inPath, "-o", outPath};
45*6f2e92c1SAlexandre Ganea   lld::Result s = lld::lldMain(args, llvm::outs(), llvm::errs(),
46*6f2e92c1SAlexandre Ganea                                {{lld::Gnu, &lld::elf::link}});
47*6f2e92c1SAlexandre Ganea   return !s.retCode && s.canRunAgain;
48*6f2e92c1SAlexandre Ganea }
49*6f2e92c1SAlexandre Ganea 
runLinker(const char * path)50*6f2e92c1SAlexandre Ganea static bool runLinker(const char *path) {
51*6f2e92c1SAlexandre Ganea   // Create a temp file for HSA code object.
52*6f2e92c1SAlexandre Ganea   int tempHsacoFD = -1;
53*6f2e92c1SAlexandre Ganea   llvm::SmallString<128> tempHsacoFilename;
54*6f2e92c1SAlexandre Ganea   if (llvm::sys::fs::createTemporaryFile("kernel", "hsaco", tempHsacoFD,
55*6f2e92c1SAlexandre Ganea                                          tempHsacoFilename)) {
56*6f2e92c1SAlexandre Ganea     return false;
57*6f2e92c1SAlexandre Ganea   }
58*6f2e92c1SAlexandre Ganea   llvm::FileRemover cleanupHsaco(tempHsacoFilename);
59*6f2e92c1SAlexandre Ganea   // Invoke lld. Expect a true return value from lld.
60*6f2e92c1SAlexandre Ganea   std::string expandedPath = expand(path);
61*6f2e92c1SAlexandre Ganea   if (!lldInvoke(expandedPath.data(), tempHsacoFilename.c_str())) {
62*6f2e92c1SAlexandre Ganea     llvm::errs() << "Failed to link: " << expandedPath << "\n";
63*6f2e92c1SAlexandre Ganea     return false;
64*6f2e92c1SAlexandre Ganea   }
65*6f2e92c1SAlexandre Ganea   return true;
66*6f2e92c1SAlexandre Ganea }
67*6f2e92c1SAlexandre Ganea 
TEST(AsLib,ROCm)68*6f2e92c1SAlexandre Ganea TEST(AsLib, ROCm) {
69*6f2e92c1SAlexandre Ganea   EXPECT_TRUE(runLinker("%S/Inputs/kernel1.o"));
70*6f2e92c1SAlexandre Ganea   EXPECT_TRUE(runLinker("%S/Inputs/kernel2.o"));
71*6f2e92c1SAlexandre Ganea   EXPECT_TRUE(runLinker("%S/Inputs/kernel1.o"));
72*6f2e92c1SAlexandre Ganea   EXPECT_TRUE(runLinker("%S/Inputs/kernel2.o"));
73*6f2e92c1SAlexandre Ganea }
74*6f2e92c1SAlexandre Ganea #endif
75