2017-12-06 21:59:07 +00:00
|
|
|
//===----------- rtl.cpp - Target independent OpenMP target RTL -----------===//
|
|
|
|
|
//
|
2019-01-19 10:56:40 +00:00
|
|
|
// 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
|
2017-12-06 21:59:07 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2023-12-05 09:01:32 -08:00
|
|
|
// Initialization and tear down of the offload runtime.
|
2017-12-06 21:59:07 +00:00
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2023-11-29 08:29:41 -08:00
|
|
|
#include "OpenMP/OMPT/Callback.h"
|
2023-11-30 13:47:47 -08:00
|
|
|
#include "PluginManager.h"
|
2022-06-10 15:16:15 -04:00
|
|
|
|
2023-11-30 15:23:34 -08:00
|
|
|
#include "Shared/Debug.h"
|
2023-11-29 07:54:35 -08:00
|
|
|
#include "Shared/Profile.h"
|
2022-08-09 11:37:59 -04:00
|
|
|
|
2023-05-05 12:00:26 +02:00
|
|
|
#ifdef OMPT_SUPPORT
|
2023-12-05 09:01:32 -08:00
|
|
|
extern void llvm::omp::target::ompt::connectLibrary();
|
2023-05-05 12:00:26 +02:00
|
|
|
#endif
|
|
|
|
|
|
2024-02-22 12:01:52 -06:00
|
|
|
static std::mutex PluginMtx;
|
|
|
|
|
static uint32_t RefCount = 0;
|
2025-08-06 23:34:39 +09:00
|
|
|
std::atomic<bool> RTLAlive{false};
|
|
|
|
|
std::atomic<int> RTLOngoingSyncs{0};
|
2024-02-22 12:01:52 -06:00
|
|
|
|
|
|
|
|
void initRuntime() {
|
|
|
|
|
std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
|
2023-12-05 14:33:16 -08:00
|
|
|
Profiler::get();
|
|
|
|
|
TIMESCOPE();
|
|
|
|
|
|
2024-02-22 12:01:52 -06:00
|
|
|
if (PM == nullptr)
|
|
|
|
|
PM = new PluginManager();
|
2020-12-10 11:24:27 -08:00
|
|
|
|
2024-02-22 12:01:52 -06:00
|
|
|
RefCount++;
|
|
|
|
|
if (RefCount == 1) {
|
|
|
|
|
DP("Init offload library!\n");
|
2023-06-20 18:24:05 +02:00
|
|
|
#ifdef OMPT_SUPPORT
|
2024-02-22 12:01:52 -06:00
|
|
|
// Initialize OMPT first
|
|
|
|
|
llvm::omp::target::ompt::connectLibrary();
|
2023-06-20 18:24:05 +02:00
|
|
|
#endif
|
2023-05-05 12:00:26 +02:00
|
|
|
|
2024-02-22 12:01:52 -06:00
|
|
|
PM->init();
|
|
|
|
|
PM->registerDelayedLibraries();
|
2025-08-06 23:34:39 +09:00
|
|
|
|
|
|
|
|
// RTL initialization is complete
|
|
|
|
|
RTLAlive = true;
|
2024-02-22 12:01:52 -06:00
|
|
|
}
|
2020-02-19 09:41:50 -05:00
|
|
|
}
|
2017-12-06 21:59:07 +00:00
|
|
|
|
2024-02-22 12:01:52 -06:00
|
|
|
void deinitRuntime() {
|
|
|
|
|
std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
|
|
|
|
|
assert(PM && "Runtime not initialized");
|
|
|
|
|
|
|
|
|
|
if (RefCount == 1) {
|
|
|
|
|
DP("Deinit offload library!\n");
|
2025-08-06 23:34:39 +09:00
|
|
|
// RTL deinitialization has started
|
|
|
|
|
RTLAlive = false;
|
|
|
|
|
while (RTLOngoingSyncs > 0) {
|
|
|
|
|
DP("Waiting for ongoing syncs to finish, count: %d\n",
|
|
|
|
|
RTLOngoingSyncs.load());
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
}
|
2024-05-09 06:35:54 -05:00
|
|
|
PM->deinit();
|
2024-02-22 12:01:52 -06:00
|
|
|
delete PM;
|
|
|
|
|
PM = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefCount--;
|
2020-02-19 09:41:50 -05:00
|
|
|
}
|