forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllvm-pass-helpers.cpp
276 lines (241 loc) · 10 KB
/
llvm-pass-helpers.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// This file is a part of Julia. License is MIT: https://julialang.org/license
//
// This file implements common functionality that is useful for the late GC frame
// lowering and final GC intrinsic lowering passes. See the corresponding header
// for docs.
#include "llvm-version.h"
#include <llvm/IR/Function.h>
#include <llvm/IR/Metadata.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <iostream>
#include "codegen_shared.h"
#include "julia_assert.h"
#include "llvm-pass-helpers.h"
using namespace llvm;
extern std::pair<MDNode*,MDNode*> tbaa_make_child(const char *name, MDNode *parent=nullptr, bool isConstant=false);
JuliaPassContext::JuliaPassContext()
: T_size(nullptr), T_int8(nullptr), T_int32(nullptr),
T_pint8(nullptr), T_jlvalue(nullptr), T_prjlvalue(nullptr),
T_ppjlvalue(nullptr), T_pjlvalue(nullptr), T_pjlvalue_der(nullptr),
T_ppjlvalue_der(nullptr), ptls_getter(nullptr), gc_flush_func(nullptr),
gc_preserve_begin_func(nullptr), gc_preserve_end_func(nullptr),
pointer_from_objref_func(nullptr), alloc_obj_func(nullptr),
typeof_func(nullptr), write_barrier_func(nullptr), module(nullptr)
{
tbaa_gcframe = tbaa_make_child("jtbaa_gcframe").first;
MDNode *tbaa_data;
MDNode *tbaa_data_scalar;
std::tie(tbaa_data, tbaa_data_scalar) = tbaa_make_child("jtbaa_data");
tbaa_tag = tbaa_make_child("jtbaa_tag", tbaa_data_scalar).first;
}
void JuliaPassContext::initFunctions(Module &M)
{
module = &M;
ptls_getter = M.getFunction("julia.ptls_states");
gc_flush_func = M.getFunction("julia.gcroot_flush");
gc_preserve_begin_func = M.getFunction("llvm.julia.gc_preserve_begin");
gc_preserve_end_func = M.getFunction("llvm.julia.gc_preserve_end");
pointer_from_objref_func = M.getFunction("julia.pointer_from_objref");
typeof_func = M.getFunction("julia.typeof");
write_barrier_func = M.getFunction("julia.write_barrier");
alloc_obj_func = M.getFunction("julia.gc_alloc_obj");
}
void JuliaPassContext::initAll(Module &M)
{
// First initialize the functions.
initFunctions(M);
// Then initialize types and metadata nodes.
auto &ctx = M.getContext();
T_size = M.getDataLayout().getIntPtrType(ctx);
T_int8 = Type::getInt8Ty(ctx);
T_pint8 = PointerType::get(T_int8, 0);
T_int32 = Type::getInt32Ty(ctx);
// Find 'jl_value_t' by searching through the module's
// identified struct types. This is a much more robust way
// to find 'jl_value_t' than an ad-hoc search through
// intrinsics that may or may not be defined in the module.
T_jlvalue = nullptr;
for (auto type : M.getIdentifiedStructTypes()) {
if (type->hasName() && type->getName() == "jl_value_t") {
T_jlvalue = type;
break;
}
}
// If 'jl_value_t' doesn't exist yet then we'll just define it.
if (!T_jlvalue) {
T_jlvalue = StructType::create(ctx, "jl_value_t");
}
// Construct derived types.
T_pjlvalue = PointerType::get(T_jlvalue, 0);
T_prjlvalue = PointerType::get(T_jlvalue, AddressSpace::Tracked);
T_ppjlvalue = PointerType::get(T_pjlvalue, 0);
T_pjlvalue_der = PointerType::get(T_jlvalue, AddressSpace::Derived);
T_ppjlvalue_der = PointerType::get(T_prjlvalue, AddressSpace::Derived);
}
llvm::CallInst *JuliaPassContext::getPtls(llvm::Function &F) const
{
for (auto I = F.getEntryBlock().begin(), E = F.getEntryBlock().end();
ptls_getter && I != E; ++I) {
if (CallInst *callInst = dyn_cast<CallInst>(&*I)) {
if (callInst->getCalledValue() == ptls_getter) {
return callInst;
}
}
}
return nullptr;
}
llvm::Function *JuliaPassContext::getOrNull(
const jl_intrinsics::IntrinsicDescription &desc) const
{
return module->getFunction(desc.name);
}
llvm::Function *JuliaPassContext::getOrDeclare(
const jl_intrinsics::IntrinsicDescription &desc)
{
auto local = getOrNull(desc);
if (local) {
// If the function exists already, then we'll
// just return it.
return local;
}
else {
// Otherwise, we'll declare it and add it to the module.
// Declare the function.
auto func = desc.declare(*this);
// Add it to the function list.
module->getFunctionList().push_back(func);
// Return the newly created function.
return func;
}
}
namespace jl_intrinsics {
static const char *GET_GC_FRAME_SLOT_NAME = "julia.get_gc_frame_slot";
static const char *GC_ALLOC_BYTES_NAME = "julia.gc_alloc_bytes";
static const char *NEW_GC_FRAME_NAME = "julia.new_gc_frame";
static const char *PUSH_GC_FRAME_NAME = "julia.push_gc_frame";
static const char *POP_GC_FRAME_NAME = "julia.pop_gc_frame";
static const char *QUEUE_GC_ROOT_NAME = "julia.queue_gc_root";
// Annotates a function with attributes suitable for GC allocation
// functions. Specifically, the return value is marked noalias and nonnull.
// The allocation size is set to the first argument.
static Function *addGCAllocAttributes(Function *target, LLVMContext &context)
{
target->addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
target->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
target->addFnAttr(Attribute::getWithAllocSizeArgs(context, 1, None)); // returns %1 bytes
return target;
}
const IntrinsicDescription getGCFrameSlot(
GET_GC_FRAME_SLOT_NAME,
[](const JuliaPassContext &context) {
return Function::Create(
FunctionType::get(
PointerType::get(context.T_prjlvalue, 0),
{PointerType::get(context.T_prjlvalue, 0), context.T_int32},
false),
Function::ExternalLinkage,
GET_GC_FRAME_SLOT_NAME);
});
const IntrinsicDescription GCAllocBytes(
GC_ALLOC_BYTES_NAME,
[](const JuliaPassContext &context) {
auto intrinsic = Function::Create(
FunctionType::get(
context.T_prjlvalue,
{ context.T_pint8, context.T_size },
false),
Function::ExternalLinkage,
GC_ALLOC_BYTES_NAME);
return addGCAllocAttributes(intrinsic, context.getLLVMContext());
});
const IntrinsicDescription newGCFrame(
NEW_GC_FRAME_NAME,
[](const JuliaPassContext &context) {
auto intrinsic = Function::Create(
FunctionType::get(PointerType::get(context.T_prjlvalue, 0), {context.T_int32}, false),
Function::ExternalLinkage,
NEW_GC_FRAME_NAME);
intrinsic->addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
intrinsic->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
return intrinsic;
});
const IntrinsicDescription pushGCFrame(
PUSH_GC_FRAME_NAME,
[](const JuliaPassContext &context) {
return Function::Create(
FunctionType::get(
Type::getVoidTy(context.getLLVMContext()),
{PointerType::get(context.T_prjlvalue, 0), context.T_int32},
false),
Function::ExternalLinkage,
PUSH_GC_FRAME_NAME);
});
const IntrinsicDescription popGCFrame(
POP_GC_FRAME_NAME,
[](const JuliaPassContext &context) {
return Function::Create(
FunctionType::get(
Type::getVoidTy(context.getLLVMContext()),
{PointerType::get(context.T_prjlvalue, 0)},
false),
Function::ExternalLinkage,
POP_GC_FRAME_NAME);
});
const IntrinsicDescription queueGCRoot(
QUEUE_GC_ROOT_NAME,
[](const JuliaPassContext &context) {
auto intrinsic = Function::Create(
FunctionType::get(
Type::getVoidTy(context.getLLVMContext()),
{ context.T_prjlvalue },
false),
Function::ExternalLinkage,
QUEUE_GC_ROOT_NAME);
intrinsic->addFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
return intrinsic;
});
}
namespace jl_well_known {
static const char *GC_BIG_ALLOC_NAME = "jl_gc_big_alloc";
static const char *GC_POOL_ALLOC_NAME = "jl_gc_pool_alloc";
static const char *GC_QUEUE_ROOT_NAME = "jl_gc_queue_root";
using jl_intrinsics::addGCAllocAttributes;
const WellKnownFunctionDescription GCBigAlloc(
GC_BIG_ALLOC_NAME,
[](const JuliaPassContext &context) {
auto bigAllocFunc = Function::Create(
FunctionType::get(
context.T_prjlvalue,
{ context.T_pint8, context.T_size },
false),
Function::ExternalLinkage,
GC_BIG_ALLOC_NAME);
return addGCAllocAttributes(bigAllocFunc, context.getLLVMContext());
});
const WellKnownFunctionDescription GCPoolAlloc(
GC_POOL_ALLOC_NAME,
[](const JuliaPassContext &context) {
auto poolAllocFunc = Function::Create(
FunctionType::get(
context.T_prjlvalue,
{ context.T_pint8, context.T_int32, context.T_int32 },
false),
Function::ExternalLinkage,
GC_POOL_ALLOC_NAME);
return addGCAllocAttributes(poolAllocFunc, context.getLLVMContext());
});
const WellKnownFunctionDescription GCQueueRoot(
GC_QUEUE_ROOT_NAME,
[](const JuliaPassContext &context) {
auto func = Function::Create(
FunctionType::get(
Type::getVoidTy(context.getLLVMContext()),
{ context.T_prjlvalue },
false),
Function::ExternalLinkage,
GC_QUEUE_ROOT_NAME);
func->addFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
return func;
});
}