Skip to content

Commit abf157b

Browse files
committed
machinst x64: Only use the feature flag to enable the x64 new backend;
Before this patch, running the x64 new backend would require both compiling with --features experimental_x64 and running with `use_new_backend`. This patches changes this behavior so that the runtime flag is not needed anymore: using the feature flag will enforce usage of the new backend everywhere, making using and testing it much simpler: cargo run --features experimental_x64 ;; other CLI options/flags This also gives a hint at what the meta language generation would look like after switching to the new backend. Compiling only with the x64 codegen flag gives a nice compile time speedup.
1 parent bc1e960 commit abf157b

File tree

14 files changed

+134
-44
lines changed

14 files changed

+134
-44
lines changed

cranelift/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ default = ["disas", "wasm", "cranelift-codegen/all-arch"]
4848
disas = ["capstone"]
4949
enable-peepmatic = ["cranelift-codegen/enable-peepmatic", "cranelift-filetests/enable-peepmatic"]
5050
wasm = ["wat", "cranelift-wasm"]
51+
experimental_x64 = ["cranelift-codegen/x64"]

cranelift/codegen/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ x64 = [] # New work-in-progress codegen backend for x86_64 based on the new isel
6666
# Option to enable all architectures.
6767
all-arch = [
6868
"x86",
69-
"x64",
7069
"arm32",
7170
"arm64",
7271
"riscv"

cranelift/codegen/build.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ fn main() {
2626
let out_dir = env::var("OUT_DIR").expect("The OUT_DIR environment variable must be set");
2727
let target_triple = env::var("TARGET").expect("The TARGET environment variable must be set");
2828

29-
// Configure isa targets cfg.
29+
let new_backend_isas = if env::var("CARGO_FEATURE_X64").is_ok() {
30+
// The x64 (new backend for x86_64) is a bit particular: it only requires generating
31+
// the shared meta code; the only ISA-specific code is for settings.
32+
vec![meta::isa::Isa::X86]
33+
} else {
34+
Vec::new()
35+
};
36+
37+
// Configure isa targets using the old backend.
3038
let isa_targets = meta::isa::Isa::all()
3139
.iter()
3240
.cloned()
@@ -36,7 +44,7 @@ fn main() {
3644
})
3745
.collect::<Vec<_>>();
3846

39-
let isas = if isa_targets.is_empty() {
47+
let old_backend_isas = if new_backend_isas.is_empty() && isa_targets.is_empty() {
4048
// Try to match native target.
4149
let target_name = target_triple.split('-').next().unwrap();
4250
let isa = meta::isa_from_arch(&target_name).expect("error when identifying target");
@@ -56,14 +64,23 @@ fn main() {
5664
crate_dir.join("build.rs").to_str().unwrap()
5765
);
5866

59-
if let Err(err) = meta::generate(&isas, &out_dir) {
67+
if let Err(err) = meta::generate(&old_backend_isas, &new_backend_isas, &out_dir) {
6068
eprintln!("Error: {}", err);
6169
process::exit(1);
6270
}
6371

6472
if env::var("CRANELIFT_VERBOSE").is_ok() {
65-
for isa in &isas {
66-
println!("cargo:warning=Includes support for {} ISA", isa.to_string());
73+
for isa in &old_backend_isas {
74+
println!(
75+
"cargo:warning=Includes old-backend support for {} ISA",
76+
isa.to_string()
77+
);
78+
}
79+
for isa in &new_backend_isas {
80+
println!(
81+
"cargo:warning=Includes new-backend support for {} ISA",
82+
isa.to_string()
83+
);
6784
}
6885
println!(
6986
"cargo:warning=Build step took {:?}.",

cranelift/codegen/meta/src/gen_legalizer.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ fn gen_isa(
700700
pub(crate) fn generate(
701701
isas: &[TargetIsa],
702702
transform_groups: &TransformGroups,
703+
extra_legalization_groups: &[&'static str],
703704
filename_prefix: &str,
704705
out_dir: &str,
705706
) -> Result<(), error::Error> {
@@ -711,8 +712,14 @@ pub(crate) fn generate(
711712
fmt.update_file(format!("{}-{}.rs", filename_prefix, isa.name), out_dir)?;
712713
}
713714

715+
// Add extra legalization groups that were explicitly requested.
716+
for group in extra_legalization_groups {
717+
shared_group_names.insert(group);
718+
}
719+
714720
// Generate shared legalize groups.
715721
let mut fmt = Formatter::new();
722+
// Generate shared legalize groups.
716723
let mut type_sets = UniqueTable::new();
717724
let mut sorted_shared_group_names = Vec::from_iter(shared_group_names);
718725
sorted_shared_group_names.sort();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use std::fmt;
66
mod arm32;
77
mod arm64;
88
mod riscv;
9-
mod x86;
9+
pub(crate) mod x86;
1010

1111
/// Represents known ISA target.
12-
#[derive(Copy, Clone)]
12+
#[derive(PartialEq, Copy, Clone)]
1313
pub enum Isa {
1414
Riscv,
1515
X86,

cranelift/codegen/meta/src/isa/x86/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod legalize;
1414
mod opcodes;
1515
mod recipes;
1616
mod registers;
17-
mod settings;
17+
pub(crate) mod settings;
1818

1919
pub(crate) fn define(shared_defs: &mut SharedDefinitions) -> TargetIsa {
2020
let settings = settings::define(&shared_defs.settings);

cranelift/codegen/meta/src/isa/x86/settings.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ use crate::cdsl::settings::{PredicateNode, SettingGroup, SettingGroupBuilder};
33
pub(crate) fn define(shared: &SettingGroup) -> SettingGroup {
44
let mut settings = SettingGroupBuilder::new("x86");
55

6-
settings.add_bool(
7-
"use_new_backend",
8-
"Whether to use the new codegen backend using the new isel",
9-
false,
10-
);
11-
126
// CPUID.01H:ECX
137
let has_sse3 = settings.add_bool("has_sse3", "SSE3: CPUID.01H:ECX.SSE3[bit 0]", false);
148
let has_ssse3 = settings.add_bool("has_ssse3", "SSSE3: CPUID.01H:ECX.SSSE3[bit 9]", false);

cranelift/codegen/meta/src/lib.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ pub fn isa_from_arch(arch: &str) -> Result<isa::Isa, String> {
2525
}
2626

2727
/// Generates all the Rust source files used in Cranelift from the meta-language.
28-
pub fn generate(isas: &[isa::Isa], out_dir: &str) -> Result<(), error::Error> {
28+
pub fn generate(
29+
old_backend_isas: &[isa::Isa],
30+
new_backend_isas: &[isa::Isa],
31+
out_dir: &str,
32+
) -> Result<(), error::Error> {
2933
// Create all the definitions:
3034
// - common definitions.
3135
let mut shared_defs = shared::define();
@@ -39,7 +43,7 @@ pub fn generate(isas: &[isa::Isa], out_dir: &str) -> Result<(), error::Error> {
3943
gen_types::generate("types.rs", &out_dir)?;
4044

4145
// - per ISA definitions.
42-
let isas = isa::define(isas, &mut shared_defs);
46+
let target_isas = isa::define(old_backend_isas, &mut shared_defs);
4347

4448
// At this point, all definitions are done.
4549
let all_formats = shared_defs.verify_instruction_formats();
@@ -53,9 +57,22 @@ pub fn generate(isas: &[isa::Isa], out_dir: &str) -> Result<(), error::Error> {
5357
&out_dir,
5458
)?;
5559

56-
gen_legalizer::generate(&isas, &shared_defs.transform_groups, "legalize", &out_dir)?;
60+
let extra_legalization_groups: &[&'static str] = if !new_backend_isas.is_empty() {
61+
// The new backend only requires the "expand" legalization group.
62+
&["expand"]
63+
} else {
64+
&[]
65+
};
5766

58-
for isa in isas {
67+
gen_legalizer::generate(
68+
&target_isas,
69+
&shared_defs.transform_groups,
70+
extra_legalization_groups,
71+
"legalize",
72+
&out_dir,
73+
)?;
74+
75+
for isa in target_isas {
5976
gen_registers::generate(&isa, &format!("registers-{}.rs", isa.name), &out_dir)?;
6077

6178
gen_settings::generate(
@@ -80,5 +97,28 @@ pub fn generate(isas: &[isa::Isa], out_dir: &str) -> Result<(), error::Error> {
8097
)?;
8198
}
8299

100+
for isa in new_backend_isas {
101+
match isa {
102+
isa::Isa::X86 => {
103+
// If the old backend ISAs contained x86, this file has already been generated.
104+
if old_backend_isas.iter().any(|isa| *isa == isa::Isa::X86) {
105+
continue;
106+
}
107+
108+
let settings = crate::isa::x86::settings::define(&shared_defs.settings);
109+
gen_settings::generate(
110+
&settings,
111+
gen_settings::ParentGroup::Shared,
112+
"settings-x86.rs",
113+
&out_dir,
114+
)?;
115+
}
116+
isa::Isa::Arm64 => {
117+
// aarch64 doesn't have platform-specific settings.
118+
}
119+
isa::Isa::Arm32 | isa::Isa::Riscv => todo!(),
120+
}
121+
}
122+
83123
Ok(())
84124
}

cranelift/codegen/src/isa/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,11 @@ pub fn lookup(triple: Triple) -> Result<Builder, LookupError> {
121121
match triple.architecture {
122122
Architecture::Riscv32 | Architecture::Riscv64 => isa_builder!(riscv, "riscv", triple),
123123
Architecture::I386 | Architecture::I586 | Architecture::I686 | Architecture::X86_64 => {
124-
isa_builder!(x86, "x86", triple)
124+
if cfg!(feature = "x64") {
125+
isa_builder!(x64, "x64", triple)
126+
} else {
127+
isa_builder!(x86, "x86", triple)
128+
}
125129
}
126130
Architecture::Arm { .. } => isa_builder!(arm32, "arm32", triple),
127131
Architecture::Aarch64 { .. } => isa_builder!(aarch64, "arm64", triple),

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@ use crate::isa::Builder as IsaBuilder;
1111
use crate::machinst::pretty_print::ShowWithRRU;
1212
use crate::machinst::{compile, MachBackend, MachCompileResult, TargetIsaAdapter, VCode};
1313
use crate::result::CodegenResult;
14-
use crate::settings::{self, Flags};
14+
use crate::settings::{self as shared_settings, Flags};
1515

16-
use crate::isa::x64::inst::regs::create_reg_universe_systemv;
16+
use crate::isa::x64::{inst::regs::create_reg_universe_systemv, settings as x64_settings};
17+
18+
use super::TargetIsa;
1719

1820
mod abi;
1921
mod inst;
2022
mod lower;
23+
mod settings;
2124

2225
/// An X64 backend.
2326
pub(crate) struct X64Backend {
2427
triple: Triple,
2528
flags: Flags,
29+
_x64_flags: x64_settings::Flags,
2630
reg_universe: RealRegUniverse,
2731
}
2832

2933
impl X64Backend {
3034
/// Create a new X64 backend with the given (shared) flags.
31-
fn new_with_flags(triple: Triple, flags: Flags) -> Self {
35+
fn new_with_flags(triple: Triple, flags: Flags, x64_flags: x64_settings::Flags) -> Self {
3236
let reg_universe = create_reg_universe_systemv(&flags);
3337
Self {
3438
triple,
3539
flags,
40+
_x64_flags: x64_flags,
3641
reg_universe,
3742
}
3843
}
@@ -103,10 +108,17 @@ impl MachBackend for X64Backend {
103108
pub(crate) fn isa_builder(triple: Triple) -> IsaBuilder {
104109
IsaBuilder {
105110
triple,
106-
setup: settings::builder(),
107-
constructor: |triple: Triple, flags: Flags, _arch_flag_builder: settings::Builder| {
108-
let backend = X64Backend::new_with_flags(triple, flags);
109-
Box::new(TargetIsaAdapter::new(backend))
110-
},
111+
setup: x64_settings::builder(),
112+
constructor: isa_constructor,
111113
}
112114
}
115+
116+
fn isa_constructor(
117+
triple: Triple,
118+
shared_flags: Flags,
119+
builder: shared_settings::Builder,
120+
) -> Box<dyn TargetIsa> {
121+
let isa_flags = x64_settings::Flags::new(&shared_flags, builder);
122+
let backend = X64Backend::new_with_flags(triple, shared_flags, isa_flags);
123+
Box::new(TargetIsaAdapter::new(backend))
124+
}

0 commit comments

Comments
 (0)