Skip to content

Commit

Permalink
Merge pull request rust-lang#148 from vext01/phi
Browse files Browse the repository at this point in the history
Serialise PHI nodes.
  • Loading branch information
ltratt authored May 1, 2024
2 parents a52f437 + bddbee5 commit 73ffc00
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions llvm/lib/YkIR/YkIRWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ enum OpCode {
OpCodeBinOp,
OpCodeCast,
OpCodeSwitch,
OpCodePHI,
OpCodeUnimplemented = 255, // YKFIXME: Will eventually be deleted.
};

Expand Down Expand Up @@ -174,6 +175,18 @@ class YkIRWriter {
vector<llvm::Constant *> Constants;
vector<llvm::GlobalVariable *> Globals;

// Instruction indices that need to be patched up later.
vector<tuple<Instruction *, MCSymbol *>> InstIdxPacthUps;

// Fill in instruction indices that had to be deferred.
void patchUpInstIdxs(ValueLoweringMap &VLMap) {
MCContext &MCtxt = OutStreamer.getContext();
for (auto &[Inst, Sym] : InstIdxPacthUps) {
auto [_, InstIdx] = VLMap.at(Inst);
OutStreamer.emitAssignment(Sym, MCConstantExpr::create(InstIdx, MCtxt));
}
}

// Return the index of the LLVM type `Ty`, inserting a new entry if
// necessary.
size_t typeIndex(llvm::Type *Ty) {
Expand Down Expand Up @@ -244,11 +257,33 @@ class YkIRWriter {
}

void serialiseLocalVariableOperand(Instruction *I, ValueLoweringMap &VLMap) {
auto [BBIdx, InstIdx] = VLMap.at(I);
serialiseOperandKind(OperandKindLocal);
OutStreamer.emitSizeT(getIndex(&M, I->getFunction()));
OutStreamer.emitSizeT(BBIdx);
OutStreamer.emitSizeT(InstIdx);

if (VLMap.count(I) == 1) {
auto [BBIdx, InstIdx] = VLMap.at(I);
OutStreamer.emitSizeT(BBIdx);
OutStreamer.emitSizeT(InstIdx);
} else {
// It's a local variable generated by an instruction that we haven't
// serialised yet. This can happen in loop bodies where a PHI node merges
// in a variable from the end of the loop body.
//
// To work around this, we emit a dummy instruction index
// and patch it up later once it becomes known.
//
// The basic block index can be immediately known, since the indices are
// the same in the LLVM IR and our AOT IR.
//
// FIXME: In light of the above, there's no need to store basic block
// indices in the VLMap?
OutStreamer.emitSizeT(getIndex(I->getFunction(), I->getParent()));

MCContext &MCtxt = OutStreamer.getContext();
MCSymbol *PatchUpSym = MCtxt.createTempSymbol();
OutStreamer.emitSymbolValue(PatchUpSym, sizeof(size_t));
InstIdxPacthUps.push_back({I, PatchUpSym});
}
}

void serialiseFunctionOperand(llvm::Function *F) {
Expand Down Expand Up @@ -690,6 +725,26 @@ class YkIRWriter {
InstIdx++;
}

void serialisePhiInst(PHINode *I, ValueLoweringMap &VLMap, unsigned BBIdx,
unsigned &InstIdx) {
// opcode:
serialiseOpcode(OpCodePHI);
// num_incoming:
size_t NumIncoming = I->getNumIncomingValues();
OutStreamer.emitSizeT(NumIncoming);
// incoming_bbs:
for (size_t J = 0; J < NumIncoming; J++) {
serialiseBlockLabel(I->getIncomingBlock(J));
}
// incoming_vals:
for (size_t J = 0; J < NumIncoming; J++) {
serialiseOperand(I, VLMap, I->getIncomingValue(J));
}

VLMap[I] = {BBIdx, InstIdx};
InstIdx++;
}

void serialiseInst(Instruction *I, ValueLoweringMap &VLMap, unsigned BBIdx,
unsigned &InstIdx) {
// Macro to make the dispatch below easier to read/sort.
Expand All @@ -707,6 +762,7 @@ class YkIRWriter {
INST_SERIALISE(I, ICmpInst, serialiseICmpInst);
INST_SERIALISE(I, InsertValueInst, serialiseInsertValueInst);
INST_SERIALISE(I, LoadInst, serialiseLoadInst);
INST_SERIALISE(I, PHINode, serialisePhiInst);
INST_SERIALISE(I, ReturnInst, serialiseReturnInst);
INST_SERIALISE(I, SExtInst, serialiseSExtInst);
INST_SERIALISE(I, StoreInst, serialiseStoreInst);
Expand Down Expand Up @@ -798,6 +854,11 @@ class YkIRWriter {
for (BasicBlock &BB : F) {
serialiseBlock(BB, VLMap, BBIdx);
}

patchUpInstIdxs(VLMap);
// FIXME: it'd be better to make a new patchup struct for each function and
// just let it fall out of scope when done. Lots of plumbing...
InstIdxPacthUps.clear();
}

void serialiseFunctionType(FunctionType *Ty) {
Expand Down

0 comments on commit 73ffc00

Please sign in to comment.