PostgreSQL Source Code  git master
llvmjit_wrap.cpp
Go to the documentation of this file.
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 #include <llvm/MC/SubtargetFeature.h>
27 #include <llvm/Support/Host.h>
28 
29 #include "jit/llvmjit.h"
30 
31 
32 /*
33  * C-API extensions.
34  */
35 #if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME
36 char *LLVMGetHostCPUName(void) {
37  return strdup(llvm::sys::getHostCPUName().data());
38 }
39 #endif
40 
41 
42 #if defined(HAVE_DECL_LLVMGETHOSTCPUFEATURES) && !HAVE_DECL_LLVMGETHOSTCPUFEATURES
43 char *LLVMGetHostCPUFeatures(void) {
44  llvm::SubtargetFeatures Features;
45  llvm::StringMap<bool> HostFeatures;
46 
47  if (llvm::sys::getHostCPUFeatures(HostFeatures))
48  for (auto &F : HostFeatures)
49  Features.AddFeature(F.first(), F.second);
50 
51  return strdup(Features.getString().c_str());
52 }
53 #endif
54 
55 /*
56  * Like LLVM's LLVMGetAttributeCountAtIndex(), works around a bug in LLVM 3.9.
57  *
58  * In LLVM <= 3.9, LLVMGetAttributeCountAtIndex() segfaults if there are no
59  * attributes at an index (fixed in LLVM commit ce9bb1097dc2).
60  */
61 unsigned
63 {
64  /*
65  * This is more expensive, so only do when using a problematic LLVM
66  * version.
67  */
68 #if LLVM_VERSION_MAJOR < 4
69  if (!llvm::unwrap<llvm::Function>(F)->getAttributes().hasAttributes(Idx))
70  return 0;
71 #endif
72 
73  /*
74  * There is no nice public API to determine the count nicely, so just
75  * always fall back to LLVM's C API.
76  */
77  return LLVMGetAttributeCountAtIndex(F, Idx);
78 }
unsigned int uint32
Definition: c.h:490
unsigned LLVMGetAttributeCountAtIndexPG(LLVMValueRef F, uint32 Idx)
#define F(X, Y, Z)
Definition: md5.c:60
const void * data