Skip to content

Commit cc4259a

Browse files
committed
Make the operand ordering of conditional branches less confusing.
In LLVM, a conditional branch (in textual representation) looks like: ``` br %cond, bb-if-true, bb-if-false ``` But under the hood, the operands are stored in a different order: cond, bb-if-false, bb-if-true This led us to invert all of our guards in our traces! Rather than inherit this oddity in our IRs, this change re-jiggles the operand ordering during lowering to make it how you might expect.
1 parent 69a5b5a commit cc4259a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

llvm/lib/YkIR/YkIRWriter.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,12 +449,28 @@ class YkIRWriter {
449449
OutStreamer.emitInt32(0);
450450

451451
VLMap[I] = {BBIdx, InstIdx};
452-
InstIdx++;
453452
} else {
453+
// type_index:
454+
OutStreamer.emitSizeT(typeIndex(I->getType()));
455+
// opcode:
456+
serialiseOpcode(OpCode::CondBr);
454457
// We DO need operands for conditional branches, so that we can build
455458
// guards.
456-
serialiseInstGeneric(I, VLMap, BBIdx, InstIdx, OpCode::CondBr);
459+
//
460+
// Note that in LLVM IR, the operands are ordered (despite the order they
461+
// appear in the language reference): cond, if-false, if-true. We
462+
// re-order those during lowering to avoid confusion.
463+
//
464+
// num_operands:
465+
OutStreamer.emitInt32(3);
466+
// OPERAND 0: condition.
467+
serialiseOperand(I, VLMap, I->getOperand(0));
468+
// OPERAND 1: block to go to if true.
469+
serialiseOperand(I, VLMap, I->getOperand(2));
470+
// OPERAND 2: block to go to if false.
471+
serialiseOperand(I, VLMap, I->getOperand(1));
457472
}
473+
InstIdx++;
458474
}
459475

460476
void serialiseGetElementPtr(GetElementPtrInst *I, ValueLoweringMap &VLMap,

0 commit comments

Comments
 (0)