Skip to content

Commit 55639c2

Browse files
committed
[OpenMP] Properly save strings when doing LTO
Summary: We were not previously saving strings when saving symbol names during LTO symbol resolution. This caused a crash inside the dense set when some of the strings would rarely be moved internally by the object file class.
1 parent d81f003 commit 55639c2

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ static SmallVector<std::string, 16> TempFiles;
142142
/// Codegen flags for LTO backend.
143143
static codegen::RegisterCodeGenFlags CodeGenFlags;
144144

145+
/// Static buffer to hold StringRef values.
146+
static BumpPtrAllocator Alloc;
147+
145148
/// Magic section string that marks the existence of offloading data. The
146149
/// section string will be formatted as `.llvm.offloading.<triple>.<arch>`.
147150
#define OFFLOAD_SECTION_MAGIC_STR ".llvm.offloading."
@@ -861,8 +864,9 @@ Error linkBitcodeFiles(SmallVectorImpl<std::string> &InputFiles,
861864
SmallVector<std::unique_ptr<MemoryBuffer>, 4> SavedBuffers;
862865
SmallVector<std::unique_ptr<lto::InputFile>, 4> BitcodeFiles;
863866
SmallVector<std::string, 4> NewInputFiles;
864-
StringMap<bool> UsedInRegularObj;
865-
StringMap<bool> UsedInSharedLib;
867+
DenseSet<StringRef> UsedInRegularObj;
868+
DenseSet<StringRef> UsedInSharedLib;
869+
StringSaver Saver(Alloc);
866870

867871
// Search for bitcode files in the input and create an LTO input file. If it
868872
// is not a bitcode file, scan its symbol table for symbols we need to
@@ -888,9 +892,9 @@ Error linkBitcodeFiles(SmallVectorImpl<std::string> &InputFiles,
888892

889893
// Record if we've seen these symbols in any object or shared libraries.
890894
if ((*ObjFile)->isRelocatableObject())
891-
UsedInRegularObj[*Name] = true;
895+
UsedInRegularObj.insert(Saver.save(*Name));
892896
else
893-
UsedInSharedLib[*Name] = true;
897+
UsedInSharedLib.insert(Saver.save(*Name));
894898
}
895899
} else {
896900
Expected<std::unique_ptr<lto::InputFile>> InputFileOrErr =
@@ -950,14 +954,15 @@ Error linkBitcodeFiles(SmallVectorImpl<std::string> &InputFiles,
950954
// We will use this as the prevailing symbol definition in LTO unless
951955
// it is undefined or another definition has already been used.
952956
Res.Prevailing =
953-
!Sym.isUndefined() && PrevailingSymbols.insert(Sym.getName()).second;
957+
!Sym.isUndefined() &&
958+
PrevailingSymbols.insert(Saver.save(Sym.getName())).second;
954959

955960
// We need LTO to preseve the following global symbols:
956961
// 1) Symbols used in regular objects.
957962
// 2) Sections that will be given a __start/__stop symbol.
958-
// 3) Prevailing symbols that are needed visibile to external libraries.
963+
// 3) Prevailing symbols that are needed visible to external libraries.
959964
Res.VisibleToRegularObj =
960-
UsedInRegularObj[Sym.getName()] ||
965+
UsedInRegularObj.contains(Sym.getName()) ||
961966
isValidCIdentifier(Sym.getSectionName()) ||
962967
(Res.Prevailing &&
963968
(Sym.getVisibility() != GlobalValue::HiddenVisibility &&
@@ -967,7 +972,7 @@ Error linkBitcodeFiles(SmallVectorImpl<std::string> &InputFiles,
967972
// referenced by other files.
968973
Res.ExportDynamic =
969974
Sym.getVisibility() != GlobalValue::HiddenVisibility &&
970-
(UsedInSharedLib[Sym.getName()] ||
975+
(UsedInSharedLib.contains(Sym.getName()) ||
971976
!Sym.canBeOmittedFromSymbolTable());
972977

973978
// The final definition will reside in this linkage unit if the symbol is

0 commit comments

Comments
 (0)