Re: JIT compiling with LLVM v10.0

2018-02-09 Thread Dmitry Dolgov
> On 8 February 2018 at 21:26, Thomas Munro  
> wrote:
> On Fri, Feb 9, 2018 at 3:14 AM, Dmitry Dolgov <9erthali...@gmail.com> wrote:
>> $ ./configure --prefix=/build/postgres-jit/ --with-llvm
>> --enable-debug --enable-depend --enable-cassert
>
>> /usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This
>> file requires compiler and library support for the ISO C++ 2011
>> standard. This support must be enabled with the -std=c++11 or
>> -std=gnu++11 compiler options.
>
> Did you try passing CXXFLAGS="-std=c++11" to configure?

Yes, it solved the issue, thanks.



Re: JIT compiling with LLVM v10.0

2018-02-08 Thread Thomas Munro
On Fri, Feb 9, 2018 at 3:14 AM, Dmitry Dolgov <9erthali...@gmail.com> wrote:
> $ ./configure --prefix=/build/postgres-jit/ --with-llvm
> --enable-debug --enable-depend --enable-cassert

> /usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This
> file requires compiler and library support for the ISO C++ 2011
> standard. This support must be enabled with the -std=c++11 or
> -std=gnu++11 compiler options.

Did you try passing CXXFLAGS="-std=c++11" to configure?

-- 
Thomas Munro
http://www.enterprisedb.com



Re: JIT compiling with LLVM v10.0

2018-02-08 Thread Andres Freund
On 2018-02-08 15:14:42 +0100, Dmitry Dolgov wrote:
> > On 8 February 2018 at 10:29, Andreas Karlsson  wrote:
> >> On 02/07/2018 03:54 PM, Andres Freund wrote:
> >>
> >> I've pushed v10.0. The big (and pretty painful to make) change is that
> >> now all the LLVM specific code lives in src/backend/jit/llvm, which is
> >> built as a shared library which is loaded on demand.
> >
> >
> > It does not seem to be possible build without LLVM anymore.

Yea, wrong header included. Will fix.


> Maybe I'm doing something wrong, but I also see some issues during compilation
> even with llvm included (with gcc 5.4.0 and 7.1.0). Is it expected, do I need
> to use another version to check it out?
> 
> $ git rev-parse HEAD
> e24cac5951575cf86f138080acec663a0a05983e
> 
> $ ./configure --prefix=/build/postgres-jit/ --with-llvm
> --enable-debug --enable-depend --enable-cassert

Seems you need to provide a decent C++ compiler (via CXX=... to
configure). Will test that it actually works with a recent clang.

Greetings,

Andres Freund



Re: JIT compiling with LLVM v10.0

2018-02-08 Thread Dmitry Dolgov
> On 8 February 2018 at 10:29, Andreas Karlsson  wrote:
>> On 02/07/2018 03:54 PM, Andres Freund wrote:
>>
>> I've pushed v10.0. The big (and pretty painful to make) change is that
>> now all the LLVM specific code lives in src/backend/jit/llvm, which is
>> built as a shared library which is loaded on demand.
>
>
> It does not seem to be possible build without LLVM anymore.

Maybe I'm doing something wrong, but I also see some issues during compilation
even with llvm included (with gcc 5.4.0 and 7.1.0). Is it expected, do I need
to use another version to check it out?

$ git rev-parse HEAD
e24cac5951575cf86f138080acec663a0a05983e

$ ./configure --prefix=/build/postgres-jit/ --with-llvm
--enable-debug --enable-depend --enable-cassert

In file included from llvmjit_error.cpp:22:0:
/usr/lib/llvm-5.0/include/llvm/Support/ErrorHandling.h:47:36:
warning: identifier 'nullptr' is a keyword in C++11 [-Wc++0x-compat]
void *user_data = nullptr);
^
In file included from /usr/include/c++/5/cinttypes:35:0,
 from /usr/lib/llvm-5.0/include/llvm/Support/DataTypes.h:39,
 from /usr/lib/llvm-5.0/include/llvm-c/Types.h:17,
 from ../../../../src/include/jit/llvmjit.h:13,
 from llvmjit_error.cpp:24:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This
file requires compiler and library support for the ISO C++ 2011
standard. This support must be enabled with the -std=c++11 or
-std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^
In file included from llvmjit_error.cpp:22:0:
/usr/lib/llvm-5.0/include/llvm/Support/ErrorHandling.h:47:54:
error: 'nullptr' was not declared in this scope
void *user_data = nullptr);
  ^
