[llvm] [openmp] [Offload][OMPT][NFCI] Call ompt_libomp_connect instead of dlopen libomp (PR #214556)
Michael Halkenhäuser via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 7 08:20:07 PDT 2026
https://github.com/mhalk updated https://github.com/llvm/llvm-project/pull/214556
>From c987dd8e317ad9f9c556d492febf6588f35fc151 Mon Sep 17 00:00:00 2001
From: Michael Halkenhaeuser <MichaelGerald.Halkenhauser at amd.com>
Date: Thu, 6 Aug 2026 16:44:22 +0000
Subject: [PATCH] [Offload][OMPT][NFCI] Call ompt_libomp_connect instead of
dlopen libomp
connectLibrary() reached libomp's ompt_libomp_connect through
OmptLibraryConnectorTy, which dlopens "libomp.so" and resolves the symbol at
run time. That cannot do anything the linker has not already done:
libomptarget links libomp and imports seven __kmpc_* symbols from it, so
libomp.so is a DT_NEEDED entry, and libomp defines and exports
ompt_libomp_connect unconditionally -- as a stub in kmp_utility.cpp when
built without OMPT support. Declare it, call it, and delete Connector.h.
AI-assisted.
---
offload/include/OpenMP/OMPT/Callback.h | 8 +-
offload/include/OpenMP/OMPT/Connector.h | 112 ------------------
offload/libomptarget/OpenMP/OMPT/Callback.cpp | 17 ++-
openmp/runtime/src/ompt-general.cpp | 4 +-
4 files changed, 11 insertions(+), 130 deletions(-)
delete mode 100644 offload/include/OpenMP/OMPT/Connector.h
diff --git a/offload/include/OpenMP/OMPT/Callback.h b/offload/include/OpenMP/OMPT/Callback.h
index 9d545c643223f..2bc3f59dcd3ee 100644
--- a/offload/include/OpenMP/OMPT/Callback.h
+++ b/offload/include/OpenMP/OMPT/Callback.h
@@ -69,18 +69,14 @@ extern ompt_get_callback_t lookupCallbackByCode;
/// \p InterfaceFunctionName the name of the OMPT callback function to look up
extern ompt_function_lookup_t lookupCallbackByName;
-/// This is the function called by the higher layer (libomp / libomtarget)
-/// responsible for initializing OMPT in this library. This is passed to libomp
-/// as part of the OMPT connector object.
+/// Initializes OMPT in this library. Passed to libomp via ompt_libomp_connect.
/// \p lookup to be used to query callbacks registered with libomp
/// \p initial_device_num initial device num (id) provided by libomp
/// \p tool_data as provided by the tool
int initializeLibrary(ompt_function_lookup_t lookup, int initial_device_num,
ompt_data_t *tool_data);
-/// This function is passed to libomp / libomtarget as part of the OMPT
-/// connector object. It is called by libomp during finalization of OMPT in
-/// libomptarget -OR- by libomptarget during finalization of OMPT in the plugin.
+/// Finalizes OMPT in this library. Passed to libomp via ompt_libomp_connect.
/// \p tool_data as provided by the tool
void finalizeLibrary(ompt_data_t *tool_data);
diff --git a/offload/include/OpenMP/OMPT/Connector.h b/offload/include/OpenMP/OMPT/Connector.h
deleted file mode 100644
index add8941cc4905..0000000000000
--- a/offload/include/OpenMP/OMPT/Connector.h
+++ /dev/null
@@ -1,112 +0,0 @@
-//===-- OpenMP/OMPT/Connector.h - OpenMP Tooling lib connector -*- C++ -*-===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-//
-// Support used by OMPT implementation to establish communication between
-// various OpenMP runtime libraries: host openmp library, target-independent
-// runtime library, and device-dependent runtime libraries.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef OMPTARGET_OPENMP_OMPT_CONNECTOR_H
-#define OMPTARGET_OPENMP_OMPT_CONNECTOR_H
-
-#ifdef OMPT_SUPPORT
-
-#include "llvm/Support/DynamicLibrary.h"
-
-#include <memory>
-#include <string>
-
-#include "omp-tools.h"
-#include "omptarget.h"
-
-#include "Shared/Debug.h"
-
-using namespace llvm::omp::target::debug;
-
-#pragma push_macro("DEBUG_PREFIX")
-#undef DEBUG_PREFIX
-#define DEBUG_PREFIX "OMPT"
-
-/// Type for the function to be invoked for connecting two libraries.
-typedef void (*OmptConnectRtnTy)(ompt_start_tool_result_t *result);
-
-/// Establish connection between openmp runtime libraries
-///
-/// This class is used to communicate between an OMPT implementation in
-/// libomptarget and libomp. It is also used to communicate between an
-/// OMPT implementation in a device-specific plugin and
-/// libomptarget. The decision whether OMPT is enabled or not needs to
-/// be made when the library is loaded before any functions in the
-/// library are invoked. For that reason, an instance of this class is
-/// intended to be defined in the constructor for libomptarget or a
-/// plugin so that the decision about whether OMPT is supposed to be
-/// enabled is known before any interface function in the library is
-/// invoked.
-class OmptLibraryConnectorTy {
-public:
- /// Use \p LibName as the prefix of the global function used for connecting
- /// two libraries, the source indicated by \p LibName and the destination
- /// being the one that creates this object.
- OmptLibraryConnectorTy(const char *Ident) {
- LibIdent.append(Ident);
- IsInitialized = false;
- }
- OmptLibraryConnectorTy() = delete;
- /// Use \p OmptResult init to connect the two libraries denoted by this
- /// object. The init function of \p OmptResult will be used during connection
- /// and the fini function of \p OmptResult will be used during teardown.
- void connect(ompt_start_tool_result_t *OmptResult) {
- initialize();
- if (!LibConnHandle)
- return;
- // Call the function provided by the source library for connect
- LibConnHandle(OmptResult);
- }
-
-private:
- void initialize() {
- if (IsInitialized)
- return;
-
- std::string ErrMsg;
- std::string LibName = LibIdent;
- LibName += ".so";
-
- ODBG(ODT_Tool) << "OMPT: Trying to load library " << LibName;
- auto DynLibHandle = std::make_unique<llvm::sys::DynamicLibrary>(
- llvm::sys::DynamicLibrary::getPermanentLibrary(LibName.c_str(),
- &ErrMsg));
- if (!DynLibHandle->isValid()) {
- // The upper layer will bail out if the handle is null.
- LibConnHandle = nullptr;
- } else {
- auto LibConnRtn = "ompt_" + LibIdent + "_connect";
- ODBG(ODT_Tool) << "OMPT: Trying to get address of connection routine "
- << LibConnRtn;
- LibConnHandle = reinterpret_cast<OmptConnectRtnTy>(
- DynLibHandle->getAddressOfSymbol(LibConnRtn.c_str()));
- }
- ODBG(ODT_Tool) << "OMPT: Library connection handle = "
- << reinterpret_cast<void *>(LibConnHandle);
- IsInitialized = true;
- }
-
- /// Ensure initialization occurs only once
- bool IsInitialized;
- /// Handle of connect routine provided by source library
- OmptConnectRtnTy LibConnHandle;
- /// Name of connect routine provided by source library
- std::string LibIdent;
-};
-
-#endif // OMPT_SUPPORT
-
-#pragma pop_macro("DEBUG_PREFIX")
-
-#endif // OMPTARGET_OPENMP_OMPT_CONNECTOR_H
diff --git a/offload/libomptarget/OpenMP/OMPT/Callback.cpp b/offload/libomptarget/OpenMP/OMPT/Callback.cpp
index 1e03f1455d1b2..150fcb85015f7 100644
--- a/offload/libomptarget/OpenMP/OMPT/Callback.cpp
+++ b/offload/libomptarget/OpenMP/OMPT/Callback.cpp
@@ -19,14 +19,15 @@
#include "Shared/Debug.h"
#include "OpenMP/OMPT/Callback.h"
-#include "OpenMP/OMPT/Connector.h"
#include "OpenMP/OMPT/Interface.h"
-#include "llvm/Support/DynamicLibrary.h"
-
#undef DEBUG_PREFIX
#define DEBUG_PREFIX "OMPT"
+/// Registers this library's initialize and finalize functions with libomp,
+/// which always defines this entry point (a stub if built without OMPT).
+extern "C" void ompt_libomp_connect(ompt_start_tool_result_t *);
+
// Define OMPT callback functions (bound to actual callbacks later on)
#define defineOmptCallback(Name, Type, Code) \
Name##_t llvm::omp::target::ompt::Name##_fn = nullptr;
@@ -537,18 +538,14 @@ void llvm::omp::target::ompt::finalizeLibrary(ompt_data_t *data) {
void llvm::omp::target::ompt::connectLibrary() {
ODBG(ODT_Tool) << "Entering connectLibrary";
- // Connect with libomp
- static OmptLibraryConnectorTy LibompConnector("libomp");
+ // libomp retains this pointer to run the finalizer
static ompt_start_tool_result_t OmptResult;
-
- // Initialize OmptResult with the init and fini functions that will be
- // called by the connector
OmptResult.initialize = ompt::initializeLibrary;
OmptResult.finalize = ompt::finalizeLibrary;
OmptResult.tool_data.value = 0;
- // Now call connect that causes the above init/fini functions to be called
- LibompConnector.connect(&OmptResult);
+ // Calls initializeLibrary if a tool enabled OMPT
+ ompt_libomp_connect(&OmptResult);
#define bindOmptCallback(Name, Type, Code) \
if (lookupCallbackByCode) \
diff --git a/openmp/runtime/src/ompt-general.cpp b/openmp/runtime/src/ompt-general.cpp
index 8f1ba4910815e..79b56f35fb5be 100644
--- a/openmp/runtime/src/ompt-general.cpp
+++ b/openmp/runtime/src/ompt-general.cpp
@@ -948,8 +948,8 @@ static ompt_interface_fn_t ompt_libomp_target_fn_lookup(const char *s) {
return (ompt_interface_fn_t)0;
}
-/// This function is called by the libomptarget connector to assign
-/// callbacks already registered with libomp.
+/// This function is called by libomptarget to assign callbacks already
+/// registered with libomp.
_OMP_EXTERN void ompt_libomp_connect(ompt_start_tool_result_t *result) {
OMPT_VERBOSE_INIT_PRINT("libomp --> OMPT: Enter ompt_libomp_connect\n");
More information about the llvm-commits
mailing list