@@ -642,7 +642,6 @@ class YkIRWriter {
642
642
643
643
void serialiseBlock (BasicBlock &BB, ValueLoweringMap &VLMap,
644
644
unsigned &BBIdx) {
645
- // Keep the instruction skipping logic in one place.
646
645
auto ShouldSkipInstr = [](Instruction *I) {
647
646
// Skip non-semantic instrucitons for now.
648
647
//
@@ -662,25 +661,15 @@ class YkIRWriter {
662
661
return false ;
663
662
};
664
663
665
- // Count instructions.
666
- //
667
- // FIXME: I don't like this much:
668
- //
669
- // - Assumes one LLVM instruction becomes exactly one Yk IR instruction.
670
- // - Requires a second loop to count ahead of time.
664
+ // num_instrs:
671
665
//
672
- // Can we emit the instrucitons into a temp buffer and keep a running count
673
- // of how many instructions we generated instead?
674
- size_t NumInstrs = 0 ;
675
- for (Instruction &I : BB) {
676
- if (ShouldSkipInstr (&I)) {
677
- continue ;
678
- }
679
- NumInstrs++;
680
- }
666
+ // We don't know how many instructions there will be in advance, so what we
667
+ // do is emit a placeholder field (in the form of a symbol value) which is
668
+ // patched up (assigned) later.
669
+ MCContext &MCtxt = OutStreamer.getContext ();
670
+ MCSymbol *NumInstrsSym = MCtxt.createTempSymbol ();
671
+ OutStreamer.emitSymbolValue (NumInstrsSym, sizeof (size_t ));
681
672
682
- // num_instrs:
683
- OutStreamer.emitSizeT (NumInstrs);
684
673
// instrs:
685
674
unsigned InstIdx = 0 ;
686
675
for (Instruction &I : BB) {
@@ -690,8 +679,10 @@ class YkIRWriter {
690
679
serialiseInst (&I, VLMap, BBIdx, InstIdx);
691
680
}
692
681
693
- // Check we emitted the number of instructions that we promised.
694
- assert (InstIdx == NumInstrs);
682
+ // Now that we have finished serialising instructions, we know how many
683
+ // there are and we can patch up the "number of instructions" field.
684
+ OutStreamer.emitAssignment (NumInstrsSym,
685
+ MCConstantExpr::create (InstIdx, MCtxt));
695
686
696
687
BBIdx++;
697
688
}
0 commit comments