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) { 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/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(Val); + if (auto *I = dyn_cast(V)) { + if (auto *MD = I->getMetadata(KindID)) + return wrap(MD); + return nullptr; + } + if (auto *G = dyn_cast(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.