Skip to content

Commit 0f52ee6

Browse files
committed
LLVM and SPIRV-LLVM-Translator pulldown (WW41)
LLVM: llvm/llvm-project@894927b491b7 SPIRV-LLVM-Translator: KhronosGroup/SPIRV-LLVM-Translator@e68ddf7
2 parents 93eb065 + 398908e commit 0f52ee6

File tree

1,752 files changed

+125278
-46110
lines changed

Some content is hidden

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

1,752 files changed

+125278
-46110
lines changed

bolt/include/bolt/Core/MCPlus.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class MCAnnotation {
6666
kTailCall, /// Tail call.
6767
kConditionalTailCall, /// CTC.
6868
kOffset, /// Offset in the function.
69+
kLabel, /// MCSymbol pointing to this instruction.
6970
kGeneric /// First generic annotation.
7071
};
7172

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ class MCPlusBuilder {
160160
const MCInstrAnalysis *Analysis;
161161
const MCInstrInfo *Info;
162162
const MCRegisterInfo *RegInfo;
163+
const MCSubtargetInfo *STI;
163164

164165
/// Map annotation name into an annotation index.
165166
StringMap<uint64_t> AnnotationNameIndexMap;
@@ -331,8 +332,8 @@ class MCPlusBuilder {
331332

332333
public:
333334
MCPlusBuilder(const MCInstrAnalysis *Analysis, const MCInstrInfo *Info,
334-
const MCRegisterInfo *RegInfo)
335-
: Analysis(Analysis), Info(Info), RegInfo(RegInfo) {
335+
const MCRegisterInfo *RegInfo, const MCSubtargetInfo *STI)
336+
: Analysis(Analysis), Info(Info), RegInfo(RegInfo), STI(STI) {
336337
// Initialize the default annotation allocator with id 0
337338
AnnotationAllocators.emplace(0, AnnotationAllocator());
338339
MaxAllocatorId++;
@@ -1179,6 +1180,13 @@ class MCPlusBuilder {
11791180
/// Remove offset annotation.
11801181
bool clearOffset(MCInst &Inst);
11811182

1183+
/// Return the label of \p Inst, if available.
1184+
std::optional<MCSymbol *> getLabel(const MCInst &Inst) const;
1185+
1186+
/// Set the label of \p Inst. This label will be emitted right before \p Inst
1187+
/// is emitted to MCStreamer.
1188+
bool setLabel(MCInst &Inst, MCSymbol *Label, AllocatorIdTy AllocatorId = 0);
1189+
11821190
/// Return MCSymbol that represents a target of this instruction at a given
11831191
/// operand number \p OpNum. If there's no symbol associated with
11841192
/// the operand - return nullptr.
@@ -2079,15 +2087,18 @@ class MCPlusBuilder {
20792087

20802088
MCPlusBuilder *createX86MCPlusBuilder(const MCInstrAnalysis *,
20812089
const MCInstrInfo *,
2082-
const MCRegisterInfo *);
2090+
const MCRegisterInfo *,
2091+
const MCSubtargetInfo *);
20832092

20842093
MCPlusBuilder *createAArch64MCPlusBuilder(const MCInstrAnalysis *,
20852094
const MCInstrInfo *,
2086-
const MCRegisterInfo *);
2095+
const MCRegisterInfo *,
2096+
const MCSubtargetInfo *);
20872097

20882098
MCPlusBuilder *createRISCVMCPlusBuilder(const MCInstrAnalysis *,
20892099
const MCInstrInfo *,
2090-
const MCRegisterInfo *);
2100+
const MCRegisterInfo *,
2101+
const MCSubtargetInfo *);
20912102

20922103
} // namespace bolt
20932104
} // namespace llvm

bolt/include/bolt/Core/Relocation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ struct Relocation {
9898
/// Return true if relocation type is for thread local storage.
9999
static bool isTLS(uint64_t Type);
100100

101+
/// Return true of relocation type is for referencing a specific instruction
102+
/// (as opposed to a function, basic block, etc).
103+
static bool isInstructionReference(uint64_t Type);
104+
101105
/// Return code for a NONE relocation
102106
static uint64_t getNone();
103107

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ class RewriteInstance {
584584
MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
585585
const MCInstrAnalysis *Analysis,
586586
const MCInstrInfo *Info,
587-
const MCRegisterInfo *RegInfo);
587+
const MCRegisterInfo *RegInfo,
588+
const MCSubtargetInfo *STI);
588589

589590
} // namespace bolt
590591
} // namespace llvm

