Skip to content

Commit fccd293

Browse files
committed
Merge remote-tracking branch 'otcshare_llvm/sycl-web' into llvmspirv_pulldown
2 parents cbdb06a + 1306287 commit fccd293

File tree

13,924 files changed

+923417
-425228
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

13,924 files changed

+923417
-425228
lines changed

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ LLVM GN Syncbot <llvmgnsyncbot@gmail.com>
3737
Martin Storsjö <martin@martin.st>
3838
Ramkumar Ramachandra <r@artagnon.com> <artagnon@gmail.com>
3939
Saleem Abdulrasool <compnerd@compnerd.org>
40+
Tommy Chiang <ty1208chiang@gmail.com> <oToToT@users.noreply.github.com>

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,11 +1614,10 @@ class BinaryFunction {
16141614

16151615
/// Dump function information to debug output. If \p PrintInstructions
16161616
/// is true - include instruction disassembly.
1617-
void dump(bool PrintInstructions = true) const;
1617+
void dump() const;
16181618

16191619
/// Print function information to the \p OS stream.
1620-
void print(raw_ostream &OS, std::string Annotation = "",
1621-
bool PrintInstructions = true);
1620+
void print(raw_ostream &OS, std::string Annotation = "");
16221621

16231622
/// Print all relocations between \p Offset and \p Offset + \p Size in
16241623
/// this function.

bolt/include/bolt/Core/BinarySection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class BinarySection {
157157
BinarySection(BinaryContext &BC, SectionRef Section)
158158
: BC(BC), Name(getName(Section)), Section(Section),
159159
Contents(getContents(Section)), Address(Section.getAddress()),
160-
Size(Section.getSize()), Alignment(Section.getAlignment()),
160+
Size(Section.getSize()), Alignment(Section.getAlignment().value()),
161161
OutputName(Name), SectionNumber(++Count) {
162162
if (isELF()) {
163163
ELFType = ELFSectionRef(Section).getType();

bolt/include/bolt/Core/DebugData.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ class DebugAbbrevWriter {
10031003
DWARFContext &Context;
10041004

10051005
/// DWO ID used to identify unit contribution in DWP.
1006-
Optional<uint64_t> DWOId;
1006+
std::optional<uint64_t> DWOId;
10071007

10081008
/// Add abbreviations from compile/type \p Unit to the writer.
10091009
void addUnitAbbreviations(DWARFUnit &Unit);
@@ -1020,7 +1020,7 @@ class DebugAbbrevWriter {
10201020
/// If type units are used, the caller is responsible for verifying
10211021
/// that abbreviations are shared by CU and TUs.
10221022
DebugAbbrevWriter(DWARFContext &Context,
1023-
Optional<uint64_t> DWOId = std::nullopt)
1023+
std::optional<uint64_t> DWOId = std::nullopt)
10241024
: Context(Context), DWOId(DWOId) {}
10251025

10261026
DebugAbbrevWriter(const DebugAbbrevWriter &) = delete;

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,11 @@ class MCPlusBuilder {
14201420
llvm_unreachable("not implemented");
14211421
}
14221422

1423+
virtual bool matchAdrpAddPair(const MCInst &Adrp, const MCInst &Add) const {
1424+
llvm_unreachable("not implemented");
1425+
return false;
1426+
}
1427+
14231428
virtual int getShortJmpEncodingSize() const {
14241429
llvm_unreachable("not implemented");
14251430
}

bolt/include/bolt/Core/Relocation.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ struct Relocation {
5858
/// Skip relocations that we don't want to handle in BOLT
5959
static bool skipRelocationType(uint64_t Type);
6060

61-
/// Handle special cases when relocation should not be processed by BOLT
62-
static bool skipRelocationProcess(uint64_t Type, uint64_t Contents);
61+
/// Handle special cases when relocation should not be processed by BOLT or
62+
/// change relocation \p Type to proper one before continuing if \p Contents
63+
/// and \P Type mismatch occured.
64+
static bool skipRelocationProcess(uint64_t &Type, uint64_t Contents);
6365

6466
// Adjust value depending on relocation type (make it PC relative or not)
6567
static uint64_t adjustValue(uint64_t Type, uint64_t Value,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- bolt/Passes/ADRRelaxationPass.h --------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares the FixRelaxations class, which locates instructions with
10+
// wrong targets and fixes them. Such problems usually occures when linker
11+
// relaxes (changes) instructions, but doesn't fix relocations types properly
12+
// for them.
13+
//
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef BOLT_PASSES_FIXRELAXATIONPASS_H
17+
#define BOLT_PASSES_FIXRELAXATIONPASS_H
18+
19+
#include "bolt/Passes/BinaryPasses.h"
20+
21+
namespace llvm {
22+
namespace bolt {
23+
24+
class FixRelaxations : public BinaryFunctionPass {
25+
void runOnFunction(BinaryFunction &Function);
26+
27+
public:
28+
explicit FixRelaxations(const cl::opt<bool> &PrintPass)
29+
: BinaryFunctionPass(PrintPass) {}
30+
31+
const char *getName() const override { return "fix-relaxations"; }
32+
33+
/// Pass entry point
34+
void runOnFunctions(BinaryContext &BC) override;
35+
};
36+
37+
} // namespace bolt
38+
} // namespace llvm
39+
40+
#endif

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class RewriteInstance {
247247
/// The \p SymbolName, \p SymbolAddress, \p Addend and \p ExtractedValue
248248
/// parameters will be set on success. The \p Skip argument indicates
249249
/// that the relocation was analyzed, but it must not be processed.
250-
bool analyzeRelocation(const object::RelocationRef &Rel, uint64_t RType,
250+
bool analyzeRelocation(const object::RelocationRef &Rel, uint64_t &RType,
251251
std::string &SymbolName, bool &IsSectionRelocation,
252252
uint64_t &SymbolAddress, int64_t &Addend,
253253
uint64_t &ExtractedValue, bool &Skip) const;

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool BinaryFunction::isForwardCall(const MCSymbol *CalleeSymbol) const {
392392
}
393393
}
394394

395-
void BinaryFunction::dump(bool PrintInstructions) const {
395+
void BinaryFunction::dump() const {
396396
// getDynoStats calls FunctionLayout::updateLayoutIndices and
397397
// BasicBlock::analyzeBranch. The former cannot be const, but should be
398398
// removed, the latter should be made const, but seems to require refactoring.
@@ -402,11 +402,10 @@ void BinaryFunction::dump(bool PrintInstructions) const {
402402
// modified. Only BinaryBasicBlocks are actually modified (if it all) and we
403403
// have mutable pointers to those regardless whether this function is
404404
// const-qualified or not.
405-
const_cast<BinaryFunction &>(*this).print(dbgs(), "", PrintInstructions);
405+
const_cast<BinaryFunction &>(*this).print(dbgs(), "");
406406
}
407407

408-
void BinaryFunction::print(raw_ostream &OS, std::string Annotation,
409-
bool PrintInstructions) {
408+
void BinaryFunction::print(raw_ostream &OS, std::string Annotation) {
410409
if (!opts::shouldPrint(*this))
411410
return;
412411

@@ -485,7 +484,7 @@ void BinaryFunction::print(raw_ostream &OS, std::string Annotation,
485484

486485
OS << "\n}\n";
487486

488-
if (opts::PrintDynoStatsOnly || !PrintInstructions || !BC.InstPrinter)
487+
if (opts::PrintDynoStatsOnly || !BC.InstPrinter)
489488
return;
490489

491490
// Offset of the instruction in function.

bolt/lib/Core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
MCDisassembler
66
Object
77
Support
8+
TargetParser
89
)
910

1011
add_llvm_library(LLVMBOLTCore

bolt/lib/Core/Relocation.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ bool skipRelocationTypeAArch64(uint64_t Type) {
171171
return Type == ELF::R_AARCH64_NONE || Type == ELF::R_AARCH64_LD_PREL_LO19;
172172
}
173173

174-
bool skipRelocationProcessX86(uint64_t Type, uint64_t Contents) {
174+
bool skipRelocationProcessX86(uint64_t &Type, uint64_t Contents) {
175175
return false;
176176
}
177177

178-
bool skipRelocationProcessAArch64(uint64_t Type, uint64_t Contents) {
178+
bool skipRelocationProcessAArch64(uint64_t &Type, uint64_t Contents) {
179179
auto IsMov = [](uint64_t Contents) -> bool {
180180
// The bits 28-23 are 0b100101
181181
return (Contents & 0x1f800000) == 0x12800000;
@@ -191,12 +191,25 @@ bool skipRelocationProcessAArch64(uint64_t Type, uint64_t Contents) {
191191
return (Contents & 0x9f000000) == 0x10000000;
192192
};
193193

194+
auto IsAddImm = [](uint64_t Contents) -> bool {
195+
// The bits 30-23 are 0b00100010
196+
return (Contents & 0x7F800000) == 0x11000000;
197+
};
198+
194199
auto IsNop = [](uint64_t Contents) -> bool { return Contents == 0xd503201f; };
195200

196201
// The linker might eliminate the instruction and replace it with NOP, ignore
197202
if (IsNop(Contents))
198203
return true;
199204

205+
// The linker might relax ADRP+LDR instruction sequence for loading symbol
206+
// address from GOT table to ADRP+ADD sequence that would point to the
207+
// binary-local symbol. Change relocation type in order to process it right.
208+
if (Type == ELF::R_AARCH64_LD64_GOT_LO12_NC && IsAddImm(Contents)) {
209+
Type = ELF::R_AARCH64_ADD_ABS_LO12_NC;
210+
return false;
211+
}
212+
200213
// The linker might perform TLS relocations relaxations, such as
201214
// changed TLS access model (e.g. changed global dynamic model
202215
// to initial exec), thus changing the instructions. The static
@@ -548,7 +561,7 @@ bool Relocation::skipRelocationType(uint64_t Type) {
548561
return skipRelocationTypeX86(Type);
549562
}
550563

551-
bool Relocation::skipRelocationProcess(uint64_t Type, uint64_t Contents) {
564+
bool Relocation::skipRelocationProcess(uint64_t &Type, uint64_t Contents) {
552565
if (Arch == Triple::aarch64)
553566
return skipRelocationProcessAArch64(Type, Contents);
554567
return skipRelocationProcessX86(Type, Contents);

bolt/lib/Passes/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_llvm_library(LLVMBOLTPasses
1313
DataflowInfoManager.cpp
1414
FrameAnalysis.cpp
1515
FrameOptimizer.cpp
16+
FixRelaxationPass.cpp
1617
HFSort.cpp
1718
HFSortPlus.cpp
1819
Hugify.cpp
@@ -56,6 +57,7 @@ add_llvm_library(LLVMBOLTPasses
5657
AsmPrinter
5758
MC
5859
Support
60+
TargetParser
5961
TransformUtils
6062
)
6163

bolt/lib/Passes/FixRelaxationPass.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "bolt/Passes/FixRelaxationPass.h"
2+
#include "bolt/Core/ParallelUtilities.h"
3+
4+
using namespace llvm;
5+
6+
namespace llvm {
7+
namespace bolt {
8+
9+
// This function finds ADRP+ADD instruction sequences that originally before
10+
// linker relaxations were ADRP+LDR. We've modified LDR/ADD relocation properly
11+
// during relocation reading, so its targeting right symbol. As for ADRP its
12+
// target is wrong before this pass since we won't be able to recognize and
13+
// properly change R_AARCH64_ADR_GOT_PAGE relocation to
14+
// R_AARCH64_ADR_PREL_PG_HI21 during relocation reading. Now we're searching for
15+
// ADRP+ADD sequences, checking that ADRP points to the GOT-table symbol and the
16+
// target of ADD is another symbol. When found change ADRP symbol reference to
17+
// the ADDs one.
18+
void FixRelaxations::runOnFunction(BinaryFunction &BF) {
19+
BinaryContext &BC = BF.getBinaryContext();
20+
for (BinaryBasicBlock &BB : BF) {
21+
for (auto II = BB.begin(); II != BB.end(); ++II) {
22+
MCInst &Adrp = *II;
23+
if (BC.MIB->isPseudo(Adrp) || !BC.MIB->isADRP(Adrp))
24+
continue;
25+
26+
const MCSymbol *AdrpSymbol = BC.MIB->getTargetSymbol(Adrp);
27+
if (!AdrpSymbol || AdrpSymbol->getName() != "__BOLT_got_zero")
28+
continue;
29+
30+
auto NextII = std::next(II);
31+
if (NextII == BB.end())
32+
continue;
33+
34+
const MCInst &Add = *NextII;
35+
if (!BC.MIB->matchAdrpAddPair(Adrp, Add))
36+
continue;
37+
38+
const MCSymbol *Symbol = BC.MIB->getTargetSymbol(Add);
39+
if (!Symbol || AdrpSymbol == Symbol)
40+
continue;
41+
42+
const int64_t Addend = BC.MIB->getTargetAddend(Add);
43+
BC.MIB->setOperandToSymbolRef(Adrp, /*OpNum*/ 1, Symbol, Addend,
44+
BC.Ctx.get(), ELF::R_AARCH64_NONE);
45+
}
46+
}
47+
}
48+
49+
void FixRelaxations::runOnFunctions(BinaryContext &BC) {
50+
if (!BC.isAArch64() || !BC.HasRelocations)
51+
return;
52+
53+
ParallelUtilities::WorkFuncTy WorkFun = [&](BinaryFunction &BF) {
54+
runOnFunction(BF);
55+
};
56+
57+
ParallelUtilities::runOnEachFunction(
58+
BC, ParallelUtilities::SchedulingPolicy::SP_INST_LINEAR, WorkFun, nullptr,
59+
"FixRelaxations");
60+
}
61+
62+
} // namespace bolt
63+
} // namespace llvm

bolt/lib/Passes/Hugify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ void HugePage::runOnFunctions(BinaryContext &BC) {
4747
BC.MIB->createSymbolTrampoline(StartSym, BC.Ctx.get()));
4848
}
4949
} // namespace bolt
50-
} // namespace llvm
50+
} // namespace llvm

bolt/lib/Passes/MCF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,10 @@ void estimateEdgeCounts(BinaryFunction &BF) {
448448
computeEdgeWeights<BinaryBasicBlock *>(BF, SuccEdgeWeights);
449449
}
450450
if (opts::EqualizeBBCounts) {
451-
LLVM_DEBUG(BF.print(dbgs(), "before equalize BB counts", true));
451+
LLVM_DEBUG(BF.print(dbgs(), "before equalize BB counts"));
452452
auto Info = DataflowInfoManager(BF, nullptr, nullptr);
453453
equalizeBBCounts(Info, BF);
454-
LLVM_DEBUG(BF.print(dbgs(), "after equalize BB counts", true));
454+
LLVM_DEBUG(BF.print(dbgs(), "after equalize BB counts"));
455455
}
456456
if (opts::IterativeGuess)
457457
guessEdgeByIterativeApproach(BF);

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,10 @@ void DataAggregator::abort() {
199199
std::string Error;
200200

201201
// Kill subprocesses in case they are not finished
202-
sys::Wait(TaskEventsPPI.PI, 1, false, &Error);
203-
sys::Wait(MMapEventsPPI.PI, 1, false, &Error);
204-
sys::Wait(MainEventsPPI.PI, 1, false, &Error);
205-
sys::Wait(MemEventsPPI.PI, 1, false, &Error);
202+
sys::Wait(TaskEventsPPI.PI, 1, &Error);
203+
sys::Wait(MMapEventsPPI.PI, 1, &Error);
204+
sys::Wait(MainEventsPPI.PI, 1, &Error);
205+
sys::Wait(MemEventsPPI.PI, 1, &Error);
206206

207207
deleteTempFiles();
208208

@@ -487,7 +487,7 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
487487
std::string Error;
488488
outs() << "PERF2BOLT: waiting for perf " << Name
489489
<< " collection to finish...\n";
490-
sys::ProcessInfo PI = sys::Wait(Process.PI, 0, true, &Error);
490+
sys::ProcessInfo PI = sys::Wait(Process.PI, std::nullopt, &Error);
491491

492492
if (!Error.empty()) {
493493
errs() << "PERF-ERROR: " << PerfPath << ": " << Error << "\n";
@@ -572,7 +572,7 @@ Error DataAggregator::preprocessProfile(BinaryContext &BC) {
572572

573573
// Special handling for memory events
574574
std::string Error;
575-
sys::ProcessInfo PI = sys::Wait(MemEventsPPI.PI, 0, true, &Error);
575+
sys::ProcessInfo PI = sys::Wait(MemEventsPPI.PI, std::nullopt, &Error);
576576
if (PI.ReturnCode != 0) {
577577
ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
578578
MemoryBuffer::getFileOrSTDIN(MemEventsPPI.StderrPath.data());

bolt/lib/Rewrite/BinaryPassManager.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "bolt/Passes/AllocCombiner.h"
1313
#include "bolt/Passes/AsmDump.h"
1414
#include "bolt/Passes/CMOVConversion.h"
15+
#include "bolt/Passes/FixRelaxationPass.h"
1516
#include "bolt/Passes/FrameOptimizer.h"
1617
#include "bolt/Passes/Hugify.h"
1718
#include "bolt/Passes/IdenticalCodeFolding.h"
@@ -179,6 +180,11 @@ static cl::opt<bool>
179180
PrintStoke("print-stoke", cl::desc("print functions after stoke analysis"),
180181
cl::cat(BoltOptCategory));
181182

183+
static cl::opt<bool>
184+
PrintFixRelaxations("print-fix-relaxations",
185+
cl::desc("print functions after fix relaxations pass"),
186+
cl::cat(BoltOptCategory));
187+
182188
static cl::opt<bool> PrintVeneerElimination(
183189
"print-veneer-elimination",
184190
cl::desc("print functions after veneer elimination pass"),
@@ -298,7 +304,7 @@ void BinaryFunctionPassManager::runPasses() {
298304
if (!Pass->shouldPrint(Function))
299305
continue;
300306

301-
Function.print(outs(), Message, true);
307+
Function.print(outs(), Message);
302308

303309
if (opts::DumpDotAll)
304310
Function.dumpGraphForPass(PassIdName);
@@ -315,9 +321,12 @@ void BinaryFunctionPassManager::runAllPasses(BinaryContext &BC) {
315321
Manager.registerPass(std::make_unique<AsmDumpPass>(),
316322
opts::AsmDump.getNumOccurrences());
317323

318-
if (BC.isAArch64())
324+
if (BC.isAArch64()) {
325+
Manager.registerPass(std::make_unique<FixRelaxations>(PrintFixRelaxations));
326+
319327
Manager.registerPass(
320328
std::make_unique<VeneerElimination>(PrintVeneerElimination));
329+
}
321330

322331
// Here we manage dependencies/order manually, since passes are run in the
323332
// order they're registered.

bolt/lib/Rewrite/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
MC
66
Object
77
Support
8+
TargetParser
89
)
910

1011
set(TARGET_LINK_LIBRARIES

0 commit comments

Comments
 (0)