Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * llvmjit_wrap.cpp
4 : * Parts of the LLVM interface not (yet) exposed to C.
5 : *
6 : * Copyright (c) 2016-2026, PostgreSQL Global Development Group
7 : *
8 : * IDENTIFICATION
9 : * src/backend/lib/llvm/llvmjit_wrap.cpp
10 : *
11 : *-------------------------------------------------------------------------
12 : */
13 :
14 : extern "C"
15 : {
16 : #include "postgres.h"
17 : }
18 :
19 : #include <llvm-c/Core.h>
20 : #include <llvm/IR/Function.h>
21 :
22 : #include "jit/llvmjit.h"
23 : #include "jit/llvmjit_backport.h"
24 :
25 : #ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
26 : #include <llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h>
27 : #include <llvm/ExecutionEngine/SectionMemoryManager.h>
28 : #include "jit/SectionMemoryManager.h"
29 : #include <llvm/Support/CBindingWrapping.h>
30 : #endif
31 :
32 :
33 : /*
34 : * C-API extensions.
35 : */
36 :
37 : LLVMTypeRef
38 7122 : LLVMGetFunctionReturnType(LLVMValueRef r)
39 : {
40 7122 : return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getReturnType());
41 : }
42 :
43 : LLVMTypeRef
44 21166 : LLVMGetFunctionType(LLVMValueRef r)
45 : {
46 21166 : return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getFunctionType());
47 : }
48 :
49 : #ifdef USE_LLVM_BACKPORT_SECTION_MEMORY_MANAGER
50 : DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ExecutionSession, LLVMOrcExecutionSessionRef)
51 : DEFINE_SIMPLE_CONVERSION_FUNCTIONS(llvm::orc::ObjectLayer, LLVMOrcObjectLayerRef);
52 :
53 : LLVMOrcObjectLayerRef
54 : LLVMOrcCreateRTDyldObjectLinkingLayerWithSafeSectionMemoryManager(LLVMOrcExecutionSessionRef ES)
55 : {
56 : #if LLVM_VERSION_MAJOR >= 21
57 : return wrap(new llvm::orc::RTDyldObjectLinkingLayer(
58 : *unwrap(ES), [](const llvm::MemoryBuffer&) {
59 : return std::make_unique<llvm::backport::SectionMemoryManager>(nullptr, true);
60 : }));
61 : #else
62 : return wrap(new llvm::orc::RTDyldObjectLinkingLayer(
63 : *unwrap(ES), [] { return std::make_unique<llvm::backport::SectionMemoryManager>(nullptr, true); }));
64 : #endif
65 : }
66 : #endif
|