@@ -135,13 +135,20 @@ template <class C, class E> size_t getIndex(C *Container, E *FindElement) {
135
135
return Idx;
136
136
}
137
137
138
- // A <BBIdx, InstrIdx> pair that Uniquely identifies an Yk IR instruction within
139
- // a function.
140
- using InstrLoc = std::tuple<size_t , size_t >;
138
+ // An instruction index that uniquely identifies an Yk instruction within
139
+ // a basic block.
140
+ //
141
+ // FIXME: At some point it may be worth making type-safe index types (for
142
+ // instruction, block and function indices) and using them throughout
143
+ // the serialiser.
144
+ using InstIdx = size_t ;
141
145
142
- // Maps an LLVM instruction that generates a value to the corresponding Yk IR
143
- // instruction.
144
- using ValueLoweringMap = map<Instruction *, InstrLoc>;
146
+ // Maps an LLVM local (the instruction that creates it) to the correspoinding Yk
147
+ // instruction index in its parent basic block.
148
+ //
149
+ // Note: The Yk basic block index is not stored because it's the same as
150
+ // the LLVM IR block index, which can be found elsewhere (see `getIndex()`).
151
+ using ValueLoweringMap = map<Instruction *, InstIdx>;
145
152
146
153
// Function lowering context.
147
154
//
@@ -169,18 +176,18 @@ class FuncLowerCtxt {
169
176
void patchUpInstIdxs (MCStreamer &OutStreamer) {
170
177
MCContext &MCtxt = OutStreamer.getContext ();
171
178
for (auto &[Inst, Sym] : InstIdxPatchUps) {
172
- auto [_, InstIdx] = VLMap.at (Inst);
179
+ InstIdx InstIdx = VLMap.at (Inst);
173
180
OutStreamer.emitAssignment (Sym, MCConstantExpr::create (InstIdx, MCtxt));
174
181
}
175
182
}
176
183
177
184
// Add/update an entry in the value lowering map.
178
- void updateVLMap (Instruction *I, InstrLoc L) { VLMap[I] = L; }
185
+ void updateVLMap (Instruction *I, InstIdx L) { VLMap[I] = L; }
179
186
180
187
// Get the entry for `I` in the value lowering map.
181
188
//
182
189
// Raises `std::out_of_range` if not present.
183
- InstrLoc lookupInVLMap (Instruction *I) { return VLMap.at (I); }
190
+ InstIdx lookupInVLMap (Instruction *I) { return VLMap.at (I); }
184
191
185
192
// Determines if there's an entry for `I` in the value lowering map.
186
193
bool vlMapContains (Instruction *I) { return VLMap.count (I) == 1 ; }
@@ -289,12 +296,16 @@ class YkIRWriter {
289
296
}
290
297
291
298
void serialiseLocalVariableOperand (Instruction *I, FuncLowerCtxt &FLCtxt) {
299
+ // operand kind:
292
300
serialiseOperandKind (OperandKindLocal);
301
+ // func_idx:
293
302
OutStreamer.emitSizeT (getIndex (&M, I->getFunction ()));
303
+ // bb_idx:
304
+ OutStreamer.emitSizeT (getIndex (I->getFunction (), I->getParent ()));
294
305
306
+ // inst_idx:
295
307
if (FLCtxt.vlMapContains (I)) {
296
- auto [BBIdx, InstIdx] = FLCtxt.lookupInVLMap (I);
297
- OutStreamer.emitSizeT (BBIdx);
308
+ InstIdx InstIdx = FLCtxt.lookupInVLMap (I);
298
309
OutStreamer.emitSizeT (InstIdx);
299
310
} else {
300
311
// It's a local variable generated by an instruction that we haven't
@@ -303,16 +314,7 @@ class YkIRWriter {
303
314
//
304
315
// To work around this, we emit a dummy instruction index
305
316
// and patch it up later once it becomes known.
306
- //
307
- // The basic block index can be immediately known, since the indices are
308
- // the same in the LLVM IR and our AOT IR.
309
- //
310
- // FIXME: In light of the above, there's no need to store basic block
311
- // indices in the VLMap?
312
- OutStreamer.emitSizeT (getIndex (I->getFunction (), I->getParent ()));
313
-
314
- MCContext &MCtxt = OutStreamer.getContext ();
315
- MCSymbol *PatchUpSym = MCtxt.createTempSymbol ();
317
+ MCSymbol *PatchUpSym = OutStreamer.getContext ().createTempSymbol ();
316
318
OutStreamer.emitSymbolValue (PatchUpSym, sizeof (size_t ));
317
319
FLCtxt.deferInstIdx (I, PatchUpSym);
318
320
}
@@ -375,7 +377,7 @@ class YkIRWriter {
375
377
// right-hand side:
376
378
serialiseOperand (I, FLCtxt, I->getOperand (1 ));
377
379
378
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
380
+ FLCtxt.updateVLMap (I, InstIdx);
379
381
InstIdx++;
380
382
}
381
383
@@ -457,7 +459,7 @@ class YkIRWriter {
457
459
// XXX guard cast
458
460
OutStreamer.emitSizeT (CI->getZExtValue ());
459
461
460
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
462
+ FLCtxt.updateVLMap (I, InstIdx);
461
463
InstIdx++;
462
464
}
463
465
@@ -540,7 +542,7 @@ class YkIRWriter {
540
542
541
543
// If the return type is non-void, then this defines a local.
542
544
if (!I->getType ()->isVoidTy ()) {
543
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
545
+ FLCtxt.updateVLMap (I, InstIdx);
544
546
}
545
547
InstIdx++;
546
548
}
@@ -585,7 +587,7 @@ class YkIRWriter {
585
587
// type_idx:
586
588
OutStreamer.emitSizeT (typeIndex (I->getType ()));
587
589
588
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
590
+ FLCtxt.updateVLMap (I, InstIdx);
589
591
InstIdx++;
590
592
}
591
593
@@ -619,7 +621,7 @@ class YkIRWriter {
619
621
// offset:
620
622
serialiseOperand (I, FLCtxt, ConstantInt::get (I->getContext (), Offset));
621
623
622
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
624
+ FLCtxt.updateVLMap (I, InstIdx);
623
625
InstIdx++;
624
626
}
625
627
@@ -676,7 +678,7 @@ class YkIRWriter {
676
678
// rhs:
677
679
serialiseOperand (I, FLCtxt, I->getOperand (1 ));
678
680
679
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
681
+ FLCtxt.updateVLMap (I, InstIdx);
680
682
InstIdx++;
681
683
}
682
684
@@ -725,7 +727,7 @@ class YkIRWriter {
725
727
// dest_type_idx:
726
728
OutStreamer.emitSizeT (typeIndex (I->getDestTy ()));
727
729
728
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
730
+ FLCtxt.updateVLMap (I, InstIdx);
729
731
InstIdx++;
730
732
}
731
733
@@ -772,7 +774,7 @@ class YkIRWriter {
772
774
serialiseOperand (I, FLCtxt, I->getIncomingValue (J));
773
775
}
774
776
775
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
777
+ FLCtxt.updateVLMap (I, InstIdx);
776
778
InstIdx++;
777
779
}
778
780
@@ -812,7 +814,7 @@ class YkIRWriter {
812
814
serialiseString (toString (I));
813
815
814
816
if (!I->getType ()->isVoidTy ()) {
815
- FLCtxt.updateVLMap (I, {BBIdx, InstIdx} );
817
+ FLCtxt.updateVLMap (I, InstIdx);
816
818
}
817
819
InstIdx++;
818
820
}
0 commit comments