-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcodegen_llvm.h
More file actions
435 lines (285 loc) · 12.9 KB
/
codegen_llvm.h
File metadata and controls
435 lines (285 loc) · 12.9 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// The LLVM backend for CPUs/NVPTX/AMDGPU
#pragma once
#include <set>
#include <unordered_map>
#ifdef TI_WITH_LLVM
#include "gstaichi/ir/ir.h"
#include "gstaichi/codegen/llvm/llvm_codegen_utils.h"
#include "gstaichi/codegen/llvm/llvm_compiled_data.h"
#include "gstaichi/program/program.h"
namespace gstaichi::lang {
class TaskCodeGenLLVM;
class FunctionCreationGuard {
public:
TaskCodeGenLLVM *mb;
llvm::Function *old_func;
llvm::Function *body;
llvm::BasicBlock *old_entry, *allocas, *entry, *old_final, *final;
llvm::IRBuilder<>::InsertPoint ip;
FunctionCreationGuard(TaskCodeGenLLVM *mb,
std::vector<llvm::Type *> arguments,
const std::string &func_name);
~FunctionCreationGuard();
};
class TaskCodeGenLLVM : public IRVisitor, public LLVMModuleBuilder {
public:
const CompileConfig &compile_config;
const Kernel *kernel;
IRNode *ir;
Program *prog;
std::string kernel_name;
std::vector<llvm::Value *> kernel_args;
llvm::Type *context_ty;
llvm::Type *physical_coordinate_ty;
llvm::Value *current_coordinates;
llvm::Value *parent_coordinates{nullptr};
llvm::Value *block_corner_coordinates{nullptr};
llvm::GlobalVariable *bls_buffer{nullptr};
// Mainly for supporting continue stmt
llvm::BasicBlock *current_loop_reentry;
// Mainly for supporting break stmt
llvm::BasicBlock *current_while_after_loop;
llvm::FunctionType *task_function_type;
std::unordered_map<Stmt *, llvm::Value *> llvm_val;
llvm::Function *func;
OffloadedStmt *current_offload{nullptr};
std::unique_ptr<OffloadedTask> current_task;
std::vector<OffloadedTask> offloaded_tasks;
llvm::BasicBlock *func_body_bb;
llvm::BasicBlock *final_block;
std::set<std::string> linked_modules;
bool returned{false};
std::unordered_set<int> used_tree_ids;
std::unordered_set<int> struct_for_tls_sizes;
const Callable *current_callable{nullptr};
// The task_codegen_id represents the id of the offloaded task
int task_codegen_id{0};
std::unordered_map<const Stmt *, std::vector<llvm::Value *>> loop_vars_llvm;
std::unordered_map<Function *, llvm::Function *> func_map;
using IRVisitor::visit;
using LLVMModuleBuilder::call;
explicit TaskCodeGenLLVM(int id,
const CompileConfig &config,
GsTaichiLLVMContext &tlctx,
const Kernel *kernel,
IRNode *ir,
std::unique_ptr<llvm::Module> &&module = nullptr);
Arch current_arch() const {
return compile_config.arch;
}
void initialize_context();
llvm::Value *get_arg(int i);
llvm::Value *get_struct_arg(const std::vector<int> &index, bool create_load);
llvm::Value *get_args_ptr(const Callable *callable, llvm::Value *context);
void set_args_ptr(Callable *callable, llvm::Value *context, llvm::Value *ptr);
llvm::Value *get_context();
llvm::Value *get_tls_base_ptr();
llvm::Type *get_tls_buffer_type();
std::vector<llvm::Type *> get_xlogue_argument_types();
std::vector<llvm::Type *> get_mesh_xlogue_argument_types();
llvm::Type *get_xlogue_function_type();
llvm::Type *get_mesh_xlogue_function_type();
llvm::IntegerType *get_integer_type(int bits);
llvm::Value *get_root(int snode_tree_id);
llvm::Value *get_runtime();
void emit_struct_meta_base(const std::string &name,
llvm::Value *node_meta,
SNode *snode);
void create_elementwise_binary(
BinaryOpStmt *stmt,
std::function<llvm::Value *(llvm::Value *lhs, llvm::Value *rhs)> f);
void create_elementwise_cast(
UnaryOpStmt *stmt,
llvm::Type *to_ty,
std::function<llvm::Value *(llvm::Value *, llvm::Type *)> f,
bool on_self = false);
std::unique_ptr<RuntimeObject> emit_struct_meta_object(SNode *snode);
llvm::Value *emit_struct_meta(SNode *snode);
virtual void emit_to_module();
void eliminate_unused_functions();
/**
* @brief Runs the codegen and produces the compiled result.
*
* After this call, `module` and `tasks` will be moved.
*
* @return LLVMCompiledTask
*/
virtual LLVMCompiledTask run_compilation();
// For debugging only
virtual llvm::Value *create_print(std::string tag,
DataType dt,
llvm::Value *value);
llvm::Value *create_print(std::string tag, llvm::Value *value);
void set_struct_to_buffer(const StructType *struct_type,
llvm::Value *buffer,
const std::vector<Stmt *> &elements);
llvm::Value *cast_pointer(llvm::Value *val,
std::string dest_ty_name,
int addr_space = 0);
void emit_list_gen(OffloadedStmt *listgen);
void emit_gc(OffloadedStmt *stmt);
llvm::Value *call(SNode *snode,
llvm::Value *node_ptr,
const std::string &method,
const std::vector<llvm::Value *> &arguments);
llvm::Function *get_struct_function(const std::string &name, int tree_id);
template <typename... Args>
llvm::Value *call_struct_func(int tree_id,
const std::string &func_name,
Args &&...args);
void create_increment(llvm::Value *ptr, llvm::Value *value);
// Direct translation
void create_naive_range_for(RangeForStmt *for_stmt);
static std::string get_runtime_snode_name(SNode *snode);
void visit(Block *stmt_list) override;
void visit(AllocaStmt *stmt) override;
void visit(RandStmt *stmt) override;
virtual void emit_extra_unary(UnaryOpStmt *stmt);
void visit(DecorationStmt *stmt) override;
void visit(UnaryOpStmt *stmt) override;
void visit(BinaryOpStmt *stmt) override;
void visit(TernaryOpStmt *stmt) override;
void visit(IfStmt *if_stmt) override;
void visit(PrintStmt *stmt) override;
void visit(ConstStmt *stmt) override;
void visit(WhileControlStmt *stmt) override;
void visit(ContinueStmt *stmt) override;
void visit(WhileStmt *stmt) override;
void visit(RangeForStmt *for_stmt) override;
void visit(ArgLoadStmt *stmt) override;
void visit(ReturnStmt *stmt) override;
void visit(LocalLoadStmt *stmt) override;
void visit(LocalStoreStmt *stmt) override;
void visit(AssertStmt *stmt) override;
void visit(SNodeOpStmt *stmt) override;
llvm::Value *atomic_add_quant_fixed(llvm::Value *ptr,
llvm::Type *physical_type,
QuantFixedType *qfxt,
llvm::Value *value);
llvm::Value *atomic_add_quant_int(llvm::Value *ptr,
llvm::Type *physical_type,
QuantIntType *qit,
llvm::Value *value,
bool value_is_signed);
llvm::Value *to_quant_fixed(llvm::Value *real, QuantFixedType *qfxt);
virtual llvm::Value *optimized_reduction(AtomicOpStmt *stmt);
virtual llvm::Value *quant_type_atomic(AtomicOpStmt *stmt);
virtual llvm::Value *integral_type_atomic(AtomicOpStmt *stmt);
virtual llvm::Value *atomic_op_using_cas(
llvm::Value *output_address,
llvm::Value *val,
std::function<llvm::Value *(llvm::Value *, llvm::Value *)> op,
const DataType &type);
virtual llvm::Value *real_type_atomic(AtomicOpStmt *stmt);
void visit(AtomicOpStmt *stmt) override;
void visit(GlobalPtrStmt *stmt) override;
void visit(MatrixPtrStmt *stmt) override;
void store_quant_int(llvm::Value *ptr,
llvm::Type *physical_type,
QuantIntType *qit,
llvm::Value *value,
bool atomic);
void store_quant_fixed(llvm::Value *ptr,
llvm::Type *physical_type,
QuantFixedType *qfxt,
llvm::Value *value,
bool atomic);
void store_masked(llvm::Value *ptr,
llvm::Type *ty,
uint64 mask,
llvm::Value *value,
bool atomic);
void visit(GlobalStoreStmt *stmt) override;
llvm::Value *quant_int_or_quant_fixed_to_bits(llvm::Value *val,
Type *input_type,
llvm::Type *output_type);
void visit(BitStructStoreStmt *stmt) override;
void store_quant_floats_with_shared_exponents(BitStructStoreStmt *stmt);
llvm::Value *extract_quant_float(llvm::Value *physical_value,
BitStructType *bit_struct,
int digits_id);
llvm::Value *extract_quant_int(llvm::Value *physical_value,
llvm::Value *bit_offset,
QuantIntType *qit);
llvm::Value *reconstruct_quant_fixed(llvm::Value *digits,
QuantFixedType *qfxt);
llvm::Value *reconstruct_quant_float(llvm::Value *input_digits,
llvm::Value *input_exponent_val,
QuantFloatType *qflt,
bool shared_exponent);
virtual llvm::Value *create_intrinsic_load(llvm::Value *ptr, llvm::Type *ty);
void create_global_load(GlobalLoadStmt *stmt, bool should_cache_as_read_only);
void visit(GlobalLoadStmt *stmt) override;
void visit(GetRootStmt *stmt) override;
void visit(LinearizeStmt *stmt) override;
void visit(IntegerOffsetStmt *stmt) override;
llvm::Value *create_bit_ptr(llvm::Value *byte_ptr, llvm::Value *bit_offset);
std::tuple<llvm::Value *, llvm::Value *> load_bit_ptr(llvm::Value *bit_ptr);
void visit(SNodeLookupStmt *stmt) override;
void visit(GetChStmt *stmt) override;
void visit(ExternalPtrStmt *stmt) override;
void visit(ExternalTensorShapeAlongAxisStmt *stmt) override;
void visit(ExternalTensorBasePtrStmt *stmt) override;
virtual bool kernel_argument_by_val() const {
return false; // on CPU devices just pass in a pointer
}
std::string init_offloaded_task_function(OffloadedStmt *stmt,
std::string suffix = "");
void finalize_offloaded_task_function();
FunctionCreationGuard get_function_creation_guard(
std::vector<llvm::Type *> argument_types,
const std::string &func_name = "function_body");
std::tuple<llvm::Value *, llvm::Value *> get_range_for_bounds(
OffloadedStmt *stmt);
virtual void create_offload_range_for(OffloadedStmt *stmt) = 0;
virtual void create_offload_mesh_for(OffloadedStmt *stmt) {
TI_NOT_IMPLEMENTED;
}
void create_offload_struct_for(OffloadedStmt *stmt);
void visit(LoopIndexStmt *stmt) override;
void visit(LoopLinearIndexStmt *stmt) override;
void visit(BlockCornerIndexStmt *stmt) override;
void visit(GlobalTemporaryStmt *stmt) override;
void visit(ThreadLocalPtrStmt *stmt) override;
void visit(BlockLocalPtrStmt *stmt) override;
void visit(ClearListStmt *stmt) override;
void visit(InternalFuncStmt *stmt) override;
// Stack statements
void visit(AdStackAllocaStmt *stmt) override;
void visit(AdStackPopStmt *stmt) override;
void visit(AdStackPushStmt *stmt) override;
void visit(AdStackLoadTopStmt *stmt) override;
void visit(AdStackLoadTopAdjStmt *stmt) override;
void visit(AdStackAccAdjointStmt *stmt) override;
void visit(RangeAssumptionStmt *stmt) override;
void visit(LoopUniqueStmt *stmt) override;
void visit_call_bitcode(ExternalFuncCallStmt *stmt);
void visit_call_shared_object(ExternalFuncCallStmt *stmt);
void visit(ExternalFuncCallStmt *stmt) override;
void visit(MeshPatchIndexStmt *stmt) override;
void visit(ReferenceStmt *stmt) override;
void visit(MatrixInitStmt *stmt) override;
llvm::Value *create_xlogue(std::unique_ptr<Block> &block);
llvm::Value *create_mesh_xlogue(std::unique_ptr<Block> &block);
llvm::Value *extract_exponent_from_f32(llvm::Value *f);
llvm::Value *extract_digits_from_f32(llvm::Value *f, bool full);
llvm::Value *extract_digits_from_f32_with_shared_exponent(
llvm::Value *f,
llvm::Value *shared_exp);
llvm::Value *get_exponent_offset(llvm::Value *exponent, QuantFloatType *qflt);
void visit(FuncCallStmt *stmt) override;
void visit(GetElementStmt *stmt) override;
llvm::Value *bitcast_from_u64(llvm::Value *val, DataType type);
llvm::Value *bitcast_to_u64(llvm::Value *val, DataType type);
~TaskCodeGenLLVM() override = default;
private:
void set_struct_to_buffer(llvm::Value *buffer,
llvm::Type *buffer_type,
const std::vector<Stmt *> &elements,
const Type *current_type,
int ¤t_element,
std::vector<llvm::Value *> ¤t_index);
virtual std::tuple<llvm::Value *, llvm::Value *> get_spmd_info() = 0;
};
} // namespace gstaichi::lang
#endif // #ifdef TI_WITH_LLVM