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