From 021a1f69742aab7d650887a0978db5cf007967d3 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 28 Mar 2025 23:28:14 +0100 Subject: [PATCH] [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 --- offload/include/OffloadPolicy.h | 7 ++++++- offload/libomptarget/PluginManager.cpp | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/offload/include/OffloadPolicy.h b/offload/include/OffloadPolicy.h index 858d9c323b15..800fefb22432 100644 --- a/offload/include/OffloadPolicy.h +++ b/offload/include/OffloadPolicy.h @@ -46,10 +46,15 @@ class OffloadPolicy { Kind = DISABLED; } return; - }; + } } public: + static bool isOffloadDisabled() { + return static_cast( + __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 8b77852d1343..d6d529a20758 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 @@ 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.