[llvm] [OFFLOAD] Stricter enforcement of user offload disable (PR #133470)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Mar 28 13:49:35 PDT 2025
https://github.com/adurang updated https://github.com/llvm/llvm-project/pull/133470
>From c073357cce4bd2e877629eb0c53146265160249d Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Fri, 28 Mar 2025 02:31:24 -0700
Subject: [PATCH 1/7] [OFFLOAD] Stricter enforcement of user offload disable
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.
---
offload/include/OffloadPolicy.h | 22 ++++++++++++++++++++--
offload/libomptarget/OffloadRTL.cpp | 7 +++++++
2 files changed, 27 insertions(+), 2 deletions(-)
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");
>From df2585292f3b61ffa419c69e7490d773d7432cc1 Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Fri, 28 Mar 2025 11:59:49 -0700
Subject: [PATCH 2/7] Rework it without static var
---
offload/include/OffloadPolicy.h | 22 +++++++---------------
offload/libomptarget/OffloadRTL.cpp | 2 +-
2 files changed, 8 insertions(+), 16 deletions(-)
diff --git a/offload/include/OffloadPolicy.h b/offload/include/OffloadPolicy.h
index 7f57d4da8b364..03af0915fafa8 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() {
+ OffloadPolicy(PluginManager &PM) {
// TODO: Check for OpenMP.
switch ((kmp_target_offload_kind_t)__kmpc_get_target_offload()) {
case tgt_disabled:
@@ -36,15 +36,6 @@ 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()) {
@@ -58,13 +49,15 @@ class OffloadPolicy {
}
return;
}
- Kind = OP.Kind;
}
public:
- static const OffloadPolicy &get() {
- static OffloadPolicy OP;
- return OP;
+ static bool isOffloadDisabled() {
+ if((kmp_target_offload_kind_t)__kmpc_get_target_offload() ==
+ tgt_disabled) {
+ return true;
+ }
+ return false;
}
static const OffloadPolicy &get(PluginManager &PM) {
@@ -75,7 +68,6 @@ 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 ecf7b501efcbc..1c6f0f55113a6 100644
--- a/offload/libomptarget/OffloadRTL.cpp
+++ b/offload/libomptarget/OffloadRTL.cpp
@@ -32,7 +32,7 @@ void initRuntime() {
if (PM == nullptr)
PM = new PluginManager();
- if (OffloadPolicy::get().Kind == OffloadPolicy::DISABLED) {
+ if (OffloadPolicy::isOffloadDisabled()) {
DP("Offload is disabled. Skipping library initialization\n");
// Do only absolutely needed initialization
return;
>From 1eb05a1da25e0fb90a60b14c3ea8211362e57ad0 Mon Sep 17 00:00:00 2001
From: Alex <alejandro.duran at intel.com>
Date: Fri, 28 Mar 2025 20:20:33 +0100
Subject: [PATCH 3/7] Update offload/include/OffloadPolicy.h
Co-authored-by: Joseph Huber <huberjn at outlook.com>
---
offload/include/OffloadPolicy.h | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/offload/include/OffloadPolicy.h b/offload/include/OffloadPolicy.h
index 03af0915fafa8..523d734238a76 100644
--- a/offload/include/OffloadPolicy.h
+++ b/offload/include/OffloadPolicy.h
@@ -53,11 +53,7 @@ class OffloadPolicy {
public:
static bool isOffloadDisabled() {
- if((kmp_target_offload_kind_t)__kmpc_get_target_offload() ==
- tgt_disabled) {
- return true;
- }
- return false;
+ return static_cast<kmp_target_offload_kind_t>(__kmpc_get_target_offload()) == tgt_disabled);
}
static const OffloadPolicy &get(PluginManager &PM) {
>From 6baf698e7e1a12184e0e601736845b1ed7f99111 Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Fri, 28 Mar 2025 12:28:39 -0700
Subject: [PATCH 4/7] remove comment
---
offload/include/OffloadPolicy.h | 2 --
1 file changed, 2 deletions(-)
diff --git a/offload/include/OffloadPolicy.h b/offload/include/OffloadPolicy.h
index 523d734238a76..ab36e2125b8bd 100644
--- a/offload/include/OffloadPolicy.h
+++ b/offload/include/OffloadPolicy.h
@@ -36,8 +36,6 @@ class OffloadPolicy {
Kind = MANDATORY;
return;
default:
- // 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");
>From 9ff4b28049492dd2ab79f986175c27467a225036 Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Fri, 28 Mar 2025 13:25:26 -0700
Subject: [PATCH 5/7] fix issue with refcount
---
offload/libomptarget/OffloadRTL.cpp | 10 ++++------
1 file changed, 4 insertions(+), 6 deletions(-)
diff --git a/offload/libomptarget/OffloadRTL.cpp b/offload/libomptarget/OffloadRTL.cpp
index 1c6f0f55113a6..a48cdc4d5ef5c 100644
--- a/offload/libomptarget/OffloadRTL.cpp
+++ b/offload/libomptarget/OffloadRTL.cpp
@@ -32,12 +32,6 @@ void initRuntime() {
if (PM == nullptr)
PM = new PluginManager();
- if (OffloadPolicy::isOffloadDisabled()) {
- DP("Offload is disabled. Skipping library initialization\n");
- // Do only absolutely needed initialization
- return;
- }
-
RefCount++;
if (RefCount == 1) {
DP("Init offload library!\n");
@@ -46,6 +40,10 @@ void initRuntime() {
llvm::omp::target::ompt::connectLibrary();
#endif
+ if (OffloadPolicy::isOffloadDisabled()) {
+ DP("Offload is disabled. Skipping plugin initialization\n");
+ return;
+ }
PM->init();
PM->registerDelayedLibraries();
}
>From 370c7d62fb603e64e82df5c8369762e08ff5a55f Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Fri, 28 Mar 2025 13:31:44 -0700
Subject: [PATCH 6/7] Move check to PluginManager::init
---
offload/libomptarget/OffloadRTL.cpp | 4 ----
offload/libomptarget/PluginManager.cpp | 5 +++++
2 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/offload/libomptarget/OffloadRTL.cpp b/offload/libomptarget/OffloadRTL.cpp
index a48cdc4d5ef5c..589c8ab256255 100644
--- a/offload/libomptarget/OffloadRTL.cpp
+++ b/offload/libomptarget/OffloadRTL.cpp
@@ -40,10 +40,6 @@ void initRuntime() {
llvm::omp::target::ompt::connectLibrary();
#endif
- if (OffloadPolicy::isOffloadDisabled()) {
- DP("Offload is disabled. Skipping plugin initialization\n");
- return;
- }
PM->init();
PM->registerDelayedLibraries();
}
diff --git a/offload/libomptarget/PluginManager.cpp b/offload/libomptarget/PluginManager.cpp
index 8b77852d13439..324911474972e 100644
--- a/offload/libomptarget/PluginManager.cpp
+++ b/offload/libomptarget/PluginManager.cpp
@@ -30,6 +30,11 @@ PluginManager *PM = nullptr;
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.
>From c87236f1bcbd6e32d8a13929c56bf3dc06421a80 Mon Sep 17 00:00:00 2001
From: "Duran, Alex" <alejandro.duran at intel.com>
Date: Fri, 28 Mar 2025 13:49:20 -0700
Subject: [PATCH 7/7] fix format
---
offload/include/OffloadPolicy.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/offload/include/OffloadPolicy.h b/offload/include/OffloadPolicy.h
index ab36e2125b8bd..d3c8128b8ce74 100644
--- a/offload/include/OffloadPolicy.h
+++ b/offload/include/OffloadPolicy.h
@@ -51,7 +51,8 @@ class OffloadPolicy {
public:
static bool isOffloadDisabled() {
- return static_cast<kmp_target_offload_kind_t>(__kmpc_get_target_offload()) == tgt_disabled);
+ return static_cast<kmp_target_offload_kind_t>(__kmpc_get_target_offload())
+ == tgt_disabled;
}
static const OffloadPolicy &get(PluginManager &PM) {
More information about the llvm-commits
mailing list