xref: /llvm-project/offload/include/OpenMP/OMPT/Connector.h (revision 330d8983d25d08580fc1642fea48b2473f47a9da)
1 //===-- OpenMP/OMPT/Connector.h - OpenMP Tooling lib connector -*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef OMPTARGET_OPENMP_OMPT_CONNECTOR_H
16 #define OMPTARGET_OPENMP_OMPT_CONNECTOR_H
17 
18 #ifdef OMPT_SUPPORT
19 
20 #include "llvm/Support/DynamicLibrary.h"
21 
22 #include <memory>
23 #include <string>
24 
25 #include "omp-tools.h"
26 #include "omptarget.h"
27 
28 #include "Shared/Debug.h"
29 
30 #pragma push_macro("DEBUG_PREFIX")
31 #undef DEBUG_PREFIX
32 #define DEBUG_PREFIX "OMPT"
33 
34 /// Type for the function to be invoked for connecting two libraries.
35 typedef void (*OmptConnectRtnTy)(ompt_start_tool_result_t *result);
36 
37 /// Establish connection between openmp runtime libraries
38 ///
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
48 /// invoked.
49 class OmptLibraryConnectorTy {
50 public:
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.
OmptLibraryConnectorTy(const char * Ident)54   OmptLibraryConnectorTy(const char *Ident) {
55     LibIdent.append(Ident);
56     IsInitialized = false;
57   }
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.
connect(ompt_start_tool_result_t * OmptResult)62   void connect(ompt_start_tool_result_t *OmptResult) {
63     initialize();
64     if (!LibConnHandle)
65       return;
66     // Call the function provided by the source library for connect
67     LibConnHandle(OmptResult);
68   }
69 
70 private:
initialize()71   void initialize() {
72     if (IsInitialized)
73       return;
74 
75     std::string ErrMsg;
76     std::string LibName = LibIdent;
77     LibName += ".so";
78 
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(),
82                                                        &ErrMsg));
83     if (!DynLibHandle->isValid()) {
84       // The upper layer will bail out if the handle is null.
85       LibConnHandle = nullptr;
86     } else {
87       auto LibConnRtn = "ompt_" + LibIdent + "_connect";
88       DP("OMPT: Trying to get address of connection routine %s\n",
89          LibConnRtn.c_str());
90       LibConnHandle = reinterpret_cast<OmptConnectRtnTy>(
91           DynLibHandle->getAddressOfSymbol(LibConnRtn.c_str()));
92     }
93     DP("OMPT: Library connection handle = %p\n", LibConnHandle);
94     IsInitialized = true;
95   }
96 
97   /// Ensure initialization occurs only once
98   bool IsInitialized;
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;
103 };
104 
105 #endif // OMPT_SUPPORT
106 
107 #pragma pop_macro("DEBUG_PREFIX")
108 
109 #endif // OMPTARGET_OPENMP_OMPT_CONNECTOR_H
110