Skip to content

Commit b390cdb

Browse files
committed
LLVM and SPIRV-LLVM-Translator pulldown (WW09)
LLVM: llvm/llvm-project@55639c2 SPIRV-LLVM-Translator: KhronosGroup/SPIRV-LLVM-Translator@f797de2
2 parents 551c07b + d66037f commit b390cdb

File tree

3,573 files changed

+103464
-53631
lines changed

Some content is hidden

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

3,573 files changed

+103464
-53631
lines changed

bolt/include/bolt/Core/BinaryBasicBlock.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class MCCodeEmitter;
3131
namespace bolt {
3232

3333
class BinaryFunction;
34+
class JumpTable;
3435

3536
class BinaryBasicBlock {
3637
public:
@@ -623,6 +624,10 @@ class BinaryBasicBlock {
623624
/// remove the conditional successor and branch instruction.
624625
void removeDuplicateConditionalSuccessor(MCInst *CondBranch);
625626

627+
/// Update successors of the basic block based on the jump table instruction.
628+
/// The block must end with a jump table instruction.
629+
void updateJumpTableSuccessors();
630+
626631
/// Test if BB is a predecessor of this block.
627632
bool isPredecessor(const BinaryBasicBlock *BB) const {
628633
auto Itr = std::find(Predecessors.begin(), Predecessors.end(), BB);
@@ -909,7 +914,12 @@ class BinaryBasicBlock {
909914
return Index;
910915
}
911916

912-
bool hasJumpTable() const;
917+
/// Return jump table if the block contains a jump table instruction or
918+
/// nullptr otherwise.
919+
const JumpTable *getJumpTable() const;
920+
921+
/// Check if the block has a jump table instruction.
922+
bool hasJumpTable() const { return getJumpTable() != nullptr; }
913923

914924
private:
915925
void adjustNumPseudos(const MCInst &Inst, int Sign);

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,14 +1192,14 @@ class BinaryContext {
11921192
/*PIC=*/!HasFixedLoadAddress));
11931193
MCEInstance.LocalCtx->setObjectFileInfo(MCEInstance.LocalMOFI.get());
11941194
MCEInstance.MCE.reset(
1195-
TheTarget->createMCCodeEmitter(*MII, *MRI, *MCEInstance.LocalCtx));
1195+
TheTarget->createMCCodeEmitter(*MII, *MCEInstance.LocalCtx));
11961196
return MCEInstance;
11971197
}
11981198

11991199
/// Creating MCStreamer instance.
12001200
std::unique_ptr<MCStreamer>
12011201
createStreamer(llvm::raw_pwrite_stream &OS) const {
1202-
MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *Ctx);
1202+
MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *Ctx);
12031203
MCAsmBackend *MAB =
12041204
TheTarget->createMCAsmBackend(*STI, *MRI, MCTargetOptions());
12051205
std::unique_ptr<MCObjectWriter> OW = MAB->createObjectWriter(OS);

bolt/include/bolt/Core/BinarySection.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,16 @@ class BinarySection {
296296
return make_range(Relocations.begin(), Relocations.end());
297297
}
298298

299+
/// Iterate over all dynamic relocations for this section.
300+
iterator_range<RelocationSetType::iterator> dynamicRelocations() {
301+
return make_range(DynamicRelocations.begin(), DynamicRelocations.end());
302+
}
303+
304+
/// Iterate over all dynamic relocations for this section.
305+
iterator_range<RelocationSetType::const_iterator> dynamicRelocations() const {
306+
return make_range(DynamicRelocations.begin(), DynamicRelocations.end());
307+
}
308+
299309
/// Does this section have any non-pending relocations?
300310
bool hasRelocations() const { return !Relocations.empty(); }
301311

bolt/include/bolt/Core/DebugData.h

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
494494
PatchValueVariable,
495495
ReferencePatchValue,
496496
DWARFUnitOffsetBaseLabel,
497-
DestinationReferenceLabel
497+
DestinationReferenceLabel,
498+
NewDebugEntry
498499
};
499500

500501
struct Patch {
@@ -605,6 +606,22 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
605606
}
606607
};
607608