/usr/lib/llvm-5.0/include/llvm/Support/ErrorHandling.h:57:56:
error: 'nullptr' was not declared in this scope
  void *user_data = nullptr) {
^
/usr/lib/llvm-5.0/include/llvm/Support/ErrorHandling.h:98:56:
error: 'nullptr' was not declared in this scope
  void *user_data = nullptr);
^
/usr/lib/llvm-5.0/include/llvm/Support/ErrorHandling.h:121:45:
error: 'nullptr' was not declared in this scope
 llvm_unreachable_internal(const char *msg = nullptr, const char
*file = nullptr,
 ^
/usr/lib/llvm-5.0/include/llvm/Support/ErrorHandling.h:121:73:
error: 'nullptr' was not declared in this scope
 llvm_unreachable_internal(const char *msg = nullptr, const char
*file = nullptr,
 ^
../../../../src/Makefile.global:838: recipe for target
'llvmjit_error.o' failed
make[2]: *** [llvmjit_error.o] Error 1
make[2]: Leaving directory '/postgres/src/backend/jit/llvm'
Makefile:42: recipe for target 'all-backend/jit/llvm-recurse' failed
make[1]: *** [all-backend/jit/llvm-recurse] Error 2
make[1]: Leaving directory '/postgres/src'
GNUmakefile:11: recipe for target 'all-src-recurse' failed
make: *** [all-src-recurse] Error 2



Re: JIT compiling with LLVM v10.0

2018-02-08 Thread Andreas Karlsson

On 02/07/2018 03:54 PM, Andres Freund wrote:

I've pushed v10.0. The big (and pretty painful to make) change is that
now all the LLVM specific code lives in src/backend/jit/llvm, which is
built as a shared library which is loaded on demand.


It does not seem to be possible build without LLVM anymore.

Error:

In file included from planner.c:32:0:
../../../../src/include/jit/llvmjit.h:13:10: fatal error: 
llvm-c/Types.h: No such file or directory

 #include 
  ^~~~

Options:

./configure --prefix=/home/andreas/dev/postgresql-inst 
--enable-tap-tests --enable-cassert --enable-debug


I also noticed the following typo:

diff --git a/configure.in b/configure.in
index b035966c0a..b89c4a138a 100644
--- a/configure.in
+++ b/configure.in
@@ -499,7 +499,7 @@ fi
 if test "$enable_coverage" = yes; then
   if test "$GCC" = yes; then
 CFLAGS="$CFLAGS -fprofile-arcs -ftest-coverage"
-CFLAGS="$CXXFLAGS -fprofile-arcs -ftest-coverage"
+CXXFLAGS="$CXXFLAGS -fprofile-arcs -ftest-coverage"
   else
 AC_MSG_ERROR([--enable-coverage is supported only when using GCC])
   fi

Andreas



Re: JIT compiling with LLVM v10.0

2018-02-07 Thread Andres Freund
On 2018-02-08 11:50:17 +1300, Thomas Munro wrote:
> You are asking LLVM to dlopen(""), which doesn't work on my not-Linux,
> explaining the errors I reported in the older thread.  The portable
> way to dlopen the main binary is dlopen(NULL), so I think you need to
> pass NULL in to LLVMLoadLibraryPermanently(), even though that isn't
> really clear from any LLVM documentation I've looked at.

Ugh. Thanks for figuring that out, will incorporate!

Greetings,

Andres Freund



Re: JIT compiling with LLVM v10.0

2018-02-07 Thread Thomas Munro
On Thu, Feb 8, 2018 at 3:54 AM, Andres Freund  wrote:
> I've pushed v10.0. The big (and pretty painful to make) change is that
> now all the LLVM specific code lives in src/backend/jit/llvm, which is
> built as a shared library which is loaded on demand.
>
> The layout is now as follows:
>
> src/backend/jit/jit.c:
> Part of JITing always linked into the server. Supports loading the
> LLVM using JIT library.
>
> src/backend/jit/llvm/
> Infrastructure:
>  llvmjit.c:
> General code generation and optimization infrastructure
>  llvmjit_error.cpp, llvmjit_wrap.cpp:
> Error / backward compat wrappers
>  llvmjit_inline.cpp:
> Cross module inlining support
> Code-Gen:
>   llvmjit_expr.c
> Expression compilation
>   llvmjit_deform.c
> Deform compilation

You are asking LLVM to dlopen(""), which doesn't work on my not-Linux,
explaining the errors I reported in the older thread.  The portable
way to dlopen the main binary is dlopen(NULL), so I think you need to
pass NULL in to LLVMLoadLibraryPermanently(), even though that isn't
really clear from any LLVM documentation I've looked at.

diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index a1bc6449f7..874bddf81e 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -443,7 +443,7 @@ llvm_session_initialize(void)
cpu = NULL;

/* force symbols in main binary to be loaded */
-   LLVMLoadLibraryPermanently("");
+   LLVMLoadLibraryPermanently(NULL);

llvm_opt0_orc = LLVMOrcCreateInstance(llvm_opt0_targetmachine);
llvm_opt3_orc = LLVMOrcCreateInstance(llvm_opt3_targetmachine);

-- 
Thomas Munro
http://www.enterprisedb.com



Re: JIT compiling with LLVM v10.0

2018-02-07 Thread Andres Freund
Hi,

On 2018-02-07 20:35:12 +0100, Pierre Ducroquet wrote:
> I also find it more readable and it looks cleaner, insane guys could be able 
> to write their own JIT engines for PostgreSQL by patching a single
> file :)

Right - we could easily make the libname configurable if requested.


> Since it's now in its own .so file, does it still make as much sense using 
> mostly the LLVM C API ?

Yes, I definitely want to continue that. For one the C API is a *lot*
more stable, for another postgres is C.


> I included a small addition to the gitignore file, I'm surprised you were not 
> bothered by the various .bc files generated.

I use a VPATH build (i.e. source code is in a different directory than
the build products), so I do not really see that. But yes, it makes
sense to add ignores

Thanks for looking,

Andres Freund



Re: JIT compiling with LLVM v10.0

2018-02-07 Thread Pierre Ducroquet
On Wednesday, February 7, 2018 3:54:05 PM CET Andres Freund wrote:
> Hi,
> 
> I've pushed v10.0. The big (and pretty painful to make) change is that
> now all the LLVM specific code lives in src/backend/jit/llvm, which is
> built as a shared library which is loaded on demand.
> 
> The layout is now as follows:
> 
> src/backend/jit/jit.c:
> Part of JITing always linked into the server. Supports loading the
> LLVM using JIT library.
> 
> src/backend/jit/llvm/
> Infrastructure:
>  llvmjit.c:
> General code generation and optimization infrastructure
>  llvmjit_error.cpp, llvmjit_wrap.cpp:
> Error / backward compat wrappers
>  llvmjit_inline.cpp:
> Cross module inlining support
> Code-Gen:
>   llvmjit_expr.c
> Expression compilation
>   llvmjit_deform.c
> Deform compilation
> 
> I generally like how this shaped out. There's a good amount of followup
> cleanup needed, but I'd appreciate some early feedback.

Hi

I also find it more readable and it looks cleaner, insane guys could be able 
to write their own JIT engines for PostgreSQL by patching a single file :) 
Since it's now in its own .so file, does it still make as much sense using 
mostly the LLVM C API ?
I'll really look in the jit code itself later, right now I've just rebased my 
previous patches and did a quick check that everything worked for LLVM4 and 
3.9.
I included a small addition to the gitignore file, I'm surprised you were not 
bothered by the various .bc files generated.

