From: Bruno Ricci Date: Thu, 18 Apr 2019 15:13:27 +0000 (+0000) Subject: [Serialization] Stable serialization order for OpenCLTypeExtMap and OpenCLDeclExtMap X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=678f3edd112db678614c087c793cc8ca4afb294b;p=clang [Serialization] Stable serialization order for OpenCLTypeExtMap and OpenCLDeclExtMap Sort the elements of Sema::OpenCLTypeExtMap and Sema::OpenCLDeclExtMap by TypeIDs and DeclIDs to guarantee a stable serialization order. Differential Revision: https://reviews.llvm.org/D60835 Reviewed By: Anastasia Reviewers: Anastasia, lebedev.ri git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@358674 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 3609e58919..756411a8c5 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -4278,14 +4278,32 @@ void ASTWriter::WriteOpenCLExtensionTypes(Sema &SemaRef) { if (!SemaRef.Context.getLangOpts().OpenCL) return; + // Sort the elements of the map OpenCLTypeExtMap by TypeIDs, + // without copying them. + const llvm::DenseMap> &OpenCLTypeExtMap = + SemaRef.OpenCLTypeExtMap; + using ElementTy = std::pair *>; + llvm::SmallVector StableOpenCLTypeExtMap; + StableOpenCLTypeExtMap.reserve(OpenCLTypeExtMap.size()); + + for (const auto &I : OpenCLTypeExtMap) + StableOpenCLTypeExtMap.emplace_back( + getTypeID(I.first->getCanonicalTypeInternal()), &I.second); + + auto CompareByTypeID = [](const ElementTy &E1, const ElementTy &E2) -> bool { + return E1.first < E2.first; + }; + llvm::sort(StableOpenCLTypeExtMap, CompareByTypeID); + RecordData Record; - for (const auto &I : SemaRef.OpenCLTypeExtMap) { - Record.push_back( - static_cast(getTypeID(I.first->getCanonicalTypeInternal()))); - Record.push_back(I.second.size()); - for (auto Ext : I.second) + for (const ElementTy &E : StableOpenCLTypeExtMap) { + Record.push_back(E.first); // TypeID + const std::set *ExtSet = E.second; + Record.push_back(static_cast(ExtSet->size())); + for (const std::string &Ext : *ExtSet) AddString(Ext, Record); } + Stream.EmitRecord(OPENCL_EXTENSION_TYPES, Record); } @@ -4293,13 +4311,31 @@ void ASTWriter::WriteOpenCLExtensionDecls(Sema &SemaRef) { if (!SemaRef.Context.getLangOpts().OpenCL) return; + // Sort the elements of the map OpenCLDeclExtMap by DeclIDs, + // without copying them. + const llvm::DenseMap> &OpenCLDeclExtMap = + SemaRef.OpenCLDeclExtMap; + using ElementTy = std::pair *>; + llvm::SmallVector StableOpenCLDeclExtMap; + StableOpenCLDeclExtMap.reserve(OpenCLDeclExtMap.size()); + + for (const auto &I : OpenCLDeclExtMap) + StableOpenCLDeclExtMap.emplace_back(getDeclID(I.first), &I.second); + + auto CompareByDeclID = [](const ElementTy &E1, const ElementTy &E2) -> bool { + return E1.first < E2.first; + }; + llvm::sort(StableOpenCLDeclExtMap, CompareByDeclID); + RecordData Record; - for (const auto &I : SemaRef.OpenCLDeclExtMap) { - Record.push_back(getDeclID(I.first)); - Record.push_back(static_cast(I.second.size())); - for (auto Ext : I.second) + for (const ElementTy &E : StableOpenCLDeclExtMap) { + Record.push_back(E.first); // DeclID + const std::set *ExtSet = E.second; + Record.push_back(static_cast(ExtSet->size())); + for (const std::string &Ext : *ExtSet) AddString(Ext, Record); } + Stream.EmitRecord(OPENCL_EXTENSION_DECLS, Record); }