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-2023, 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 : 21 : /* Avoid macro clash with LLVM's C++ headers */ 22 : #undef Min 23 : 24 : #include <llvm/IR/Attributes.h> 25 : #include <llvm/IR/Function.h> 26 : #if LLVM_VERSION_MAJOR < 17 27 : #include <llvm/MC/SubtargetFeature.h> 28 : #endif 29 : #if LLVM_VERSION_MAJOR > 16 30 : #include <llvm/TargetParser/Host.h> 31 : #else 32 : #include <llvm/Support/Host.h> 33 : #endif 34 : 35 : #include "jit/llvmjit.h" 36 : 37 : 38 : /* 39 : * C-API extensions. 40 : */ 41 : #if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME 42 : char *LLVMGetHostCPUName(void) { 43 : return strdup(llvm::sys::getHostCPUName().data()); 44 : } 45 : #endif 46 : 47 : 48 : #if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES 49 : char *LLVMGetHostCPUFeatures(void) { 50 : llvm::SubtargetFeatures Features; 51 : llvm::StringMap<bool> HostFeatures; 52 : 53 : if (llvm::sys::getHostCPUFeatures(HostFeatures)) 54 : for (auto &F : HostFeatures) 55 : Features.AddFeature(F.first(), F.second); 56 : 57 : return strdup(Features.getString().c_str()); 58 : } 59 : #endif 60 : 61 : /* 62 : * Like LLVM's LLVMGetAttributeCountAtIndex(), works around a bug in LLVM 3.9. 63 : * 64 : * In LLVM <= 3.9, LLVMGetAttributeCountAtIndex() segfaults if there are no 65 : * attributes at an index (fixed in LLVM commit ce9bb1097dc2). 66 : */ 67 : unsigned 68 65360 : LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx) 69 : { 70 : /* 71 : * This is more expensive, so only do when using a problematic LLVM 72 : * version. 73 : */ 74 : #if LLVM_VERSION_MAJOR < 4 75 : if (!llvm::unwrap<llvm::Function>(F)->getAttributes().hasAttributes(Idx)) 76 : return 0; 77 : #endif 78 : 79 : /* 80 : * There is no nice public API to determine the count nicely, so just 81 : * always fall back to LLVM's C API. 82 : */ 83 65360 : return LLVMGetAttributeCountAtIndex(F, Idx); 84 : } 85 : 86 : LLVMTypeRef 87 770 : LLVMGetFunctionReturnType(LLVMValueRef r) 88 : { 89 770 : return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getReturnType()); 90 : } 91 : 92 : LLVMTypeRef 93 52402 : LLVMGetFunctionType(LLVMValueRef r) 94 : { 95 52402 : return llvm::wrap(llvm::unwrap<llvm::Function>(r)->getFunctionType()); 96 : } 97 : 98 : #if LLVM_VERSION_MAJOR < 8 99 : LLVMTypeRef 100 : LLVMGlobalGetValueType(LLVMValueRef g) 101 : { 102 : return llvm::wrap(llvm::unwrap<llvm::GlobalValue>(g)->getValueType()); 103 : } 104 : #endif