Skip to content

Commit 05ac2f5

Browse files
committed
CAS.o/FlatV1: Inline TargetInfoLists that have no addends
As a follow-up to storing addends outside of blocks, inline the target lists into the block's list of indexes (rather than creating a TargetInfoList) if there are no non-zero addends. This avoids fixes the regression for the workloads I tested.
1 parent 75946ec commit 05ac2f5

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

llvm/include/llvm/CASObjectFormats/FlatV1.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ class CompileUnitBuilder {
330330
// Encode the index into compile unit.
331331
void encodeIndex(unsigned Index);
332332

333-
// Encode a target info list and return an offset.
334-
Error encodeTargetInfo(ArrayRef<const jitlink::Edge *> Edges);
333+
// Encode a target info list and return the offset.
334+
Expected<size_t> encodeTargetInfo(ArrayRef<const jitlink::Edge *> Edges);
335335

336336
private:
337337
friend class CompileUnitRef;

llvm/lib/CASObjectFormats/FlatV1.cpp

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -303,12 +303,42 @@ static Error encodeEdge(CompileUnitBuilder &CUB,
303303
return Error::success();
304304
}
305305

306-
Error CompileUnitBuilder::encodeTargetInfo(
306+
static Error encodeEdges(CompileUnitBuilder &CUB,
307+
ArrayRef<const jitlink::Edge *> Edges) {
308+
assert(!Edges.empty() && "Expected edges");
309+
bool HasAddends = false;
310+
for (const jitlink::Edge *E : Edges)
311+
if ((HasAddends = E->getAddend()))
312+
break;
313+
314+
if (HasAddends) {
315+
size_t Start;
316+
if (Error Err = CUB.encodeTargetInfo(Edges).moveInto(Start))
317+
return Err;
318+
CUB.encodeIndex((Start << 1) | 1U);
319+
return Error::success();
320+
}
321+
322+
bool IsFirst = true;
323+
for (const jitlink::Edge *E : Edges) {
324+
auto SymbolIndex = CUB.getSymbolIndex(E->getTarget());
325+
if (!SymbolIndex)
326+
return SymbolIndex.takeError();
327+
328+
// First symbol is left-shifted to differentiate from TargetInfo.
329+
unsigned IndexToEncode = IsFirst ? *SymbolIndex << 1 : *SymbolIndex;
330+
IsFirst = false;
331+
CUB.encodeIndex(IndexToEncode);
332+
}
333+
return Error::success();
334+
}
335+
336+
Expected<size_t> CompileUnitBuilder::encodeTargetInfo(
307337
ArrayRef<const jitlink::Edge *> Edges) {
308338
SmallVector<data::TargetInfo> List;
309339
for (const jitlink::Edge *E : Edges)
310340
if (Error Err = encodeEdge(*this, List, E))
311-
return Err;
341+
return std::move(Err);
312342

313343
size_t Start = FlatTargetInfo.size();
314344
data::TargetInfoList::encode(List, FlatTargetInfo);
@@ -323,11 +353,7 @@ Error CompileUnitBuilder::encodeTargetInfo(
323353
MaybeExisting = Start + 1;
324354
}
325355

326-
// FIXME: Maybe should use uint64_t, or be a different range from CAS object
327-
// indexes.
328-
assert(Start < UINT_MAX && "Index out of range");
329-
encodeIndex(Start);
330-
return Error::success();
356+
return Start;
331357
}
332358

333359
data::TargetInfoList LinkGraphBuilder::getTargetInfoFrom(unsigned Start) {
@@ -347,15 +373,27 @@ static Error decodeEdge(LinkGraphBuilder &LGB, jitlink::Block &Parent,
347373

348374
static Error decodeEdges(LinkGraphBuilder &LGB, jitlink::Block &Parent,
349375
unsigned BlockIdx, const data::FixupList &FL) {
350-
if (FL.empty())
376+
assert(!FL.empty() && "Expected edges");
377+
378+
unsigned TargetInfoIndex = LGB.nextIdxForBlock(BlockIdx);
379+
if (TargetInfoIndex & 1U) {
380+
unsigned TargetInfoStart = TargetInfoIndex >> 1;
381+
data::TargetInfoList TIL(LGB.getTargetInfoFrom(TargetInfoStart));
382+
data::TargetInfoList::iterator TI = TIL.begin();
383+
for (data::Fixup Fixup : FL)
384+
if (Error E = decodeEdge(LGB, Parent, *TI++, Fixup))
385+
return E;
351386
return Error::success();
387+
}
352388

353-
unsigned TargetInfoStart = LGB.nextIdxForBlock(BlockIdx);
354-
data::TargetInfoList TIL(LGB.getTargetInfoFrom(TargetInfoStart));
355-
data::TargetInfoList::iterator TI = TIL.begin();
356-
for (const data::Fixup &Fixup : FL)
357-
if (Error E = decodeEdge(LGB, Parent, *TI++, Fixup))
358-
return E;
389+
// No addends.
390+
bool IsFirst = true;
391+
for (data::Fixup Fixup : FL) {
392+
unsigned SymbolIndex = IsFirst ? TargetInfoIndex >> 1 : LGB.nextIdxForBlock(BlockIdx);
393+
IsFirst = false;
394+
if (Error Err = decodeEdge(LGB, Parent, data::TargetInfo{0, SymbolIndex}, Fixup))
395+
return Err;
396+
}
359397
return Error::success();
360398
}
361399

@@ -421,7 +459,7 @@ Expected<BlockRef> BlockRef::create(CompileUnitBuilder &CUB,
421459
}
422460

423461
if (!Edges.empty())
424-
if (auto Err = CUB.encodeTargetInfo(Edges))
462+
if (Error Err = encodeEdges(CUB, Edges))
425463
return std::move(Err);
426464

427465
return get(B->build());

0 commit comments

Comments
 (0)