Skip to content

Commit

Permalink
Merge pull request #608 from egraphs-good/ajpal-cfg
Browse files Browse the repository at this point in the history
Show CFGs on Nightly
  • Loading branch information
ajpal authored May 24, 2024
2 parents ccadcf9 + a25e62e commit 81cdeef
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 117 deletions.
59 changes: 59 additions & 0 deletions infra/generate_cfgs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3
import glob
import os

def make_cfgs(bench, data_dir):
cwd = os.getcwd()
path = f"{data_dir}/{bench}"
runmodes = os.listdir(path)
for mode in runmodes:
os.chdir(f"{path}/{mode}")

# HACK: check if opt-18 exists
# otherwise use opt
# On Linux, sometimes it's called opt-18, while on mac it seems to be just opt
# Also, on some machines, just running `opt-18` hangs, so we pass the version flag
if os.system("opt-18 --version") == 0:
opt = "opt-18"
else:
opt = "opt"

# https://llvm.org/docs/Passes.html#dot-cfg-print-cfg-of-function-to-dot-file
cmd = f"{opt} -disable-output -passes=dot-cfg {bench}-{mode}.ll"
os.system(cmd)

# Delete the -init.ll file (We don't need it for nightly,
# so just reduce the amount of clutter we copy to the nightly machine)
os.system(f"rm {bench}-{mode}-init.ll")

# Find all the dot files (can't use glob because it doesn't match hidden files)
# There are also a bunch of files that start with ._Z that I don't think we care about?
dots = [f for f in os.listdir(".") if f.endswith(".dot") and not f.startswith("._Z") and not f.startswith("._bril")]
for dot in dots:
name = dot.split(".")[1]

# Convert to png
cmd = f"dot -Tpng -o {name}.png {dot}"
os.system(cmd)

pngs = glob.glob("*.png")
print(f"Generated {len(pngs)} CFGs for {bench} {mode}")
with open("png_names.txt", "w") as f:
f.write("\n".join(pngs))

# Clean up dot files
os.system("rm .*.dot")

# Reset dir
os.chdir(cwd)


if __name__ == '__main__':
# expect a single argument
if len(os.sys.argv) != 2:
print("Usage: generate_line_counts.py <data directory>")
exit(1)
data_dir = os.sys.argv[1]
benchmarks = os.listdir(data_dir)
for bench in benchmarks:
make_cfgs(bench, data_dir)
12 changes: 6 additions & 6 deletions infra/generate_line_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get_rows_for_benchmark(bench, profile_data):