bolt/lib/Core/BinaryContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,8 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
18881888
}
18891889
if (std::optional<uint32_t> Offset = MIB->getOffset(Instruction))
18901890
OS << " # Offset: " << *Offset;
1891+
if (auto Label = MIB->getLabel(Instruction))
1892+
OS << " # Label: " << **Label;
18911893

18921894
MIB->printAnnotations(Instruction, OS);
18931895

bolt/lib/Core/BinaryEmitter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ void BinaryEmitter::emitFunctionBody(BinaryFunction &BF, FunctionFragment &FF,
498498
BB->getLocSyms().emplace_back(Offset, LocSym);
499499
}
500500

501+
if (auto Label = BC.MIB->getLabel(Instr))
502+
Streamer.emitLabel(*Label);
503+
501504
Streamer.emitInstruction(Instr, *BC.STI);
502505
LastIsPrefix = BC.MIB->isPrefix(Instr);
503506
}

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,6 +1173,13 @@ bool BinaryFunction::disassemble() {
11731173
// basic block.
11741174
Labels[0] = Ctx->createNamedTempSymbol("BB0");
11751175

1176+
// Map offsets in the function to a label that should always point to the
1177+
// corresponding instruction. This is used for labels that shouldn't point to
1178+
// the start of a basic block but always to a specific instruction. This is
1179+
// used, for example, on RISC-V where %pcrel_lo relocations point to the
1180+
// corresponding %pcrel_hi.
1181+
LabelsMapType InstructionLabels;
1182+
11761183
uint64_t Size = 0; // instruction size
11771184
for (uint64_t Offset = 0; Offset < getSize(); Offset += Size) {
11781185
MCInst Instruction;
@@ -1329,9 +1336,23 @@ bool BinaryFunction::disassemble() {
13291336
ItrE = Relocations.lower_bound(Offset + Size);
13301337
Itr != ItrE; ++Itr) {
13311338
const Relocation &Relocation = Itr->second;
1339+
MCSymbol *Symbol = Relocation.Symbol;
1340+
1341+
if (Relocation::isInstructionReference(Relocation.Type)) {
1342+
uint64_t RefOffset = Relocation.Value - getAddress();
1343+
LabelsMapType::iterator LI = InstructionLabels.find(RefOffset);
1344+
1345+
if (LI == InstructionLabels.end()) {
1346+
Symbol = BC.Ctx->createNamedTempSymbol();
1347+
InstructionLabels.emplace(RefOffset, Symbol);
1348+
} else {
1349+
Symbol = LI->second;
1350+
}
1351+
}
1352+
13321353
int64_t Value = Relocation.Value;
13331354
const bool Result = BC.MIB->replaceImmWithSymbolRef(
1334-
Instruction, Relocation.Symbol, Relocation.Addend, Ctx.get(), Value,
1355+
Instruction, Symbol, Relocation.Addend, Ctx.get(), Value,
13351356
Relocation.Type);
13361357
(void)Result;
13371358
assert(Result && "cannot replace immediate with relocation");
@@ -1366,6 +1387,13 @@ bool BinaryFunction::disassemble() {
13661387
addInstruction(Offset, std::move(Instruction));
13671388
}
13681389

1390+
for (auto [Offset, Label] : InstructionLabels) {
1391+
InstrMapType::iterator II = Instructions.find(Offset);
1392+
assert(II != Instructions.end() && "reference to non-existing instruction");
1393+
1394+
BC.MIB->setLabel(II->second, Label);
1395+
}
1396+
13691397
// Reset symbolizer for the disassembler.
13701398
BC.SymbolicDisAsm->setSymbolizer(nullptr);
13711399

@@ -4489,7 +4517,7 @@ void BinaryFunction::addRelocation(uint64_t Address, MCSymbol *Symbol,
44894517
uint64_t Offset = Address - getAddress();
44904518
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: addRelocation in "
44914519
<< formatv("{0}@{1:x} against {2}\n", *this, Offset,
4492-
Symbol->getName()));
4520+
(Symbol ? Symbol->getName() : "<undef>")));
44934521
bool IsCI = BC.isAArch64() && isInConstantIsland(Address);
44944522
std::map<uint64_t, Relocation> &Rels =
44954523
IsCI ? Islands->Relocations : Relocations;

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,19 @@ bool MCPlusBuilder::clearOffset(MCInst &Inst) {
268268
return true;
269269
}
270270

271+
std::optional<MCSymbol *> MCPlusBuilder::getLabel(const MCInst &Inst) const {
272+
if (auto Label = tryGetAnnotationAs<MCSymbol *>(Inst, MCAnnotation::kLabel))
273+
return *Label;
274+
return std::nullopt;
275+
}
276+
277+
bool MCPlusBuilder::setLabel(MCInst &Inst, MCSymbol *Label,
278+
AllocatorIdTy AllocatorId) {
279+
getOrCreateAnnotationAs<MCSymbol *>(Inst, MCAnnotation::kLabel, AllocatorId) =
280+
Label;
281+
return true;
282+
}
283+
271284
bool MCPlusBuilder::hasAnnotation(const MCInst &Inst, unsigned Index) const {
272285
const MCInst *AnnotationInst = getAnnotationInst(Inst);
273286
if (!AnnotationInst)

bolt/lib/Core/Relocation.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,19 @@ bool Relocation::isTLS(uint64_t Type) {
858858
return isTLSX86(Type);
859859
}
860860

861+
bool Relocation::isInstructionReference(uint64_t Type) {
862+
if (Arch != Triple::riscv64)
863+
return false;
864+
865+
switch (Type) {
866+
default:
867+
return false;
868+
case ELF::R_RISCV_PCREL_LO12_I:
869+
case ELF::R_RISCV_PCREL_LO12_S:
870+
return true;
871+
}
872+
}
873+
861874
uint64_t Relocation::getNone() {
862875
if (Arch == Triple::aarch64)
863876
return ELF::R_AARCH64_NONE;

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ bool CheckLargeFunctions::shouldOptimize(const BinaryFunction &BF) const {
575575

576576
void LowerAnnotations::runOnFunctions(BinaryContext &BC) {
577577
std::vector<std::pair<MCInst *, uint32_t>> PreservedOffsetAnnotations;
578+
std::vector<std::pair<MCInst *, MCSymbol *>> PreservedLabelAnnotations;
578579

579580
for (auto &It : BC.getBinaryFunctions()) {
580581
BinaryFunction &BF = It.second;
@@ -609,22 +610,29 @@ void LowerAnnotations::runOnFunctions(BinaryContext &BC) {
609610
if (BF.requiresAddressTranslation() && BC.MIB->getOffset(*II))
610611
PreservedOffsetAnnotations.emplace_back(&(*II),
611612
*BC.MIB->getOffset(*II));
613+
if (auto Label = BC.MIB->getLabel(*II))
614+
PreservedLabelAnnotations.emplace_back(&*II, *Label);
612615
BC.MIB->stripAnnotations(*II);
613616
}
614617
}
615618
}
616619
}
617620
for (BinaryFunction *BF : BC.getInjectedBinaryFunctions())
618621
for (BinaryBasicBlock &BB : *BF)
619-
for (MCInst &Instruction : BB)
622+
for (MCInst &Instruction : BB) {
623+
if (auto Label = BC.MIB->getLabel(Instruction))
624+
PreservedLabelAnnotations.emplace_back(&Instruction, *Label);
620625
BC.MIB->stripAnnotations(Instruction);
626+
}
621627

622628
// Release all memory taken by annotations
623629
BC.MIB->freeAnnotations();
624630

625631
// Reinsert preserved annotations we need during code emission.
626632
for (const std::pair<MCInst *, uint32_t> &Item : PreservedOffsetAnnotations)
627633
BC.MIB->setOffset(*Item.first, Item.second);
634+
for (auto [Instr, Label] : PreservedLabelAnnotations)
635+
BC.MIB->setLabel(*Instr, Label);
628636
}
629637

630638
// Check for dirty state in MCSymbol objects that might be a consequence

bolt/lib/Passes/LongJmp.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,18 +138,21 @@ BinaryBasicBlock *LongJmpPass::lookupStubFromGroup(
138138
Cand = LeftCand;
139139
}
140140
int BitsAvail = BC.MIB->getPCRelEncodingSize(Inst) - 1;
141-
uint64_t Mask = ~((1ULL << BitsAvail) - 1);
141+
assert(BitsAvail < 63 && "PCRelEncodingSize is too large to use int64_t to"
142+
"check for out-of-bounds.");
143+
int64_t MaxVal = (1ULL << BitsAvail) - 1;
144+
int64_t MinVal = -(1ULL << BitsAvail);
142145
uint64_t PCRelTgtAddress = Cand->first;
143-
PCRelTgtAddress = DotAddress > PCRelTgtAddress ? DotAddress - PCRelTgtAddress
144-
: PCRelTgtAddress - DotAddress;
146+
int64_t PCOffset = (int64_t)(PCRelTgtAddress - DotAddress);
147+
145148
LLVM_DEBUG({
146149
if (Candidates.size() > 1)
147150
dbgs() << "Considering stub group with " << Candidates.size()
148151
<< " candidates. DotAddress is " << Twine::utohexstr(DotAddress)
149152
<< ", chosen candidate address is "
150153
<< Twine::utohexstr(Cand->first) << "\n";
151154
});
152-
return PCRelTgtAddress & Mask ? nullptr : Cand->second;
155+
return (PCOffset < MinVal || PCOffset > MaxVal) ? nullptr : Cand->second;
153156
}
154157

155158
BinaryBasicBlock *
@@ -512,13 +515,15 @@ bool LongJmpPass::needsStub(const BinaryBasicBlock &BB, const MCInst &Inst,
512515
}
513516

514517
int BitsAvail = BC.MIB->getPCRelEncodingSize(Inst) - 1;
515-
uint64_t Mask = ~((1ULL << BitsAvail) - 1);
518+
assert(BitsAvail < 63 && "PCRelEncodingSize is too large to use int64_t to"
519+
"check for out-of-bounds.");
520+
int64_t MaxVal = (1ULL << BitsAvail) - 1;
521+
int64_t MinVal = -(1ULL << BitsAvail);
516522

517523
uint64_t PCRelTgtAddress = getSymbolAddress(BC, TgtSym, TgtBB);
518-
PCRelTgtAddress = DotAddress > PCRelTgtAddress ? DotAddress - PCRelTgtAddress
519-
: PCRelTgtAddress - DotAddress;
524+
int64_t PCOffset = (int64_t)(PCRelTgtAddress - DotAddress);
520525

521-
return PCRelTgtAddress & Mask;
526+
return PCOffset < MinVal || PCOffset > MaxVal;
522527
}
523528

