|
| 1 | +//=== ompt_connector.h - Target independent OpenMP target RTL -- 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 _OMPT_CONNECTOR_H |
| 16 | +#define _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 "Debug.h" |
| 26 | +#include "omp-tools.h" |
| 27 | +#include "omptarget.h" |
| 28 | + |
| 29 | +#define LIBOMPTARGET_STRINGIFY(s) #s |
| 30 | + |
| 31 | +/// Type for the function to be invoked for connecting two libraries. |
| 32 | +typedef void (*OmptConnectRtnTy)(ompt_start_tool_result_t *result); |
| 33 | + |
| 34 | +/// Establish connection between openmp runtime libraries |
| 35 | +/// |
| 36 | +/// This class is used to communicate between an OMPT implementation in |
| 37 | +/// libomptarget and libomp. It is also used to communicate between an |
| 38 | +/// OMPT implementation in a device-specific plugin and |
| 39 | +/// libomptarget. The decision whether OMPT is enabled or not needs to |
| 40 | +/// be made when the library is loaded before any functions in the |
| 41 | +/// library are invoked. For that reason, an instance of this class is |
| 42 | +/// intended to be defined in the constructor for libomptarget or a |
| 43 | +/// plugin so that the decision about whether OMPT is supposed to be |
| 44 | +/// enabled is known before any interface function in the library is |
| 45 | +/// invoked. |
| 46 | +class OmptLibraryConnectorTy { |
| 47 | +public: |
| 48 | + /// Use \p LibName as the prefix of the global function used for connecting |
| 49 | + /// two libraries, the source indicated by \p LibName and the destination |
| 50 | + /// being the one that creates this object. |
| 51 | + OmptLibraryConnectorTy(const char *Ident) { |
| 52 | + LibIdent.append(Ident); |
| 53 | + IsInitialized = false; |
| 54 | + } |
| 55 | + OmptLibraryConnectorTy() = delete; |
| 56 | + /// Use \p OmptResult init to connect the two libraries denoted by this |
| 57 | + /// object. The init function of \p OmptResult will be used during connection |
| 58 | + /// and the fini function of \p OmptResult will be used during teardown. |
| 59 | + void connect(ompt_start_tool_result_t *OmptResult) { |
| 60 | + initialize(); |
| 61 | + if (!LibConnHandle) |
| 62 | + return; |
| 63 | + // Call the function provided by the source library for connect |
| 64 | + LibConnHandle(OmptResult); |
| 65 | + } |
| 66 | + |
| 67 | +private: |
| 68 | + void initialize() { |
| 69 | + if (IsInitialized) |
| 70 | + return; |
| 71 | + |
| 72 | + std::string ErrMsg; |
| 73 | + std::string LibName = LibIdent; |
| 74 | + LibName += ".so"; |
| 75 | + |
| 76 | + DP("OMPT: Trying to load library %s\n", LibName.c_str()); |
| 77 | + auto DynLibHandle = std::make_unique<llvm::sys::DynamicLibrary>( |
| 78 | + llvm::sys::DynamicLibrary::getPermanentLibrary(LibName.c_str(), |
| 79 | + &ErrMsg)); |
| 80 | + if (!DynLibHandle->isValid()) { |
| 81 | + // The upper layer will bail out if the handle is null. |
| 82 | + LibConnHandle = nullptr; |
| 83 | + } else { |
| 84 | + auto LibConnRtn = "ompt_" + LibIdent + "_connect"; |
| 85 | + DP("OMPT: Trying to get address of connection routine %s\n", |
| 86 | + LibConnRtn.c_str()); |
| 87 | + LibConnHandle = reinterpret_cast<OmptConnectRtnTy>( |
| 88 | + DynLibHandle->getAddressOfSymbol(LibConnRtn.c_str())); |
| 89 | + } |
| 90 | + DP("OMPT: Library connection handle = %p\n", LibConnHandle); |
| 91 | + IsInitialized = true; |
| 92 | + } |
| 93 | + |
| 94 | + /// Ensure initialization occurs only once |
| 95 | + bool IsInitialized; |
| 96 | + /// Handle of connect routine provided by source library |
| 97 | + OmptConnectRtnTy LibConnHandle; |
| 98 | + /// Name of connect routine provided by source library |
| 99 | + std::string LibIdent; |
| 100 | +}; |
| 101 | + |
| 102 | +#endif // OMPT_SUPPORT |
| 103 | + |
| 104 | +#endif // _OMPT_CONNECTOR_H |
0 commit comments