Skip to content

Conversation

@jasilvanus
Copy link
Contributor

Function::deleteBody deletes all function metadata, which is undesirable in some cases.

This patch adds a boolean argument PreserveMetadata to control that. The new argument defaults to false, preserving the old behavior.

`Function::deleteBody` deletes all function metadata,
which is undesirable in some cases.

This patch adds a boolean argument `PreserveMetadata` to control that.
The new argument defaults to `false`, preserving the old behavior.
@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2025

@llvm/pr-subscribers-llvm-ir

Author: Jannik Silvanus (jasilvanus)

Changes

Function::deleteBody deletes all function metadata, which is undesirable in some cases.

This patch adds a boolean argument PreserveMetadata to control that. The new argument defaults to false, preserving the old behavior.


Full diff: https://github.com/llvm/llvm-project/pull/164960.diff

2 Files Affected:

  • (modified) llvm/include/llvm/IR/Function.h (+4-4)
  • (modified) llvm/lib/IR/Function.cpp (+3-2)
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index d3497716ca844..02232f6dc2f62 100644
--- a/llvm/include/llvm/IR/Function.h
+++ b/llvm/include/llvm/IR/Function.h
@@ -144,7 +144,7 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
 
   void clearArguments();
 
-  void deleteBodyImpl(bool ShouldDrop);
+  void deleteBodyImpl(bool ShouldDrop, bool PreserveMetadata);
 
   /// Function ctor - If the (optional) Module argument is specified, the
   /// function is automatically inserted into the end of the function list for
@@ -727,8 +727,8 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
   /// deleteBody - This method deletes the body of the function, and converts
   /// the linkage to external.
   ///
-  void deleteBody() {
-    deleteBodyImpl(/*ShouldDrop=*/false);
+  void deleteBody(bool PreserveMetadata = false) {
+    deleteBodyImpl(/*ShouldDrop=*/false, PreserveMetadata);
     setLinkage(ExternalLinkage);
   }
 
@@ -982,7 +982,7 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
   /// including any contained basic blocks.
   ///
   void dropAllReferences() {
-    deleteBodyImpl(/*ShouldDrop=*/true);
+    deleteBodyImpl(/*ShouldDrop=*/true, /*PreserveMetadata=*/false);
   }
 
   /// hasAddressTaken - returns true if there are any uses of this function
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index fc067459dcba3..d50a711b83fa9 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -600,7 +600,7 @@ void Function::stealArgumentListFrom(Function &Src) {
   Src.setValueSubclassData(Src.getSubclassDataFromValue() | (1 << 0));
 }
 
-void Function::deleteBodyImpl(bool ShouldDrop) {
+void Function::deleteBodyImpl(bool ShouldDrop, bool PreserveMetadata) {
   setIsMaterializable(false);
 
   for (BasicBlock &BB : *this)
@@ -627,7 +627,8 @@ void Function::deleteBodyImpl(bool ShouldDrop) {
   }
 
   // Metadata is stored in a side-table.
-  clearMetadata();
+  if (!PreserveMetadata)
+    clearMetadata();
 }
 
 void Function::addAttributeAtIndex(unsigned i, Attribute Attr) {

@jasilvanus
Copy link
Contributor Author

I realized Function::deleteBody also changes linkage, which my use case also does not want, instead of adding more options to Function::deleteBody I ended up just manually deleting the BBs, no longer needing this PR.

However, I'm not sure that behavior of changing a lot more than just deleting the body is what we want by default...

@jasilvanus jasilvanus closed this Oct 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants