Skip to content

Commit

Permalink
[NFC][offload][OMPT] Cleanup of OMPT internals
Browse files Browse the repository at this point in the history
Removed OmptCallbacks.cpp since relevant contents were duplicated.
Because of the static linking there should be no change in functionality.

This commit is meant to replace the following upstream PR:
llvm#109005

Change-Id: Iddc56ae559f7387056bdcf2d6b8860ab9fc5956a
  • Loading branch information
mhalk authored and ronlieb committed Sep 23, 2024
1 parent b0c4093 commit 65fd8ab
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 147 deletions.
13 changes: 6 additions & 7 deletions offload/include/OmptCommonDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef OPENMP_LIBOMPTARGET_INCLUDE_OMPTCOMMONDEFS_H
#define OPENMP_LIBOMPTARGET_INCLUDE_OMPTCOMMONDEFS_H
#ifndef OFFLOAD_INCLUDE_OMPTCOMMONDEFS_H
#define OFFLOAD_INCLUDE_OMPTCOMMONDEFS_H

#ifdef OMPT_SUPPORT

Expand Down Expand Up @@ -47,14 +47,13 @@

#define OMPT_API_ROUTINE static

#define OMPT_CALLBACK_AVAILABLE(fn) \
(llvm::omp::target::ompt::CallbacksInitialized && fn)
#define OMPT_CALLBACK_AVAILABLE(fn) (llvm::omp::target::ompt::Initialized && fn)

#define OMPT_IF_BUILT(stmt) stmt

#define OMPT_IF_ENABLED(stmts) \
do { \
if (llvm::omp::target::ompt::CallbacksInitialized) { \
if (llvm::omp::target::ompt::Initialized) { \
stmts \
} \
} while (0)
Expand All @@ -80,7 +79,7 @@

#define performIfOmptInitialized(stmt) \
do { \
if (llvm::omp::target::ompt::CallbacksInitialized) { \
if (llvm::omp::target::ompt::Initialized) { \
stmt; \
} \
} while (0)
Expand Down Expand Up @@ -125,4 +124,4 @@ typedef uint64_t (*IdInterfaceTy)();
#define OMPT_IF_TRACING_ENABLED(stmts)
#endif // OMPT_SUPPORT

#endif // OPENMP_LIBOMPTARGET_INCLUDE_OMPTCOMMONDEFS_H
#endif // OFFLOAD_INCLUDE_OMPTCOMMONDEFS_H
8 changes: 4 additions & 4 deletions offload/include/OpenMP/OMPT/Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef OMPTARGET_OPENMP_OMPT_CALLBACK_H
#define OMPTARGET_OPENMP_OMPT_CALLBACK_H
#ifndef OFFLOAD_INCLUDE_OPENMP_OMPT_CALLBACK_H
#define OFFLOAD_INCLUDE_OPENMP_OMPT_CALLBACK_H

#ifdef OMPT_SUPPORT

Expand Down Expand Up @@ -67,7 +67,7 @@ void finalizeLibrary(ompt_data_t *tool_data);
void connectLibrary();

/// OMPT initialization status; false if initializeLibrary has not been executed
extern bool CallbacksInitialized;
extern bool Initialized;

} // namespace ompt
} // namespace target
Expand All @@ -78,4 +78,4 @@ extern bool CallbacksInitialized;

#endif // OMPT_SUPPORT

#endif // OMPTARGET_OPENMP_OMPT_CALLBACK_H
#endif // OFFLOAD_INCLUDE_OPENMP_OMPT_CALLBACK_H
12 changes: 6 additions & 6 deletions offload/include/OpenMP/OMPT/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef OPENMP_LIBOMPTARGET_SRC_OMPTINTERFACE_H
#define OPENMP_LIBOMPTARGET_SRC_OMPTINTERFACE_H
#ifndef OFFLOAD_INCLUDE_OPENMP_OMPT_INTERFACE_H
#define OFFLOAD_INCLUDE_OPENMP_OMPT_INTERFACE_H

