Heavily-WIP: LLVM perf integration.
authorAndres Freund <[email protected]>
Tue, 14 Mar 2017 03:22:10 +0000 (20:22 -0700)
committerAndres Freund <[email protected]>
Tue, 14 Mar 2017 06:34:02 +0000 (23:34 -0700)
configure
configure.in
src/Makefile.global.in
src/backend/Makefile
src/backend/lib/Makefile
src/backend/lib/llvmjit.c
src/backend/lib/llvmjit_profile.cpp [new file with mode: 0644]
src/include/lib/llvmjit.h

index 94724a5aa4a9a5c9a763cae9b49c7659967506d3..466b712f99aba7fdaf3e9cc0d6155deb62eff391 100755 (executable)
--- a/configure
+++ b/configure
@@ -702,6 +702,8 @@ ELF_SYS
 EGREP
 GREP
 with_llvm
+LLVM_CXXFLAGS
+LLVM_CFLAGS
 LLVM_LIBS
 LLVM_CONFIG
 with_zlib
@@ -6210,6 +6212,14 @@ done
     for pgac_option in `$LLVM_CONFIG --cflags`; do
       case $pgac_option in
         -I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
+        -W*);; # ignore
+      esac
+    done
+
+    for pgac_option in `$LLVM_CONFIG --cxxflags`; do
+      case $pgac_option in
+        -fno-exceptions*) LLVM_CXXFLAGS="$LLVM_CXXFLAGS $pgac_option";;
+        -fno-rtti*) LLVM_CXXFLAGS="$LLVM_CXXFLAGS $pgac_option";;
       esac
     done
     for pgac_option in `$LLVM_CONFIG --ldflags`; do
@@ -6217,7 +6227,7 @@ done
         -L*) LDFLAGS="$LDFLAGS $pgac_option";;
       esac
     done
-    for pgac_option in `$LLVM_CONFIG --libs --system-libs engine`; do
+    for pgac_option in `$LLVM_CONFIG --libs --system-libs engine debuginfodwarf perfjitevents`; do
       case $pgac_option in
         -l*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
       esac
@@ -6228,6 +6238,8 @@ fi
 
 
 
+
+
 #
 # Elf
 #
index d91defc7c5d5ec3810456e922f17adcb77a4ee33..cc867568497f2c91e793fa463880950866a15ae3 100644 (file)
@@ -848,6 +848,14 @@ if test "$with_llvm" = yes ; then
     for pgac_option in `$LLVM_CONFIG --cflags`; do
       case $pgac_option in
         -I*|-D*) CPPFLAGS="$CPPFLAGS $pgac_option";;
+        -W*);; # ignore
+      esac
+    done
+
+    for pgac_option in `$LLVM_CONFIG --cxxflags`; do
+      case $pgac_option in
+        -fno-exceptions*) LLVM_CXXFLAGS="$LLVM_CXXFLAGS $pgac_option";;
+        -fno-rtti*) LLVM_CXXFLAGS="$LLVM_CXXFLAGS $pgac_option";;
       esac
     done
     for pgac_option in `$LLVM_CONFIG --ldflags`; do
@@ -855,7 +863,7 @@ if test "$with_llvm" = yes ; then
         -L*) LDFLAGS="$LDFLAGS $pgac_option";;
       esac
     done
-    for pgac_option in `$LLVM_CONFIG --libs --system-libs engine`; do
+    for pgac_option in `$LLVM_CONFIG --libs --system-libs engine debuginfodwarf perfjitevents`; do
       case $pgac_option in
         -l*) LLVM_LIBS="$LLVM_LIBS $pgac_option";;
       esac
@@ -863,6 +871,8 @@ if test "$with_llvm" = yes ; then
   fi
 fi
 AC_SUBST(LLVM_LIBS)
+AC_SUBST(LLVM_CFLAGS)
+AC_SUBST(LLVM_CXXFLAGS)
 AC_SUBST(with_llvm)
 
 
index f8306100500358b2110428c18e609dfad9591602..8bba7a35b4d379b25b9ac5c2b6c5a6130da755d5 100644 (file)
@@ -244,6 +244,8 @@ SUN_STUDIO_CC = @SUN_STUDIO_CC@
 CFLAGS = @CFLAGS@
 CFLAGS_VECTOR = @CFLAGS_VECTOR@
 CFLAGS_SSE42 = @CFLAGS_SSE42@
+LLVM_CFLAGS = @LLVM_CFLAGS@
+LLVM_CXXFLAGS = @LLVM_CXXFLAGS@
 
 # Kind-of compilers
 
@@ -786,6 +788,10 @@ ifndef COMPILE.c
 COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c
 endif
 
