[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
Mon Sep 15 15:52:37 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:

> Use getSectionPrefix instead? It does some assertion checking that could be useful.

done.

> does it make sense to just make this the default behavior of setSectionPrefix?

Implementation wise, the update method currently calls the set method (the other way around). Technically, no existing caller of 'set' depends on the behavior that 'set' method will overwrite, so either way (with a brief comment on the function decl in header) is fine imo.  Doing it the current way will avoid affecting (fwiw) existing user in codegen prepare to [set function section prefix](https://github.com/llvm/llvm-project/blob/4a8bb08bd2261afe22c8addea9f830906084869e/llvm/lib/CodeGen/CodeGenPrepare.cpp#L581-L603) though. 

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


More information about the llvm-commits mailing list