72 lines · cpp
1//===----------- rtl.cpp - Target independent OpenMP target RTL -----------===//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-exception6//7//===----------------------------------------------------------------------===//8//9// Initialization and tear down of the offload runtime.10//11//===----------------------------------------------------------------------===//12 13#include "OpenMP/OMPT/Callback.h"14#include "PluginManager.h"15 16#include "Shared/Debug.h"17#include "Shared/Profile.h"18 19#ifdef OMPT_SUPPORT20extern void llvm::omp::target::ompt::connectLibrary();21#endif22 23static std::mutex PluginMtx;24static uint32_t RefCount = 0;25std::atomic<bool> RTLAlive{false};26std::atomic<int> RTLOngoingSyncs{0};27 28void initRuntime() {29 std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);30 Profiler::get();31 TIMESCOPE();32 33 if (PM == nullptr)34 PM = new PluginManager();35 36 RefCount++;37 if (RefCount == 1) {38 ODBG() << "Init offload library!";39#ifdef OMPT_SUPPORT40 // Initialize OMPT first41 llvm::omp::target::ompt::connectLibrary();42#endif43 44 PM->init();45 PM->registerDelayedLibraries();46 47 // RTL initialization is complete48 RTLAlive = true;49 }50}51 52void deinitRuntime() {53 std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);54 assert(PM && "Runtime not initialized");55 56 if (RefCount == 1) {57 DP("Deinit offload library!\n");58 // RTL deinitialization has started59 RTLAlive = false;60 while (RTLOngoingSyncs > 0) {61 DP("Waiting for ongoing syncs to finish, count: %d\n",62 RTLOngoingSyncs.load());63 std::this_thread::sleep_for(std::chrono::milliseconds(100));64 }65 PM->deinit();66 delete PM;67 PM = nullptr;68 }69 70 RefCount--;71}72