Skip to content

Commit bf8061e

Browse files
committed
[WIP] Introduce per-function caching
1 parent 59a7c8b commit bf8061e

File tree

4 files changed

+189
-2
lines changed

4 files changed

+189
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
perf.data
1616
perf.data.old
1717
*.mm_profdata
18+
19+
/cache

Cargo.lock

Lines changed: 137 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ crate-type = ["dylib"]
88

99
[dependencies]
1010
# These have to be in sync with each other
11-
cranelift-codegen = { version = "0.121.0", default-features = false, features = ["std", "timing", "unwind", "all-native-arch"] }
11+
cranelift-codegen = { version = "0.121.0", default-features = false, features = ["std", "timing", "unwind", "all-native-arch", "incremental-cache"] }
1212
cranelift-frontend = { version = "0.121.0" }
1313
cranelift-module = { version = "0.121.0" }
1414
cranelift-native = { version = "0.121.0" }

src/unwind_module.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use cranelift_codegen::Context;
22
use cranelift_codegen::control::ControlPlane;
3+
use cranelift_codegen::incremental_cache::CacheKvStore;
34
use cranelift_codegen::ir::Signature;
45
use cranelift_codegen::isa::{TargetFrontendConfig, TargetIsa};
56
use cranelift_module::{
@@ -90,7 +91,28 @@ impl<T: Module> Module for UnwindModule<T> {
9091
ctx: &mut Context,
9192
ctrl_plane: &mut ControlPlane,
9293
) -> ModuleResult<()> {
93-
self.module.define_function_with_control_plane(func, ctx, ctrl_plane)?;
94+
if std::env::var("CG_CLIF_FUNCTION_CACHE").as_deref() == Ok("naive") {
95+
if ctx.func.layout.blocks().nth(1).is_none()
96+
|| ctx.func.layout.blocks().nth(2).is_none()
97+
{
98+
ctx.compile(self.module.isa(), ctrl_plane)?;
99+
} else {
100+
ctx.compile_with_cache(self.module.isa(), &mut Cache, ctrl_plane)?;
101+
}
102+
} else {
103+
ctx.compile(self.module.isa(), ctrl_plane)?;
104+
}
105+
let res = ctx.compiled_code().unwrap();
106+
107+
let alignment = res.buffer.alignment as u64;
108+
let relocs = res
109+
.buffer
110+
.relocs()
111+
.iter()
112+
.map(|reloc| ModuleReloc::from_mach_reloc(&reloc, &ctx.func, func))
113+
.collect::<Vec<_>>();
114+
self.module.define_function_bytes(func, alignment, res.buffer.data(), &relocs)?;
115+
94116
self.unwind_context.add_function(&mut self.module, func, ctx);
95117
Ok(())
96118
}
@@ -109,3 +131,29 @@ impl<T: Module> Module for UnwindModule<T> {
109131
self.module.define_data(data_id, data)
110132
}
111133
}
134+
135+
struct Cache;
136+
137+
impl Cache {
138+
fn file_for_key(&self, key: &[u8]) -> String {
139+
let mut path = key.iter().map(|b| format!("{:02x}", b)).collect::<String>();
140+
path.push_str(".clif_cache");
141+
"/home/bjorn/Projects/cg_clif/cache/".to_owned() + &path
142+
}
143+
}
144+
145+
impl CacheKvStore for Cache {
146+
fn get(&self, key: &[u8]) -> Option<std::borrow::Cow<'_, [u8]>> {
147+
let path = self.file_for_key(key);
148+
if std::fs::exists(&path).unwrap() {
149+
Some(std::fs::read(path).unwrap().into())
150+
} else {
151+
None
152+
}
153+
}
154+
155+
fn insert(&mut self, key: &[u8], val: Vec<u8>) {
156+
let path = self.file_for_key(key);
157+
std::fs::write(path, val).unwrap();
158+
}
159+
}

0 commit comments

Comments
 (0)