// Only provide functionality if target OMPT support is enabled
#ifdef OMPT_SUPPORT
Expand Down Expand Up @@ -44,9 +44,9 @@ namespace ompt {

/// Function pointers that will be used to track task_data and
/// target_task_data.
extern ompt_get_task_data_t ompt_get_task_data_fn;
extern ompt_get_target_task_data_t ompt_get_target_task_data_fn;
extern ompt_set_frame_enter_t ompt_set_frame_enter_fn;
static ompt_get_task_data_t ompt_get_task_data_fn;
static ompt_get_target_task_data_t ompt_get_target_task_data_fn;
static ompt_set_frame_enter_t ompt_set_frame_enter_fn;

/// OMPT global tracing status. Indicates if at least one device is traced.
extern bool TracingActive;
Expand Down Expand Up @@ -567,4 +567,4 @@ class ReturnAddressSetterRAII {

#endif // OMPT_SUPPORT

#endif // OPENMP_LIBOMPTARGET_SRC_OMPTINTERFACE_H
#endif // OFFLOAD_INCLUDE_OPENMP_OMPT_INTERFACE_H
83 changes: 0 additions & 83 deletions offload/plugins-nextgen/common/OMPT/OmptCallback.cpp

This file was deleted.

10 changes: 5 additions & 5 deletions offload/plugins-nextgen/common/src/PluginInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ GenericDeviceTy::GenericDeviceTy(GenericPluginTy &Plugin, int32_t DeviceId,
OmptInitialized.store(false);
// Bind the callbacks to this device's member functions
#define bindOmptCallback(Name, Type, Code) \
if (ompt::CallbacksInitialized && ompt::lookupCallbackByCode) { \
if (ompt::Initialized && ompt::lookupCallbackByCode) { \
ompt::lookupCallbackByCode((ompt_callbacks_t)(Code), \
((ompt_callback_t *)&(Name##_fn))); \
DP("class bound %s=%p\n", #Name, ((void *)(uint64_t)Name##_fn)); \
Expand All @@ -929,7 +929,7 @@ GenericDeviceTy::GenericDeviceTy(GenericPluginTy &Plugin, int32_t DeviceId,
#undef bindOmptCallback

#define bindOmptTracingFunction(FunctionName) \
if (ompt::CallbacksInitialized && ompt::lookupDeviceTracingFn) { \
if (ompt::Initialized && ompt::lookupDeviceTracingFn) { \
FunctionName##_fn = ompt::lookupDeviceTracingFn(#FunctionName); \
DP("device tracing fn bound %s=%p\n", #FunctionName, \
((void *)(uint64_t)FunctionName##_fn)); \
Expand All @@ -948,7 +948,7 @@ Error GenericDeviceTy::init(GenericPluginTy &Plugin) {
#ifdef OMPT_SUPPORT
auto DevicePtr = reinterpret_cast<ompt_device_t *>(this);
ompt::setDeviceId(DevicePtr, Plugin.getUserId(DeviceId));
if (ompt::CallbacksInitialized) {
if (ompt::Initialized) {
bool ExpectedStatus = false;
if (OmptInitialized.compare_exchange_strong(ExpectedStatus, true))
performOmptCallback(device_initialize, Plugin.getUserId(DeviceId),
Expand Down Expand Up @@ -1060,7 +1060,7 @@ Error GenericDeviceTy::deinit(GenericPluginTy &Plugin) {
return Err;

#ifdef OMPT_SUPPORT
if (ompt::CallbacksInitialized) {
if (ompt::Initialized) {
bool ExpectedStatus = true;
if (OmptInitialized.compare_exchange_strong(ExpectedStatus, false))
performOmptCallback(device_finalize, Plugin.getUserId(DeviceId));
Expand Down Expand Up @@ -1119,7 +1119,7 @@ GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
return std::move(Err);

#ifdef OMPT_SUPPORT
if (ompt::CallbacksInitialized) {
if (ompt::Initialized) {
size_t Bytes =
utils::getPtrDiff(InputTgtImage->ImageEnd, InputTgtImage->ImageStart);
performOmptCallback(
Expand Down
60 changes: 18 additions & 42 deletions offload/src/OpenMP/OMPT/Callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
//
//===----------------------------------------------------------------------===//

#ifndef OMPT_SUPPORT

extern "C" {
/// Dummy definition when OMPT is disabled
void ompt_libomptarget_connect() {}
}

#else // OMPT_SUPPORT is set
#ifdef OMPT_SUPPORT

#include <atomic>
#include <cassert>
Expand All @@ -27,27 +20,28 @@ void ompt_libomptarget_connect() {}
#include <mutex>
#include <thread>

#pragma push_macro("DEBUG_PREFIX")
#include "Shared/Debug.h"

#include "OpenMP/OMPT/Callback.h"
#include "OpenMP/OMPT/Connector.h"
#include "OpenMP/OMPT/Interface.h"

#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/ErrorHandling.h"

#pragma push_macro("DEBUG_PREFIX")
#undef DEBUG_PREFIX
#define DEBUG_PREFIX "OMPT"

using namespace llvm::omp::target::ompt;

// Define OMPT callback functions (bound to actual callbacks later on)
#define defineOmptCallback(Name, Type, Code) \
Name##_t llvm::omp::target::ompt::Name##_fn = nullptr;
FOREACH_OMPT_NOEMI_EVENT(defineOmptCallback)
FOREACH_OMPT_EMI_EVENT(defineOmptCallback)
#undef defineOmptCallback

using namespace llvm::omp::target::ompt;

/// Forward declaration
class LibomptargetRtlFinalizer;

Expand All @@ -58,15 +52,13 @@ thread_local Interface llvm::omp::target::ompt::RegionInterface;

thread_local void *llvm::omp::target::ompt::ReturnAddress = nullptr;

bool llvm::omp::target::ompt::CallbacksInitialized = false;
bool llvm::omp::target::ompt::Initialized = false;

ompt_get_callback_t llvm::omp::target::ompt::lookupCallbackByCode = nullptr;
ompt_function_lookup_t llvm::omp::target::ompt::lookupCallbackByName = nullptr;
ompt_get_target_task_data_t
llvm::omp::target::ompt::ompt_get_target_task_data_fn = nullptr;
ompt_get_task_data_t llvm::omp::target::ompt::ompt_get_task_data_fn = nullptr;
ompt_set_frame_enter_t llvm::omp::target::ompt::ompt_set_frame_enter_fn =
nullptr;
ompt_get_target_task_data_t ompt_get_target_task_data_fn = nullptr;
ompt_get_task_data_t ompt_get_task_data_fn = nullptr;
ompt_set_frame_enter_t ompt_set_frame_enter_fn = nullptr;

/// Unique correlation id
static std::atomic<uint64_t> IdCounter(1);
Expand Down Expand Up @@ -482,10 +474,11 @@ class LibomptargetRtlFinalizer {
int llvm::omp::target::ompt::initializeLibrary(ompt_function_lookup_t lookup,
int initial_device_num,
ompt_data_t *tool_data) {
DP("Executing initializeLibrary (libomp)\n");
DP("Executing initializeLibrary\n");
#define bindOmptFunctionName(OmptFunction, DestinationFunction) \
DestinationFunction = (OmptFunction##_t)lookup(#OmptFunction); \
DP("initializeLibrary (libomp) bound %s=%p\n", #DestinationFunction, \
if (lookup) \
DestinationFunction = (OmptFunction##_t)lookup(#OmptFunction); \
DP("initializeLibrary bound %s=%p\n", #DestinationFunction, \
((void *)(uint64_t)DestinationFunction));

bindOmptFunctionName(ompt_get_callback, lookupCallbackByCode);
Expand All @@ -509,22 +502,22 @@ int llvm::omp::target::ompt::initializeLibrary(ompt_function_lookup_t lookup,

LibraryFinalizer = new LibomptargetRtlFinalizer();

CallbacksInitialized = true;
Initialized = true;

return 0;
}

void llvm::omp::target::ompt::finalizeLibrary(ompt_data_t *data) {
DP("Executing finalizeLibrary (libomp)\n");
DP("Executing finalizeLibrary\n");
// Before disabling OMPT, call the (plugin) finalizations that were registered
// with this library
LibraryFinalizer->finalize();
delete LibraryFinalizer;
CallbacksInitialized = false;
Initialized = false;
}

void llvm::omp::target::ompt::connectLibrary() {
DP("Entering connectLibrary (libomp)\n");
DP("Entering connectLibrary\n");
// Connect with libomp
static OmptLibraryConnectorTy LibompConnector("libomp");
static ompt_start_tool_result_t OmptResult;
Expand All @@ -547,24 +540,7 @@ void llvm::omp::target::ompt::connectLibrary() {
FOREACH_OMPT_EMI_EVENT(bindOmptCallback)
#undef bindOmptCallback

DP("Exiting connectLibrary (libomp)\n");
}

extern "C" {
/// Used for connecting libomptarget with a plugin
void ompt_libomptarget_connect(ompt_start_tool_result_t *result) {
DP("Enter ompt_libomptarget_connect\n");
if (CallbacksInitialized && result && LibraryFinalizer) {
// Cache each fini function, so that they can be invoked on exit
LibraryFinalizer->registerRtl(result->finalize);
// Invoke the provided init function with the lookup function maintained
// in this library so that callbacks maintained by this library are
// retrieved.
result->initialize(lookupCallbackByName,
/*initial_device_num=*/0, /*tool_data=*/nullptr);
}
DP("Leave ompt_libomptarget_connect\n");
}
DP("Exiting connectLibrary\n");
}

#pragma pop_macro("DEBUG_PREFIX")
Expand Down

0 comments on commit 65fd8ab

Please sign in to comment.