Skip to content

Commit 3ad27e9

Browse files
committed
Code cleanup in preparation for adding LTO for wasm. NFC.
- Move some common code into Common/rrorHandler.cpp and Common/Strings.h. - Don't use `fatal` when incompatible bitcode files are encountered. - Rename NameRef variable to just Name See D47162 Differential Revision: https://reviews.llvm.org/D47206 llvm-svn: 333021
1 parent a82ee18 commit 3ad27e9

File tree

11 files changed

+72
-79
lines changed

11 files changed

+72
-79
lines changed

lld/COFF/Config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ struct Configuration {
129129
Symbol *SEHCount = nullptr;
130130

131131
// Used for /opt:lldlto=N
132-
unsigned LTOOptLevel = 2;
132+
unsigned LTOO = 2;
133133

134134
// Used for /opt:lldltojobs=N
135-
unsigned LTOJobs = 0;
135+
unsigned ThinLTOJobs = 0;
136136
// Used for /opt:lldltopartitions=N
137137
unsigned LTOPartitions = 1;
138138

lld/COFF/Driver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,12 +1086,12 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
10861086
TailMerge = 0;
10871087
} else if (S.startswith("lldlto=")) {
10881088
StringRef OptLevel = S.substr(7);
1089-
if (OptLevel.getAsInteger(10, Config->LTOOptLevel) ||
1090-
Config->LTOOptLevel > 3)
1089+
if (OptLevel.getAsInteger(10, Config->LTOO) || Config->LTOO > 3)
10911090
error("/opt:lldlto: invalid optimization level: " + OptLevel);
10921091
} else if (S.startswith("lldltojobs=")) {
10931092
StringRef Jobs = S.substr(11);
1094-
if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0)
1093+
if (Jobs.getAsInteger(10, Config->ThinLTOJobs) ||
1094+
Config->ThinLTOJobs == 0)
10951095
error("/opt:lldltojobs: invalid job count: " + Jobs);
10961096
} else if (S.startswith("lldltopartitions=")) {
10971097
StringRef N = S.substr(17);

lld/COFF/LTO.cpp

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "InputFiles.h"
1313
#include "Symbols.h"
1414
#include "lld/Common/ErrorHandler.h"
15+
#include "lld/Common/Strings.h"
1516
#include "lld/Common/TargetOptionsCommandFlags.h"
1617
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/ADT/SmallString.h"
@@ -40,53 +41,32 @@ using namespace llvm::object;
4041
using namespace lld;
4142
using namespace lld::coff;
4243

43-
static void diagnosticHandler(const DiagnosticInfo &DI) {
44-
SmallString<128> ErrStorage;
45-
raw_svector_ostream OS(ErrStorage);
46-
DiagnosticPrinterRawOStream DP(OS);
47-
DI.print(DP);
48-
warn(ErrStorage);
49-
}
50-
51-
static void checkError(Error E) {
52-
handleAllErrors(std::move(E),
53-
[&](ErrorInfoBase &EIB) { error(EIB.message()); });
54-
}
55-
56-
static void saveBuffer(StringRef Buffer, const Twine &Path) {
57-
std::error_code EC;
58-
raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
59-
if (EC)
60-
error("cannot create " + Path + ": " + EC.message());
61-
OS << Buffer;
62-
}
63-
6444
static std::unique_ptr<lto::LTO> createLTO() {
65-
lto::Config Conf;
66-
Conf.Options = InitTargetOptionsFromCodeGenFlags();
45+
lto::Config C;
46+
C.Options = InitTargetOptionsFromCodeGenFlags();
6747

6848
// Always emit a section per function/datum with LTO. LLVM LTO should get most
6949
// of the benefit of linker GC, but there are still opportunities for ICF.
70-
Conf.Options.FunctionSections = true;
71-
Conf.Options.DataSections = true;
50+
C.Options.FunctionSections = true;
51+
C.Options.DataSections = true;
7252

7353
// Use static reloc model on 32-bit x86 because it usually results in more
7454
// compact code, and because there are also known code generation bugs when
7555
// using the PIC model (see PR34306).
7656
if (Config->Machine == COFF::IMAGE_FILE_MACHINE_I386)
77-
Conf.RelocModel = Reloc::Static;
57+
C.RelocModel = Reloc::Static;
7858
else
79-
Conf.RelocModel = Reloc::PIC_;
80-
Conf.DisableVerify = true;
81-
Conf.DiagHandler = diagnosticHandler;
82-
Conf.OptLevel = Config->LTOOptLevel;
59+
C.RelocModel = Reloc::PIC_;
60+
C.DisableVerify = true;
61+
C.DiagHandler = diagnosticHandler;
62+
C.OptLevel = Config->LTOO;
8363
if (Config->SaveTemps)
84-
checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
85-
/*UseInputModulePath*/ true));
64+
checkError(C.addSaveTemps(std::string(Config->OutputFile) + ".",
65+
/*UseInputModulePath*/ true));
8666
lto::ThinBackend Backend;
87-
if (Config->LTOJobs != 0)
88-
Backend = lto::createInProcessThinBackend(Config->LTOJobs);
89-
return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
67+
if (Config->ThinLTOJobs != 0)
68+
Backend = lto::createInProcessThinBackend(Config->ThinLTOJobs);
69+
return llvm::make_unique<lto::LTO>(std::move(C), Backend,
9070
Config->LTOPartitions);
9171
}
9272