def benchmarks_table():
profile_data = json.load(open(f'{output_path}/data/profile.json'))
profile_data = json.load(open(f'{output_path}/profile.json'))
benchmarks = set([x["benchmark"] for x in profile_data])
rows = header()
rows += [
Expand All @@ -121,15 +121,15 @@ def benchmarks_table():
return "\n".join(rows)

def generate_latex(output_path):
with open(f'{output_path}/data/linecount.tex', "w") as f:
with open(f'{output_path}/linecount.tex', "w") as f:
f.write(linecount_table())
with open(f'{output_path}/data/detailed-linecount.tex', "w") as f:
with open(f'{output_path}/detailed-linecount.tex', "w") as f:
f.write(detailed_linecount_table())
with open(f'{output_path}/data/benchmarks.tex', "w") as f:
with open(f'{output_path}/benchmarks.tex', "w") as f:
f.write(benchmarks_table())
tex_files = glob.glob(f"{output_path}/data/*.tex")
tex_files = glob.glob(f"{output_path}/*.tex")
for tex in tex_files:
cmd = " ".join(["pdflatex", f"-output-directory {output_path}/data/", tex])
cmd = " ".join(["pdflatex", f"-output-directory {output_path}/", tex, "> /dev/null 2>&1"])
os.system(cmd)


Expand Down
74 changes: 45 additions & 29 deletions infra/nightly-resources/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ async function load_index() {

// Top-level load function for the llvm page
async function load_llvm() {
await loadCommonData();
const params = new URLSearchParams(window.location.search);
const benchmark = params.get("benchmark");
const runMode = params.get("runmode");

if (!benchmark || !runMode) {
console.error("missing query params, this probably shouldn't happen");
return;
}
const llvm = await fetchText(`./data/llvm/${benchmark}-${runMode}.ll`);
document.getElementById("llvm").innerText = llvm;
showIR(benchmark, runMode);
showCFGs(benchmark, runMode);
}

async function load_table() {
Expand Down Expand Up @@ -99,32 +100,6 @@ async function loadBaseline(url) {
refreshView();
}

function toggleWarnings() {
const elt = document.getElementById("warnings-toggle");
elt.classList.toggle("active");
const content = elt.nextElementSibling;
if (content.style.display === "block") {
elt.innerText = `Show ${GLOBAL_DATA.warnings.size} Warnings`;
content.style.display = "none";
} else {
elt.innerText = `Hide ${GLOBAL_DATA.warnings.size} Warnings`;
content.style.display = "block";
}
}

function toggleChart() {
const elt = document.getElementById("chart-toggle");
elt.classList.toggle("active");
const content = elt.nextElementSibling;
if (content.style.display === "block") {
elt.innerText = "Show Chart";
content.style.display = "none";
} else {
elt.innerText = "Hide Chart";
content.style.display = "block";
}
}

function onRadioClick(elt) {
GLOBAL_DATA.chart.mode = elt.value;
document.getElementById("speedup-formula").style.visibility =
Expand All @@ -135,3 +110,44 @@ function onRadioClick(elt) {
function copyToClipboard(eltId) {
navigator.clipboard.writeText(document.getElementById(eltId).innerText);
}

function expand(elt, labelElt, text) {
elt.classList.add("expanded");
elt.classList.remove("collapsed");
labelElt.innerText = text;
}

function collapse(elt, labelElt, text) {
elt.classList.add("collapsed");
elt.classList.remove("expanded");
labelElt.innerText = text;
}

function toggle(elt, showText, hideText) {
const content = elt.nextElementSibling;
if (content.classList.contains("expanded")) {
collapse(content, elt, showText);
} else {
expand(content, elt, hideText);
}
}

function toggleAllPngs(elt) {
const btns = Array.from(document.getElementsByClassName("pngToggle"));

if (elt.innerText == "Expand All") {
elt.innerText = "Collapse All";
btns.forEach((btn) => {
const txt = btn.innerText.replace("\u25B6 Show", "\u25BC Hide");
const content = btn.nextElementSibling;
expand(content, btn, txt);
});
} else {
elt.innerText = "Expand All";
btns.forEach((btn) => {
const txt = btn.innerText.replace("\u25BC Hide", "\u25B6 Show");
const content = btn.nextElementSibling;
collapse(content, btn, txt);
});
}
}
28 changes: 17 additions & 11 deletions infra/nightly-resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
<script src="./Plugin.Errorbars.js"></script>
<script src="chart.js"></script>
<script src="data.js"></script>
<script src="index.js"></script>
<script src="handlers.js"></script>
<script src="index.js"></script>
<script src="previousRuns.js"></script>
<script src="table.js"></script>
<script src="utils.js"></script>
<script src="data/profile.json"></script>
</head>

<title>Nightlies</title>
Expand All @@ -22,22 +21,29 @@ <h2>Nightlies</h2>
<h2>Profile</h2>

<div>
<button type="button" class="collapsible" id="chart-toggle" onclick="toggleChart()">Hide Chart</button>
<input onclick="onRadioClick(this);" checked type="radio" id="absolute" name="chart-type" value="absolute">
<label for="absolute">Absolute</label>
<input onclick="onRadioClick(this);" type="radio" id="speedup" name="chart-type" value="speedup">
<label for="speedup">Speedup</label><br>
<p id="speedup-formula">Speedup = (LLVM-O0 TIME / RUN MODE TIME)</p>
<canvas class="content" id="chart"></canvas>
<button type="button" class="collapsible" onclick="toggle(this, '\u25B6 Show Chart', '\u25BC Hide Chart')">&#x25BC; Hide Chart</button>
<div class="expanded">
<input onclick="onRadioClick(this);" checked type="radio" id="absolute" name="chart-type" value="absolute">
<label for="absolute">Absolute</label>
<input onclick="onRadioClick(this);" type="radio" id="speedup" name="chart-type" value="speedup">
<label for="speedup">Speedup</label><br>
<p id="speedup-formula">Speedup = (LLVM-O0 TIME / RUN MODE TIME)</p>
<canvas class="content" id="chart"></canvas>
</div>
</div>

<div>
<label for="comparison">Compare To:</label>
<select id="comparison"></select>
</div>

<button type="button" class="collapsible" id="warnings-toggle" onclick="toggleWarnings()"></button>
<div class="content" id="warnings"></div>
<button
type="button"
class="collapsible"
id="warnings-toggle"
onclick="toggle(this, `\u25B6 Show ${GLOBAL_DATA.warnings.size} Warnings`, `\u25BC Hide ${GLOBAL_DATA.warnings.size} Warnings`)">
</button>
<div class="content collapsed" id="warnings"></div>

<div class="filter-container">
<div class="filters">
Expand Down
2 changes: 1 addition & 1 deletion infra/nightly-resources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function refreshView() {

function renderWarnings() {
const toggle = document.getElementById("warnings-toggle");
toggle.innerText = `Show ${GLOBAL_DATA.warnings.size} Warnings`;
toggle.innerText = `\u25B6 Show ${GLOBAL_DATA.warnings.size} Warnings`;

const warningContainer = document.getElementById("warnings");
warningContainer.innerHTML = "";
Expand Down
15 changes: 6 additions & 9 deletions infra/nightly-resources/llvm.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
<html>

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
<script src="./Plugin.Errorbars.js"></script>
<script src="chart.js"></script>
<script src="data.js"></script>
<script src="index.js"></script>
<script src="handlers.js"></script>
<script src="previousRuns.js"></script>
<script src="table.js"></script>
<script src="utils.js"></script>
<script src="data/profile.json"></script>
<script src="index.js"></script>
<script src="llvm.js"></script>
</head>

<title>LLVM</title>

<body onload="load_llvm()">
<h2>LLVM</h2>
<div id="llvm"></div>
<button class="collapsible" onclick="toggleAllPngs(this)">Expand All</button>
<div id="llvm-cfg"></div>
<button type="button" class="collapsible" onclick="toggle(this, '\u25B6 Show LLVM', '\u25BC Hide LLVM')">\u25BC Hide LLVM</button>
<div id="llvm-ir" style="display: block;"></div>
</body>

</html>
46 changes: 46 additions & 0 deletions infra/nightly-resources/llvm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
async function showIR(benchmark, runMode) {
const llvm = await fetchText(
`./data/llvm/${benchmark}/${runMode}/${benchmark}-${runMode}.ll`,
);
document.getElementById("llvm-ir").innerText = llvm;
}

async function showCFGs(benchmark, runMode) {
let pngs = (
await fetchText(`./data/llvm/${benchmark}/${runMode}/png_names.txt`)
).split("\n");

// Move main.png and _main.png to top
const _main = "_main.png";
if (pngs.includes(_main)) {
pngs = pngs.filter((x) => x !== _main);
pngs.unshift(_main);
}
const main = "main.png";
if (pngs.includes(main)) {
pngs = pngs.filter((x) => x !== main);
pngs.unshift(main);
}

const pngContainer = document.getElementById("llvm-cfg");
pngs.forEach((png) => {
const elt = document.createElement("div");

const btn = document.createElement("button");
btn.innerText = `\u25B6 Show ${png}`;
btn.classList.add("collapsible");
btn.classList.add("pngToggle");
btn.onclick = (elt) =>
toggle(elt.target, `\u25B6 Show ${png}`, `\u25BC Hide ${png}`);

elt.appendChild(btn);

const img = document.createElement("img");
img.classList.add("cfg");
img.classList.add("collapsed");
img.src = `data/llvm/${benchmark}/${runMode}/${png}`;
elt.appendChild(img);

pngContainer.appendChild(elt);
});
}
23 changes: 17 additions & 6 deletions infra/nightly-resources/stylesheet.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ body {
}

.collapsible {
background-color: #777;
color: white;
background-color: white;
cursor: pointer;
padding: 18px;
width: 100%;
padding: 10px 0px;
border: none;
text-align: left;
outline: none;
font-size: 15px;
margin: 10px 0px;
font-weight: normal;
}

.active, .collapsible:hover {
background-color: #555;
font-weight: bold;
}

.content {
Expand All @@ -58,4 +56,17 @@ body {

#speedup-formula {
visibility: hidden;
}

.cfg {
max-height: 600px;
display: block;
}

.expanded {
display: block;
}

.collapsed {
display: none;
}
6 changes: 0 additions & 6 deletions infra/nightly-resources/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
<html>

<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.bundle.min.js"></script>
<script src="./Plugin.Errorbars.js"></script>
<script src="chart.js"></script>
<script src="data.js"></script>
<script src="handlers.js"></script>
<script src="index.js"></script>
<script src="previousRuns.js"></script>
<script src="table.js"></script>
<script src="utils.js"></script>
<script src="data/profile.json"></script>
</head>

<title>TABLE</title>
Expand Down
Loading

0 comments on commit 81cdeef

Please sign in to comment.