Skip to content

Commit

Permalink
Merge pull request #137 from vext01/more-flexible-lowering
Browse files Browse the repository at this point in the history
Make serialising Yk IR instructions more flexible.
  • Loading branch information
ltratt authored Apr 23, 2024
2 parents 502f743 + 95e95f2 commit 9cb7ad9
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions llvm/lib/YkIR/YkIRWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,6 @@ class YkIRWriter {

void serialiseBlock(BasicBlock &BB, ValueLoweringMap &VLMap,
unsigned &BBIdx) {
// Keep the instruction skipping logic in one place.
auto ShouldSkipInstr = [](Instruction *I) {
// Skip non-semantic instrucitons for now.
//
Expand All @@ -662,25 +661,15 @@ class YkIRWriter {
return false;
};

// Count instructions.
//
// FIXME: I don't like this much:
//
// - Assumes one LLVM instruction becomes exactly one Yk IR instruction.
// - Requires a second loop to count ahead of time.
// num_instrs:
//
// Can we emit the instrucitons into a temp buffer and keep a running count
// of how many instructions we generated instead?
size_t NumInstrs = 0;
for (Instruction &I : BB) {
if (ShouldSkipInstr(&I)) {
continue;
}
NumInstrs++;
}
// We don't know how many instructions there will be in advance, so what we
// do is emit a placeholder field (in the form of a symbol value) which is
// patched up (assigned) later.
MCContext &MCtxt = OutStreamer.getContext();
MCSymbol *NumInstrsSym = MCtxt.createTempSymbol();
OutStreamer.emitSymbolValue(NumInstrsSym, sizeof(size_t));

// num_instrs:
OutStreamer.emitSizeT(NumInstrs);
// instrs:
unsigned InstIdx = 0;
for (Instruction &I : BB) {
Expand All @@ -690,8 +679,10 @@ class YkIRWriter {
serialiseInst(&I, VLMap, BBIdx, InstIdx);
}

// Check we emitted the number of instructions that we promised.
assert(InstIdx == NumInstrs);
// Now that we have finished serialising instructions, we know how many
// there are and we can patch up the "number of instructions" field.
OutStreamer.emitAssignment(NumInstrsSym,
MCConstantExpr::create(InstIdx, MCtxt));

BBIdx++;
}
Expand Down

0 comments on commit 9cb7ad9

Please sign in to comment.