Anyway, great work, and I look forward exploring the code :)

 Pierre
>From f461c3d6d0deec52b66f3276a87cfdb3ab65b259 Mon Sep 17 00:00:00 2001
From: Pierre 
Date: Fri, 2 Feb 2018 09:11:55 +0100
Subject: [PATCH 1/8] Add support for LLVM4 in llvmjit.c

---
 src/backend/jit/llvm/llvmjit.c | 21 ++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/backend/jit/llvm/llvmjit.c b/src/backend/jit/llvm/llvmjit.c
index a1bc6449f7..e81764af98 100644
--- a/src/backend/jit/llvm/llvmjit.c
+++ b/src/backend/jit/llvm/llvmjit.c
@@ -215,12 +215,19 @@ llvm_get_function(LLVMJitContext *context, const char *funcname)
 
 		addr = 0;
 		if (LLVMOrcGetSymbolAddressIn(handle->stack, &addr, handle->orc_handle, mangled))
-			elog(ERROR, "failed to lookup symbol");
+			elog(ERROR, "failed to lookup symbol %s", mangled);
 		if (addr)
 			return (void *) addr;
 	}
 #endif
 
+#if LLVM_VERSION_MAJOR < 5
+	if ((addr = LLVMOrcGetSymbolAddress(llvm_opt0_orc, mangled)))
+		return (void *) addr;
+	if ((addr = LLVMOrcGetSymbolAddress(llvm_opt3_orc, mangled)))
+		return (void *) addr;
+	elog(ERROR, "failed to lookup symbol %s for %s", mangled, funcname);
+#else
 	if (LLVMOrcGetSymbolAddress(llvm_opt0_orc, &addr, mangled))
 		elog(ERROR, "failed to lookup symbol");
 	if (addr)
