LLVMJIT: fix asserts in latest LLVM

Make sure session is initialized before it is used by objectLayer.
Fixes assert related to objectLayer using an invalid
std::recursive_mutex in the session instance.

Also added an explicit resource removal to fix assert in
~RTDyldObjectLinkingLayer().

Bug: b/171402030
Change-Id: I4f628a9ed4afd0126462858f8f0e3e42f90e21b3
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/49508
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Reactor/LLVMJIT.cpp b/src/Reactor/LLVMJIT.cpp
index 67cbbda..ba07d8f 100644
--- a/src/Reactor/LLVMJIT.cpp
+++ b/src/Reactor/LLVMJIT.cpp
@@ -484,11 +484,11 @@
 // settings and no Reactor routine directly links against another.
 class JITRoutine : public rr::Routine
 {
+	llvm::orc::ExecutionSession session;
 	llvm::orc::RTDyldObjectLinkingLayer objectLayer;
 	llvm::orc::IRCompileLayer compileLayer;
 	llvm::orc::MangleAndInterner mangle;
 	llvm::orc::ThreadSafeContext ctx;
-	llvm::orc::ExecutionSession session;
 	llvm::orc::JITDylib &dylib;
 	std::vector<const void *> addresses;
 
@@ -564,6 +564,15 @@
 		}
 	}
 
+	~JITRoutine()
+	{
+		// TODO(b/165000222): Unconditional after LLVM 11 upgrade
+#if LLVM_VERSION_MAJOR >= 11
+		// Avoid assert in ~RTDyldObjectLinkingLayer()
+		dylib.getDefaultResourceTracker()->remove();
+#endif
+	}
+
 	const void *getEntry(int index) const override
 	{
 		return addresses[index];