From 4f85a16ba716d647645525e60d950374e88560d2 Mon Sep 17 00:00:00 2001 From: Nazim Bhuiyan Date: Thu, 11 Aug 2022 16:41:04 -0600 Subject: [PATCH] Enable PortLib init/shutdown in JitBuiler and compiler tests This change ensures that Port library is initialized and shutdown when JitBuilder and testcompiler are initialized and shutdown. This paves the way for using port library features within OMR Compiler code and replace duplicated functionality through use of the port library API. Signed-off-by: Nazim Bhuiyan --- compiler/env/CompilerEnv.hpp | 6 +++--- fvtest/compilertest/control/TestJit.cpp | 25 ++++++++++++++++++++++-- fvtest/tril/tril/CMakeLists.txt | 3 ++- jitbuilder/control/Jit.cpp | 26 +++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/compiler/env/CompilerEnv.hpp b/compiler/env/CompilerEnv.hpp index 5639be67cfc..6bfd641015e 100644 --- a/compiler/env/CompilerEnv.hpp +++ b/compiler/env/CompilerEnv.hpp @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2019 IBM Corp. and others + * Copyright (c) 2000, 2022 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -32,8 +32,8 @@ namespace TR class OMR_EXTENSIBLE CompilerEnv : public OMR::CompilerEnvConnector { public: - CompilerEnv(TR::RawAllocator raw, const TR::PersistentAllocatorKit &persistentAllocatorKit) : - OMR::CompilerEnvConnector(raw, persistentAllocatorKit) + CompilerEnv(TR::RawAllocator raw, const TR::PersistentAllocatorKit &persistentAllocatorKit, OMRPortLibrary* const omrPortLib=NULL) : + OMR::CompilerEnvConnector(raw, persistentAllocatorKit, omrPortLib) {} }; diff --git a/fvtest/compilertest/control/TestJit.cpp b/fvtest/compilertest/control/TestJit.cpp index c2a8a730689..85049555810 100644 --- a/fvtest/compilertest/control/TestJit.cpp +++ b/fvtest/compilertest/control/TestJit.cpp @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 IBM Corp. and others + * Copyright (c) 2000, 2022 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -38,6 +38,8 @@ extern TR_RuntimeHelperTable runtimeHelpers; extern void setupCodeCacheParameters(int32_t *, OMR::CodeCacheCodeGenCallbacks *callBacks, int32_t *numHelpers, int32_t *CCPreLoadedCodeSize); +static OMRPortLibrary portLibrary; + static void initHelper(void *helper, TR_RuntimeHelper id) { @@ -150,11 +152,19 @@ initializeTestJit(TR_RuntimeHelper *helperIDs, void **helperAddresses, int32_t n // TR::RawAllocator rawAllocator; + // initialize and attach current thread to thread library, and then initialize port library + if (0 == omrthread_init_library()) + { + omrthread_t currentThread = NULL; + if (0 == omrthread_attach_ex(¤tThread, J9THREAD_ATTR_DEFAULT)) + omrport_init_library(&portLibrary, sizeof(OMRPortLibrary)); + } + try { // Allocate the host environment structure // - TR::Compiler = new (rawAllocator) TR::CompilerEnv(rawAllocator, TR::PersistentAllocatorKit(rawAllocator)); + TR::Compiler = new (rawAllocator) TR::CompilerEnv(rawAllocator, TR::PersistentAllocatorKit(rawAllocator), &portLibrary); } catch (const std::bad_alloc&) { @@ -174,6 +184,9 @@ initializeTestJit(TR_RuntimeHelper *helperIDs, void **helperAddresses, int32_t n initializeCodeCache(fe.codeCacheManager()); + if (TR::Compiler->omrPortLib == NULL) + return false; + return true; } @@ -201,6 +214,14 @@ shutdownJit() codeCacheManager.destroy(); TR::CompilationController::shutdown(); + + omrthread_t currentThread = NULL; + if (0 == omrthread_attach_ex(¤tThread, J9THREAD_ATTR_DEFAULT)) + { + portLibrary.port_shutdown_library(&portLibrary); + omrthread_detach(currentThread); + omrthread_shutdown_library(); + } } extern "C" diff --git a/fvtest/tril/tril/CMakeLists.txt b/fvtest/tril/tril/CMakeLists.txt index 0e9486d5db2..d8b5607bc1a 100644 --- a/fvtest/tril/tril/CMakeLists.txt +++ b/fvtest/tril/tril/CMakeLists.txt @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2017, 2021 IBM Corp. and others +# Copyright (c) 2017, 2022 IBM Corp. and others # # This program and the accompanying materials are made available under # the terms of the Eclipse Public License 2.0 which accompanies this @@ -52,6 +52,7 @@ target_include_directories(tril PUBLIC target_link_libraries(tril PUBLIC ${TRIL_BACKEND_LIB} ${CMAKE_DL_LIBS} + ${OMR_PORT_LIB} ) # TODO system thread library should be linked in a more generic way. diff --git a/jitbuilder/control/Jit.cpp b/jitbuilder/control/Jit.cpp index 09d8a9ec65b..63bbc2a13b7 100644 --- a/jitbuilder/control/Jit.cpp +++ b/jitbuilder/control/Jit.cpp @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2014, 2020 IBM Corp. and others + * Copyright (c) 2014, 2022 IBM Corp. and others * * This program and the accompanying materials are made available under * the terms of the Eclipse Public License 2.0 which accompanies this @@ -109,6 +109,8 @@ initializeCodeCache(TR::CodeCacheManager & codeCacheManager) TR::CodeCache *firstCodeCache = codeCacheManager.initialize(true, 1); } +static OMRPortLibrary portLibrary; + // helperIDs is an array of helper id corresponding to the addresses passed in "helpers" // helpers is an array of pointers to helpers that compiled code needs to reference // currently this argument isn't needed by anything so this function can stay internal @@ -121,11 +123,19 @@ initializeJitBuilder(TR_RuntimeHelper *helperIDs, void **helperAddresses, int32_ // TR::RawAllocator rawAllocator; + // initialize and attach current thread to thread library, and then initialize port library + if (0 == omrthread_init_library()) + { + omrthread_t currentThread = NULL; + if (0 == omrthread_attach_ex(¤tThread, J9THREAD_ATTR_DEFAULT)) + omrport_init_library(&portLibrary, sizeof(OMRPortLibrary)); + } + try { // Allocate the host environment structure // - TR::Compiler = new (rawAllocator) TR::CompilerEnv(rawAllocator, TR::PersistentAllocatorKit(rawAllocator)); + TR::Compiler = new (rawAllocator) TR::CompilerEnv(rawAllocator, TR::PersistentAllocatorKit(rawAllocator), &portLibrary); } catch (const std::bad_alloc&) { @@ -145,6 +155,9 @@ initializeJitBuilder(TR_RuntimeHelper *helperIDs, void **helperAddresses, int32_ initializeCodeCache(fe.codeCacheManager()); + if (TR::Compiler->omrPortLib == NULL) + return false; + return true; } @@ -219,4 +232,13 @@ internal_shutdownJit() codeCacheManager.destroy(); TR::CompilationController::shutdown(); + + omrthread_t currentThread = NULL; + if (0 == omrthread_attach_ex(¤tThread, J9THREAD_ATTR_DEFAULT)) + { + portLibrary.port_shutdown_library(&portLibrary); + omrthread_detach(currentThread); + omrthread_shutdown_library(); + } + }