Skip to content

Commit 9400c50

Browse files
[Prototype] Print dominant instruction size
1 parent e2a20f2 commit 9400c50

File tree

5 files changed

+75
-5
lines changed

5 files changed

+75
-5
lines changed

include/swift/Serialization/ModuleSummary.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ class FunctionSummary {
9999
TypeRefListTy TypeRefList;
100100
/// The symbol name of the function only for debug and test purposes.
101101
std::string Name;
102+
/// Size of instructions only for debug purpose
103+
size_t InstSize;
102104

103105
public:
104106
FunctionSummary() = default;
@@ -126,6 +128,9 @@ class FunctionSummary {
126128
void setName(std::string name) { this->Name = name; }
127129

128130
GUID getGUID() const { return Guid; }
131+
132+
size_t getInstSize() const { return InstSize; }
133+
void setInstSize(size_t size) { this->InstSize = size; }
129134
};
130135

131136
/// A slot in a set of virtual tables.

lib/Serialization/ModuleSummaryFormat.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void Serializer::emitFunctionSummary(const FunctionSummary *summary) {
122122
FunctionMetadataLayout::emitRecord(
123123
Out, ScratchRecord, AbbrCodes[FunctionMetadataLayout::Code],
124124
summary->getGUID(), unsigned(summary->isLive()),
125-
unsigned(summary->isPreserved()), debugFuncName);
125+
unsigned(summary->isPreserved()), summary->getInstSize(), debugFuncName);
126126

127127
for (auto call : summary->calls()) {
128128
std::string debugName = ModuleSummaryEmbedDebugName ? call.getName() : "";
@@ -361,8 +361,10 @@ bool Deserializer::readModuleSummary() {
361361
std::string name;
362362
unsigned isLive, isPreserved;
363363
bool shouldMerge = false;
364+
uint32_t instSize;
364365

365-
FunctionMetadataLayout::readRecord(Scratch, guid, isLive, isPreserved);
366+
FunctionMetadataLayout::readRecord(Scratch, guid, isLive, isPreserved,
367+
instSize);
366368
name = BlobData.str();
367369
if (auto summary = moduleSummary.getFunctionSummary(guid)) {
368370
CurrentFunc = summary;
@@ -382,6 +384,7 @@ bool Deserializer::readModuleSummary() {
382384
if (!shouldMerge || !name.empty()) {
383385
CurrentFunc->setName(name);
384386
}
387+
CurrentFunc->setInstSize(instSize);
385388
break;
386389
}
387390
case CALL_GRAPH_EDGE: {

lib/Serialization/ModuleSummaryFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace modulesummary {
2323
using llvm::BCBlob;
2424
using llvm::BCFixed;
2525
using llvm::BCRecordLayout;
26+
using llvm::BCVBR;
2627

2728
const unsigned char MODULE_SUMMARY_SIGNATURE[] = {'M', 'O', 'D', 'S'};
2829
const unsigned RECORD_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID;
@@ -48,6 +49,7 @@ using FunctionMetadataLayout = BCRecordLayout<FUNC_METADATA,
4849
BCGUID, // function guid
4950
BCFixed<1>, // live
5051
BCFixed<1>, // preserved
52+
BCVBR<16>, // instruction size
5153
BCBlob // name (debug purpose)
5254
>;
5355
using CallGraphEdgeLayout =

lib/Serialization/ModuleSummaryIndexer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,17 @@ bool shouldPreserveFunction(const SILFunction &F) {
320320

321321
void FunctionSummaryIndexer::indexFunction() {
322322
GUID guid = getGUIDFromUniqueName(F.getName());
323+
size_t instSize = 0;
323324
TheSummary = std::make_unique<FunctionSummary>(guid);
324325
TheSummary->setName(F.getName());
325326
for (auto &BB : F) {
326327
for (auto &I : BB) {
327328
visit(&I);
329+
instSize++;
328330
}
329331
}
330332
TheSummary->setPreserved(shouldPreserveFunction(F));
333+
TheSummary->setInstSize(instSize);
331334
}
332335

333336
class ModuleSummaryIndexer {
@@ -447,7 +450,6 @@ void ModuleSummaryIndexer::indexModule() {
447450
FunctionSummaryIndexer indexer(F);
448451
indexer.indexFunction();
449452
std::unique_ptr<FunctionSummary> FS = indexer.takeSummary();
450-
FS->setPreserved(shouldPreserveFunction(F));
451453
TheSummary->addFunctionSummary(std::move(FS));
452454
}
453455

tools/swift-module-summary-test/swift-module-summary-test.cpp

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ struct CallGraph {
207207

208208
CallGraph *getParent() const { return Parent; }
209209

210+
FunctionSummary *getFS() const {
211+
return Parent->Summary.getFunctionSummary(Guid);
212+
}
213+
210214
void printAsOperand(raw_ostream &OS, bool /*PrintType*/) {
211215
FunctionSummary *FS = Parent->Summary.getFunctionSummary(Guid);
212216
if (!FS) {
@@ -410,9 +414,55 @@ class DomCallTree : public DomCallTreeBase {
410414
DomCallTree(CallGraph &CG) : DominatorTreeBase() { recalculate(CG); }
411415
};
412416

417+
class RetainedSizeCalculator {
418+
struct Entry {
419+
DomCallInfoNode *DomNode;
420+
size_t InstSize;
421+
};
422+
std::map<DomCallInfoNode *, size_t> SizeCache;
423+
std::vector<Entry> Entries;
424+
425+
size_t getInstSize(DomCallInfoNode *domNode) {
426+
auto FS = domNode->getBlock()->getFS();
427+
size_t size = FS->getInstSize();
428+
for (auto I = domNode->begin(), E = domNode->end(); I != E; ++I) {
429+
auto Child = *I;
430+
size += getInstSize(Child);
431+
}
432+
SizeCache[domNode] = size;
433+
return size;
434+
}
435+
436+
void calculateRecursively(DomCallInfoNode *domNode) {
437+
Entries.push_back({domNode, getInstSize(domNode)});
438+
for (auto child : domNode->getChildren()) {
439+
calculateRecursively(child);
440+
}
441+
}
442+
443+
public:
444+
void calculate(DomCallInfoNode *root) {
445+
for (auto child : root->getChildren()) {
446+
calculateRecursively(child);
447+
}
448+
std::sort(Entries.begin(), Entries.end(),
449+
[](Entry lhs, Entry rhs) { return lhs.InstSize > rhs.InstSize; });
450+
}
451+
452+
void display(llvm::raw_ostream &O) {
453+
for (auto entry : Entries) {
454+
auto FS = entry.DomNode->getBlock()->getFS();
455+
O << entry.InstSize << "\t| " << FS->getName() << "\n";
456+
}
457+
}
458+
};
459+
413460
int dominance_analysis(CallGraph &CG) {
414461
DomCallTree DT(CG);
415-
DT.print(llvm::dbgs());
462+
DomCallInfoNode *Root = DT.getRootNode();
463+
RetainedSizeCalculator calculator;
464+
calculator.calculate(Root);
465+
calculator.display(llvm::dbgs());
416466
return 0;
417467
};
418468

@@ -487,7 +537,15 @@ int main(int argc, char *argv[]) {
487537
modulesummary::loadModuleSummaryIndex(fileBufOrErr.get()->getMemBufferRef(),
488538
summary);
489539
CallGraph CG(std::move(summary));
490-
return dominance_analysis(CG);
540+
DomCallTree DT(CG);
541+
DomCallInfoNode *Root = DT.getRootNode();
542+
RetainedSizeCalculator calculator;
543+
calculator.calculate(Root);
544+
withOutputFile(diags, options::OutputFilename, [&](raw_ostream &out) {
545+
calculator.display(out);
546+
return false;
547+
});
548+
break;
491549
}
492550
}
493551
return 0;

0 commit comments

Comments
 (0)