[llvm] [LLVM-C] Add Bindings for eraseNamedMetadata (PR #82549)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 21 15:03:54 PST 2024
https://github.com/dumbasPL updated https://github.com/llvm/llvm-project/pull/82549
>From 276bb0480ac2c5b8bf3168c9fc57425cdf2672d3 Mon Sep 17 00:00:00 2001
From: dumbasPL <nezu at nezu.cc>
Date: Thu, 22 Feb 2024 00:03:22 +0100
Subject: [PATCH] [LLVM-C] Add Bindings for eraseNamedMetadata
There wasn't any way to remove named metadata from a module. This adds
the LLVMEraseNamedMetadata binding and a test for it
---
llvm/include/llvm-c/Core.h | 7 +++++++
llvm/lib/IR/Core.cpp | 5 +++++
llvm/test/Bindings/llvm-c/erase_named_metadata.ll | 1 +
llvm/tools/llvm-c-test/llvm-c-test.h | 1 +
llvm/tools/llvm-c-test/main.c | 4 ++++
llvm/tools/llvm-c-test/metadata.c | 15 +++++++++++++++
6 files changed, 33 insertions(+)
create mode 100644 llvm/test/Bindings/llvm-c/erase_named_metadata.ll
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 09746bdaf0c94e..a1f0410846e156 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -1037,6 +1037,13 @@ LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
const char *Name,
size_t NameLen);
+/**
+ * Remove the given NamedMDNode from this module and delete it.
+ *
+ * @see llvm::Module::eraseNamedMetadata()
+ */
+void LLVMEraseNamedMetadata(LLVMModuleRef M, LLVMNamedMDNodeRef NMD);
+
/**
* Retrieve the name of a NamedMDNode.
*
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index d6d159ab8b9e83..fd83c3cc210b53 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1315,6 +1315,11 @@ LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
}
+void LLVMEraseNamedMetadata(LLVMModuleRef M, LLVMNamedMDNodeRef NMD) {
+ NamedMDNode *NamedNode = unwrap(NMD);
+ unwrap(M)->eraseNamedMetadata(NamedNode);
+}
+
const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
NamedMDNode *NamedNode = unwrap(NMD);
*NameLen = NamedNode->getName().size();
diff --git a/llvm/test/Bindings/llvm-c/erase_named_metadata.ll b/llvm/test/Bindings/llvm-c/erase_named_metadata.ll
new file mode 100644
index 00000000000000..e517c48441a8d6
--- /dev/null
+++ b/llvm/test/Bindings/llvm-c/erase_named_metadata.ll
@@ -0,0 +1 @@
+; RUN: llvm-c-test --erase-named-metadata < /dev/null
\ No newline at end of file
diff --git a/llvm/tools/llvm-c-test/llvm-c-test.h b/llvm/tools/llvm-c-test/llvm-c-test.h
index 00566660257e07..daef463285cc89 100644
--- a/llvm/tools/llvm-c-test/llvm-c-test.h
+++ b/llvm/tools/llvm-c-test/llvm-c-test.h
@@ -43,6 +43,7 @@ int llvm_di_type_get_name(void);
// metadata.c
int llvm_add_named_metadata_operand(void);
int llvm_set_metadata(void);
+int llvm_erase_named_metadata(void);
int llvm_replace_md_operand(void);
int llvm_is_a_value_as_metadata(void);
diff --git a/llvm/tools/llvm-c-test/main.c b/llvm/tools/llvm-c-test/main.c
index badbe4b13b6ba5..e1a7c85d40bb66 100644
--- a/llvm/tools/llvm-c-test/main.c
+++ b/llvm/tools/llvm-c-test/main.c
@@ -44,6 +44,8 @@ static void print_usage(void) {
fprintf(stderr, " Read lines of triple, hex ascii machine code from stdin "
"- print disassembly\n\n");
fprintf(stderr, " * --calc\n");
+ fprintf(stderr, " * --erase-named-metadata\n"
+ " Run test for erasing named metadata\n");
fprintf(
stderr,
" Read lines of name, rpn from stdin - print generated module\n\n");
@@ -93,6 +95,8 @@ int main(int argc, char **argv) {
return llvm_add_named_metadata_operand();
} else if (argc == 2 && !strcmp(argv[1], "--set-metadata")) {
return llvm_set_metadata();
+ } else if (argc == 2 && !strcmp(argv[1], "--erase-named-metadata")) {
+ return llvm_erase_named_metadata();
} else if (argc == 2 && !strcmp(argv[1], "--get-di-tag")) {
return llvm_get_di_tag();
} else if (argc == 2 && !strcmp(argv[1], "--di-type-get-name")) {
diff --git a/llvm/tools/llvm-c-test/metadata.c b/llvm/tools/llvm-c-test/metadata.c
index 4fe8c00c57481b..72db9af104675b 100644
--- a/llvm/tools/llvm-c-test/metadata.c
+++ b/llvm/tools/llvm-c-test/metadata.c
@@ -47,6 +47,21 @@ int llvm_set_metadata(void) {
return 0;
}
+int llvm_erase_named_metadata(void) {
+ LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
+
+ const char Name[] = "foo";
+ LLVMNamedMDNodeRef MD = LLVMGetOrInsertNamedMetadata(M, Name, strlen(Name));
+ assert(LLVMGetFirstNamedMetadata(M));
+
+ LLVMEraseNamedMetadata(M, MD);
+ assert(!LLVMGetFirstNamedMetadata(M));
+
+ LLVMDisposeModule(M);
+
+ return 0;
+}
+
int llvm_replace_md_operand(void) {
LLVMModuleRef M = LLVMModuleCreateWithName("Mod");
LLVMContextRef Context = LLVMGetModuleContext(M);
More information about the llvm-commits
mailing list