[OFFLOAD] Stricter enforcement of user offload disable (#133470)
If user specifies offload is disabled (e.g.,
OMP_TARGET_OFFLOAD=disable), disable library almost completely. This
reduces resources spent to a minimum and ensures all APIs behave as if
the only available device is the host device.
Currently some of the APIs behave as if there were devices avaible for
offload even when under OMP_TARGET_OFFLOAD=disable.
---------
Co-authored-by: Joseph Huber <huberjn@outlook.com>
diff --git a/offload/include/OffloadPolicy.h b/offload/include/OffloadPolicy.h
index 858d9c3..800fefb 100644
--- a/offload/include/OffloadPolicy.h
+++ b/offload/include/OffloadPolicy.h
@@ -46,10 +46,15 @@
Kind = DISABLED;
}
return;
- };
+ }
}
public:
+ static bool isOffloadDisabled() {
+ return static_cast<kmp_target_offload_kind_t>(
+ __kmpc_get_target_offload()) == tgt_disabled;
+ }
+
static const OffloadPolicy &get(PluginManager &PM) {
static OffloadPolicy OP(PM);
return OP;
diff --git a/offload/libomptarget/PluginManager.cpp b/offload/libomptarget/PluginManager.cpp
index 8b77852..d6d529a 100644
--- a/offload/libomptarget/PluginManager.cpp
+++ b/offload/libomptarget/PluginManager.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "PluginManager.h"
+#include "OffloadPolicy.h"
#include "Shared/Debug.h"
#include "Shared/Profile.h"
#include "device.h"
@@ -30,6 +31,11 @@
void PluginManager::init() {
TIMESCOPE();
+ if (OffloadPolicy::isOffloadDisabled()) {
+ DP("Offload is disabled. Skipping plugin initialization\n");
+ return;
+ }
+
DP("Loading RTLs...\n");
// Attempt to create an instance of each supported plugin.