1 /*-------------------------------------------------------------------------
2 *
3 * llvmjit.c
4 * Core part of the LLVM JIT provider.
5 *
6 * Copyright (c) 2016-2019, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/backend/jit/llvm/llvmjit.c
10 *
11 *-------------------------------------------------------------------------
12 */
13
14 #include "postgres.h"
15
16 #include "jit/llvmjit.h"
17 #include "jit/llvmjit_emit.h"
18
19 #include "miscadmin.h"
20
21 #include "utils/memutils.h"
22 #include "utils/resowner_private.h"
23 #include "portability/instr_time.h"
24 #include "storage/ipc.h"
25
26
27 #include <llvm-c/Analysis.h>
28 #include <llvm-c/BitReader.h>
29 #include <llvm-c/BitWriter.h>
30 #include <llvm-c/Core.h>
31 #include <llvm-c/ExecutionEngine.h>
32 #if LLVM_VERSION_MAJOR > 11
33 #include <llvm-c/Orc.h>
34 #include <llvm-c/OrcEE.h>
35 #include <llvm-c/LLJIT.h>
36 #else
37 #include <llvm-c/OrcBindings.h>
38 #endif
39 #include <llvm-c/Support.h>
40 #include <llvm-c/Target.h>
41 #include <llvm-c/Transforms/IPO.h>
42 #include <llvm-c/Transforms/PassManagerBuilder.h>
43 #include <llvm-c/Transforms/Scalar.h>
44 #if LLVM_VERSION_MAJOR > 6
45 #include <llvm-c/Transforms/Utils.h>
~InstrumentParameterPanel()46 #endif
47
48
49 /* Handle of a module emitted via ORC JIT */
50 typedef struct LLVMJitHandle
51 {
52 #if LLVM_VERSION_MAJOR > 11
53 LLVMOrcLLJITRef lljit;
54 LLVMOrcResourceTrackerRef resource_tracker;
55 #else
56 LLVMOrcJITStackRef stack;
57 LLVMOrcModuleHandle orc_handle;
58 #endif
59 } LLVMJitHandle;
60
61
62 /* types & functions commonly needed for JITing */
63 LLVMTypeRef TypeSizeT;
64 LLVMTypeRef TypeParamBool;
65 LLVMTypeRef TypeStorageBool;
66 LLVMTypeRef TypePGFunction;
67 LLVMTypeRef StructNullableDatum;
68 LLVMTypeRef StructHeapTupleFieldsField3;
69 LLVMTypeRef StructHeapTupleFields;
70 LLVMTypeRef StructHeapTupleHeaderData;
71 LLVMTypeRef StructHeapTupleDataChoice;
72 LLVMTypeRef StructHeapTupleData;
73 LLVMTypeRef StructMinimalTupleData;
74 LLVMTypeRef StructItemPointerData;
75 LLVMTypeRef StructBlockId;
76 LLVMTypeRef StructFormPgAttribute;
77 LLVMTypeRef StructTupleConstr;
78 LLVMTypeRef StructTupleDescData;
79 LLVMTypeRef StructTupleTableSlot;
80 LLVMTypeRef StructHeapTupleTableSlot;
81 LLVMTypeRef StructMinimalTupleTableSlot;
82 LLVMTypeRef StructMemoryContextData;
83 LLVMTypeRef StructPGFinfoRecord;
84 LLVMTypeRef StructFmgrInfo;
85 LLVMTypeRef StructFunctionCallInfoData;
86 LLVMTypeRef StructExprContext;
87 LLVMTypeRef StructExprEvalStep;
88 LLVMTypeRef StructExprState;
89 LLVMTypeRef StructAggState;
90 LLVMTypeRef StructAggStatePerGroupData;
91 LLVMTypeRef StructAggStatePerTransData;
92
93 LLVMValueRef AttributeTemplate;
94 LLVMValueRef FuncStrlen;
95 LLVMValueRef FuncVarsizeAny;
96 LLVMValueRef FuncSlotGetsomeattrsInt;
97 LLVMValueRef FuncSlotGetmissingattrs;
98 LLVMValueRef FuncMakeExpandedObjectReadOnlyInternal;
99 LLVMValueRef FuncExecEvalSubscriptingRef;
100 LLVMValueRef FuncExecEvalSysVar;
101 LLVMValueRef FuncExecAggTransReparent;
102 LLVMValueRef FuncExecAggInitGroup;
103
104
105 static bool llvm_session_initialized = false;
106 static size_t llvm_generation = 0;
107 static const char *llvm_triple = NULL;
108 static const char *llvm_layout = NULL;
109
110
111 static LLVMTargetRef llvm_targetref;
112 #if LLVM_VERSION_MAJOR > 11
113 static LLVMOrcThreadSafeContextRef llvm_ts_context;
114 static LLVMOrcLLJITRef llvm_opt0_orc;
115 static LLVMOrcLLJITRef llvm_opt3_orc;
116 #else /* LLVM_VERSION_MAJOR > 11 */
117 static LLVMOrcJITStackRef llvm_opt0_orc;
118 static LLVMOrcJITStackRef llvm_opt3_orc;
119 #endif /* LLVM_VERSION_MAJOR > 11 */
120
121
122 static void llvm_release_context(JitContext *context);
123 static void llvm_session_initialize(void);
124 static void llvm_shutdown(int code, Datum arg);
125 static void llvm_compile_module(LLVMJitContext *context);
126 static void llvm_optimize_module(LLVMJitContext *context, LLVMModuleRef module);
127
128 static void llvm_create_types(void);
129 static uint64_t llvm_resolve_symbol(const char *name, void *ctx);
130
131 #if LLVM_VERSION_MAJOR > 11
132 static LLVMOrcLLJITRef llvm_create_jit_instance(LLVMTargetMachineRef tm);
133 static char *llvm_error_message(LLVMErrorRef error);
134 #endif /* LLVM_VERSION_MAJOR > 11 */
135
136 PG_MODULE_MAGIC;
137
138
139 /*
140 * Initialize LLVM JIT provider.
141 */
142 void
143 _PG_jit_provider_init(JitProviderCallbacks *cb)
144 {
145 cb->reset_after_error = llvm_reset_after_error;
146 cb->release_context = llvm_release_context;
147 cb->compile_expr = llvm_compile_expr;
148 }
149
150 /*
151 * Create a context for JITing work.
152 *
153 * The context, including subsidiary resources, will be cleaned up either when
154 * the context is explicitly released, or when the lifetime of
155 * CurrentResourceOwner ends (usually the end of the current [sub]xact).
156 */
157 LLVMJitContext *
158 llvm_create_context(int jitFlags)
159 {
160 LLVMJitContext *context;
161
162 llvm_assert_in_fatal_section();
163
164 llvm_session_initialize();
165
166 ResourceOwnerEnlargeJIT(CurrentResourceOwner);
167
168 context = MemoryContextAllocZero(TopMemoryContext,
169 sizeof(LLVMJitContext));
170 context->base.flags = jitFlags;
171
172 /* ensure cleanup */
173 context->base.resowner = CurrentResourceOwner;
174 ResourceOwnerRememberJIT(CurrentResourceOwner, PointerGetDatum(context));
175
176 return context;
177 }
178
179 /*
180 * Release resources required by one llvm context.
181 */
182 static void
183 llvm_release_context(JitContext *context)
184 {
185 LLVMJitContext *llvm_context = (LLVMJitContext *) context;
186
187 /*
188 * When this backend is exiting, don't clean up LLVM. As an error might
189 * have occurred from within LLVM, we do not want to risk reentering. All
190 * resource cleanup is going to happen through process exit.
191 */
192 if (proc_exit_inprogress)
193 return;
194
195 llvm_enter_fatal_on_oom();
196
197 if (llvm_context->module)
198 {
199 LLVMDisposeModule(llvm_context->module);
200 llvm_context->module = NULL;
201 }
202
203 while (llvm_context->handles != NIL)
204 {
205 LLVMJitHandle *jit_handle;
206
207 jit_handle = (LLVMJitHandle *) linitial(llvm_context->handles);
208 llvm_context->handles = list_delete_first(llvm_context->handles);
209
210 #if LLVM_VERSION_MAJOR > 11
211 {
212 LLVMOrcExecutionSessionRef ee;
213 LLVMOrcSymbolStringPoolRef sp;
214
215 LLVMOrcResourceTrackerRemove(jit_handle->resource_tracker);
216 LLVMOrcReleaseResourceTracker(jit_handle->resource_tracker);
217
218 /*
219 * Without triggering cleanup of the string pool, we'd leak
220 * memory. It'd be sufficient to do this far less often, but in
221 * experiments the required time was small enough to just always
222 * do it.
223 */
224 ee = LLVMOrcLLJITGetExecutionSession(jit_handle->lljit);
225 sp = LLVMOrcExecutionSessionGetSymbolStringPool(ee);
226 LLVMOrcSymbolStringPoolClearDeadEntries(sp);
227 }
228 #else /* LLVM_VERSION_MAJOR > 11 */
229 {
230 LLVMOrcRemoveModule(jit_handle->stack, jit_handle->orc_handle);
231 }
232 #endif /* LLVM_VERSION_MAJOR > 11 */
233
234 pfree(jit_handle);
235 }
236 }
237
238 /*
239 * Return module which may be modified, e.g. by creating new functions.
240 */
241 LLVMModuleRef
242 llvm_mutable_module(LLVMJitContext *context)
243 {
244 llvm_assert_in_fatal_section();
245
246 /*
247 * If there's no in-progress module, create a new one.
248 */
249 if (!context->module)
250 {
251 context->compiled = false;
252 context->module_generation = llvm_generation++;
253 context->module = LLVMModuleCreateWithName("pg");
254 LLVMSetTarget(context->module, llvm_triple);
255 LLVMSetDataLayout(context->module, llvm_layout);
256 }
257
258 return context->module;
259 }
260
261 /*
262 * Expand function name to be non-conflicting. This should be used by code
263 * generating code, when adding new externally visible function definitions to
264 * a Module.
265 */
266 char *
267 llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
268 {
269 Assert(context->module != NULL);
270
271 context->base.instr.created_functions++;
272
273 /*
274 * Previously we used dots to separate, but turns out some tools, e.g.
275 * GDB, don't like that and truncate name.
276 */
277 return psprintf("%s_%zu_%d",
278 basename,
279 context->module_generation,
280 context->counter++);
281 }
282
283 /*
284 * Return pointer to function funcname, which has to exist. If there's pending
285 * code to be optimized and emitted, do so first.
286 */
287 void *
288 llvm_get_function(LLVMJitContext *context, const char *funcname)
289 {
290 #if LLVM_VERSION_MAJOR > 11 || \
291 defined(HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN) && HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN
292 ListCell *lc;
293 #endif
294
295 llvm_assert_in_fatal_section();
296
297 /*
298 * If there is a pending / not emitted module, compile and emit now.
299 * Otherwise we might not find the [correct] function.
300 */
301 if (!context->compiled)
302 {
303 llvm_compile_module(context);
304 }
305
306 /*
307 * ORC's symbol table is of *unmangled* symbols. Therefore we don't need
308 * to mangle here.
309 */
310
311 #if LLVM_VERSION_MAJOR > 11
312 foreach(lc, context->handles)
313 {
314 LLVMJitHandle *handle = (LLVMJitHandle *) lfirst(lc);
315 instr_time starttime;
316 instr_time endtime;
317 LLVMErrorRef error;
318 LLVMOrcJITTargetAddress addr;
319
320 INSTR_TIME_SET_CURRENT(starttime);
321
322 addr = 0;
323 error = LLVMOrcLLJITLookup(handle->lljit, &addr, funcname);
324 if (error)
325 elog(ERROR, "failed to look up symbol \"%s\": %s",
326 funcname, llvm_error_message(error));
327
328 /*
329 * LLJIT only actually emits code the first time a symbol is
330 * referenced. Thus add lookup time to emission time. That's counting
331 * a bit more than with older LLVM versions, but unlikely to ever
332 * matter.
333 */
334 INSTR_TIME_SET_CURRENT(endtime);
335 INSTR_TIME_ACCUM_DIFF(context->base.instr.emission_counter,
336 endtime, starttime);
337
338 if (addr)
339 return (void *) (uintptr_t) addr;
340 }
341 #elif defined(HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN) && HAVE_DECL_LLVMORCGETSYMBOLADDRESSIN
342 foreach(lc, context->handles)
343 {
344 LLVMOrcTargetAddress addr;
345 LLVMJitHandle *handle = (LLVMJitHandle *) lfirst(lc);
346
347 addr = 0;
348 if (LLVMOrcGetSymbolAddressIn(handle->stack, &addr, handle->orc_handle, funcname))
349 elog(ERROR, "failed to look up symbol \"%s\"", funcname);
350 if (addr)
351 return (void *) (uintptr_t) addr;
352 }
353 #elif LLVM_VERSION_MAJOR < 5
354 {
355 LLVMOrcTargetAddress addr;
356
357 if ((addr = LLVMOrcGetSymbolAddress(llvm_opt0_orc, funcname)))
358 return (void *) (uintptr_t) addr;
359 if ((addr = LLVMOrcGetSymbolAddress(llvm_opt3_orc, funcname)))
360 return (void *) (uintptr_t) addr;
361 }
362 #else
363 {
364 LLVMOrcTargetAddress addr;
365
366 if (LLVMOrcGetSymbolAddress(llvm_opt0_orc, &addr, funcname))
367 elog(ERROR, "failed to look up symbol \"%s\"", funcname);
368 if (addr)
369 return (void *) (uintptr_t) addr;
370 if (LLVMOrcGetSymbolAddress(llvm_opt3_orc, &addr, funcname))
371 elog(ERROR, "failed to look up symbol \"%s\"", funcname);
372 if (addr)
373 return (void *) (uintptr_t) addr;
374 }
375 #endif
376
377 elog(ERROR, "failed to JIT: %s", funcname);
378
379 return NULL;
380 }
381
382 /*
383 * Return declaration for passed function, adding it to the module if
384 * necessary.
385 *
386 * This is used to make functions imported by llvm_create_types() known to the
387 * module that's currently being worked on.
388 */
389 LLVMValueRef
390 llvm_get_decl(LLVMModuleRef mod, LLVMValueRef v_src)
391 {
392 LLVMValueRef v_fn;
393
394 /* don't repeatedly add function */
395 v_fn = LLVMGetNamedFunction(mod, LLVMGetValueName(v_src));
396 if (v_fn)
397 return v_fn;
398
399 v_fn = LLVMAddFunction(mod,
400 LLVMGetValueName(v_src),
401 LLVMGetElementType(LLVMTypeOf(v_src)));
402 llvm_copy_attributes(v_src, v_fn);
403
404 return v_fn;
405 }
406
407 /*
408 * Copy attributes from one function to another, for a specific index (an
409 * index can reference return value, function and parameter attributes).
410 */
411 static void
412 llvm_copy_attributes_at_index(LLVMValueRef v_from, LLVMValueRef v_to, uint32 index)
413 {
414 int num_attributes;
415 LLVMAttributeRef *attrs;
416
417 num_attributes = LLVMGetAttributeCountAtIndexPG(v_from, index);
418
419 /*
420 * Not just for efficiency: LLVM <= 3.9 crashes when
421 * LLVMGetAttributesAtIndex() is called for an index with 0 attributes.
422 */
423 if (num_attributes == 0)
424 return;
425
426 attrs = palloc(sizeof(LLVMAttributeRef) * num_attributes);
427 LLVMGetAttributesAtIndex(v_from, index, attrs);
428
429 for (int attno = 0; attno < num_attributes; attno++)
430 LLVMAddAttributeAtIndex(v_to, index, attrs[attno]);
431
432 pfree(attrs);
433 }
434
435 /*
436 * Copy all attributes from one function to another. I.e. function, return and
437 * parameters will be copied.
438 */
439 void
440 llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
441 {
442 uint32 param_count;
443
444 /* copy function attributes */
445 llvm_copy_attributes_at_index(v_from, v_to, LLVMAttributeFunctionIndex);
446
447 /* and the return value attributes */
448 llvm_copy_attributes_at_index(v_from, v_to, LLVMAttributeReturnIndex);
449
450 /* and each function parameter's attribute */
451 param_count = LLVMCountParams(v_from);
452
453 for (int paramidx = 1; paramidx <= param_count; paramidx++)
454 llvm_copy_attributes_at_index(v_from, v_to, paramidx);
455 }
456
457 /*
458 * Return a callable LLVMValueRef for fcinfo.
459 */
460 LLVMValueRef
461 llvm_function_reference(LLVMJitContext *context,
462 LLVMBuilderRef builder,
463 LLVMModuleRef mod,
464 FunctionCallInfo fcinfo)
465 {
466 char *modname;
467 char *basename;
468 char *funcname;
469
470 LLVMValueRef v_fn;
471
472 fmgr_symbol(fcinfo->flinfo->fn_oid, &modname, &basename);
473
474 if (modname != NULL && basename != NULL)
475 {
476 /* external function in loadable library */
477 funcname = psprintf("pgextern.%s.%s", modname, basename);
478 }
479 else if (basename != NULL)
480 {
481 /* internal function */
482 funcname = psprintf("%s", basename);
483 }
484 else
485 {
486 /*
487 * Function we don't know to handle, return pointer. We do so by
488 * creating a global constant containing a pointer to the function.
489 * Makes IR more readable.
490 */
491 LLVMValueRef v_fn_addr;
492
493 funcname = psprintf("pgoidextern.%u",
494 fcinfo->flinfo->fn_oid);
495 v_fn = LLVMGetNamedGlobal(mod, funcname);
496 if (v_fn != 0)
497 return LLVMBuildLoad(builder, v_fn, "");
498
499 v_fn_addr = l_ptr_const(fcinfo->flinfo->fn_addr, TypePGFunction);
500
501 v_fn = LLVMAddGlobal(mod, TypePGFunction, funcname);
502 LLVMSetInitializer(v_fn, v_fn_addr);
503 LLVMSetGlobalConstant(v_fn, true);
504 LLVMSetLinkage(v_fn, LLVMPrivateLinkage);
505 LLVMSetUnnamedAddr(v_fn, true);
506
507 return LLVMBuildLoad(builder, v_fn, "");
508 }
509
510 /* check if function already has been added */
511 v_fn = LLVMGetNamedFunction(mod, funcname);
512 if (v_fn != 0)
513 return v_fn;
514
515 v_fn = LLVMAddFunction(mod, funcname, LLVMGetElementType(TypePGFunction));
516
517 return v_fn;
518 }
519
520 /*
521 * Optimize code in module using the flags set in context.
522 */
523 static void
524 llvm_optimize_module(LLVMJitContext *context, LLVMModuleRef module)
525 {
526 LLVMPassManagerBuilderRef llvm_pmb;
527 LLVMPassManagerRef llvm_mpm;
528 LLVMPassManagerRef llvm_fpm;
529 LLVMValueRef func;
530 int compile_optlevel;
531
532 if (context->base.flags & PGJIT_OPT3)
533 compile_optlevel = 3;
534 else
535 compile_optlevel = 0;
536
537 /*
538 * Have to create a new pass manager builder every pass through, as the
539 * inliner has some per-builder state. Otherwise one ends up only inlining
540 * a function the first time though.
541 */
542 llvm_pmb = LLVMPassManagerBuilderCreate();
543 LLVMPassManagerBuilderSetOptLevel(llvm_pmb, compile_optlevel);
544 llvm_fpm = LLVMCreateFunctionPassManagerForModule(module);
545
546 if (context->base.flags & PGJIT_OPT3)
547 {
548 /* TODO: Unscientifically determined threshold */
549 LLVMPassManagerBuilderUseInlinerWithThreshold(llvm_pmb, 512);
550 }
551 else
552 {
553 /* we rely on mem2reg heavily, so emit even in the O0 case */
554 LLVMAddPromoteMemoryToRegisterPass(llvm_fpm);
555 }
556
557 LLVMPassManagerBuilderPopulateFunctionPassManager(llvm_pmb, llvm_fpm);
558
559 /*
560 * Do function level optimization. This could be moved to the point where
561 * functions are emitted, to reduce memory usage a bit.
562 */
563 LLVMInitializeFunctionPassManager(llvm_fpm);
564 for (func = LLVMGetFirstFunction(context->module);
565 func != NULL;
566 func = LLVMGetNextFunction(func))
567 LLVMRunFunctionPassManager(llvm_fpm, func);
568 LLVMFinalizeFunctionPassManager(llvm_fpm);
569 LLVMDisposePassManager(llvm_fpm);
570
571 /*
572 * Perform module level optimization. We do so even in the non-optimized
573 * case, so always-inline functions etc get inlined. It's cheap enough.
574 */
575 llvm_mpm = LLVMCreatePassManager();
576 LLVMPassManagerBuilderPopulateModulePassManager(llvm_pmb,
577 llvm_mpm);
578 /* always use always-inliner pass */
579 if (!(context->base.flags & PGJIT_OPT3))
580 LLVMAddAlwaysInlinerPass(llvm_mpm);
581 /* if doing inlining, but no expensive optimization, add inlining pass */
582 if (context->base.flags & PGJIT_INLINE
583 && !(context->base.flags & PGJIT_OPT3))
584 LLVMAddFunctionInliningPass(llvm_mpm);
585 LLVMRunPassManager(llvm_mpm, context->module);
586 LLVMDisposePassManager(llvm_mpm);
587
588 LLVMPassManagerBuilderDispose(llvm_pmb);
589 }
590
591 /*
592 * Emit code for the currently pending module.
593 */
594 static void
595 llvm_compile_module(LLVMJitContext *context)
596 {
597 LLVMJitHandle *handle;
598 MemoryContext oldcontext;
599 instr_time starttime;
600 instr_time endtime;
601 #if LLVM_VERSION_MAJOR > 11
602 LLVMOrcLLJITRef compile_orc;
603 #else
604 LLVMOrcJITStackRef compile_orc;
605 #endif
606
607 if (context->base.flags & PGJIT_OPT3)
608 compile_orc = llvm_opt3_orc;
609 else
610 compile_orc = llvm_opt0_orc;
611
612 /* perform inlining */
613 if (context->base.flags & PGJIT_INLINE)
614 {
615 INSTR_TIME_SET_CURRENT(starttime);
616 llvm_inline(context->module);
617 INSTR_TIME_SET_CURRENT(endtime);
618 INSTR_TIME_ACCUM_DIFF(context->base.instr.inlining_counter,
619 endtime, starttime);
620 }
621
622 if (jit_dump_bitcode)
623 {
624 char *filename;
625
626 filename = psprintf("%u.%zu.bc",
627 MyProcPid,
628 context->module_generation);
629 LLVMWriteBitcodeToFile(context->module, filename);
630 pfree(filename);
631 }
632
633
634 /* optimize according to the chosen optimization settings */
635 INSTR_TIME_SET_CURRENT(starttime);
636 llvm_optimize_module(context, context->module);
637 INSTR_TIME_SET_CURRENT(endtime);
638 INSTR_TIME_ACCUM_DIFF(context->base.instr.optimization_counter,
639 endtime, starttime);
640
641 if (jit_dump_bitcode)
642 {
643 char *filename;
644
645 filename = psprintf("%u.%zu.optimized.bc",
646 MyProcPid,
647 context->module_generation);
648 LLVMWriteBitcodeToFile(context->module, filename);
649 pfree(filename);
650 }
651
652 handle = (LLVMJitHandle *)
653 MemoryContextAlloc(TopMemoryContext, sizeof(LLVMJitHandle));
654
655 /*
656 * Emit the code. Note that this can, depending on the optimization
657 * settings, take noticeable resources as code emission executes low-level
658 * instruction combining/selection passes etc. Without optimization a
659 * faster instruction selection mechanism is used.
660 */
661 INSTR_TIME_SET_CURRENT(starttime);
662 #if LLVM_VERSION_MAJOR > 11
663 {
664 LLVMOrcThreadSafeModuleRef ts_module;
665 LLVMErrorRef error;
666 LLVMOrcJITDylibRef jd = LLVMOrcLLJITGetMainJITDylib(compile_orc);
667
668 ts_module = LLVMOrcCreateNewThreadSafeModule(context->module, llvm_ts_context);
669
670 handle->lljit = compile_orc;
671 handle->resource_tracker = LLVMOrcJITDylibCreateResourceTracker(jd);
672
673 /*
674 * NB: This doesn't actually emit code. That happens lazily the first
675 * time a symbol defined in the module is requested. Due to that
676 * llvm_get_function() also accounts for emission time.
677 */
678
679 context->module = NULL; /* will be owned by LLJIT */
680 error = LLVMOrcLLJITAddLLVMIRModuleWithRT(compile_orc,
681 handle->resource_tracker,
682 ts_module);
683
684 if (error)
685 elog(ERROR, "failed to JIT module: %s",
686 llvm_error_message(error));
687
688 handle->lljit = compile_orc;
689
690 /* LLVMOrcLLJITAddLLVMIRModuleWithRT takes ownership of the module */
691 }
692 #elif LLVM_VERSION_MAJOR > 6
693 {
694 handle->stack = compile_orc;
695 if (LLVMOrcAddEagerlyCompiledIR(compile_orc, &handle->orc_handle, context->module,
696 llvm_resolve_symbol, NULL))
697 elog(ERROR, "failed to JIT module");
698
699 /* LLVMOrcAddEagerlyCompiledIR takes ownership of the module */
700 }
701 #elif LLVM_VERSION_MAJOR > 4
702 {
703 LLVMSharedModuleRef smod;
704
705 smod = LLVMOrcMakeSharedModule(context->module);
706 handle->stack = compile_orc;
707 if (LLVMOrcAddEagerlyCompiledIR(compile_orc, &handle->orc_handle, smod,
708 llvm_resolve_symbol, NULL))
709 elog(ERROR, "failed to JIT module");
710
711 LLVMOrcDisposeSharedModuleRef(smod);
712 }
713 #else /* LLVM 4.0 and 3.9 */
714 {
715 handle->stack = compile_orc;
716 handle->orc_handle = LLVMOrcAddEagerlyCompiledIR(compile_orc, context->module,
717 llvm_resolve_symbol, NULL);
718
719 LLVMDisposeModule(context->module);
720 }
721 #endif
722
723 INSTR_TIME_SET_CURRENT(endtime);
724 INSTR_TIME_ACCUM_DIFF(context->base.instr.emission_counter,
725 endtime, starttime);
726
727 context->module = NULL;
728 context->compiled = true;
729
730 /* remember emitted code for cleanup and lookups */
731 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
732 context->handles = lappend(context->handles, handle);
733 MemoryContextSwitchTo(oldcontext);
734
735 ereport(DEBUG1,
736 (errmsg("time to inline: %.3fs, opt: %.3fs, emit: %.3fs",
737 INSTR_TIME_GET_DOUBLE(context->base.instr.inlining_counter),
738 INSTR_TIME_GET_DOUBLE(context->base.instr.optimization_counter),
739 INSTR_TIME_GET_DOUBLE(context->base.instr.emission_counter)),
740 errhidestmt(true),
741 errhidecontext(true)));
742 }
743
744 /*
745 * Per session initialization.
746 */
747 static void
748 llvm_session_initialize(void)
749 {
750 MemoryContext oldcontext;
751 char *error = NULL;
752 char *cpu = NULL;
753 char *features = NULL;
754 LLVMTargetMachineRef opt0_tm;
755 LLVMTargetMachineRef opt3_tm;
756
757 if (llvm_session_initialized)
758 return;
759
760 oldcontext = MemoryContextSwitchTo(TopMemoryContext);
761
762 LLVMInitializeNativeTarget();
763 LLVMInitializeNativeAsmPrinter();
764 LLVMInitializeNativeAsmParser();
765
766 /*
767 * Synchronize types early, as that also includes inferring the target
768 * triple.
769 */
770 llvm_create_types();
771
772 if (LLVMGetTargetFromTriple(llvm_triple, &llvm_targetref, &error) != 0)
773 {
774 elog(FATAL, "failed to query triple %s\n", error);
775 }
776
777 /*
778 * We want the generated code to use all available features. Therefore
779 * grab the host CPU string and detect features of the current CPU. The
780 * latter is needed because some CPU architectures default to enabling
781 * features not all CPUs have (weird, huh).
782 */
783 cpu = LLVMGetHostCPUName();
784 features = LLVMGetHostCPUFeatures();
785 elog(DEBUG2, "LLVMJIT detected CPU \"%s\", with features \"%s\"",
786 cpu, features);
787
788 opt0_tm =
789 LLVMCreateTargetMachine(llvm_targetref, llvm_triple, cpu, features,
790 LLVMCodeGenLevelNone,
791 LLVMRelocDefault,
792 LLVMCodeModelJITDefault);
793 opt3_tm =
794 LLVMCreateTargetMachine(llvm_targetref, llvm_triple, cpu, features,
795 LLVMCodeGenLevelAggressive,
796 LLVMRelocDefault,
797 LLVMCodeModelJITDefault);
798
799 LLVMDisposeMessage(cpu);
800 cpu = NULL;
801 LLVMDisposeMessage(features);
802 features = NULL;
803
804 /* force symbols in main binary to be loaded */
805 LLVMLoadLibraryPermanently(NULL);
806
807 #if LLVM_VERSION_MAJOR > 11
808 {
809 llvm_ts_context = LLVMOrcCreateNewThreadSafeContext();
810
811 llvm_opt0_orc = llvm_create_jit_instance(opt0_tm);
812 opt0_tm = 0;
813
814 llvm_opt3_orc = llvm_create_jit_instance(opt3_tm);
815 opt3_tm = 0;
816 }
817 #else /* LLVM_VERSION_MAJOR > 11 */
818 {
819 llvm_opt0_orc = LLVMOrcCreateInstance(opt0_tm);
820 llvm_opt3_orc = LLVMOrcCreateInstance(opt3_tm);
821
822 #if defined(HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER) && HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER
823 if (jit_debugging_support)
824 {
825 LLVMJITEventListenerRef l = LLVMCreateGDBRegistrationListener();
826
827 LLVMOrcRegisterJITEventListener(llvm_opt0_orc, l);
828 LLVMOrcRegisterJITEventListener(llvm_opt3_orc, l);
829 }
830 #endif
831 #if defined(HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER) && HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER
832 if (jit_profiling_support)
833 {
834 LLVMJITEventListenerRef l = LLVMCreatePerfJITEventListener();
835
836 LLVMOrcRegisterJITEventListener(llvm_opt0_orc, l);
837 LLVMOrcRegisterJITEventListener(llvm_opt3_orc, l);
838 }
839 #endif
840 }
841 #endif /* LLVM_VERSION_MAJOR > 11 */
842
843 before_shmem_exit(llvm_shutdown, 0);
844
845 llvm_session_initialized = true;
846
847 MemoryContextSwitchTo(oldcontext);
848 }
849
850 static void
851 llvm_shutdown(int code, Datum arg)
852 {
853 /*
854 * If llvm_shutdown() is reached while in a fatal-on-oom section an error
855 * has occurred in the middle of LLVM code. It is not safe to call back
856 * into LLVM (which is why a FATAL error was thrown).
857 *
858 * We do need to shutdown LLVM in other shutdown cases, otherwise
859 * e.g. profiling data won't be written out.
860 */
861 if (llvm_in_fatal_on_oom())
862 {
863 Assert(proc_exit_inprogress);
864 return;
865 }
866
867 #if LLVM_VERSION_MAJOR > 11
868 {
869 if (llvm_opt3_orc)
870 {
871 LLVMOrcDisposeLLJIT(llvm_opt3_orc);
872 llvm_opt3_orc = NULL;
873 }
874 if (llvm_opt0_orc)
875 {
876 LLVMOrcDisposeLLJIT(llvm_opt0_orc);
877 llvm_opt0_orc = NULL;
878 }
879 if (llvm_ts_context)
880 {
881 LLVMOrcDisposeThreadSafeContext(llvm_ts_context);
882 llvm_ts_context = NULL;
883 }
884 }
885 #else /* LLVM_VERSION_MAJOR > 11 */
886 {
887 /* unregister profiling support, needs to be flushed to be useful */
888
889 if (llvm_opt3_orc)
890 {
891 #if defined(HAVE_DECL_LLVMORCREGISTERPERF) && HAVE_DECL_LLVMORCREGISTERPERF
892 if (jit_profiling_support)
893 LLVMOrcUnregisterPerf(llvm_opt3_orc);
894 #endif
895 LLVMOrcDisposeInstance(llvm_opt3_orc);
896 llvm_opt3_orc = NULL;
897 }
898
899 if (llvm_opt0_orc)
900 {
901 #if defined(HAVE_DECL_LLVMORCREGISTERPERF) && HAVE_DECL_LLVMORCREGISTERPERF
902 if (jit_profiling_support)
903 LLVMOrcUnregisterPerf(llvm_opt0_orc);
904 #endif
905 LLVMOrcDisposeInstance(llvm_opt0_orc);
906 llvm_opt0_orc = NULL;
907 }
908 }
909 #endif /* LLVM_VERSION_MAJOR > 11 */
910 }
911
912 /* helper for llvm_create_types, returning a global var's type */
913 static LLVMTypeRef
914 load_type(LLVMModuleRef mod, const char *name)
915 {
916 LLVMValueRef value;
917 LLVMTypeRef typ;
918
919 /* this'll return a *pointer* to the global */
920 value = LLVMGetNamedGlobal(mod, name);
921 if (!value)
922 elog(ERROR, "type %s is unknown", name);
923
924 /* therefore look at the contained type and return that */
925 typ = LLVMTypeOf(value);
926 Assert(typ != NULL);
927 typ = LLVMGetElementType(typ);
928 Assert(typ != NULL);
929 return typ;
930 }
931
932 /* helper for llvm_create_types, returning a function's return type */
933 static LLVMTypeRef
934 load_return_type(LLVMModuleRef mod, const char *name)
935 {
936 LLVMValueRef value;
937 LLVMTypeRef typ;
938
939 /* this'll return a *pointer* to the function */
940 value = LLVMGetNamedFunction(mod, name);
941 if (!value)
942 elog(ERROR, "function %s is unknown", name);
943
944 /* get type of function pointer */
945 typ = LLVMTypeOf(value);
946 Assert(typ != NULL);
947 /* dereference pointer */
948 typ = LLVMGetElementType(typ);
949 Assert(typ != NULL);
950 /* and look at return type */
951 typ = LLVMGetReturnType(typ);
952 Assert(typ != NULL);
953
954 return typ;
955 }
956
957 /*
958 * Load required information, types, function signatures from llvmjit_types.c
959 * and make them available in global variables.
960 *
961 * Those global variables are then used while emitting code.
962 */
963 static void
964 llvm_create_types(void)
965 {
966 char path[MAXPGPATH];
967 LLVMMemoryBufferRef buf;
968 char *msg;
969 LLVMModuleRef mod = NULL;
970
971 snprintf(path, MAXPGPATH, "%s/%s", pkglib_path, "llvmjit_types.bc");
972
973 /* open file */
974 if (LLVMCreateMemoryBufferWithContentsOfFile(path, &buf, &msg))
975 {
976 elog(ERROR, "LLVMCreateMemoryBufferWithContentsOfFile(%s) failed: %s",
977 path, msg);
978 }
979
980 /* eagerly load contents, going to need it all */
981 if (LLVMParseBitcode2(buf, &mod))
982 {
983 elog(ERROR, "LLVMParseBitcode2 of %s failed", path);
984 }
985 LLVMDisposeMemoryBuffer(buf);
986
987 /*
988 * Load triple & layout from clang emitted file so we're guaranteed to be
989 * compatible.
990 */
991 llvm_triple = pstrdup(LLVMGetTarget(mod));
992 llvm_layout = pstrdup(LLVMGetDataLayoutStr(mod));
993
994 TypeSizeT = load_type(mod, "TypeSizeT");
995 TypeParamBool = load_return_type(mod, "FunctionReturningBool");
996 TypeStorageBool = load_type(mod, "TypeStorageBool");
997 TypePGFunction = load_type(mod, "TypePGFunction");
998 StructNullableDatum = load_type(mod, "StructNullableDatum");
999 StructExprContext = load_type(mod, "StructExprContext");
1000 StructExprEvalStep = load_type(mod, "StructExprEvalStep");
1001 StructExprState = load_type(mod, "StructExprState");
1002 StructFunctionCallInfoData = load_type(mod, "StructFunctionCallInfoData");
1003 StructMemoryContextData = load_type(mod, "StructMemoryContextData");
1004 StructTupleTableSlot = load_type(mod, "StructTupleTableSlot");
1005 StructHeapTupleTableSlot = load_type(mod, "StructHeapTupleTableSlot");
1006 StructMinimalTupleTableSlot = load_type(mod, "StructMinimalTupleTableSlot");
1007 StructHeapTupleData = load_type(mod, "StructHeapTupleData");
1008 StructTupleDescData = load_type(mod, "StructTupleDescData");
1009 StructAggState = load_type(mod, "StructAggState");
1010 StructAggStatePerGroupData = load_type(mod, "StructAggStatePerGroupData");
1011 StructAggStatePerTransData = load_type(mod, "StructAggStatePerTransData");
1012
1013 AttributeTemplate = LLVMGetNamedFunction(mod, "AttributeTemplate");
1014 FuncStrlen = LLVMGetNamedFunction(mod, "strlen");
1015 FuncVarsizeAny = LLVMGetNamedFunction(mod, "varsize_any");
1016 FuncSlotGetsomeattrsInt = LLVMGetNamedFunction(mod, "slot_getsomeattrs_int");
1017 FuncSlotGetmissingattrs = LLVMGetNamedFunction(mod, "slot_getmissingattrs");
1018 FuncMakeExpandedObjectReadOnlyInternal = LLVMGetNamedFunction(mod, "MakeExpandedObjectReadOnlyInternal");
1019 FuncExecEvalSubscriptingRef = LLVMGetNamedFunction(mod, "ExecEvalSubscriptingRef");
1020 FuncExecEvalSysVar = LLVMGetNamedFunction(mod, "ExecEvalSysVar");
1021 FuncExecAggTransReparent = LLVMGetNamedFunction(mod, "ExecAggTransReparent");
1022 FuncExecAggInitGroup = LLVMGetNamedFunction(mod, "ExecAggInitGroup");
1023
1024 /*
1025 * Leave the module alive, otherwise references to function would be
1026 * dangling.
1027 */
1028
1029 return;
1030 }
1031
1032 /*
1033 * Split a symbol into module / function parts. If the function is in the
1034 * main binary (or an external library) *modname will be NULL.
1035 */
1036 void
1037 llvm_split_symbol_name(const char *name, char **modname, char **funcname)
1038 {
1039 *modname = NULL;
1040 *funcname = NULL;
1041
1042 /*
1043 * Module function names are pgextern.$module.$funcname
1044 */
1045 if (strncmp(name, "pgextern.", strlen("pgextern.")) == 0)
1046 {
1047 /*
1048 * Symbol names cannot contain a ., therefore we can split based on
1049 * first and last occurrence of one.
1050 */
1051 *funcname = rindex(name, '.');
1052 (*funcname)++; /* jump over . */
1053
1054 *modname = pnstrdup(name + strlen("pgextern."),
1055 *funcname - name - strlen("pgextern.") - 1);
1056 Assert(funcname);
1057
1058 *funcname = pstrdup(*funcname);
1059 }
1060 else
1061 {
1062 *modname = NULL;
1063 *funcname = pstrdup(name);
1064 }
1065 }
1066
1067 /*
1068 * Attempt to resolve symbol, so LLVM can emit a reference to it.
1069 */
1070 static uint64_t
1071 llvm_resolve_symbol(const char *symname, void *ctx)
1072 {
1073 uintptr_t addr;
1074 char *funcname;
1075 char *modname;
1076
1077 /*
1078 * macOS prefixes all object level symbols with an underscore. But neither
1079 * dlsym() nor PG's inliner expect that. So undo.
1080 */
1081 #if defined(__darwin__)
1082 if (symname[0] != '_')
1083 elog(ERROR, "expected prefixed symbol name, but got \"%s\"", symname);
1084 symname++;
1085 #endif
1086
1087 llvm_split_symbol_name(symname, &modname, &funcname);
1088
1089 /* functions that aren't resolved to names shouldn't ever get here */
1090 Assert(funcname);
1091
1092 if (modname)
1093 addr = (uintptr_t) load_external_function(modname, funcname,
1094 true, NULL);
1095 else
1096 addr = (uintptr_t) LLVMSearchForAddressOfSymbol(symname);
1097
1098 pfree(funcname);
1099 if (modname)
1100 pfree(modname);
1101
1102 /* let LLVM will error out - should never happen */
1103 if (!addr)
1104 elog(WARNING, "failed to resolve name %s", symname);
1105
1106 return (uint64_t) addr;
1107 }
1108
1109 #if LLVM_VERSION_MAJOR > 11
1110
1111 static LLVMErrorRef
1112 llvm_resolve_symbols(LLVMOrcDefinitionGeneratorRef GeneratorObj, void *Ctx,
1113 LLVMOrcLookupStateRef *LookupState, LLVMOrcLookupKind Kind,
1114 LLVMOrcJITDylibRef JD, LLVMOrcJITDylibLookupFlags JDLookupFlags,
1115 LLVMOrcCLookupSet LookupSet, size_t LookupSetSize)
1116 {
1117 LLVMOrcCSymbolMapPairs symbols = palloc0(sizeof(LLVMJITCSymbolMapPair) * LookupSetSize);
1118 LLVMErrorRef error;
1119 LLVMOrcMaterializationUnitRef mu;
1120
1121 for (int i = 0; i < LookupSetSize; i++)
1122 {
1123 const char *name = LLVMOrcSymbolStringPoolEntryStr(LookupSet[i].Name);
1124
1125 #if LLVM_VERSION_MAJOR > 12
1126 LLVMOrcRetainSymbolStringPoolEntry(LookupSet[i].Name);
1127 #endif
1128 symbols[i].Name = LookupSet[i].Name;
1129 symbols[i].Sym.Address = llvm_resolve_symbol(name, NULL);
1130 symbols[i].Sym.Flags.GenericFlags = LLVMJITSymbolGenericFlagsExported;
1131 }
1132
1133 mu = LLVMOrcAbsoluteSymbols(symbols, LookupSetSize);
1134 error = LLVMOrcJITDylibDefine(JD, mu);
1135 if (error != LLVMErrorSuccess)
1136 LLVMOrcDisposeMaterializationUnit(mu);
1137
1138 pfree(symbols);
1139
1140 return error;
1141 }
1142
1143 /*
1144 * We cannot throw errors through LLVM (without causing a FATAL at least), so
1145 * just use WARNING here. That's OK anyway, as the error is also reported at
1146 * the top level action (with less detail) and there might be multiple
1147 * invocations of errors with details.
1148 *
1149 * This doesn't really happen during normal operation, but in cases like
1150 * symbol resolution breakage. So just using elog(WARNING) is fine.
1151 */
1152 static void
1153 llvm_log_jit_error(void *ctx, LLVMErrorRef error)
1154 {
1155 elog(WARNING, "error during JITing: %s",
1156 llvm_error_message(error));
1157 }
1158
1159 /*
1160 * Create our own object layer, so we can add event listeners.
1161 */
1162 static LLVMOrcObjectLayerRef
1163 llvm_create_object_layer(void *Ctx, LLVMOrcExecutionSessionRef ES, const char *Triple)
1164 {
1165 LLVMOrcObjectLayerRef objlayer =
1166 LLVMOrcCreateRTDyldObjectLinkingLayerWithSectionMemoryManager(ES);
1167
1168 #if defined(HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER) && HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER
1169 if (jit_debugging_support)
1170 {
1171 LLVMJITEventListenerRef l = LLVMCreateGDBRegistrationListener();
1172
1173 LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(objlayer, l);
1174 }
1175 #endif
1176
1177 #if defined(HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER) && HAVE_DECL_LLVMCREATEPERFJITEVENTLISTENER
1178 if (jit_profiling_support)
1179 {
1180 LLVMJITEventListenerRef l = LLVMCreatePerfJITEventListener();
1181
1182 LLVMOrcRTDyldObjectLinkingLayerRegisterJITEventListener(objlayer, l);
1183 }
1184 #endif
1185
1186 return objlayer;
1187 }
1188
1189 /*
1190 * Create LLJIT instance, using the passed in target machine. Note that the
1191 * target machine afterwards is owned by the LLJIT instance.
1192 */
1193 static LLVMOrcLLJITRef
1194 llvm_create_jit_instance(LLVMTargetMachineRef tm)
1195 {
1196 LLVMOrcLLJITRef lljit;
1197 LLVMOrcJITTargetMachineBuilderRef tm_builder;
1198 LLVMOrcLLJITBuilderRef lljit_builder;
1199 LLVMErrorRef error;
1200 LLVMOrcDefinitionGeneratorRef main_gen;
1201 LLVMOrcDefinitionGeneratorRef ref_gen;
1202
1203 lljit_builder = LLVMOrcCreateLLJITBuilder();
1204 tm_builder = LLVMOrcJITTargetMachineBuilderCreateFromTargetMachine(tm);
1205 LLVMOrcLLJITBuilderSetJITTargetMachineBuilder(lljit_builder, tm_builder);
1206
1207 LLVMOrcLLJITBuilderSetObjectLinkingLayerCreator(lljit_builder,
1208 llvm_create_object_layer,
1209 NULL);
1210
1211 error = LLVMOrcCreateLLJIT(&lljit, lljit_builder);
1212 if (error)
1213 elog(ERROR, "failed to create lljit instance: %s",
1214 llvm_error_message(error));
1215
1216 LLVMOrcExecutionSessionSetErrorReporter(LLVMOrcLLJITGetExecutionSession(lljit),
1217 llvm_log_jit_error, NULL);
1218
1219 /*
1220 * Symbol resolution support for symbols in the postgres binary /
1221 * libraries already loaded.
1222 */
1223 error = LLVMOrcCreateDynamicLibrarySearchGeneratorForProcess(&main_gen,
1224 LLVMOrcLLJITGetGlobalPrefix(lljit),
1225 0, NULL);
1226 if (error)
1227 elog(ERROR, "failed to create generator: %s",
1228 llvm_error_message(error));
1229 LLVMOrcJITDylibAddGenerator(LLVMOrcLLJITGetMainJITDylib(lljit), main_gen);
1230
1231 /*
1232 * Symbol resolution support for "special" functions, e.g. a call into an
1233 * SQL callable function.
1234 */
1235 ref_gen = LLVMOrcCreateCustomCAPIDefinitionGenerator(llvm_resolve_symbols, NULL);
1236 LLVMOrcJITDylibAddGenerator(LLVMOrcLLJITGetMainJITDylib(lljit), ref_gen);
1237
1238 return lljit;
1239 }
1240
1241 static char *
1242 llvm_error_message(LLVMErrorRef error)
1243 {
1244 char *orig = LLVMGetErrorMessage(error);
1245 char *msg = pstrdup(orig);
1246
1247 LLVMDisposeErrorMessage(orig);
1248
1249 return msg;
1250 }
1251
1252 #endif /* LLVM_VERSION_MAJOR > 11 */
1253