[llvm] [NFCI][Globals]For GlobalObjects, add updateSectionPrefix and change setSectionPrefix to handle empty strings (PR #158460)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 16 10:29:33 PDT 2025


================
@@ -289,11 +289,28 @@ void GlobalObject::setSection(StringRef S) {
 }
 
 void GlobalObject::setSectionPrefix(StringRef Prefix) {
+  if (Prefix.empty()) {
+    setMetadata(LLVMContext::MD_section_prefix, nullptr);
+    return;
+  }
   MDBuilder MDB(getContext());
   setMetadata(LLVMContext::MD_section_prefix,
               MDB.createGlobalObjectSectionPrefix(Prefix));
 }
 
+bool GlobalObject::updateSectionPrefix(StringRef Prefix) {
+  auto MD = getMetadata(LLVMContext::MD_section_prefix);
----------------
mingmingl-llvm wrote:

> How would existing code be affected if setSectionPrefix was changed to do the update? 

The existing code calls 'set' once for function ([callsite](https://github.com/llvm/llvm-project/blob/7bc91f3580836c13cfcf511147585a5f2f00a7f5/llvm/lib/CodeGen/CodeGenPrepare.cpp#L578-L603)) or global variable ([callsite](https://github.com/llvm/llvm-project/blob/40f2da5c04042dcf6ae8dfc757d31a32da7f329e/llvm/lib/CodeGen/StaticDataAnnotator.cpp#L94)). If setSectionPrefix is changed to call update, the outcome is the same, except that they get additional comparisons (and makes a difference to compile time in cases where additional comparisons are not needed, like for functions).

> What is the current behavior if setSectionPrefix is used in a context where a different section prefix already exists?

It'll overwrite the existing section prefix per implementation, although existing callers calls this function once, so either an update or not works (in terms of the outcome).



https://github.com/llvm/llvm-project/pull/158460


More information about the llvm-commits mailing list