-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[OFFLOAD] Stricter enforcement of user offload disable #133470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
|
@llvm/pr-subscribers-offload Author: Alex (adurang) ChangesIf 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. Full diff: https://github.com/llvm/llvm-project/pull/133470.diff 2 Files Affected:
diff --git a/offload/include/OffloadPolicy.h b/offload/include/OffloadPolicy.h
index 858d9c323b15a..7f57d4da8b364 100644
--- a/offload/include/OffloadPolicy.h
+++ b/offload/include/OffloadPolicy.h
@@ -26,7 +26,7 @@ extern "C" int __kmpc_get_target_offload(void) __attribute__((weak));
class OffloadPolicy {
- OffloadPolicy(PluginManager &PM) {
+ OffloadPolicy() {
// TODO: Check for OpenMP.
switch ((kmp_target_offload_kind_t)__kmpc_get_target_offload()) {
case tgt_disabled:
@@ -36,6 +36,17 @@ class OffloadPolicy {
Kind = MANDATORY;
return;
default:
+ // delay DEFAULT policy until PluginManager is ready
+ UserValue = false;
+ return;
+ };
+ }
+
+ OffloadPolicy(PluginManager &PM) {
+ const OffloadPolicy &OP = get();
+ if (!OP.UserValue) {
+ // User didn't specify a policy, decide
+ // based on number of devices discovered
if (PM.getNumDevices()) {
DP("Default TARGET OFFLOAD policy is now mandatory "
"(devices were found)\n");
@@ -46,10 +57,16 @@ class OffloadPolicy {
Kind = DISABLED;
}
return;
- };
+ }
+ Kind = OP.Kind;
}
public:
+ static const OffloadPolicy &get() {
+ static OffloadPolicy OP;
+ return OP;
+ }
+
static const OffloadPolicy &get(PluginManager &PM) {
static OffloadPolicy OP(PM);
return OP;
@@ -58,6 +75,7 @@ class OffloadPolicy {
enum OffloadPolicyKind { DISABLED, MANDATORY };
OffloadPolicyKind Kind = MANDATORY;
+ bool UserValue = true;
};
#endif // OMPTARGET_OFFLOAD_POLICY_H
diff --git a/offload/libomptarget/OffloadRTL.cpp b/offload/libomptarget/OffloadRTL.cpp
index 29b573a27d087..ecf7b501efcbc 100644
--- a/offload/libomptarget/OffloadRTL.cpp
+++ b/offload/libomptarget/OffloadRTL.cpp
@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
+#include "OffloadPolicy.h"
#include "OpenMP/OMPT/Callback.h"
#include "PluginManager.h"
@@ -31,6 +32,12 @@ void initRuntime() {
if (PM == nullptr)
PM = new PluginManager();
+ if (OffloadPolicy::get().Kind == OffloadPolicy::DISABLED) {
+ DP("Offload is disabled. Skipping library initialization\n");
+ // Do only absolutely needed initialization
+ return;
+ }
+
RefCount++;
if (RefCount == 1) {
DP("Init offload library!\n");
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Co-authored-by: Joseph Huber <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should just omit calling PM->init() (Possibly just an early exit in init() itself. Since offloading is disabled we need no plugins so it's logical).
jhuber6
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LG, thanks.
|
thank you @jhuber6! |
Add early exit during plugin de-init - when OffloadPolicy::isOffloadDisabled - e.g.: `OMP_TARGET_OFFLOAD=DISABLED` See also: llvm#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.