xref: /llvm-project/offload/src/OffloadRTL.cpp (revision fa9e90f5d23312587b3a17920941334e0d1a58a1)
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-exception
6 //
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_SUPPORT
20 extern void llvm::omp::target::ompt::connectLibrary();
21 #endif
22 
23 static std::mutex PluginMtx;
24 static uint32_t RefCount = 0;
25 
initRuntime()26 void initRuntime() {
27   std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
28   Profiler::get();
29   TIMESCOPE();
30 
31   if (PM == nullptr)
32     PM = new PluginManager();
33 
34   RefCount++;
35   if (RefCount == 1) {
36     DP("Init offload library!\n");
37 #ifdef OMPT_SUPPORT
38     // Initialize OMPT first
39     llvm::omp::target::ompt::connectLibrary();
40 #endif
41 
42     PM->init();
43     PM->registerDelayedLibraries();
44   }
45 }
46 
deinitRuntime()47 void deinitRuntime() {
48   std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
49   assert(PM && "Runtime not initialized");
50 
51   if (RefCount == 1) {
52     DP("Deinit offload library!\n");
53     PM->deinit();
54     delete PM;
55     PM = nullptr;
56   }
57 
58   RefCount--;
59 }
60