Skip to content

Commit 8bf801a

Browse files
SC llvm teamSC llvm team
SC llvm team
authored and
SC llvm team
committed
Merged main:8b356f496b18 into amd-gfx:d7a064a92fb5
Local branch amd-gfx d7a064a Merged main:a06e94cf3b37 into amd-gfx:2cbc551d7f73 Remote branch main 8b356f4 [llvm-rc] add support for MENUEX (llvm#67464)
2 parents d7a064a + 8b356f4 commit 8bf801a

Some content is hidden

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

58 files changed

+1142
-477
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);
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ 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+
getOrCreateAnnotationAs<MCSymbol *>(Inst, MCAnnotation::kLabel) = Label;
279+
return true;
280+
}
281+
271282
bool MCPlusBuilder::hasAnnotation(const MCInst &Inst, unsigned Index) const {
272283
const MCInst *AnnotationInst = getAnnotationInst(Inst);
273284
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: 5 additions & 0 deletions
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,6 +610,8 @@ 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
}
@@ -625,6 +628,8 @@ void LowerAnnotations::runOnFunctions(BinaryContext &BC) {
625628
// Reinsert preserved annotations we need during code emission.
626629
for (const std::pair<MCInst *, uint32_t> &Item : PreservedOffsetAnnotations)
627630
BC.MIB->setOffset(*Item.first, Item.second);
631+
for (auto [Instr, Label] : PreservedLabelAnnotations)
632+
BC.MIB->setLabel(*Instr, Label);
628633
}
629634

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

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
}

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,20 +272,21 @@ extern const char *BoltRevision;
272272
MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
273273
const MCInstrAnalysis *Analysis,
274274
const MCInstrInfo *Info,
275-
const MCRegisterInfo *RegInfo) {
275+
const MCRegisterInfo *RegInfo,
276+
const MCSubtargetInfo *STI) {
276277
#ifdef X86_AVAILABLE
277278
if (Arch == Triple::x86_64)
278-
return createX86MCPlusBuilder(Analysis, Info, RegInfo);
279+
return createX86MCPlusBuilder(Analysis, Info, RegInfo, STI);
279280
#endif
280281

281282
#ifdef AARCH64_AVAILABLE
282283
if (Arch == Triple::aarch64)
283-
return createAArch64MCPlusBuilder(Analysis, Info, RegInfo);
284+
return createAArch64MCPlusBuilder(Analysis, Info, RegInfo, STI);
284285
#endif
285286

286287
#ifdef RISCV_AVAILABLE
287288
if (Arch == Triple::riscv64)
288-
return createRISCVMCPlusBuilder(Analysis, Info, RegInfo);
289+
return createRISCVMCPlusBuilder(Analysis, Info, RegInfo, STI);
289290
#endif
290291

291292
llvm_unreachable("architecture unsupported by MCPlusBuilder");
@@ -348,8 +349,9 @@ RewriteInstance::RewriteInstance(ELFObjectFileBase *File, const int Argc,
348349
return;
349350
}
350351
BC = std::move(BCOrErr.get());
351-
BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(createMCPlusBuilder(
352-
BC->TheTriple->getArch(), BC->MIA.get(), BC->MII.get(), BC->MRI.get())));
352+
BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(
353+
createMCPlusBuilder(BC->TheTriple->getArch(), BC->MIA.get(),
354+
BC->MII.get(), BC->MRI.get(), BC->STI.get())));
353355

354356
BAT = std::make_unique<BoltAddressTranslation>();
355357

