forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
llvm-ptls.cpp
218 lines (197 loc) · 7.37 KB
/
llvm-ptls.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// This file is a part of Julia. License is MIT: http://julialang.org/license
#define DEBUG_TYPE "lower_ptls"
#undef DEBUG
// LLVM pass to optimize TLS access and remove references to julia intrinsics
#include "llvm-version.h"
#include "support/dtypes.h"
#include <sstream>
#include <llvm/Pass.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/LLVMContext.h>
#include "julia.h"
#include "julia_internal.h"
#if defined(LLVM37) && defined(JULIA_ENABLE_THREADING)
# include <llvm/IR/InlineAsm.h>
#endif
using namespace llvm;
namespace {
struct LowerPTLS: public ModulePass {
static char ID;
LowerPTLS(bool _imaging_mode=false, MDNode *_tbaa_const=nullptr)
: ModulePass(ID),
imaging_mode(_imaging_mode),
tbaa_const(_tbaa_const)
{}
private:
bool imaging_mode;
MDNode *tbaa_const; // One `LLVMContext` only
bool runOnModule(Module &M) override;
void runOnFunction(LLVMContext &ctx, Module &M, Function *F,
Function *ptls_getter, Type *T_ppjlvalue);
};
static void ensure_global(const char *name, Type *t, Module &M,
bool dllimport=false)
{
if (M.getNamedValue(name))
return;
GlobalVariable *proto = new GlobalVariable(M, t, false,
GlobalVariable::ExternalLinkage,
NULL, name);
#ifdef _OS_WINDOWS_
// setting JL_DLLEXPORT correctly only matters when building a binary
// (global_proto will strip this from the JIT)
if (dllimport) {
#ifdef LLVM35
// add the __declspec(dllimport) attribute
proto->setDLLStorageClass(GlobalValue::DLLImportStorageClass);
#else
proto->setLinkage(GlobalValue::DLLImportLinkage);
#endif
}
#else // _OS_WINDOWS_
(void)proto;
#endif // _OS_WINDOWS_
}
void LowerPTLS::runOnFunction(LLVMContext &ctx, Module &M, Function *F,
Function *ptls_getter, Type *T_ppjlvalue)
{
CallInst *ptlsStates = NULL;
for (auto I = F->getEntryBlock().begin(), E = F->getEntryBlock().end();
I != E; ++I) {
if (CallInst *callInst = dyn_cast<CallInst>(&*I)) {
if (callInst->getCalledValue() == ptls_getter) {
ptlsStates = callInst;
break;
}
}
}
if (!ptlsStates)
return;
if (ptlsStates->use_empty()) {
ptlsStates->eraseFromParent();
return;
}
#ifdef JULIA_ENABLE_THREADING
if (imaging_mode) {
GlobalVariable *GV = cast<GlobalVariable>(
M.getNamedValue("jl_get_ptls_states.ptr"));
LoadInst *getter = new LoadInst(GV, "", ptlsStates);
getter->setMetadata(llvm::LLVMContext::MD_tbaa, tbaa_const);
ptlsStates->setCalledFunction(getter);
ptlsStates->addAttribute(AttributeSet::FunctionIndex,
Attribute::ReadNone);
ptlsStates->addAttribute(AttributeSet::FunctionIndex,
Attribute::NoUnwind);
}
#ifdef LLVM37
else if (jl_tls_offset != -1) {
auto T_int8 = Type::getInt8Ty(ctx);
auto T_pint8 = PointerType::get(T_int8, 0);
// Replace the function call with inline assembly if we know
// how to generate it.
# if defined(_CPU_X86_64_) || defined(_CPU_X86_)
// Workaround LLVM bug by hiding the offset computation
// (and therefore the optimization opportunity) from LLVM.
static const std::string asm_str = [&] () {
std::stringstream stm;
# if defined(_CPU_X86_64_)
stm << "movq %fs:0, $0;\naddq $$" << jl_tls_offset << ", $0";
# else
stm << "movl %gs:0, $0;\naddl $$" << jl_tls_offset << ", $0";
# endif
return stm.str();
}();
// The add instruction clobbers flags
auto tp = InlineAsm::get(FunctionType::get(T_pint8, false),
asm_str.c_str(),
"=r,~{dirflag},~{fpsr},~{flags}", false);
Value *tls = CallInst::Create(tp, "ptls_i8", ptlsStates);
tls = new BitCastInst(tls, PointerType::get(T_ppjlvalue, 0),
"ptls", ptlsStates);
# elif defined(_CPU_AARCH64_)
// AArch64 doesn't seem to have this issue.
// (Possibly because there are many more registers and the offset is
// positive and small)
// It's also harder to emit the offset in a generic way on AArch64
// (need to generate one or two `add` with shift) so let llvm emit
// the add for now.
auto T_size = (sizeof(size_t) == 8 ? Type::getInt64Ty(ctx) :
Type::getInt32Ty(ctx));
const char *asm_str = "mrs $0, tpidr_el0";
auto offset = ConstantInt::getSigned(T_size, jl_tls_offset);
auto tp = InlineAsm::get(FunctionType::get(T_pint8, false),
asm_str, "=r", false);
Value *tls = CallInst::Create(tp, "thread_ptr", ptlsStates);
tls = GetElementPtrInst::Create(T_int8, tls, {offset},
"ptls_i8", ptlsStates);
tls = new BitCastInst(tls, PointerType::get(T_ppjlvalue, 0),
"ptls", ptlsStates);
# else
Value *tls = nullptr;
assert(0 && "Cannot emit thread pointer for this architecture.");
# endif
(void)T_pint8;
ptlsStates->replaceAllUsesWith(tls);
ptlsStates->eraseFromParent();
}
#endif
else {
ptlsStates->addAttribute(AttributeSet::FunctionIndex,
Attribute::ReadNone);
ptlsStates->addAttribute(AttributeSet::FunctionIndex,
Attribute::NoUnwind);
}
#else
ptlsStates->replaceAllUsesWith(M.getNamedValue("jl_tls_states"));
ptlsStates->eraseFromParent();
#endif
}
static void eraseFunction(Module &M, const char *name)
{
if (Function *f = M.getFunction(name)) {
f->eraseFromParent();
}
}
bool LowerPTLS::runOnModule(Module &M)
{
// Cleanup for GC frame lowering.
eraseFunction(M, "julia.gc_root_decl");
eraseFunction(M, "julia.gc_root_kill");
eraseFunction(M, "julia.jlcall_frame_decl");
eraseFunction(M, "julia.gcroot_flush");
Function *ptls_getter = M.getFunction("jl_get_ptls_states");
if (!ptls_getter)
return true;
LLVMContext &ctx = M.getContext();
FunctionType *functype = ptls_getter->getFunctionType();
auto T_ppjlvalue =
cast<PointerType>(functype->getReturnType())->getElementType();
#ifdef JULIA_ENABLE_THREADING
if (imaging_mode)
ensure_global("jl_get_ptls_states.ptr", functype->getPointerTo(), M);
#else
ensure_global("jl_tls_states", T_ppjlvalue, M, imaging_mode);
#endif
for (auto F = M.begin(), E = M.end(); F != E; ++F) {
if (F->isDeclaration())
continue;
runOnFunction(ctx, M, &*F, ptls_getter, T_ppjlvalue);
}
#ifndef JULIA_ENABLE_THREADING
ptls_getter->eraseFromParent();
#endif
return true;
}
char LowerPTLS::ID = 0;
static RegisterPass<LowerPTLS> X("LowerPTLS", "LowerPTLS Pass",
false /* Only looks at CFG */,
false /* Analysis Pass */);
} // anonymous namespace
Pass *createLowerPTLSPass(bool imaging_mode, MDNode *tbaa_const)
{
assert(tbaa_const);
return new LowerPTLS(imaging_mode, tbaa_const);
}