1 //===- OmptConnector.h - Target independent OpenMP target RTL -- C++ ------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Support used by OMPT implementation to establish communication between
10 // various OpenMP runtime libraries: host openmp library, target-independent
11 // runtime library, and device-dependent runtime libraries.
13 //===----------------------------------------------------------------------===//
15 #ifndef _OMPTCONNECTOR_H
16 #define _OMPTCONNECTOR_H
20 #include "llvm/Support/DynamicLibrary.h"
25 #include "omp-tools.h"
28 #include "omptarget.h"
30 #pragma push_macro("DEBUG_PREFIX")
32 #define DEBUG_PREFIX "OMPT"
34 /// Type for the function to be invoked for connecting two libraries.
35 typedef void (*OmptConnectRtnTy
)(ompt_start_tool_result_t
*result
);
37 /// Establish connection between openmp runtime libraries
39 /// This class is used to communicate between an OMPT implementation in
40 /// libomptarget and libomp. It is also used to communicate between an
41 /// OMPT implementation in a device-specific plugin and
42 /// libomptarget. The decision whether OMPT is enabled or not needs to
43 /// be made when the library is loaded before any functions in the
44 /// library are invoked. For that reason, an instance of this class is
45 /// intended to be defined in the constructor for libomptarget or a
46 /// plugin so that the decision about whether OMPT is supposed to be
47 /// enabled is known before any interface function in the library is
49 class OmptLibraryConnectorTy
{
51 /// Use \p LibName as the prefix of the global function used for connecting
52 /// two libraries, the source indicated by \p LibName and the destination
53 /// being the one that creates this object.
54 OmptLibraryConnectorTy(const char *Ident
) {
55 LibIdent
.append(Ident
);
56 IsInitialized
= false;
58 OmptLibraryConnectorTy() = delete;
59 /// Use \p OmptResult init to connect the two libraries denoted by this
60 /// object. The init function of \p OmptResult will be used during connection
61 /// and the fini function of \p OmptResult will be used during teardown.
62 void connect(ompt_start_tool_result_t
*OmptResult
) {
66 // Call the function provided by the source library for connect
67 LibConnHandle(OmptResult
);
76 std::string LibName
= LibIdent
;
79 DP("OMPT: Trying to load library %s\n", LibName
.c_str());
80 auto DynLibHandle
= std::make_unique
<llvm::sys::DynamicLibrary
>(
81 llvm::sys::DynamicLibrary::getPermanentLibrary(LibName
.c_str(),
83 if (!DynLibHandle
->isValid()) {
84 // The upper layer will bail out if the handle is null.
85 LibConnHandle
= nullptr;
87 auto LibConnRtn
= "ompt_" + LibIdent
+ "_connect";
88 DP("OMPT: Trying to get address of connection routine %s\n",
90 LibConnHandle
= reinterpret_cast<OmptConnectRtnTy
>(
91 DynLibHandle
->getAddressOfSymbol(LibConnRtn
.c_str()));
93 DP("OMPT: Library connection handle = %p\n", LibConnHandle
);
97 /// Ensure initialization occurs only once
99 /// Handle of connect routine provided by source library
100 OmptConnectRtnTy LibConnHandle
;
101 /// Name of connect routine provided by source library
102 std::string LibIdent
;
105 #endif // OMPT_SUPPORT
107 #pragma pop_macro("DEBUG_PREFIX")
109 #endif // _OMPTCONNECTOR_H