@@ -229,7 +236,7 @@ llvm_get_function(LLVMJitContext *context, const char *funcname)
 		elog(ERROR, "failed to lookup symbol");
 	if (addr)
 		return (void *) addr;
-
+#endif
 	elog(ERROR, "failed to JIT: %s", funcname);
 
 	return NULL;
@@ -365,11 +372,18 @@ llvm_compile_module(LLVMJitContext *context)
 	 * faster instruction selection mechanism is used.
 	 */
 	{
-		LLVMSharedModuleRef smod;
 		instr_time tb, ta;
 
 		/* emit the code */
 		INSTR_TIME_SET_CURRENT(ta);
+#if LLVM_VERSION_MAJOR < 5
+		orc_handle = LLVMOrcAddEagerlyCompiledIR(compile_orc, context->module,
+			 llvm_resolve_symbol, NULL);
+		// It seems there is no error return from that function in LLVM < 5.
+#else
+		LLVMSharedModuleRef smod;
+
+		LLVMSharedModuleRef smod;
 		smod = LLVMOrcMakeSharedModule(context->module);
 		if (LLVMOrcAddEagerlyCompiledIR(compile_orc, &orc_handle, smod,
 		llvm_resolve_symbol, NULL))
@@ -377,6 +391,7 @@ llvm_compile_module(LLVMJitContext *context)
 			elog(ERROR, "failed to jit module");
 		}
 		LLVMOrcDisposeSharedModuleRef(smod);
+#endif
 		INSTR_TIME_SET_CURRENT(tb);
 		INSTR_TIME_SUBTRACT(tb, ta);
 		ereport(DEBUG1, (errmsg("time to emit: %.3fs",
-- 
2.16.1

>From 7c34e2264edad3d2d99ba2ba6fbf41ec3a73e0ed Mon Sep 17 00:00:00 2001
From: Pierre 
Date: Fri, 2 Feb 2018 09:13:40 +0100
Subject: [PATCH 2/8] Add LLVM4 support in llvmjit_error.cpp

---
 src/backend/jit/llvm/llvmjit_error.cpp | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/src/backend/jit/llvm/llvmjit_error.cpp b/src/backend/jit/llvm/llvmjit_error.cpp
index 625ba2d25d..1c78bd956d 100644
--- a/src/backend/jit/llvm/llvmjit_error.cpp
+++ b/src/backend/jit/llvm/llvmjit_error.cpp
@@ -56,7 +56,9 @@ llvm_enter_fatal_on_oom(void)
 	if (fatal_new_handler_depth == 0)
 	{
 		old_new_handler = std::set_new_handler(fatal_system_new_handler);
+#if LLVM_VERSION_MAJOR > 4
 		llvm::install_bad_alloc_error_handler(fatal_llvm_new_handler);
+#endif
 		llvm::install_fatal_error_handler(fatal_llvm_error_handler);
 	}
 	fatal_new_handler_depth++;
@@ -72,7 +74,9 @@ llvm_leave_fatal_on_oom(void)
 	if (fatal_new_handler_depth == 0)
 	{
 		std::set_new_handler(old_new_handler);
+#if LLVM_VERSION_MAJOR > 4
 		llvm::remove_ba

Re: JIT compiling with LLVM v10.0

2018-02-07 Thread Andres Freund
Hi,

I've pushed v10.0. The big (and pretty painful to make) change is that
now all the LLVM specific code lives in src/backend/jit/llvm, which is
built as a shared library which is loaded on demand.

The layout is now as follows:

src/backend/jit/jit.c:
Part of JITing always linked into the server. Supports loading the
LLVM using JIT library.

src/backend/jit/llvm/
Infrastructure:
 llvmjit.c:
General code generation and optimization infrastructure
 llvmjit_error.cpp, llvmjit_wrap.cpp:
Error / backward compat wrappers
 llvmjit_inline.cpp:
Cross module inlining support
Code-Gen:
  llvmjit_expr.c
Expression compilation
  llvmjit_deform.c
Deform compilation

I generally like how this shaped out. There's a good amount of followup
cleanup needed, but I'd appreciate some early feedback.


I've also rebased onto a recent master version.

postgres[21915][1]=# SELECT pg_llvmjit_available();
┌──┐
│ pg_llvmjit_available │
├──┤
│ t│
└──┘
(1 row)

make -C src/backend/jit/llvm/ uninstall
postgres[21915][1]=# \c
You are now connected to database "postgres" as user "andres".
postgres[21922][1]=# SELECT pg_llvmjit_available();
┌──┐
│ pg_llvmjit_available │
├──┤
│ f│
└──┘
(1 row)

Yeha ;)

Greetings,

Andres Freund