+ifndef COMPILE.cpp
+COMPILE.cpp = $(CXX) $(CFLAGS) $(CPPFLAGS) -c
+endif
+
 DEPDIR = .deps
 
 ifeq ($(GCC), yes)
@@ -795,6 +801,10 @@ ifeq ($(GCC), yes)
    @if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi
    $(COMPILE.c) -o $@ $< -MMD -MP -MF $(DEPDIR)/$(*F).Po
 
+%.o : %.cpp
+   @if test ! -d $(DEPDIR); then mkdir -p $(DEPDIR); fi
+   $(COMPILE.cpp) -o $@ $< -MMD -MP -MF $(DEPDIR)/$(*F).Po
+
 endif # GCC
 
 # Include all the dependency files generated for the current
index 79c8b341e426cde0c4c9af5f12e2a569b2373c44..68d6e90a03c74f617d36ecba541d87ad9d218ad3 100644 (file)
@@ -49,6 +49,10 @@ LIBS := $(filter-out -lz -lreadline -ledit -ltermcap -lncurses -lcurses, $(LIBS)
 # only specify here
 LIBS += $(LLVM_LIBS)
 
+ifeq ($(with_llvm), yes)
+LIBS += -lstdc++
+endif
+
 ifeq ($(with_systemd),yes)
 LIBS += -lsystemd
 endif
index d4c9b3ab1aa727c58af7d17d0f087796bf4a9506..8005b8568c009b437dec093d97df1200c45cef6a 100644 (file)
@@ -13,6 +13,9 @@ top_builddir = ../../..
 include $(top_builddir)/src/Makefile.global
 
 OBJS = binaryheap.o bipartite_match.o hyperloglog.o ilist.o pairingheap.o \
-       rbtree.o stringinfo.o llvmjit.o
+       rbtree.o stringinfo.o llvmjit.o llvmjit_profile.o
+
+llvmjit_profile.o: CFLAGS+=$(LLVM_CFLAGS)
+llvmjit_profile.o: CXXFLAGS+=$(LLVM_CXXFLAGS)
 
 include $(top_srcdir)/src/backend/common.mk
index e39736c3ab4a4dadc58fc160b4355cc2b16e25a9..a70fe8a5eb385e6d8d45d8f33bfaa6b767b5470b 100644 (file)
@@ -39,6 +39,13 @@ LLVMTypeRef StructItemPointerData;
 LLVMTypeRef StructBlockId;
 LLVMTypeRef StructTupleTableSlot;
 
+static void
+shutdown_perf(void)
+{
+   if (llvm_engine)
+       llvm_shutdown_perf_support(llvm_engine);
+}
+
 void
 llvm_initialize(void)
 {
@@ -76,6 +83,9 @@ llvm_initialize(void)
        exit(EXIT_FAILURE);
    }
 
+   llvm_perf_support(llvm_engine);
+   atexit(shutdown_perf);
+
    /* so we don't constantly have to decide between 32/64 bit */
 #if SIZEOF_DATUM == 8
    TypeSizeT = LLVMInt64Type();
diff --git a/src/backend/lib/llvmjit_profile.cpp b/src/backend/lib/llvmjit_profile.cpp
new file mode 100644 (file)
index 0000000..986a85b
--- /dev/null
@@ -0,0 +1,33 @@
+extern "C"
+{
+#include "postgres.h"
+#include "lib/llvmjit.h"
+}
+
+
+#ifdef USE_LLVM
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+
+using namespace llvm;
+
+static JITEventListener *perf_listener = NULL;
+
+extern void
+llvm_perf_support(LLVMExecutionEngineRef EE)
+{
+   perf_listener = JITEventListener::createPerfJITEventListener();
+   unwrap(EE)->RegisterJITEventListener(perf_listener);
+}
+
+extern void
+llvm_shutdown_perf_support(LLVMExecutionEngineRef EE)
+{
+   if (perf_listener)
+   {
+       unwrap(EE)->UnregisterJITEventListener(perf_listener);
+       delete perf_listener;
+   }
+}
+
+#endif /* USE_LLVM */
index 54c5075955e24010edee2046acd7be0ad3866470..678320485ad03aad7594c9cac1d786cb054b471b 100644 (file)
@@ -29,5 +29,8 @@ extern void llvm_add_module(LLVMModuleRef mod, const char *funcname);
 extern void * llvm_get_function(const char *funcname);
 extern void llvm_dispose_module(LLVMModuleRef mod, const char *funcname);
 
+extern void llvm_perf_support(LLVMExecutionEngineRef EE);
+extern void llvm_shutdown_perf_support(LLVMExecutionEngineRef EE);
+
 #endif /* USE_LLVM */
 #endif /* LLVMJIT_H */