Skip to content

Commit

Permalink
[BOLT] Refactor code and data emission code
Browse files Browse the repository at this point in the history
Summary:
Consolidate code and data emission code in ELF-independent
BinaryEmitter. The high-level interface includes only two
functions emitBinaryContext() and emitFunctionBody() used
by RewriteInstance and BinaryContext respectively.

(cherry picked from FBD20332901)
  • Loading branch information
maksfb committed Mar 6, 2020
1 parent 74a2777 commit 1f3e351
Show file tree
Hide file tree
Showing 14 changed files with 1,251 additions and 1,077 deletions.
17 changes: 10 additions & 7 deletions bolt/src/BinaryContext.cpp
Expand Up @@ -10,6 +10,7 @@
//===----------------------------------------------------------------------===//

#include "BinaryContext.h"
#include "BinaryEmitter.h"
#include "BinaryFunction.h"
#include "DataReader.h"
#include "ParallelUtilities.h"
Expand Down Expand Up @@ -402,8 +403,8 @@ BinaryContext::handleAddressRef(uint64_t Address, BinaryFunction &BF,
/// a reference to its constant island. When emitting this function,
/// we will also emit IslandIter->second's constants. This only
/// happens in custom AArch64 assembly code.
BF.IslandDependency.insert(IslandIter->second);
BF.ProxyIslandSymbols[IslandSym] = IslandIter->second;
BF.Islands.Dependency.insert(IslandIter->second);
BF.Islands.ProxySymbols[IslandSym] = IslandIter->second;
return std::make_pair(IslandSym, Addend);
}
}
Expand Down Expand Up @@ -1899,9 +1900,9 @@ BinaryContext::calculateEmittedSize(BinaryFunction &BF, bool FixBranches) {
std::unique_ptr<MCStreamer> Streamer(TheTarget->createMCObjectStreamer(
*TheTriple, *LocalCtx, std::unique_ptr<MCAsmBackend>(MAB), VecOS,
std::unique_ptr<MCCodeEmitter>(MCEInstance.MCE.release()), *STI,
/* RelaxAll */ false,
/* IncrementalLinkerCompatible */ false,
/* DWARFMustBeAtTheEnd */ false));
/*RelaxAll=*/false,
/*IncrementalLinkerCompatible=*/false,
/*DWARFMustBeAtTheEnd=*/false));

Streamer->InitSections(false);

Expand All @@ -1915,7 +1916,8 @@ BinaryContext::calculateEmittedSize(BinaryFunction &BF, bool FixBranches) {

Streamer->SwitchSection(Section);
Streamer->EmitLabel(StartLabel);
BF.emitBody(*Streamer, /*EmitColdPart = */false, /*EmitCodeOnly = */true);
emitFunctionBody(*Streamer, BF, /*EmitColdPart=*/false,
/*EmitCodeOnly=*/true);
Streamer->EmitLabel(EndLabel);

if (BF.isSplit()) {
Expand All @@ -1927,7 +1929,8 @@ BinaryContext::calculateEmittedSize(BinaryFunction &BF, bool FixBranches) {

Streamer->SwitchSection(ColdSection);
Streamer->EmitLabel(ColdStartLabel);
BF.emitBody(*Streamer, /*EmitColdPart = */true, /*EmitCodeOnly = */true);
emitFunctionBody(*Streamer, BF, /*EmitColdPart=*/true,
/*EmitCodeOnly=*/true);
Streamer->EmitLabel(ColdEndLabel);
}

Expand Down
24 changes: 24 additions & 0 deletions bolt/src/BinaryContext.h
Expand Up @@ -23,6 +23,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Triple.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/MC/MCAsmBackend.h"
Expand Down Expand Up @@ -257,6 +258,24 @@ class BinaryContext {
return nullptr;
}

unsigned getDWARFEncodingSize(unsigned Encoding) {
switch (Encoding & 0x0f) {
default: llvm_unreachable("unknown encoding");
case dwarf::DW_EH_PE_absptr:
case dwarf::DW_EH_PE_signed:
return AsmInfo->getCodePointerSize();
case dwarf::DW_EH_PE_udata2:
case dwarf::DW_EH_PE_sdata2:
return 2;
case dwarf::DW_EH_PE_udata4:
case dwarf::DW_EH_PE_sdata4:
return 4;
case dwarf::DW_EH_PE_udata8:
case dwarf::DW_EH_PE_sdata8:
return 8;
}
}

/// [MCSymbol] -> [BinaryFunction]
///
/// As we fold identical functions, multiple symbols can point
Expand Down Expand Up @@ -675,6 +694,11 @@ class BinaryContext {
ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
}

/// Return data section with a given name.
MCSection *getDataSection(StringRef SectionName) const {
return Ctx->getELFSection(SectionName, ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
}

/// \name Pre-assigned Section Names
/// @{

Expand Down

0 comments on commit 1f3e351

Please sign in to comment.