Skip to content

Commit 0854775

Browse files
authored
Couple of optimizations to the Cranelift incremental cache (#11186)
* Fix a couple of comments * Remove flags.predicate_view() It is a remenant of the old backend framework. * Avoid string conversions for hashing the TargetIsa * Remove func_body_len It is identical to buffer.data.len() * Introduce IsaFlagsHashKey
1 parent 78d5940 commit 0854775

File tree

11 files changed

+60
-74
lines changed

11 files changed

+60
-74
lines changed

cranelift/codegen/meta/src/gen_settings.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,6 @@ fn gen_getters(group: &SettingGroup, fmt: &mut Formatter) {
208208
fmt.doc_comment("User-defined settings.");
209209
fmtln!(fmt, "#[allow(dead_code, reason = \"generated code\")]");
210210
fmt.add_block("impl Flags", |fmt| {
211-
fmt.doc_comment("Get a view of the boolean predicates.");
212-
fmt.add_block(
213-
"pub fn predicate_view(&self) -> crate::settings::PredicateView<'_>",
214-
|fmt| {
215-
fmtln!(
216-
fmt,
217-
"crate::settings::PredicateView::new(&self.bytes[{}..])",
218-
group.bool_start_byte_offset
219-
);
220-
},
221-
);
222-
223211
if !group.settings.is_empty() {
224212
fmt.doc_comment("Dynamic numbered predicate getter.");
225213
fmt.add_block("fn numbered_predicate(&self, p: usize) -> bool", |fmt| {
@@ -424,9 +412,18 @@ fn gen_display(group: &SettingGroup, fmt: &mut Formatter) {
424412
});
425413
}
426414

415+
fn gen_hash_key(fmt: &mut Formatter) {
416+
fmt.add_block("impl Flags", |fmt| {
417+
fmt.doc_comment("Get the flag values as raw bytes for hashing.");
418+
fmt.add_block("pub fn hash_key(&self) -> &[u8]", |fmt| {
419+
fmtln!(fmt, "&self.bytes");
420+
});
421+
});
422+
}
423+
427424
fn gen_group(group: &SettingGroup, parent: ParentGroup, fmt: &mut Formatter) {
428425
// Generate struct.
429-
fmtln!(fmt, "#[derive(Clone, Hash)]");
426+
fmtln!(fmt, "#[derive(Clone, PartialEq, Hash)]");
430427
fmt.doc_comment(format!("Flags group `{}`.", group.name));
431428
fmt.add_block("pub struct Flags", |fmt| {
432429
fmtln!(fmt, "bytes: [u8; {}],", group.byte_size());
@@ -439,6 +436,7 @@ fn gen_group(group: &SettingGroup, parent: ParentGroup, fmt: &mut Formatter) {
439436
gen_descriptors(group, fmt);
440437
gen_template(group, fmt);
441438
gen_display(group, fmt);
439+
gen_hash_key(fmt);
442440
}
443441

444442
pub(crate) fn generate(

cranelift/codegen/src/incremental_cache.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@
2121
//! the above three primitives to form a full incremental caching system.
2222
2323
use core::fmt;
24+
use core::hash::{Hash, Hasher};
2425

25-
use crate::alloc::string::String;
2626
use crate::alloc::vec::Vec;
2727
use crate::ir::Function;
2828
use crate::ir::function::{FunctionStencil, VersionMarker};
2929
use crate::machinst::{CompiledCode, CompiledCodeStencil};
3030
use crate::result::CompileResult;
3131
use crate::{CompileError, Context, trace};
3232
use crate::{isa::TargetIsa, timing};
33-
use alloc::borrow::{Cow, ToOwned as _};
34-
use alloc::string::ToString as _;
33+
use alloc::borrow::Cow;
3534
use cranelift_control::ControlPlane;
3635

3736
impl Context {
@@ -133,43 +132,29 @@ struct CachedFunc {
133132
///
134133
/// Note: the key will be invalidated across different versions of cranelift, as the
135134
/// `FunctionStencil` contains a `VersionMarker` itself.
136-
#[derive(Hash)]
137135
struct CacheKey<'a> {
138136
stencil: &'a FunctionStencil,
139-
parameters: CompileParameters,
137+
isa: &'a dyn TargetIsa,
140138
}
141139

142-
#[derive(Clone, PartialEq, Hash, serde_derive::Serialize, serde_derive::Deserialize)]
143-
struct CompileParameters {
144-
isa: String,
145-
triple: String,
146-
flags: String,
147-
isa_flags: Vec<String>,
148-
}
149-
150-
impl CompileParameters {
151-
fn from_isa(isa: &dyn TargetIsa) -> Self {
152-
Self {
153-
isa: isa.name().to_owned(),
154-
triple: isa.triple().to_string(),
155-
flags: isa.flags().to_string(),
156-
isa_flags: isa
157-
.isa_flags()
158-
.into_iter()
159-
.map(|v| v.value_string())
160-
.collect(),
161-
}
140+
impl<'a> Hash for CacheKey<'a> {
141+
fn hash<H: Hasher>(&self, state: &mut H) {
142+
self.stencil.hash(state);
143+
self.isa.name().hash(state);
144+
self.isa.triple().hash(state);
145+
self.isa.flags().hash(state);
146+
self.isa.isa_flags_hash_key().hash(state);
162147
}
163148
}
164149

165150
impl<'a> CacheKey<'a> {
166151
/// Creates a new cache store key for a function.
167152
///
168153
/// This is a bit expensive to compute, so it should be cached and reused as much as possible.
169-
fn new(isa: &dyn TargetIsa, f: &'a Function) -> Self {
154+
fn new(isa: &'a dyn TargetIsa, f: &'a Function) -> Self {
170155
CacheKey {
171156
stencil: &f.stencil,
172-
parameters: CompileParameters::from_isa(isa),
157+
isa,
173158
}
174159
}
175160
}

cranelift/codegen/src/isa/aarch64/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ir::{self, Function, Type};
55
use crate::isa::aarch64::settings as aarch64_settings;
66
#[cfg(feature = "unwind")]
77
use crate::isa::unwind::systemv;
8-
use crate::isa::{Builder as IsaBuilder, FunctionAlignment, TargetIsa};
8+
use crate::isa::{Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey, TargetIsa};
99
use crate::machinst::{
1010
CompiledCode, CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet,
1111
TextSectionBuilder, VCode, compile,
@@ -112,6 +112,10 @@ impl TargetIsa for AArch64Backend {
112112
self.isa_flags.iter().collect()
113113
}
114114

115+
fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
116+
IsaFlagsHashKey(self.isa_flags.hash_key())
117+
}
118+
115119
fn is_branch_protection_enabled(&self) -> bool {
116120
self.isa_flags.use_bti()
117121
}

cranelift/codegen/src/isa/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ pub trait TargetIsa: fmt::Display + Send + Sync {
294294
/// Get the ISA-dependent flag values that were used to make this trait object.
295295
fn isa_flags(&self) -> Vec<settings::Value>;
296296

297+
/// Get the ISA-dependent flag values as raw bytes for hashing.
298+
fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_>;
299+
297300
/// Get a flag indicating whether branch protection is enabled.
298301
fn is_branch_protection_enabled(&self) -> bool {
299302
false
@@ -415,6 +418,10 @@ pub trait TargetIsa: fmt::Display + Send + Sync {
415418
fn default_argument_extension(&self) -> ir::ArgumentExtension;
416419
}
417420

421+
/// A wrapper around the ISA-dependent flags types which only implements `Hash`.
422+
#[derive(Hash)]
423+
pub struct IsaFlagsHashKey<'a>(&'a [u8]);
424+
418425
/// Function alignment specifications as required by an ISA, returned by
419426
/// [`TargetIsa::function_alignment`].
420427
#[derive(Copy, Clone)]

cranelift/codegen/src/isa/pulley_shared/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
MachTextSectionBuilder, TextSectionBuilder,
1212
dominator_tree::DominatorTree,
1313
ir,
14-
isa::{self, OwnedTargetIsa, TargetIsa},
14+
isa::{self, IsaFlagsHashKey, OwnedTargetIsa, TargetIsa},
1515
machinst::{self, CompiledCodeStencil, MachInst, SigSet, VCode},
1616
result::CodegenResult,
1717
settings::{self as shared_settings, Flags},
@@ -155,6 +155,10 @@ where
155155
self.isa_flags.iter().collect()
156156
}
157157

158+
fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
159+
IsaFlagsHashKey(self.isa_flags.hash_key())
160+
}
161+
158162
fn dynamic_vector_bytes(&self, _dynamic_ty: ir::Type) -> u32 {
159163
512
160164
}

cranelift/codegen/src/isa/riscv64/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
use crate::dominator_tree::DominatorTree;
44
use crate::ir::{Function, Type};
55
use crate::isa::riscv64::settings as riscv_settings;
6-
use crate::isa::{Builder as IsaBuilder, FunctionAlignment, OwnedTargetIsa, TargetIsa};
6+
use crate::isa::{
7+
Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey, OwnedTargetIsa, TargetIsa,
8+
};
79
use crate::machinst::{
810
CompiledCode, CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet,
911
TextSectionBuilder, VCode, compile,
@@ -114,6 +116,10 @@ impl TargetIsa for Riscv64Backend {
114116
self.isa_flags.iter().collect()
115117
}
116118

119+
fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
120+
IsaFlagsHashKey(self.isa_flags.hash_key())
121+
}
122+
117123
#[cfg(feature = "unwind")]
118124
fn emit_unwind_info(
119125
&self,

cranelift/codegen/src/isa/s390x/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::ir::{self, Function, Type};
55
use crate::isa::s390x::settings as s390x_settings;
66
#[cfg(feature = "unwind")]
77
use crate::isa::unwind::systemv::RegisterMappingError;
8-
use crate::isa::{Builder as IsaBuilder, FunctionAlignment, TargetIsa};
8+
use crate::isa::{Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey, TargetIsa};
99
use crate::machinst::{
1010
CompiledCode, CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet,
1111
TextSectionBuilder, VCode, compile,
@@ -112,6 +112,10 @@ impl TargetIsa for S390xBackend {
112112
self.isa_flags.iter().collect()
113113
}
114114

115+
fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
116+
IsaFlagsHashKey(self.isa_flags.hash_key())
117+
}
118+
115119
fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {
116120
16
117121
}

cranelift/codegen/src/isa/x64/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::ir::{self, Function, Type, types};
88
#[cfg(feature = "unwind")]
99
use crate::isa::unwind::systemv;
1010
use crate::isa::x64::settings as x64_settings;
11-
use crate::isa::{Builder as IsaBuilder, FunctionAlignment};
11+
use crate::isa::{Builder as IsaBuilder, FunctionAlignment, IsaFlagsHashKey};
1212
use crate::machinst::{
1313
CompiledCode, CompiledCodeStencil, MachInst, MachTextSectionBuilder, Reg, SigSet,
1414
TextSectionBuilder, VCode, compile,
@@ -104,6 +104,10 @@ impl TargetIsa for X64Backend {
104104
self.x64_flags.iter().collect()
105105
}
106106

107+
fn isa_flags_hash_key(&self) -> IsaFlagsHashKey<'_> {
108+
IsaFlagsHashKey(self.x64_flags.hash_key())
109+
}
110+
107111
fn dynamic_vector_bytes(&self, _dyn_ty: Type) -> u32 {
108112
16
109113
}

cranelift/codegen/src/machinst/buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1977,7 +1977,7 @@ pub struct MachSrcLoc<T: CompilePhase> {
19771977
/// section.
19781978
pub start: CodeOffset,
19791979
/// The end of the region of code corresponding to a source location.
1980-
/// This is relative to the start of the section, not to the start of the
1980+
/// This is relative to the start of the function, not to the start of the
19811981
/// section.
19821982
pub end: CodeOffset,
19831983
/// The source location.

cranelift/codegen/src/machinst/vcode.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,13 @@ pub struct EmitResult {
208208
pub buffer: MachBufferFinalized<Stencil>,
209209

210210
/// Offset of each basic block, recorded during emission. Computed
211-
/// only if `debug_value_labels` is non-empty.
211+
/// only if `machine_code_cfg_info` is enabled.
212212
pub bb_offsets: Vec<CodeOffset>,
213213

214214
/// Final basic-block edges, in terms of code offsets of
215-
/// bb-starts. Computed only if `debug_value_labels` is non-empty.
215+
/// bb-starts. Computed only if `machine_code_cfg_info` is enabled.
216216
pub bb_edges: Vec<(CodeOffset, CodeOffset)>,
217217

218-
/// Final length of function body.
219-
pub func_body_len: CodeOffset,
220-
221218
/// The pretty-printed disassembly, if any. This uses the same
222219
/// pretty-printing for MachInsts as the pre-regalloc VCode Debug
223220
/// implementation, but additionally includes the prologue and
@@ -1110,7 +1107,6 @@ impl<I: VCodeInst> VCode<I> {
11101107
buffer: buffer.finish(&self.constants, ctrl_plane),
11111108
bb_offsets,
11121109
bb_edges,
1113-
func_body_len,
11141110
disasm: if want_disasm { Some(disasm) } else { None },
11151111
sized_stackslot_offsets: self.abi.sized_stackslot_offsets().clone(),
11161112
dynamic_stackslot_offsets: self.abi.dynamic_stackslot_offsets().clone(),

0 commit comments

Comments
 (0)