Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / openmp / libomptarget / include / OmptConnector.h
blob75473b8dda486f114b75a7a7748a7134b64d7eeb
1 //===- OmptConnector.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.
13 //===----------------------------------------------------------------------===//
15 #ifndef _OMPTCONNECTOR_H
16 #define _OMPTCONNECTOR_H
18 #ifdef OMPT_SUPPORT
20 #include "llvm/Support/DynamicLibrary.h"
22 #include <memory>
23 #include <string>
25 #include "omp-tools.h"
27 #include "Debug.h"
28 #include "omptarget.h"
30 #pragma push_macro("DEBUG_PREFIX")
31 #undef 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
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.
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) {
63 initialize();
64 if (!LibConnHandle)
65 return;
66 // Call the function provided by the source library for connect
67 LibConnHandle(OmptResult);
70 private:
71 void initialize() {
72 if (IsInitialized)
73 return;
75 std::string ErrMsg;
76 std::string LibName = LibIdent;
77 LibName += ".so";
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()));
93 DP("OMPT: Library connection handle = %p\n", LibConnHandle);
94 IsInitialized = true;
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;
105 #endif // OMPT_SUPPORT
107 #pragma pop_macro("DEBUG_PREFIX")
109 #endif // _OMPTCONNECTOR_H