@@ -125,7 +105,7 @@ void BitcodeCompiler::add(BitcodeFile &F) {
125105
// and return the resulting objects.
126106
std::vector<StringRef> BitcodeCompiler::compile() {
127107
unsigned MaxTasks = LTOObj->getMaxTasks();
128-
Buff.resize(MaxTasks);
108+
Buf.resize(MaxTasks);
129109
Files.resize(MaxTasks);
130110

131111
// The /lldltocache option specifies the path to a directory in which to cache
@@ -141,7 +121,7 @@ std::vector<StringRef> BitcodeCompiler::compile() {
141121
checkError(LTOObj->run(
142122
[&](size_t Task) {
143123
return llvm::make_unique<lto::NativeObjectStream>(
144-
llvm::make_unique<raw_svector_ostream>(Buff[Task]));
124+
llvm::make_unique<raw_svector_ostream>(Buf[Task]));
145125
},
146126
Cache));
147127

@@ -150,15 +130,15 @@ std::vector<StringRef> BitcodeCompiler::compile() {
150130

151131
std::vector<StringRef> Ret;
152132
for (unsigned I = 0; I != MaxTasks; ++I) {
153-
if (Buff[I].empty())
133+
if (Buf[I].empty())
154134
continue;
155135
if (Config->SaveTemps) {
156136
if (I == 0)
157-
saveBuffer(Buff[I], Config->OutputFile + ".lto.obj");
137+
saveBuffer(Buf[I], Config->OutputFile + ".lto.obj");
158138
else
159-
saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.obj");
139+
saveBuffer(Buf[I], Config->OutputFile + Twine(I) + ".lto.obj");
160140
}
161-
Ret.emplace_back(Buff[I].data(), Buff[I].size());
141+
Ret.emplace_back(Buf[I].data(), Buf[I].size());
162142
}
163143

164144
for (std::unique_ptr<MemoryBuffer> &File : Files)

lld/COFF/LTO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class BitcodeCompiler {
4848

4949
private:
5050
std::unique_ptr<llvm::lto::LTO> LTOObj;
51-
std::vector<SmallString<0>> Buff;
51+
std::vector<SmallString<0>> Buf;
5252
std::vector<std::unique_ptr<MemoryBuffer>> Files;
5353
};
5454
}

lld/COFF/SymbolTable.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ void SymbolTable::addFile(InputFile *File) {
3838
if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
3939
Config->Machine = MT;
4040
} else if (MT != IMAGE_FILE_MACHINE_UNKNOWN && Config->Machine != MT) {
41-
fatal(toString(File) + ": machine type " + machineToStr(MT) +
41+
error(toString(File) + ": machine type " + machineToStr(MT) +
4242
" conflicts with " + machineToStr(Config->Machine));
43+
return;
4344
}
4445

4546
if (auto *F = dyn_cast<ObjFile>(File)) {

lld/Common/ErrorHandler.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#include "lld/Common/Threads.h"
1313

1414
#include "llvm/ADT/Twine.h"
15-
#include "llvm/Support/Error.h"
15+
#include "llvm/IR/DiagnosticInfo.h"
16+
#include "llvm/IR/DiagnosticPrinter.h"
1617
#include "llvm/Support/ManagedStatic.h"
1718
#include "llvm/Support/raw_ostream.h"
1819
#include <mutex>
@@ -59,6 +60,19 @@ void lld::exitLld(int Val) {
5960
_exit(Val);
6061
}
6162

63+
void lld::diagnosticHandler(const DiagnosticInfo &DI) {
64+
SmallString<128> S;
65+
raw_svector_ostream OS(S);
66+
DiagnosticPrinterRawOStream DP(OS);
67+
DI.print(DP);
68+
warn(S);
69+
}
70+
71+
void lld::checkError(Error E) {
72+
handleAllErrors(std::move(E),
73+
[&](ErrorInfoBase &EIB) { error(EIB.message()); });
74+
}
75+
6276
void ErrorHandler::print(StringRef S, raw_ostream::Colors C) {
6377
*ErrorOS << LogName << ": ";
6478
if (ColorDiagnostics) {

lld/Common/Strings.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,12 @@ bool lld::isValidCIdentifier(StringRef S) {
9898
std::all_of(S.begin() + 1, S.end(),
9999
[](char C) { return C == '_' || isAlnum(C); });
100100
}
101+
102+
// Write the contents of the a buffer to a file
103+
void lld::saveBuffer(StringRef Buffer, const Twine &Path) {
104+
std::error_code EC;
105+
raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
106+
if (EC)
107+
error("cannot create " + Path + ": " + EC.message());
108+
OS << Buffer;
109+
}

lld/ELF/InputFiles.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,9 @@ static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) {
10171017
case Triple::x86_64:
10181018
return EM_X86_64;
10191019
default:
1020-
fatal(Path + ": could not infer e_machine from bitcode target triple " +
1020+
error(Path + ": could not infer e_machine from bitcode target triple " +
10211021
T.str());
1022+
return EM_NONE;
10221023
}
10231024
}
10241025

@@ -1065,7 +1066,7 @@ template <class ELFT>
10651066
static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
10661067
const lto::InputFile::Symbol &ObjSym,
10671068
BitcodeFile &F) {
1068-
StringRef NameRef = Saver.save(ObjSym.getName());
1069+
StringRef Name = Saver.save(ObjSym.getName());
10691070
uint32_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL;
10701071

10711072
uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE;
@@ -1074,20 +1075,20 @@ static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats,
10741075

10751076
int C = ObjSym.getComdatIndex();
10761077
if (C != -1 && !KeptComdats[C])
1077-
return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
1078+
return Symtab->addUndefined<ELFT>(Name, Binding, Visibility, Type,
10781079
CanOmitFromDynSym, &F);
10791080

10801081
if (ObjSym.isUndefined())
1081-
return Symtab->addUndefined<ELFT>(NameRef, Binding, Visibility, Type,
1082+
return Symtab->addUndefined<ELFT>(Name, Binding, Visibility, Type,
10821083
CanOmitFromDynSym, &F);
10831084

10841085
if (ObjSym.isCommon())
1085-
return Symtab->addCommon(NameRef, ObjSym.getCommonSize(),
1086+
return Symtab->addCommon(Name, ObjSym.getCommonSize(),
10861087
ObjSym.getCommonAlignment(), Binding, Visibility,
10871088
STT_OBJECT, F);
10881089

1089-
return Symtab->addBitcode(NameRef, Binding, Visibility, Type,
1090-
CanOmitFromDynSym, F);
1090+
return Symtab->addBitcode(Name, Binding, Visibility, Type, CanOmitFromDynSym,
1091+
F);
10911092
}
10921093

10931094
template <class ELFT>

lld/ELF/LTO.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,6 @@ using namespace llvm::ELF;
4545
using namespace lld;
4646
using namespace lld::elf;
4747

48-
// This is for use when debugging LTO.
49-
static void saveBuffer(StringRef Buffer, const Twine &Path) {
50-
std::error_code EC;
51-
raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
52-
if (EC)
53-
error("cannot create " + Path + ": " + EC.message());
54-
OS << Buffer;
55-
}
56-
57-
static void diagnosticHandler(const DiagnosticInfo &DI) {
58-
SmallString<128> S;
59-
raw_svector_ostream OS(S);
60-
DiagnosticPrinterRawOStream DP(OS);
61-
DI.print(DP);
62-
warn(S);
63-
}
64-
65-
static void checkError(Error E) {
66-
handleAllErrors(std::move(E),
67-
[&](ErrorInfoBase &EIB) { error(EIB.message()); });
68-
}
69-
7048
// Creates an empty file to store a list of object files for final
7149
// linking of distributed ThinLTO.
7250
static std::unique_ptr<raw_fd_ostream> openFile(StringRef File) {

lld/include/lld/Common/ErrorHandler.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include "llvm/Support/Error.h"
3535
#include "llvm/Support/FileOutputBuffer.h"
3636

37+
namespace llvm {
38+
class DiagnosticInfo;
39+
}
40+
3741
namespace lld {
3842

3943
class ErrorHandler {
@@ -74,6 +78,9 @@ inline uint64_t errorCount() { return errorHandler().ErrorCount; }
7478

7579
LLVM_ATTRIBUTE_NORETURN void exitLld(int Val);
7680

81+
void diagnosticHandler(const llvm::DiagnosticInfo &DI);
82+
void checkError(Error E);
83+
7784
// check functions are convenient functions to strip errors
7885
// from error-or-value objects.
7986
template <class T> T check(ErrorOr<T> E) {

0 commit comments

Comments
 (0)