Skip to content

Commit 95e95f2

Browse files
committed
Make serialising Yk IR instructions more flexible.
Initially, each LLVM IR instruction had to be lowered to exactly one Yk IR instruction. Why? Because it was easy to implement. Later we realised we needed to skip certain LLVM instructions (e.g. debug calls). At this point we added complexity. Due to the way the deserialiser works, we need to emit an instruction count field before emitting the instructions themselves. Therefore we had to first count how many instructions would be emitted in an initial loop, emit this count, then emit the instructions themselves in a second loop. This change removes the need for the first loop by "patching up" a placeholder instruction count field once the number of instructions is known. In turn this simplifies the code quite a bit, making it easier to maintain, but also will make it easier for one LLVM instruction to be lowered to more than one Yk IR instruction. For example, we might opt to lower an LLVM switch instruction into a series of Yk IR conditional instructions. No functional change. No runtime changes required.
1 parent b1a3c62 commit 95e95f2

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

llvm/lib/YkIR/YkIRWriter.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,6 @@ class YkIRWriter {
641641

642642
void serialiseBlock(BasicBlock &BB, ValueLoweringMap &VLMap,
643643
unsigned &BBIdx) {
644-
// Keep the instruction skipping logic in one place.
645644
auto ShouldSkipInstr = [](Instruction *I) {
646645
// Skip non-semantic instrucitons for now.
647646
//
@@ -661,25 +660,15 @@ class YkIRWriter {
661660
return false;
662661
};
663662

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

681-
// num_instrs:
682-
OutStreamer.emitSizeT(NumInstrs);
683672
// instrs:
684673
unsigned InstIdx = 0;
685674
for (Instruction &I : BB) {
@@ -689,8 +678,10 @@ class YkIRWriter {
689678
serialiseInst(&I, VLMap, BBIdx, InstIdx);
690679
}
691680

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

695686
BBIdx++;
696687
}

0 commit comments

Comments
 (0)