[PATCH] D125388: Allow type-mismatching RAUW of values in metadata, and add C API.

Tim Besard via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 11 07:30:33 PDT 2022


maleadt created this revision.
Herald added a reviewer: deadalnix.
Herald added a subscriber: hiraditya.
Herald added a project: All.
maleadt requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

When replacing a value that's referred to by module metadata, one needs to use RAUW to update that metadata. RAUW however doesn't work when the type of the value changes, e.g., when replacing a function with one that takes an additional argument. This kind of IR happens with e.g. the `nvvm.annotations` metadata, which contains a reference to a LLVM::Function value. It seems to make sense to relax the type equality assertion when only replacing values in metadata (see also: https://discourse.llvm.org/t/replacing-module-metadata-uses-of-function/62431). This change does exactly that, while additionally exposing that functionality to C.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125388

Files:
  llvm/include/llvm-c/Core.h
  llvm/lib/IR/Core.cpp
  llvm/lib/IR/Metadata.cpp


Index: llvm/lib/IR/Metadata.cpp
===================================================================
--- llvm/lib/IR/Metadata.cpp
+++ llvm/lib/IR/Metadata.cpp
@@ -438,7 +438,6 @@
   assert(From && "Expected valid value");
   assert(To && "Expected valid value");
   assert(From != To && "Expected changed value");
-  assert(From->getType() == To->getType() && "Unexpected type change");
 
   LLVMContext &Context = From->getType()->getContext();
   auto &Store = Context.pImpl->ValuesAsMetadata;
Index: llvm/lib/IR/Core.cpp
===================================================================
--- llvm/lib/IR/Core.cpp
+++ llvm/lib/IR/Core.cpp
@@ -904,6 +904,10 @@
   unwrap(OldVal)->replaceAllUsesWith(unwrap(NewVal));
 }
 
+void LLVMReplaceAllMetadataUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal) {
+  ValueAsMetadata::handleRAUW(unwrap<Value>(OldVal), unwrap<Value>(NewVal));
+}
+
 int LLVMHasMetadata(LLVMValueRef Inst) {
   return unwrap<Instruction>(Inst)->hasMetadata();
 }
Index: llvm/include/llvm-c/Core.h
===================================================================
--- llvm/include/llvm-c/Core.h
+++ llvm/include/llvm-c/Core.h
@@ -2902,6 +2902,13 @@
 /** Deprecated: Use LLVMMDNodeInContext2 instead. */
 LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count);
 
+/**
+ * Replace all metadata uses of a value with another one.
+ *
+ * @see llvm::Value::replaceAllUsesWith()
+ */
+void LLVMReplaceAllMetadataUsesWith(LLVMValueRef OldVal, LLVMValueRef NewVal);
+
 /**
  * @}
  */


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125388.428660.patch
Type: text/x-patch
Size: 1503 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220511/d03b88e4/attachment.bin>


More information about the llvm-commits mailing list