@@ -459,16 +459,17 @@ class YkIRWriter {
459
459
//
460
460
// Note that in LLVM IR, the operands are ordered (despite the order they
461
461
// appear in the language reference): cond, if-false, if-true. We
462
- // re-order those during lowering to avoid confusion.
462
+ // use `getSuccessor()`, so as to re-order those during lowering to avoid
463
+ // confusion.
463
464
//
464
465
// num_operands:
465
466
OutStreamer.emitInt32 (3 );
466
467
// OPERAND 0: condition.
467
468
serialiseOperand (I, VLMap, I->getOperand (0 ));
468
469
// OPERAND 1: block to go to if true.
469
- serialiseOperand (I, VLMap, I->getOperand ( 2 ));
470
+ serialiseOperand (I, VLMap, I->getSuccessor ( 0 ));
470
471
// OPERAND 2: block to go to if false.
471
- serialiseOperand (I, VLMap, I->getOperand (1 ));
472
+ serialiseOperand (I, VLMap, I->getSuccessor (1 ));
472
473
}
473
474
InstIdx++;
474
475
}
@@ -618,13 +619,46 @@ class YkIRWriter {
618
619
619
620
void serialiseBlock (BasicBlock &BB, ValueLoweringMap &VLMap,
620
621
unsigned &BBIdx) {
622
+ // Keep the instruction skipping logic in one place.
623
+ auto ShouldSkipInstr = [](Instruction *I) {
624
+ // Skip non-semantic instrucitons for now.
625
+ //
626
+ // We may come back to them later if we need better debugging
627
+ // facilities, but for now they just clutter up our AOT module.
628
+ return I->isDebugOrPseudoInst ();
629
+ };
630
+
631
+ // Count instructions.
632
+ //
633
+ // FIXME: I don't like this much:
634
+ //
635
+ // - Assumes one LLVM instruction becomes exactly one Yk IR instruction.
636
+ // - Requires a second loop to count ahead of time.
637
+ //
638
+ // Can we emit the instrucitons into a temp buffer and keep a running count
639
+ // of how many instructions we generated instead?
640
+ size_t NumInstrs = 0 ;
641
+ for (Instruction &I : BB) {
642
+ if (ShouldSkipInstr (&I)) {
643
+ continue ;
644
+ }
645
+ NumInstrs++;
646
+ }
647
+
621
648
// num_instrs:
622
- OutStreamer.emitSizeT (BB. size () );
649
+ OutStreamer.emitSizeT (NumInstrs );
623
650
// instrs:
624
651
unsigned InstIdx = 0 ;
625
652
for (Instruction &I : BB) {
653
+ if (ShouldSkipInstr (&I)) {
654
+ continue ;
655
+ }
626
656
serialiseInst (&I, VLMap, BBIdx, InstIdx);
627
657
}
658
+
659
+ // Check we emitted the number of instructions that we promised.
660
+ assert (InstIdx == NumInstrs);
661
+
628
662
BBIdx++;
629
663
}
630
664
0 commit comments