[llvm] [Offload] Store kernel name in GenericKernelTy (PR #142799)

Ross Brunton via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 2 03:24:03 PDT 2025


https://github.com/RossBrunton updated https://github.com/llvm/llvm-project/pull/142799

>From 9681ac1898d484afbaac47c725e2e6e33771ba79 Mon Sep 17 00:00:00 2001
From: Ross Brunton <ross at codeplay.com>
Date: Wed, 4 Jun 2025 16:41:50 +0100
Subject: [PATCH 1/3] [Offload] Store kernel name in GenericKernelTy

GenericKernelTy has a pointer to the name that was used to create it.
However, the name passed in as an argument may not outlive the kernel.
Instead, GenericKernelTy now contains a SmallString, and copies the
name into there.
---
 .../plugins-nextgen/common/include/PluginInterface.h  | 11 ++++++++---
 .../plugins-nextgen/common/src/PluginInterface.cpp    |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index fbc798faec24b..0aefa1412d9af 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -296,7 +296,12 @@ class DeviceImageTy {
 struct GenericKernelTy {
   /// Construct a kernel with a name and a execution mode.
   GenericKernelTy(const char *Name)
-      : Name(Name), PreferredNumThreads(0), MaxNumThreads(0) {}
+      : Name(Name), PreferredNumThreads(0), MaxNumThreads(0) {
+    // Ensure that the name is null terminated so getName() can just return the
+    // pointer
+    this->Name.push_back('\0');
+    this->Name.pop_back();
+  }
 
   virtual ~GenericKernelTy() {}
 
@@ -317,7 +322,7 @@ struct GenericKernelTy {
                            AsyncInfoWrapperTy &AsyncInfoWrapper) const = 0;
 
   /// Get the kernel name.
-  const char *getName() const { return Name; }
+  const char *getName() const { return Name.data(); }
 
   /// Get the kernel image.
   DeviceImageTy &getImage() const {
@@ -413,7 +418,7 @@ struct GenericKernelTy {
   }
 
   /// The kernel name.
-  const char *Name;
+  SmallString<32> Name;
 
   /// The image that contains this kernel.
   DeviceImageTy *ImagePtr = nullptr;
diff --git a/offload/plugins-nextgen/common/src/PluginInterface.cpp b/offload/plugins-nextgen/common/src/PluginInterface.cpp
index ac7031b6e881c..81b9d423e13d8 100644
--- a/offload/plugins-nextgen/common/src/PluginInterface.cpp
+++ b/offload/plugins-nextgen/common/src/PluginInterface.cpp
@@ -456,7 +456,7 @@ Error GenericKernelTy::init(GenericDeviceTy &GenericDevice,
     KernelEnvironment = KernelEnvironmentTy{};
     DP("Failed to read kernel environment for '%s' Using default Bare (0) "
        "execution mode\n",
-       Name);
+       getName());
   }
 
   // Max = Config.Max > 0 ? min(Config.Max, Device.Max) : Device.Max;

>From 782aa093abb8f470d3fc96580fd45792d390d189 Mon Sep 17 00:00:00 2001
From: Ross Brunton <ross at codeplay.com>
Date: Tue, 1 Jul 2025 16:41:40 +0100
Subject: [PATCH 2/3] Copy the null terminator explicitly

---
 offload/plugins-nextgen/common/include/PluginInterface.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index 0aefa1412d9af..2112c5d8baebc 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -296,10 +296,11 @@ class DeviceImageTy {
 struct GenericKernelTy {
   /// Construct a kernel with a name and a execution mode.
   GenericKernelTy(const char *Name)
-      : Name(Name), PreferredNumThreads(0), MaxNumThreads(0) {
-    // Ensure that the name is null terminated so getName() can just return the
-    // pointer
-    this->Name.push_back('\0');
+      : Name(Name, &Name[strlen(Name) + 1]), PreferredNumThreads(0),
+        MaxNumThreads(0) {
+    // The null terminator from the input string was also copied to ensure that
+    // Name.data() will always be null terminated. Pop the last character to
+    // ensure that Name.size is accurate.
     this->Name.pop_back();
   }
 

>From eea82cf614dc3747438406fa72a19dfa9ff189b4 Mon Sep 17 00:00:00 2001
From: Ross Brunton <ross at codeplay.com>
Date: Wed, 2 Jul 2025 11:23:42 +0100
Subject: [PATCH 3/3] Use stringref rather than iterators

---
 offload/plugins-nextgen/common/include/PluginInterface.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/offload/plugins-nextgen/common/include/PluginInterface.h b/offload/plugins-nextgen/common/include/PluginInterface.h
index 2112c5d8baebc..ddad41f91a920 100644
--- a/offload/plugins-nextgen/common/include/PluginInterface.h
+++ b/offload/plugins-nextgen/common/include/PluginInterface.h
@@ -296,7 +296,7 @@ class DeviceImageTy {
 struct GenericKernelTy {
   /// Construct a kernel with a name and a execution mode.
   GenericKernelTy(const char *Name)
-      : Name(Name, &Name[strlen(Name) + 1]), PreferredNumThreads(0),
+      : Name(StringRef{Name, strlen(Name) + 1}), PreferredNumThreads(0),
         MaxNumThreads(0) {
     // The null terminator from the input string was also copied to ensure that
     // Name.data() will always be null terminated. Pop the last character to



More information about the llvm-commits mailing list