Index: lib/IR/Core.cpp =================================================================== --- lib/IR/Core.cpp +++ lib/IR/Core.cpp @@ -872,6 +872,30 @@ unwrap(Inst)->setMetadata(KindID, N); } +struct LLVMOpaqueValueMetadataEntry { + unsigned Kind; + LLVMMetadataRef Metadata; +}; + +using MetadataEntries = SmallVectorImpl>; +static LLVMValueMetadataEntry * +llvm_getMetadata(size_t *NumEntries, + llvm::function_ref AccessMD) { + SmallVector, 8> MVEs; + AccessMD(MVEs); + + LLVMOpaqueValueMetadataEntry *Result = + static_cast( + safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry))); + for (unsigned i = 0; i < MVEs.size(); ++i) { + const auto &ModuleFlag = MVEs[i]; + Result[i].Kind = ModuleFlag.first; + Result[i].Metadata = wrap(ModuleFlag.second); + } + *NumEntries = MVEs.size(); + return Result; +} + LLVMValueMetadataEntry * LLVMInstructionGetAllMetadataOtherThanDebugLoc(LLVMValueRef Value, size_t *NumEntries) { @@ -1884,30 +1908,6 @@ "only GlobalValue, AllocaInst, LoadInst and StoreInst have alignment"); } -struct LLVMOpaqueValueMetadataEntry { - unsigned Kind; - LLVMMetadataRef Metadata; -}; - -using MetadataEntries = SmallVectorImpl>; -static LLVMValueMetadataEntry * -llvm_getMetadata(size_t *NumEntries, - llvm::function_ref AccessMD) { - SmallVector, 8> MVEs; - AccessMD(MVEs); - - LLVMOpaqueValueMetadataEntry *Result = - static_cast( - safe_malloc(MVEs.size() * sizeof(LLVMOpaqueValueMetadataEntry))); - for (unsigned i = 0; i < MVEs.size(); ++i) { - const auto &ModuleFlag = MVEs[i]; - Result[i].Kind = ModuleFlag.first; - Result[i].Metadata = wrap(ModuleFlag.second); - } - *NumEntries = MVEs.size(); - return Result; -} - LLVMValueMetadataEntry *LLVMGlobalCopyAllMetadata(LLVMValueRef Value, size_t *NumEntries) { return llvm_getMetadata(NumEntries, [&Value](MetadataEntries &Entries) { @@ -1940,7 +1940,7 @@ void LLVMGlobalSetMetadata(LLVMValueRef Global, unsigned Kind, LLVMMetadataRef MD) { - unwrap(Global)->setMetadata(Kind, unwrapDI(MD)); + unwrap(Global)->setMetadata(Kind, unwrap(MD)); } void LLVMGlobalEraseMetadata(LLVMValueRef Global, unsigned Kind) {