Skip to content

Commit 89d9b4d

Browse files
[Prototype] Add dominator tree mode for tools
1 parent 159ac2b commit 89d9b4d

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

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

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Support/MemoryBuffer.h"
2727
#include "llvm/Support/YAMLParser.h"
2828
#include "llvm/Support/YAMLTraits.h"
29+
#include <iomanip>
2930

3031
using namespace swift;
3132
using namespace modulesummary;
@@ -35,6 +36,7 @@ enum class ActionType : unsigned {
3536
BinaryToYAML,
3637
YAMLToBinary,
3738
BinaryToDominators,
39+
BinaryToDominatorTree,
3840
BinaryToDot,
3941
};
4042

@@ -62,8 +64,11 @@ static llvm::cl::opt<ActionType> Action(
6264
"Convert new binary .swiftmodule.summary format to YAML"),
6365
clEnumValN(ActionType::YAMLToBinary, "from-yaml",
6466
"Convert YAML to new binary .swiftmodule.summary format"),
67+
clEnumValN(ActionType::BinaryToDominators, "binary-to-dom",
68+
"Convert new binary .swiftmodule.summary format to "
69+
"dominators list"),
6570
clEnumValN(
66-
ActionType::BinaryToDominators, "binary-to-dom",
71+
ActionType::BinaryToDominatorTree, "binary-to-dom-tree",
6772
"Convert new binary .swiftmodule.summary format to dominator tree"),
6873
clEnumValN(ActionType::BinaryToDot, "binary-to-dot",
6974
"Convert new binary .swiftmodule.summary format to Dot")));
@@ -418,14 +423,14 @@ class DomCallTree : public DomCallTreeBase {
418423
class RetainedSizeCalculator {
419424
struct Entry {
420425
DomCallInfoNode *DomNode;
421-
size_t InstSize;
426+
uint32_t InstSize;
422427
};
423-
std::map<DomCallInfoNode *, size_t> SizeCache;
428+
std::map<DomCallInfoNode *, uint32_t> SizeCache;
424429
std::vector<Entry> Entries;
425430

426-
size_t getInstSize(DomCallInfoNode *domNode) {
431+
uint32_t getInstSize(DomCallInfoNode *domNode) {
427432
auto FS = domNode->getBlock()->getFS();
428-
size_t size = FS->getInstSize();
433+
uint32_t size = FS->getInstSize();
429434
for (auto I = domNode->begin(), E = domNode->end(); I != E; ++I) {
430435
auto Child = *I;
431436
size += getInstSize(Child);
@@ -457,10 +462,46 @@ class RetainedSizeCalculator {
457462
O << entry.InstSize << "\t| " << FS->getName() << "\n";
458463
}
459464
}
465+
466+
void displayTreeRec(llvm::raw_ostream &O, uint32_t TotalSize,
467+
DomCallInfoNode *Root, unsigned Lev) {
468+
auto Size = getInstSize(Root);
469+
auto Percent = (double(Size) / TotalSize * 100);
470+
auto FS = Root->getBlock()->getFS();
471+
O << Size << "\t| ";
472+
llvm::format_provider<double>::format(Percent, O, "f2");
473+
O << "\t| ";
474+
O.indent(2 * Lev) << FS->getName() << "\n";
475+
476+
auto Children = Root->getChildren();
477+
std::sort(Children.begin(), Children.end(),
478+
[&](DomCallInfoNode *lhs, DomCallInfoNode *rhs) {
479+
return getInstSize(lhs) > getInstSize(rhs);
480+
});
481+
for (auto Child : Children) {
482+
displayTreeRec(O, TotalSize, Child, Lev + 1);
483+
}
484+
}
485+
void displayTree(llvm::raw_ostream &O, DomCallInfoNode *Root) {
486+
O << "size\t| %\t| symbol\n";
487+
uint32_t TotalSize = 0;
488+
for (auto Child : Root->getChildren()) {
489+
TotalSize += getInstSize(Child);
490+
}
491+
auto Children = Root->getChildren();
492+
std::sort(Children.begin(), Children.end(),
493+
[&](DomCallInfoNode *lhs, DomCallInfoNode *rhs) {
494+
return getInstSize(lhs) > getInstSize(rhs);
495+
});
496+
for (auto Child : Children) {
497+
displayTreeRec(O, TotalSize, Child, 0);
498+
}
499+
}
460500
};
461501

462502
int dominance_analysis(CallGraph &CG) {
463503
DomCallTree DT(CG);
504+
DT.print(llvm::dbgs());
464505
DomCallInfoNode *Root = DT.getRootNode();
465506
RetainedSizeCalculator calculator;
466507
calculator.calculate(Root);
@@ -534,7 +575,8 @@ int main(int argc, char *argv[]) {
534575
});
535576
break;
536577
}
537-
case ActionType::BinaryToDominators: {
578+
case ActionType::BinaryToDominators:
579+
case ActionType::BinaryToDominatorTree: {
538580
modulesummary::ModuleSummaryIndex summary;
539581
modulesummary::loadModuleSummaryIndex(fileBufOrErr.get()->getMemBufferRef(),
540582
summary);
@@ -544,7 +586,11 @@ int main(int argc, char *argv[]) {
544586
RetainedSizeCalculator calculator;
545587
calculator.calculate(Root);
546588
withOutputFile(diags, options::OutputFilename, [&](raw_ostream &out) {
547-
calculator.display(out);
589+
if (options::Action == ActionType::BinaryToDominators) {
590+
calculator.display(out);
591+
} else {
592+
calculator.displayTree(out, Root);
593+
}
548594
return false;
549595
});
550596
break;

0 commit comments

Comments
 (0)