Index: docs/ReleaseNotes.rst =================================================================== --- docs/ReleaseNotes.rst +++ docs/ReleaseNotes.rst @@ -57,6 +57,8 @@ * The C API enum LLVMAttribute is deprecated in favor of LLVMGetAttrKindID. +* The C API function LLVMMDNodeis deprecated in favor of LLVMGetMDNode. + * ``TargetFrameLowering::eliminateCallFramePseudoInstr`` now returns an iterator to the next instruction instead of ``void``. Targets that previously did ``MBB.erase(I); return;`` now probably want ``return MBB.erase(I);``. Index: include/llvm-c/Core.h =================================================================== --- include/llvm-c/Core.h +++ include/llvm-c/Core.h @@ -2128,13 +2128,32 @@ * * The returned value corresponds to the llvm::MDNode class. */ -LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, - unsigned Count); +// LLVMValueRef LLVMMDNodeInContext(LLVMContextRef C, LLVMValueRef *Vals, +// unsigned Count); /** * Obtain a MDNode value from the global context. */ -LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count); +// LLVMValueRef LLVMMDNode(LLVMValueRef *Vals, unsigned Count); + +/** + * Obtain a MDNode metadata from the global context. + * + * While LLVMMDNode and LLVMMDNodeInContext will accept a variety + * of inputs, LLVMGetMDNode will only accept metadata as arguments. + */ +LLVMValueRef LLVMGetMDNode(LLVMContextRef C, LLVMValueRef *Vals, + unsigned Count); + +/** + * Obtain a Metadata as a Value. + */ +LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMValueRef MD); + +/** + * Obtain a Value as a Metadata. + */ +LLVMValueRef LLVMValueAsMetadata(LLVMValueRef Val); /** * Obtain the underlying string from a MDString value. Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -814,6 +814,31 @@ return LLVMMDNodeInContext(LLVMGetGlobalContext(), Vals, Count); } +LLVMValueRef LLVMGetMDNode(LLVMContextRef C, LLVMValueRef *Vals, + unsigned Count) { + LLVMContext &Context = *unwrap(C); + SmallVector MDs; + for (auto *V : makeArrayRef(Vals, Count)) + MDs.push_back(unwrap(V)->getMetadata()); + return wrap(MetadataAsValue::get(Context, MDNode::get(Context, MDs))); +} + +LLVMValueRef LLVMMetadataAsValue(LLVMContextRef C, LLVMValueRef MD) { + return wrap(unwrap(MD)); +} + +LLVMValueRef LLVMValueAsMetadata(LLVMValueRef Val) { + auto *V = unwrap(Val); + Metadata *MD; + if (auto *C = dyn_cast(V)) + MD = ConstantAsMetadata::get(C); + if (auto *MAV = dyn_cast(V)) + MD = MAV->getMetadata(); + else + MD = ValueAsMetadata::get(V); + return wrap(MetadataAsValue::get(V->getContext(), MD)); +} + const char *LLVMGetMDString(LLVMValueRef V, unsigned *Length) { if (const auto *MD = dyn_cast(unwrap(V))) if (const MDString *S = dyn_cast(MD->getMetadata())) { Index: tools/llvm-c-test/metadata.c =================================================================== --- tools/llvm-c-test/metadata.c +++ tools/llvm-c-test/metadata.c @@ -15,28 +15,33 @@ #include "llvm-c-test.h" int llvm_add_named_metadata_operand(void) { - LLVMModuleRef m = LLVMModuleCreateWithName("Mod"); - LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; + LLVMContextRef C = LLVMGetGlobalContext(); + LLVMModuleRef M = LLVMModuleCreateWithName("Mod"); + LLVMValueRef Values[] = { + LLVMValueAsMetadata(LLVMConstInt(LLVMInt32Type(), 0, 0)) + }; // This used to trigger an assertion - LLVMAddNamedMetadataOperand(m, "name", LLVMMDNode(values, 1)); + LLVMAddNamedMetadataOperand(M, "name", LLVMGetMDNode(C, Values, 1)); - LLVMDisposeModule(m); + LLVMDisposeModule(M); return 0; } int llvm_set_metadata(void) { - LLVMBuilderRef b = LLVMCreateBuilder(); - LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; + LLVMBuilderRef B = LLVMCreateBuilder(); + LLVMValueRef Values[] = { + LLVMValueAsMetadata(LLVMConstInt(LLVMInt32Type(), 0, 0)) + }; // This used to trigger an assertion LLVMSetMetadata( - LLVMBuildRetVoid(b), + LLVMBuildRetVoid(B), LLVMGetMDKindID("kind", 4), - LLVMMDNode(values, 1)); + LLVMGetMDNode(LLVMGetGlobalContext(), Values, 1)); - LLVMDisposeBuilder(b); + LLVMDisposeBuilder(B); return 0; }