[PATCH] D72207: [LLVM-C][bindings/go] Get metadata from a value

Ayke via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 4 09:49:18 PST 2020


aykevl created this revision.
aykevl added reviewers: whitequark, CodaFi.
Herald added a reviewer: deadalnix.
Herald added subscribers: hiraditya, aprantl.
Herald added a project: LLVM.

I added a new function that returns a metadata node directly instead of wrapping it in a value, which IIRC is now the recommended approach.

This is especially useful for getting debug info metadata from global variables.

Note that this is a breaking change for the Go bindings. I think this is fine because having a metadata-wrapped-as-value wasn't really usable anyway from the API and the old API was inconsistent with `SetMetadata` (which accepts a `Metadata` instead of a `Value` object). Also, I couldn't find any uses of the `Metadata()` call in llgo and I can tell you that it is not used in TinyGo.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72207

Files:
  llvm/bindings/go/llvm/ir.go
  llvm/include/llvm-c/Core.h
  llvm/lib/IR/Core.cpp


Index: llvm/lib/IR/Core.cpp
===================================================================
--- llvm/lib/IR/Core.cpp
+++ llvm/lib/IR/Core.cpp
@@ -868,6 +868,21 @@
   return nullptr;
 }
 
+LLVMMetadataRef LLVMGetMetadata2(LLVMValueRef Val, unsigned KindID) {
+  auto *V = unwrap<Value>(Val);
+  if (auto *I = dyn_cast<Instruction>(V)) {
+    if (auto *MD = I->getMetadata(KindID))
+      return wrap(MD);
+    return nullptr;
+  }
+  if (auto *G = dyn_cast<GlobalObject>(V)) {
+    if (auto *MD = G->getMetadata(KindID))
+      return wrap(MD);
+    return nullptr;
+  }
+  return nullptr;
+}
+
 // MetadataAsValue uses a canonical format which strips the actual MDNode for
 // MDNode with just a single constant value, storing just a ConstantAsMetadata
 // This undoes this canonicalization, reconstructing the MDNode.
Index: llvm/include/llvm-c/Core.h
===================================================================
--- llvm/include/llvm-c/Core.h
+++ llvm/include/llvm-c/Core.h
@@ -3055,10 +3055,16 @@
 int LLVMHasMetadata(LLVMValueRef Val);
 
 /**
- * Return metadata associated with an instruction value.
+ * Return metadata associated with an instruction value. It returns a metadata
+ * object wrapped as a value.
  */
 LLVMValueRef LLVMGetMetadata(LLVMValueRef Val, unsigned KindID);
 
+/**
+ * Return metadata associated with an instruction value or global object.
+ */
+LLVMMetadataRef LLVMGetMetadata2(LLVMValueRef Val, unsigned KindID);
+
 /**
  * Set metadata associated with an instruction value.
  */
Index: llvm/bindings/go/llvm/ir.go
===================================================================
--- llvm/bindings/go/llvm/ir.go
+++ llvm/bindings/go/llvm/ir.go
@@ -712,8 +712,8 @@
 func (v Value) Dump()                       { C.LLVMDumpValue(v.C) }
 func (v Value) ReplaceAllUsesWith(nv Value) { C.LLVMReplaceAllUsesWith(v.C, nv.C) }
 func (v Value) HasMetadata() bool           { return C.LLVMHasMetadata(v.C) != 0 }
-func (v Value) Metadata(kind int) (rv Value) {
-	rv.C = C.LLVMGetMetadata(v.C, C.unsigned(kind))
+func (v Value) Metadata(kind int) (md Metadata) {
+	md.C = C.LLVMGetMetadata2(v.C, C.unsigned(kind))
 	return
 }
 func (v Value) SetMetadata(kind int, node Metadata) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72207.236185.patch
Type: text/x-patch
Size: 2215 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200104/1652f799/attachment.bin>


More information about the llvm-commits mailing list