AMDGPU: Mark test as XFAIL in expensive_checks builds
[llvm-project.git] / llvm / lib / ExecutionEngine / Orc / LinkGraphLayer.cpp
blob5789079c2bf6caca7640636451582872dee475dd
1 //===----- LinkGraphLayer.cpp - Add LinkGraphs to an ExecutionSession -----===//
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/LinkGraphLayer.h"
11 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
12 #include "llvm/ExecutionEngine/Orc/Shared/MachOObjectFormat.h"
13 #include "llvm/ExecutionEngine/Orc/Shared/ObjectFormats.h"
15 #define DEBUG_TYPE "orc"
17 using namespace llvm;
18 using namespace llvm::jitlink;
19 using namespace llvm::orc;
21 namespace {
23 bool hasInitializerSection(LinkGraph &G) {
24 bool IsMachO = G.getTargetTriple().isOSBinFormatMachO();
25 bool IsElf = G.getTargetTriple().isOSBinFormatELF();
26 if (!IsMachO && !IsElf)
27 return false;
29 for (auto &Sec : G.sections()) {
30 if (IsMachO && isMachOInitializerSection(Sec.getName()))
31 return true;
32 if (IsElf && isELFInitializerSection(Sec.getName()))
33 return true;
36 return false;
39 } // end anonymous namespace
41 namespace llvm::orc {
43 LinkGraphLayer::~LinkGraphLayer() = default;
45 MaterializationUnit::Interface LinkGraphLayer::getInterface(LinkGraph &G) {
47 MaterializationUnit::Interface LGI;
49 auto AddSymbol = [&](Symbol *Sym) {
50 // Skip local symbols.
51 if (Sym->getScope() == Scope::Local)
52 return;
53 assert(Sym->hasName() && "Anonymous non-local symbol?");
55 LGI.SymbolFlags[Sym->getName()] = getJITSymbolFlagsForSymbol(*Sym);
58 for (auto *Sym : G.defined_symbols())
59 AddSymbol(Sym);
60 for (auto *Sym : G.absolute_symbols())
61 AddSymbol(Sym);
63 if (hasInitializerSection(G)) {
64 std::string InitSymString;
66 raw_string_ostream(InitSymString)
67 << "$." << G.getName() << ".__inits" << Counter++;
69 LGI.InitSymbol = ES.intern(InitSymString);
72 return LGI;
75 JITSymbolFlags LinkGraphLayer::getJITSymbolFlagsForSymbol(Symbol &Sym) {
76 JITSymbolFlags Flags;
78 if (Sym.getLinkage() == Linkage::Weak)
79 Flags |= JITSymbolFlags::Weak;
81 if (Sym.getScope() == Scope::Default)
82 Flags |= JITSymbolFlags::Exported;
83 else if (Sym.getScope() == Scope::SideEffectsOnly)
84 Flags |= JITSymbolFlags::MaterializationSideEffectsOnly;
86 if (Sym.isCallable())
87 Flags |= JITSymbolFlags::Callable;
89 return Flags;
92 StringRef LinkGraphMaterializationUnit::getName() const { return G->getName(); }
94 void LinkGraphMaterializationUnit::discard(const JITDylib &JD,
95 const SymbolStringPtr &Name) {
96 for (auto *Sym : G->defined_symbols())
97 if (Sym->getName() == Name) {
98 assert(Sym->getLinkage() == Linkage::Weak &&
99 "Discarding non-weak definition");
100 G->makeExternal(*Sym);
101 break;
105 } // namespace llvm::orc