Skip to content

Commit d0314d6

Browse files
authored
Merge pull request rust-lang#150 from vext01/vlmap-tweak
Yk IR serialiser: Simplify the value lowering map.
2 parents 6cd3dd7 + eaac052 commit d0314d6

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

llvm/lib/YkIR/YkIRWriter.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,20 @@ template <class C, class E> size_t getIndex(C *Container, E *FindElement) {
135135
return Idx;
136136
}
137137

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;
141145

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>;
145152

146153
// Function lowering context.
147154
//
@@ -169,18 +176,18 @@ class FuncLowerCtxt {
169176
void patchUpInstIdxs(MCStreamer &OutStreamer) {
170177
MCContext &MCtxt = OutStreamer.getContext();
171178
for (auto &[Inst, Sym] : InstIdxPatchUps) {
172-
auto [_, InstIdx] = VLMap.at(Inst);
179+
InstIdx InstIdx = VLMap.at(Inst);
173180
OutStreamer.emitAssignment(Sym, MCConstantExpr::create(InstIdx, MCtxt));
174181
}
175182
}
176183

177184
// 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; }
179186

180187
// Get the entry for `I` in the value lowering map.
181188
//
182189
// 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); }
184191

185192
// Determines if there's an entry for `I` in the value lowering map.
186193
bool vlMapContains(Instruction *I) { return VLMap.count(I) == 1; }
@@ -289,12 +296,16 @@ class YkIRWriter {
289296
}
290297

291298
void serialiseLocalVariableOperand(Instruction *I, FuncLowerCtxt &FLCtxt) {
299+
// operand kind:
292300
serialiseOperandKind(OperandKindLocal);
301+
// func_idx:
293302
OutStreamer.emitSizeT(getIndex(&M, I->getFunction()));
303+
// bb_idx:
304+
OutStreamer.emitSizeT(getIndex(I->getFunction(), I->getParent()));
294305

306+
// inst_idx:
295307
if (FLCtxt.vlMapContains(I)) {
296-
auto [BBIdx, InstIdx] = FLCtxt.lookupInVLMap(I);
297-
OutStreamer.emitSizeT(BBIdx);
308+
InstIdx InstIdx = FLCtxt.lookupInVLMap(I);
298309
OutStreamer.emitSizeT(InstIdx);
299310
} else {
300311
// It's a local variable generated by an instruction that we haven't
@@ -303,16 +314,7 @@ class YkIRWriter {
303314
//
304315
// To work around this, we emit a dummy instruction index
305316
// 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();
316318
OutStreamer.emitSymbolValue(PatchUpSym, sizeof(size_t));
317319
FLCtxt.deferInstIdx(I, PatchUpSym);
318320
}
@@ -375,7 +377,7 @@ class YkIRWriter {
375377
// right-hand side:
376378
serialiseOperand(I, FLCtxt, I->getOperand(1));
377379

378-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
380+
FLCtxt.updateVLMap(I, InstIdx);
379381
InstIdx++;
380382
}
381383

@@ -457,7 +459,7 @@ class YkIRWriter {
457459
// XXX guard cast
458460
OutStreamer.emitSizeT(CI->getZExtValue());
459461

460-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
462+
FLCtxt.updateVLMap(I, InstIdx);
461463
InstIdx++;
462464
}
463465

@@ -540,7 +542,7 @@ class YkIRWriter {
540542

541543
// If the return type is non-void, then this defines a local.
542544
if (!I->getType()->isVoidTy()) {
543-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
545+
FLCtxt.updateVLMap(I, InstIdx);
544546
}
545547
InstIdx++;
546548
}
@@ -585,7 +587,7 @@ class YkIRWriter {
585587
// type_idx:
586588
OutStreamer.emitSizeT(typeIndex(I->getType()));
587589

588-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
590+
FLCtxt.updateVLMap(I, InstIdx);
589591
InstIdx++;
590592
}
591593

@@ -619,7 +621,7 @@ class YkIRWriter {
619621
// offset:
620622
serialiseOperand(I, FLCtxt, ConstantInt::get(I->getContext(), Offset));
621623

622-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
624+
FLCtxt.updateVLMap(I, InstIdx);
623625
InstIdx++;
624626
}
625627

@@ -676,7 +678,7 @@ class YkIRWriter {
676678
// rhs:
677679
serialiseOperand(I, FLCtxt, I->getOperand(1));
678680

679-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
681+
FLCtxt.updateVLMap(I, InstIdx);
680682
InstIdx++;
681683
}
682684

@@ -725,7 +727,7 @@ class YkIRWriter {
725727
// dest_type_idx:
726728
OutStreamer.emitSizeT(typeIndex(I->getDestTy()));
727729

728-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
730+
FLCtxt.updateVLMap(I, InstIdx);
729731
InstIdx++;
730732
}
731733

@@ -772,7 +774,7 @@ class YkIRWriter {
772774
serialiseOperand(I, FLCtxt, I->getIncomingValue(J));
773775
}
774776

775-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
777+
FLCtxt.updateVLMap(I, InstIdx);
776778
InstIdx++;
777779
}
778780

@@ -812,7 +814,7 @@ class YkIRWriter {
812814
serialiseString(toString(I));
813815

814816
if (!I->getType()->isVoidTy()) {
815-
FLCtxt.updateVLMap(I, {BBIdx, InstIdx});
817+
FLCtxt.updateVLMap(I, InstIdx);
816818
}
817819
InstIdx++;
818820
}

0 commit comments

Comments
 (0)