609+
struct NewDebugEntry : public Patch {
610+
NewDebugEntry() = delete;
611+
NewDebugEntry(uint32_t O, std::string &&V)
612+
: Patch(O, DebugPatchKind::NewDebugEntry) {
613+
CurrentOrder = NewDebugEntry::OrderCounter++;
614+
Value = std::move(V);
615+
}
616+
617+
static bool classof(const Patch *Writer) {
618+
return Writer->getKind() == DebugPatchKind::NewDebugEntry;
619+
}
620+
static uint32_t OrderCounter;
621+
uint32_t CurrentOrder;
622+
std::string Value;
623+
};
624+
608625
virtual PatcherKind getKind() const override {
609626
return PatcherKind::DebugInfoBinaryPatcher;
610627
}
@@ -646,6 +663,12 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
646663
void addReferenceToPatch(uint64_t Offset, uint32_t DestinationOffset,
647664
uint32_t OldValueSize, dwarf::Form Form);
648665

666+
/// Inserts a new uint32_t \p Value at the end of \p DIE .
667+
void insertNewEntry(const DWARFDie &DIE, uint32_t);
668+
669+
/// Inserts a new encoded \p Value at the end of \p DIE .
670+
void insertNewEntry(const DWARFDie &DIE, std::string &&Value);
671+
649672
/// Clears unordered set for DestinationLabels.
650673
void clearDestinationLabels() { DestinationLabels.clear(); }
651674

@@ -685,6 +708,9 @@ class DebugInfoBinaryPatcher : public SimpleBinaryPatcher {
685708
case DebugPatchKind::DestinationReferenceLabel:
686709
delete reinterpret_cast<DestinationReferenceLabel *>(P);
687710
break;
711+
case DebugPatchKind::NewDebugEntry:
712+
delete reinterpret_cast<NewDebugEntry *>(P);
713+
break;
688714
}
689715
}
690716
};
@@ -728,10 +754,19 @@ class DebugAbbrevWriter {
728754
uint8_t NewAttrForm;
729755
};
730756

757+
struct AbbrevEntry {
758+
dwarf::Attribute Attr;
759+
dwarf::Form Form;
760+
};
761+
731762
using PatchesTy = std::unordered_map<const DWARFAbbreviationDeclaration *,
732763
SmallVector<PatchInfo, 2>>;
733764
std::unordered_map<const DWARFUnit *, PatchesTy> Patches;
734765

766+
using AbbrevEntryTy = std::unordered_map<const DWARFAbbreviationDeclaration *,
767+
SmallVector<AbbrevEntry, 2>>;
768+
std::unordered_map<const DWARFUnit *, AbbrevEntryTy> NewAbbrevEntries;
769+
735770
/// DWARF context containing abbreviations.
736771
DWARFContext &Context;
737772

@@ -777,6 +812,27 @@ class DebugAbbrevWriter {
777812
PatchInfo{AttrTag, NewAttrTag, NewAttrForm});
778813
}
779814

815+
/// Adds attribute \p AttrTag and \p NewAttrForm in abbreviation declaration
816+
/// \p Abbrev belonging to CU \p Unit .
817+
void addAttribute(const DWARFUnit &Unit,
818+
const DWARFAbbreviationDeclaration *Abbrev,
819+
dwarf::Attribute AttrTag, dwarf::Form AttrForm) {
820+
assert(&Unit.getContext() == &Context &&
821+
"cannot update attribute from a different DWARF context");
822+
std::lock_guard<std::mutex> Lock(WriterMutex);
823+
bool AlreadyAdded = false;
824+
for (AbbrevEntry &E : NewAbbrevEntries[&Unit][Abbrev])
825+
if (E.Attr == AttrTag) {
826+
AlreadyAdded = true;
827+
break;
828+
}
829+
830+
if (AlreadyAdded)
831+
return;
832+
NewAbbrevEntries[&Unit][Abbrev].emplace_back(
833+
AbbrevEntry{AttrTag, AttrForm});
834+
}
835+
780836
/// Return a buffer with concatenated abbrev sections for all CUs and TUs
781837
/// in the associated DWARF context. Section offsets could be queried using
782838
/// getAbbreviationsOffsetForUnit() interface. For DWP, we are using DWOId
@@ -882,6 +938,17 @@ class DwarfLineTable {
882938
}
883939
};
884940

941+
struct AttrInfo {
942+
DWARFFormValue V;
943+
uint64_t Offset;
944+
uint32_t Size; // Size of the attribute.
945+
};
946+
947+
Optional<AttrInfo>
948+
findAttributeInfo(const DWARFDie DIE,
949+
const DWARFAbbreviationDeclaration *AbbrevDecl,
950+
uint32_t Index);
951+
885952
} // namespace bolt
886953
} // namespace llvm
887954

