AMDGPU: Mark test as XFAIL in expensive_checks builds
[llvm-project.git] / llvm / lib / ExecutionEngine / Orc / EPCDynamicLibrarySearchGenerator.cpp
blob2a93fcbf6c8c830c9b41cd82a29a7756a0939fb3
1 //===---------------- EPCDynamicLibrarySearchGenerator.cpp ----------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
11 #include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
12 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
13 #include "llvm/Support/Error.h"
15 #define DEBUG_TYPE "orc"
17 namespace llvm {
18 namespace orc {
20 Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
21 EPCDynamicLibrarySearchGenerator::Load(
22 ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow,
23 AddAbsoluteSymbolsFn AddAbsoluteSymbols) {
24 auto Handle =
25 ES.getExecutorProcessControl().getDylibMgr().loadDylib(LibraryPath);
26 if (!Handle)
27 return Handle.takeError();
29 return std::make_unique<EPCDynamicLibrarySearchGenerator>(
30 ES, *Handle, std::move(Allow), std::move(AddAbsoluteSymbols));
33 Error EPCDynamicLibrarySearchGenerator::tryToGenerate(
34 LookupState &LS, LookupKind K, JITDylib &JD,
35 JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
37 if (Symbols.empty())
38 return Error::success();
40 LLVM_DEBUG({
41 dbgs() << "EPCDynamicLibrarySearchGenerator trying to generate "
42 << Symbols << "\n";
43 });
45 SymbolLookupSet LookupSymbols;
47 for (auto &KV : Symbols) {
48 // Skip symbols that don't match the filter.
49 if (Allow && !Allow(KV.first))
50 continue;
51 LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
54 DylibManager::LookupRequest Request(H, LookupSymbols);
55 // Copy-capture LookupSymbols, since LookupRequest keeps a reference.
56 EPC.getDylibMgr().lookupSymbolsAsync(Request, [this, &JD, LS = std::move(LS),
57 LookupSymbols](
58 auto Result) mutable {
59 if (!Result) {
60 LLVM_DEBUG({
61 dbgs() << "EPCDynamicLibrarySearchGenerator lookup failed due to error";
62 });
63 return LS.continueLookup(Result.takeError());
66 assert(Result->size() == 1 && "Results for more than one library returned");
67 assert(Result->front().size() == LookupSymbols.size() &&
68 "Result has incorrect number of elements");
70 SymbolMap NewSymbols;
71 auto ResultI = Result->front().begin();
72 for (auto &KV : LookupSymbols) {
73 if (ResultI->getAddress())
74 NewSymbols[KV.first] = *ResultI;
75 ++ResultI;
78 LLVM_DEBUG({
79 dbgs() << "EPCDynamicLibrarySearchGenerator lookup returned "
80 << NewSymbols << "\n";
81 });
83 // If there were no resolved symbols bail out.
84 if (NewSymbols.empty())
85 return LS.continueLookup(Error::success());
87 // Define resolved symbols.
88 Error Err = AddAbsoluteSymbols
89 ? AddAbsoluteSymbols(JD, std::move(NewSymbols))
90 : JD.define(absoluteSymbols(std::move(NewSymbols)));
92 LS.continueLookup(std::move(Err));
93 });
95 return Error::success();
98 } // end namespace orc
99 } // end namespace llvm