Skip to content

Commit 1cb9a4a

Browse files
bors[bot]vext01
andauthored
45: Add the `-yk-alloc-llvmbc-section` flag. r=ltratt a=vext01 Co-authored-by: Edd Barrett <vext01@gmail.com>
2 parents 43cfd81 + fd37a40 commit 1cb9a4a

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ static cl::opt<bool> WriteRelBFToSummary(
9898

9999
extern FunctionSummary::ForceSummaryHotnessType ForceSummaryEdgesCold;
100100

101+
extern bool YkAllocLLVMBCSection;
102+
101103
namespace {
102104

103105
/// These are manifest constants used by the bitcode writer. They do not need to
@@ -4992,11 +4994,32 @@ void llvm::embedBitcodeInModule(llvm::Module &M, llvm::MemoryBufferRef Buf,
49924994
ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
49934995
Buf.getBufferSize());
49944996
}
4997+
4998+
GlobalValue::LinkageTypes SymLinkage = GlobalValue::PrivateLinkage;
4999+
5000+
// For the YK JIT we prepend a header containing the size of the bitcode.
5001+
// This is required in order to load bitcode from memory.
5002+
std::vector<uint8_t> YkModuleData;
5003+
if (YkAllocLLVMBCSection) {
5004+
// Write length field.
5005+
size_t ModuleDataSize = ModuleData.size();
5006+
uint8_t *Bytes = reinterpret_cast<uint8_t *>(&ModuleDataSize);
5007+
for (size_t I = 0; I < sizeof(ModuleDataSize); I++)
5008+
YkModuleData.push_back(Bytes[I]);
5009+
5010+
// Append bitcode.
5011+
std::move(ModuleData.begin(), ModuleData.end(),
5012+
std::back_inserter(YkModuleData));
5013+
ModuleData = YkModuleData;
5014+
5015+
// Ensure the symbol is exported in the resulting binary.
5016+
SymLinkage = GlobalValue::ExternalLinkage;
5017+
}
5018+
49955019
llvm::Constant *ModuleConstant =
49965020
llvm::ConstantDataArray::get(M.getContext(), ModuleData);
49975021
llvm::GlobalVariable *GV = new llvm::GlobalVariable(
4998-
M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
4999-
ModuleConstant);
5022+
M, ModuleConstant->getType(), true, SymLinkage, ModuleConstant);
50005023
GV->setSection(getSectionNameForBitcode(T));
50015024
// Set alignment to 1 to prevent padding between two contributions from input
50025025
// sections after linking.

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
using namespace llvm;
7171
using namespace dwarf;
7272

73+
extern bool YkAllocLLVMBCSection;
74+
7375
static void GetObjCImageInfo(Module &M, unsigned &Version, unsigned &Flags,
7476
StringRef &Section) {
7577
SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
@@ -786,6 +788,12 @@ static MCSection *selectExplicitSectionGlobal(
786788
Retain, ForceUnique);
787789

788790
const MCSymbolELF *LinkedToSym = getLinkedToSymbol(GO, TM);
791+
792+
// The Yk JIT expects to load the IR from its address space. This tells the
793+
// loader to load the section.
794+
if (YkAllocLLVMBCSection && (SectionName == ".llvmbc"))
795+
Flags |= llvm::ELF::SHF_ALLOC;
796+
789797
MCSectionELF *Section = Ctx.getELFSection(
790798
SectionName, getELFSectionType(SectionName, Kind), Flags, EntrySize,
791799
Group, IsComdat, UniqueID, LinkedToSym);

llvm/lib/Support/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ add_llvm_component_library(LLVMSupport
236236
X86TargetParser.cpp
237237
YAMLParser.cpp
238238
YAMLTraits.cpp
239+
Yk.cpp
239240
raw_os_ostream.cpp
240241
raw_ostream.cpp
241242
regcomp.c

llvm/lib/Support/Yk.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include "llvm/Support/CommandLine.h"
2+
3+
using namespace llvm;
4+
5+
bool YkAllocLLVMBCSection;
6+
static cl::opt<bool, true> YkAllocLLVMBCSectionParser(
7+
"yk-alloc-llvmbc-section", cl::desc("Make the `.llvmbc` section loadable"),
8+
cl::NotHidden, cl::location(YkAllocLLVMBCSection));

0 commit comments

Comments
 (0)