Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLVM-C] Add Bindings for eraseNamedMetadata #82549

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dumbasPL
Copy link

There wasn't any way to remove named metadata from a module. This adds the LLVMEraseNamedMetadata binding and a test for it

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 21, 2024

@llvm/pr-subscribers-llvm-ir

Author: nezu (dumbasPL)

Changes

There wasn't any way to remove named metadata from a module. This adds the LLVMEraseNamedMetadata binding and a test for it


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

6 Files Affected:

  • (modified) llvm/include/llvm-c/Core.h (+7)
  • (modified) llvm/lib/IR/Core.cpp (+5)
  • (added) llvm/test/Bindings/llvm-c/erase_named_metadata.ll (+1)
  • (modified) llvm/tools/llvm-c-test/llvm-c-test.h (+1)
  • (modified) llvm/tools/llvm-c-test/main.c (+4)
  • (modified) llvm/tools/llvm-c-test/metadata.c (+15)
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);

Copy link

⚠️ We detected that you are using a GitHub private e-mail address to contribute to the repo.
Please turn off Keep my email addresses private setting in your account.
See LLVM Discourse for more information.

@dumbasPL dumbasPL force-pushed the erase-named-metadata branch 2 times, most recently from 333faf0 to 276bb04 Compare February 21, 2024 23:03
There wasn't any way to remove named metadata from a module. This adds
the LLVMEraseNamedMetadata binding and a test for it
@dumbasPL
Copy link
Author

ok, the email situation seems to be sorted out, didn't know it was required

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.

None yet

2 participants