[llvm] llvm-c: Introduce 'LLVMDISubprogramReplaceType' (PR #143461)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 17 19:06:19 PDT 2025


https://github.com/davidgmbb updated https://github.com/llvm/llvm-project/pull/143461

>From e993a25f4cc4987889beea1ecf05102bcf7a45b6 Mon Sep 17 00:00:00 2001
From: David Gonzalez Martin <davidgmbb at gmail.com>
Date: Mon, 9 Jun 2025 18:09:24 -0600
Subject: [PATCH] llvm-c: Introduce 'LLVMDISubprogramReplaceType'

The C API does not provide a way to replace the subroutine type after
creating a subprogram. This functionality is useful for creating a
subroutine type composed of types which have the subprogram as scope
---
 llvm/include/llvm-c/DebugInfo.h    | 10 ++++++++++
 llvm/lib/IR/DebugInfo.cpp          |  6 ++++++
 llvm/tools/llvm-c-test/debuginfo.c |  8 ++++----
 3 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/llvm/include/llvm-c/DebugInfo.h b/llvm/include/llvm-c/DebugInfo.h
index 991def64028da..c53d01b96dfa6 100644
--- a/llvm/include/llvm-c/DebugInfo.h
+++ b/llvm/include/llvm-c/DebugInfo.h
@@ -1420,6 +1420,16 @@ void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP);
  */
 unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram);
 
+/**
+ * Replace the subprogram subroutine type
+ * \param Subprogram        The subprogram object.
+ * \param SubroutineType    The new subroutine type
+ *
+ * @see DISubprogram::replaceType()
+ */
+void LLVMDISubprogramReplaceType(LLVMMetadataRef Subprogram,
+                                 LLVMMetadataRef SubroutineType);
+
 /**
  * Get the debug location for the given instruction.
  *
diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
index 7db9891fdbd75..dee7d7ba3c030 100644
--- a/llvm/lib/IR/DebugInfo.cpp
+++ b/llvm/lib/IR/DebugInfo.cpp
@@ -1818,6 +1818,12 @@ unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) {
   return unwrapDI<DISubprogram>(Subprogram)->getLine();
 }
 
+void LLVMDISubprogramReplaceType(LLVMMetadataRef Subprogram,
+                                 LLVMMetadataRef SubroutineType) {
+  unwrapDI<DISubprogram>(Subprogram)
+      ->replaceType(unwrapDI<DISubroutineType>(SubroutineType));
+}
+
 LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) {
   return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode());
 }
diff --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c
index 3ac5a6bc2cf03..8c6f6436782e9 100644
--- a/llvm/tools/llvm-c-test/debuginfo.c
+++ b/llvm/tools/llvm-c-test/debuginfo.c
@@ -130,12 +130,12 @@ int llvm_test_dibuilder(void) {
   LLVMMetadataRef FooParamLocation =
     LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0,
                                      ReplaceableFunctionMetadata, NULL);
-  LLVMMetadataRef FunctionMetadata =
-    LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3,
-                                File, 42, FunctionTy, true, true,
-                                42, 0, false);
+  LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(
+      DIB, File, "foo", 3, "foo", 3, File, 42, NULL, true, true, 42, 0, false);
   LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata, FunctionMetadata);
 
+  LLVMDISubprogramReplaceType(FunctionMetadata, FunctionTy);
+
   LLVMMetadataRef FooParamExpression =
     LLVMDIBuilderCreateExpression(DIB, NULL, 0);
   LLVMMetadataRef FooParamVar1 =



More information about the llvm-commits mailing list