Skip to content

Commit 2f80d63

Browse files
authored
Add version field do JsonExport (#3614)
also fixes python scripts for latest version
1 parent bfa6137 commit 2f80d63

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

autoprecompiles/scripts/plot_effectiveness.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_before_after_cost(item, eff_type):
3131
'cost_before': cost_before * item['execution_frequency'],
3232
'cost_after': cost_after * item['execution_frequency'],
3333
'effectiveness': cost_before / cost_after,
34-
'instructions': len(item['original_block']['statements']),
34+
'instructions': len(item['original_block']['instructions']),
3535
})
3636

3737
return pd.DataFrame(rows)

autoprecompiles/scripts/rank_apc_candidates.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def main():
4343
for i, candidate in enumerate(data):
4444
start_pc = candidate["original_block"]["start_pc"]
4545
freq = candidate["execution_frequency"]
46-
num_statements = len(candidate["original_block"]["statements"])
46+
num_instructions = len(candidate["original_block"]["instructions"])
4747

4848
# Get optimization stats
4949
before_constraints = candidate["stats"]["before"]["constraints"]
@@ -69,7 +69,7 @@ def main():
6969
'index': i + 1,
7070
'start_pc': start_pc,
7171
'freq': freq,
72-
'num_statements': num_statements,
72+
'num_instructions': num_instructions,
7373
'before_constraints': before_constraints,
7474
'after_constraints': after_constraints,
7575
'before_main_columns': before_main_columns,
@@ -96,7 +96,7 @@ def main():
9696
output_lines.append("=" * 120)
9797

9898
total_candidates = len(data)
99-
total_statements = sum(len(c["original_block"]["statements"]) for c in data)
99+
total_instructions = sum(len(c["original_block"]["instructions"]) for c in data)
100100

101101
total_cost_before = sum(c["cost_before"] for c in data)
102102
total_cost_after = sum(c["cost_after"] for c in data)
@@ -115,8 +115,8 @@ def main():
115115
total_bus_interactions_improvement_factor = total_before_bus_interactions / total_after_bus_interactions
116116

117117
output_lines.append(f"# of APC Candidates: {total_candidates}")
118-
output_lines.append(f"Sum of Instructions: {total_statements}")
119-
output_lines.append(f"Average Instructions per APC Candidate: {total_statements / total_candidates:.1f}")
118+
output_lines.append(f"Sum of Instructions: {total_instructions}")
119+
output_lines.append(f"Average Instructions per APC Candidate: {total_instructions / total_candidates:.1f}")
120120
output_lines.append("")
121121
output_lines.append(f"Sum of Cost: {total_cost_before}{total_cost_after} ({total_cost_improvement_factor:.2f}x reduction)")
122122
output_lines.append(f"Sum of Main Columns: {total_before_main_columns}{total_after_main_columns} ({main_columns_improvement_factor:.2f}x reduction)")
@@ -126,7 +126,7 @@ def main():
126126
# Statement count distribution
127127
stmt_dist = {}
128128
for c in data:
129-
stmt_count = len(c["original_block"]["statements"])
129+
stmt_count = len(c["original_block"]["instructions"])
130130
stmt_dist[stmt_count] = stmt_dist.get(stmt_count, 0) + 1
131131

132132
output_lines.append("")
@@ -177,7 +177,7 @@ def main():
177177
row = [
178178
i + 1,
179179
f"{candidate['start_pc']:.0f}",
180-
candidate['num_statements'],
180+
candidate['num_instructions'],
181181
f"{candidate['freq']}x",
182182
f"{candidate['value']:.0f}",
183183
f"{candidate['cost_before']:.0f} -> {candidate['cost_after']:.0f} ({candidate['cost_improvement_factor']:.1f}x)",

autoprecompiles/src/pgo/cell/mod.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub trait Candidate<A: Adapter>: Sized + KnapsackItem {
3939
}
4040

4141
#[derive(Serialize, Deserialize)]
42+
/// NOTE: When making changes to this field or any of the contained types,
43+
/// JSON_EXPORT_VERSION must be updated
4244
pub struct ApcCandidateJsonExport {
4345
// execution_frequency
4446
pub execution_frequency: usize,
@@ -77,12 +79,27 @@ impl<A, C> CellPgo<A, C> {
7779
}
7880
}
7981

82+
/// This version is used by external tools to support multiple versions of the json export.
83+
/// Version should be incremented whenever a breaking change is made to the type (or inner types).
84+
const JSON_EXPORT_VERSION: usize = 2;
85+
8086
#[derive(Serialize, Deserialize)]
8187
struct JsonExport {
88+
version: usize,
8289
apcs: Vec<ApcCandidateJsonExport>,
8390
labels: BTreeMap<u64, Vec<String>>,
8491
}
8592

93+
impl JsonExport {
94+
fn new(apcs: Vec<ApcCandidateJsonExport>, labels: BTreeMap<u64, Vec<String>>) -> Self {
95+
Self {
96+
version: JSON_EXPORT_VERSION,
97+
apcs,
98+
labels,
99+
}
100+
}
101+
}
102+
86103
impl<A: Adapter + Send + Sync, C: Candidate<A> + Send + Sync> PgoAdapter for CellPgo<A, C> {
87104
type Adapter = A;
88105

@@ -159,7 +176,7 @@ impl<A: Adapter + Send + Sync, C: Candidate<A> + Send + Sync> PgoAdapter for Cel
159176
// Write the APC candidates JSON to disk if the directory is specified.
160177
if let Some(apc_candidates_dir_path) = &config.apc_candidates_dir_path {
161178
let apcs = apc_candidates.lock().unwrap().drain(..).collect();
162-
let json = JsonExport { apcs, labels };
179+
let json = JsonExport::new(apcs, labels);
163180
let json_path = apc_candidates_dir_path.join("apc_candidates.json");
164181
let file = std::fs::File::create(&json_path)
165182
.expect("Failed to create file for APC candidates JSON");

0 commit comments

Comments
 (0)