@@ -2544,7 +2546,17 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
25442546
// Adjust the point of reference to a code location inside a function.
25452547
if (ReferencedBF->containsAddress(Address, /*UseMaxSize = */ true)) {
25462548
RefFunctionOffset = Address - ReferencedBF->getAddress();
2547-
if (RefFunctionOffset) {
2549+
if (Relocation::isInstructionReference(RType)) {
2550+
// Instruction labels are created while disassembling so we just leave
2551+
// the symbol empty for now. Since the extracted value is typically
2552+
// unrelated to the referenced symbol (e.g., %pcrel_lo in RISC-V
2553+
// references an instruction but the patched value references the low
2554+
// bits of a data address), we set the extracted value to the symbol
2555+
// address in order to be able to correctly reconstruct the reference
2556+
// later.
2557+
ReferencedSymbol = nullptr;
2558+
ExtractedValue = Address;
2559+
} else if (RefFunctionOffset) {
25482560
if (ContainingBF && ContainingBF != ReferencedBF) {
25492561
ReferencedSymbol =
25502562
ReferencedBF->addEntryPointAtOffset(RefFunctionOffset);

bolt/lib/Target/AArch64/AArch64MCPlusBuilder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ static InstructionListType createIncMemory(MCPhysReg RegTo, MCPhysReg RegTmp) {
128128
}
129129
class AArch64MCPlusBuilder : public MCPlusBuilder {
130130
public:
131-
AArch64MCPlusBuilder(const MCInstrAnalysis *Analysis, const MCInstrInfo *Info,
132-
const MCRegisterInfo *RegInfo)
133-
: MCPlusBuilder(Analysis, Info, RegInfo) {}
131+
using MCPlusBuilder::MCPlusBuilder;
134132

135133
bool equals(const MCTargetExpr &A, const MCTargetExpr &B,
136134
CompFuncTy Comp) const override {
@@ -1654,8 +1652,9 @@ namespace bolt {
16541652

16551653
MCPlusBuilder *createAArch64MCPlusBuilder(const MCInstrAnalysis *Analysis,
16561654
const MCInstrInfo *Info,
1657-
const MCRegisterInfo *RegInfo) {
1658-
return new AArch64MCPlusBuilder(Analysis, Info, RegInfo);
1655+
const MCRegisterInfo *RegInfo,
1656+
const MCSubtargetInfo *STI) {
1657+
return new AArch64MCPlusBuilder(Analysis, Info, RegInfo, STI);
16591658
}
16601659

16611660
} // namespace bolt

bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,9 @@ namespace bolt {
471471

472472
MCPlusBuilder *createRISCVMCPlusBuilder(const MCInstrAnalysis *Analysis,
473473
const MCInstrInfo *Info,
474-
const MCRegisterInfo *RegInfo) {
475-
return new RISCVMCPlusBuilder(Analysis, Info, RegInfo);
474+
const MCRegisterInfo *RegInfo,
475+
const MCSubtargetInfo *STI) {
476+
return new RISCVMCPlusBuilder(Analysis, Info, RegInfo, STI);
476477
}
477478

478479
} // namespace bolt

bolt/lib/Target/X86/X86MCPlusBuilder.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ static InstructionListType createIncMemory(const MCSymbol *Target,
8787

8888
class X86MCPlusBuilder : public MCPlusBuilder {
8989
public:
90-
X86MCPlusBuilder(const MCInstrAnalysis *Analysis, const MCInstrInfo *Info,
91-
const MCRegisterInfo *RegInfo)
92-
: MCPlusBuilder(Analysis, Info, RegInfo) {}
90+
using MCPlusBuilder::MCPlusBuilder;
9391

9492
std::unique_ptr<MCSymbolizer>
9593
createTargetSymbolizer(BinaryFunction &Function,
@@ -3579,8 +3577,9 @@ namespace bolt {
35793577

35803578
MCPlusBuilder *createX86MCPlusBuilder(const MCInstrAnalysis *Analysis,
35813579
const MCInstrInfo *Info,
3582-
const MCRegisterInfo *RegInfo) {
3583-
return new X86MCPlusBuilder(Analysis, Info, RegInfo);
3580+
const MCRegisterInfo *RegInfo,
3581+
const MCSubtargetInfo *STI) {
3582+
return new X86MCPlusBuilder(Analysis, Info, RegInfo, STI);
35843583
}
35853584

35863585
} // namespace bolt

bolt/test/RISCV/reloc-abs.s

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ _start:
1717
.option push
1818
.option norelax
1919
1:
20-
// CHECK: .Ltmp0
21-
// CHECK: auipc gp, %pcrel_hi(__global_pointer$)
20+
// CHECK: auipc gp, %pcrel_hi(__global_pointer$) # Label: .Ltmp0
2221
// CHECK-NEXT: addi gp, gp, %pcrel_lo(.Ltmp0)
2322
auipc gp, %pcrel_hi(__global_pointer$)
2423
addi gp, gp, %pcrel_lo(1b)

0 commit comments

Comments
 (0)