524529
bool LongJmpPass::relax(BinaryFunction &Func) {

bolt/lib/Rewrite/JITLinkLinker.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ void JITLinkLinker::loadObject(MemoryBufferRef Obj,
192192
exit(1);
193193
}
194194

195+
if ((*LG)->getTargetTriple().getArch() != BC.TheTriple->getArch()) {
196+
errs() << "BOLT-ERROR: linking object with arch "
197+
<< (*LG)->getTargetTriple().getArchName()
198+
<< " into context with arch " << BC.TheTriple->getArchName() << "\n";
199+
exit(1);
200+
}
201+
195202
auto Ctx = std::make_unique<Context>(*this, MapSections);
196203
jitlink::link(std::move(*LG), std::move(Ctx));
197204
}

bolt/lib/Rewrite/MachORewriteInstance.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,28 @@ namespace bolt {
5656

5757
extern MCPlusBuilder *createX86MCPlusBuilder(const MCInstrAnalysis *,
5858
const MCInstrInfo *,
59-
const MCRegisterInfo *);
59+
const MCRegisterInfo *,
60+
const MCSubtargetInfo *);
6061
extern MCPlusBuilder *createAArch64MCPlusBuilder(const MCInstrAnalysis *,
6162
const MCInstrInfo *,
62-
const MCRegisterInfo *);
63+
const MCRegisterInfo *,
64+
const MCSubtargetInfo *);
6365

