diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 26c922a7810..a81b3463c5b 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -499,11 +499,13 @@ static void WriteModuleMetadata(const ValueEnumerator &VE, for (unsigned i = 0, e = Vals.size(); i != e; ++i) { if (const MDNode *N = dyn_cast(Vals[i].first)) { - if (!StartedMetadataBlock) { - Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); - StartedMetadataBlock = true; + if (!N->isFunctionLocal()) { + if (!StartedMetadataBlock) { + Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); + StartedMetadataBlock = true; + } + WriteMDNode(N, VE, Stream, Record); } - WriteMDNode(N, VE, Stream, Record); } else if (const MDString *MDS = dyn_cast(Vals[i].first)) { if (!StartedMetadataBlock) { Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); @@ -552,6 +554,35 @@ static void WriteModuleMetadata(const ValueEnumerator &VE, Stream.ExitBlock(); } +static void WriteFunctionLocalMetadata(const ValueEnumerator &VE, + BitstreamWriter &Stream) { + bool StartedMetadataBlock = false; + SmallVector Record; + ValueEnumerator::ValueList Vals = VE.getMDValues(); + ValueEnumerator::ValueList::iterator it = Vals.begin(); + ValueEnumerator::ValueList::iterator end = Vals.end(); + + while (it != end) { + if (const MDNode *N = dyn_cast((*it).first)) { + if (N->isFunctionLocal()) { + if (!StartedMetadataBlock) { + Stream.EnterSubblock(bitc::METADATA_BLOCK_ID, 3); + StartedMetadataBlock = true; + } + WriteMDNode(N, VE, Stream, Record); + // Remove function-local MD, since it is used outside of function. + it = Vals.erase(it); + end = Vals.end(); + continue; + } + } + ++it; + } + + if (StartedMetadataBlock) + Stream.ExitBlock(); +} + static void WriteMetadataAttachment(const Function &F, const ValueEnumerator &VE, BitstreamWriter &Stream) { @@ -1210,6 +1241,7 @@ static void WriteFunction(const Function &F, ValueEnumerator &VE, // Emit names for all the instructions etc. WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream); + WriteFunctionLocalMetadata(VE, Stream); WriteMetadataAttachment(F, VE, Stream); VE.purgeFunction(); Stream.ExitBlock();