bolt/include/bolt/Core/Relocation.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ struct Relocation {
8989
/// Return true if relocation type is for thread local storage.
9090
static bool isTLS(uint64_t Type);
9191

92+
/// Return code for a NONE relocation
93+
static uint64_t getNone();
94+
9295
/// Return code for a PC-relative 4-byte relocation
9396
static uint64_t getPC32();
9497

@@ -98,6 +101,10 @@ struct Relocation {
98101
/// Return true if this relocation is PC-relative. Return false otherwise.
99102
bool isPCRelative() const { return isPCRelative(Type); }
100103

104+
/// Return true if this relocation is R_*_RELATIVE type. Return false
105+
/// otherwise.
106+
bool isRelative() const { return isRelative(Type); }
107+
101108
/// Emit relocation at a current \p Streamer' position. The caller is
102109
/// responsible for setting the position correctly.
103110
size_t emit(MCStreamer *Streamer) const;

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Support/Error.h"
2323
#include <map>
2424
#include <set>
25+
#include <unordered_map>
2526

2627
namespace llvm {
2728

@@ -124,7 +125,7 @@ class RewriteInstance {
124125
void processLKSMPLocks();
125126

126127
/// Read relocations from a given section.
127-
void readDynamicRelocations(const object::SectionRef &Section);
128+
void readDynamicRelocations(const object::SectionRef &Section, bool IsJmpRel);
128129

129130
/// Read relocations from a given section.
130131
void readRelocations(const object::SectionRef &Section);
@@ -201,6 +202,10 @@ class RewriteInstance {
201202
/// \p OldAddress address in the original binary.
202203
uint64_t getNewFunctionAddress(uint64_t OldAddress);
203204

205+
/// Return address of a function or moved data in the new binary
206+
/// corresponding to \p OldAddress address in the original binary.
207+
uint64_t getNewFunctionOrDataAddress(uint64_t OldAddress);
208+
204209
/// Return value for the symbol \p Name in the output.
205210
uint64_t getNewValueForSymbol(const StringRef Name);
206211

@@ -299,6 +304,14 @@ class RewriteInstance {
299304
const std::vector<uint32_t> &NewSectionIndex, WriteFuncTy Write,
300305
StrTabFuncTy AddToStrTab);
301306

307+
/// Get output index in dynamic symbol table.
308+
uint32_t getOutputDynamicSymbolIndex(const MCSymbol *Symbol) {
309+
auto It = SymbolIndex.find(Symbol);
310+
if (It != SymbolIndex.end())
311+
return It->second;
312+
return 0;
313+
}
314+
302315
/// Add a notes section containing the BOLT revision and command line options.
303316
void addBoltInfoSection();
304317

@@ -426,11 +439,19 @@ class RewriteInstance {
426439
/// Location and size of dynamic relocations.
427440
Optional<uint64_t> DynamicRelocationsAddress;
428441
uint64_t DynamicRelocationsSize{0};
442+
uint64_t DynamicRelativeRelocationsCount{0};
429443

430444
/// PLT relocations are special kind of dynamic relocations stored separately.
431445
Optional<uint64_t> PLTRelocationsAddress;
432446
uint64_t PLTRelocationsSize{0};
433447

448+
/// True if relocation of specified type came from .rela.plt
449+
DenseMap<uint64_t, bool> IsJmpRelocation;
450+
451+
/// Index of specified symbol in the dynamic symbol table. NOTE Currently it
452+
/// is filled and used only with the relocations-related symbols.
453+
std::unordered_map<const MCSymbol *, uint32_t> SymbolIndex;
454+
434455
/// Store all non-zero symbols in this map for a quick address lookup.
435456
std::map<uint64_t, llvm::object::SymbolRef> FileSymRefs;
436457

bolt/lib/Core/BinaryBasicBlock.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ bool BinaryBasicBlock::hasInstructions() const {
3939
return getParent()->hasInstructions();
4040
}
4141

42-
bool BinaryBasicBlock::hasJumpTable() const {
42+
const JumpTable *BinaryBasicBlock::getJumpTable() const {
4343
const MCInst *Inst = getLastNonPseudoInstr();
4444
const JumpTable *JT = Inst ? Function->getJumpTable(*Inst) : nullptr;
45-
return (JT != nullptr);
45+
return JT;
4646
}
4747

4848
void BinaryBasicBlock::adjustNumPseudos(const MCInst &Inst, int Sign) {
@@ -351,6 +351,36 @@ void BinaryBasicBlock::removeDuplicateConditionalSuccessor(MCInst *CondBranch) {
351351
BranchInfo.push_back({Count, 0});
352352
}
353353

354+
void BinaryBasicBlock::updateJumpTableSuccessors() {
355+
const JumpTable *JT = getJumpTable();
356+
assert(JT && "Expected jump table instruction.");
357+
358+
// Clear existing successors.
359+
removeAllSuccessors();
360+
361+
// Generate the list of successors in deterministic order without duplicates.
362+
SmallVector<BinaryBasicBlock *, 16> SuccessorBBs;
363+
for (const MCSymbol *Label : JT->Entries) {
364+
BinaryBasicBlock *BB = getFunction()->getBasicBlockForLabel(Label);
365+
// Ignore __builtin_unreachable()
366+
if (!BB) {
367+
assert(Label == getFunction()->getFunctionEndLabel() &&
368+
"JT label should match a block or end of function.");
369+
continue;
370+
}
371+
SuccessorBBs.emplace_back(BB);
372+
}
373+
llvm::sort(SuccessorBBs,
374+
[](const BinaryBasicBlock *BB1, const BinaryBasicBlock *BB2) {
375+
return BB1->getInputOffset() < BB2->getInputOffset();
376+
});
377+
SuccessorBBs.erase(std::unique(SuccessorBBs.begin(), SuccessorBBs.end()),
378+
SuccessorBBs.end());
379+
380+
for (BinaryBasicBlock *BB : SuccessorBBs)
381+
addSuccessor(BB);
382+
}
383+
354384
void BinaryBasicBlock::adjustExecutionCount(double Ratio) {
355385
auto adjustedCount = [&](uint64_t Count) -> uint64_t {
356386
double NewCount = Count * Ratio;

bolt/lib/Core/BinaryContext.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "bolt/Utils/NameResolver.h"
1818
#include "bolt/Utils/Utils.h"
1919
#include "llvm/ADT/Twine.h"
20+
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
2021
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
2122
#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
2223
#include "llvm/MC/MCAsmLayout.h"
@@ -222,7 +223,7 @@ BinaryContext::createBinaryContext(const ObjectFile *File, bool IsPIC,
222223
InstructionPrinter->setPrintImmHex(true);
223224

224225
std::unique_ptr<MCCodeEmitter> MCE(
225-
TheTarget->createMCCodeEmitter(*MII, *MRI, *Ctx));
226+
TheTarget->createMCCodeEmitter(*MII, *Ctx));
226227

227228
// Make sure we don't miss any output on core dumps.
228229
outs().SetUnbuffered();
@@ -712,8 +713,10 @@ void BinaryContext::skipMarkedFragments() {
712713
std::for_each(BF->ParentFragments.begin(), BF->ParentFragments.end(),
713714
addToWorklist);
714715
}
715-
errs() << "BOLT-WARNING: Ignored " << FragmentsToSkip.size() << " functions "
716-
<< "due to cold fragments.\n";
716+
if (!FragmentsToSkip.empty())
717+
errs() << "BOLT-WARNING: ignored " << FragmentsToSkip.size() << " function"
718+
<< (FragmentsToSkip.size() == 1 ? "" : "s")
719+
<< " due to cold fragments\n";
717720
FragmentsToSkip.clear();
718721
}
719722

bolt/lib/Core/BinaryEmitter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "bolt/Core/DebugData.h"
1818
#include "bolt/Utils/CommandLineOpts.h"
1919
#include "bolt/Utils/Utils.h"
20+
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
2021
#include "llvm/MC/MCSection.h"
2122
#include "llvm/MC/MCStreamer.h"
2223
#include "llvm/Support/CommandLine.h"

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,21 +1875,7 @@ bool BinaryFunction::postProcessIndirectBranches(
18751875
BC.MIB->setJumpTable(*LastIndirectJump, LastJT, LastJTIndexReg, AllocId);
18761876
HasUnknownControlFlow = false;
18771877

1878-
// re-populate successors based on the jump table.
1879-
std::set<const MCSymbol *> JTLabels;
1880-
LastIndirectJumpBB->removeAllSuccessors();
1881-
const JumpTable *JT = getJumpTableContainingAddress(LastJT);
1882-
for (const MCSymbol *Label : JT->Entries)
1883-
JTLabels.emplace(Label);
1884-
for (const MCSymbol *Label : JTLabels) {
1885-
BinaryBasicBlock *BB = getBasicBlockForLabel(Label);
1886-
// Ignore __builtin_unreachable()
1887-
if (!BB) {
1888-
assert(Label == getFunctionEndLabel() && "if no BB found, must be end");
1889-
continue;
1890-
}
1891-
LastIndirectJumpBB->addSuccessor(BB);
1892-
}
1878+
LastIndirectJumpBB->updateJumpTableSuccessors();
18931879
}
18941880

18951881
if (HasFixedIndirectBranch)

0 commit comments

Comments
 (0)