@@ -642,7 +642,6 @@ class YkIRWriter {
642642
643643 void serialiseBlock (BasicBlock &BB, ValueLoweringMap &VLMap,
644644 unsigned &BBIdx) {
645- // Keep the instruction skipping logic in one place.
646645 auto ShouldSkipInstr = [](Instruction *I) {
647646 // Skip non-semantic instrucitons for now.
648647 //
@@ -662,25 +661,15 @@ class YkIRWriter {
662661 return false ;
663662 };
664663
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:
671665 //
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 ));
681672
682- // num_instrs:
683- OutStreamer.emitSizeT (NumInstrs);
684673 // instrs:
685674 unsigned InstIdx = 0 ;
686675 for (Instruction &I : BB) {
@@ -690,8 +679,10 @@ class YkIRWriter {
690679 serialiseInst (&I, VLMap, BBIdx, InstIdx);
691680 }
692681
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));
695686
696687 BBIdx++;
697688 }
0 commit comments