6466
namespace {
6567

6668
MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
6769
const MCInstrAnalysis *Analysis,
6870
const MCInstrInfo *Info,
69-
const MCRegisterInfo *RegInfo) {
71+
const MCRegisterInfo *RegInfo,
72+
const MCSubtargetInfo *STI) {
7073
#ifdef X86_AVAILABLE
7174
if (Arch == Triple::x86_64)
72-
return createX86MCPlusBuilder(Analysis, Info, RegInfo);
75+
return createX86MCPlusBuilder(Analysis, Info, RegInfo, STI);
7376
#endif
7477

7578
#ifdef AARCH64_AVAILABLE
7679
if (Arch == Triple::aarch64)
77-
return createAArch64MCPlusBuilder(Analysis, Info, RegInfo);
80+
return createAArch64MCPlusBuilder(Analysis, Info, RegInfo, STI);
7881
#endif
7982

8083
llvm_unreachable("architecture unsupported by MCPlusBuilder");
@@ -106,8 +109,9 @@ MachORewriteInstance::MachORewriteInstance(object::MachOObjectFile *InputFile,
106109
return;
107110
}
108111
BC = std::move(BCOrErr.get());
109-
BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(createMCPlusBuilder(
110-
BC->TheTriple->getArch(), BC->MIA.get(), BC->MII.get(), BC->MRI.get())));
112+
BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(
113+
createMCPlusBuilder(BC->TheTriple->getArch(), BC->MIA.get(),
114+
BC->MII.get(), BC->MRI.get(), BC->STI.get())));
111115
if (opts::Instrument)
112116
BC->setRuntimeLibrary(std::make_unique<InstrumentationRuntimeLibrary>());
113117
}

0 commit comments

Comments
 (0)