Skip to content

Commit 76911c2

Browse files
Partial support for no_std in cranelift_codegen (#12222)
* Move most things from std to core and alloc * Port assembler_x64 to no_std * before adding prelude to each file * Most of the files now work with no_std * update isle to use alloc and core * some instances shouldn't have been renamed, fixes cargo test * add cranelift-assembler-x64 (no_std) to CI * fix codegen_meta, missed one spot with std::slice * automatically remove prelude with cargo fix * update isle changes * update assembler changes * update assembler changes * use latest codegen changes + fix FxHash problem * add imports * fix floating issues with libm * remove unused import * temporarily remove OnceLock * add no_std arm support and add it into CI * Move most things from std to core and alloc * Port assembler_x64 to no_std * before adding prelude to each file * Most of the files now work with no_std * update isle to use alloc and core * some instances shouldn't have been renamed, fixes cargo test * add cranelift-assembler-x64 (no_std) to CI * automatically remove prelude with cargo fix * update isle changes * update assembler changes * update assembler changes * use latest codegen changes + fix FxHash problem * add imports * fix floating issues with libm * remove unused import * temporarily remove OnceLock * add no_std arm support and add it into CI * Move most things from std to core and alloc * Port assembler_x64 to no_std * before adding prelude to each file * Most of the files now work with no_std * update isle to use alloc and core * add cranelift-assembler-x64 (no_std) to CI * automatically remove prelude with cargo fix * update isle changes * update assembler changes * use latest codegen changes + fix FxHash problem * add imports * fix floating issues with libm * temporarily remove OnceLock * add no_std arm support and add it into CI * revert Cargo.toml formating * remove prelude and fix cargo.toml * cargo fmt * remove empty lines * bad renames * macro_use only on no_std * revert OnceLock change * only use stable libm features * update regalloc2 * update comment * use continue instead * Update vets --------- Co-authored-by: Alex Crichton <[email protected]>
1 parent ef8a1a7 commit 76911c2

File tree

17 files changed

+54
-33
lines changed

17 files changed

+54
-33
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ cranelift-jit = { path = "cranelift/jit", version = "0.129.0" }
304304
cranelift-fuzzgen = { path = "cranelift/fuzzgen" }
305305
cranelift-bforest = { path = "cranelift/bforest", version = "0.129.0" }
306306
cranelift-bitset = { path = "cranelift/bitset", version = "0.129.0" }
307-
cranelift-control = { path = "cranelift/control", version = "0.129.0" }
307+
cranelift-control = { path = "cranelift/control", version = "0.129.0", default-features = false }
308308
cranelift-srcgen = { path = "cranelift/srcgen", version = "0.129.0" }
309309
cranelift = { path = "cranelift/umbrella", version = "0.129.0" }
310310

@@ -322,7 +322,7 @@ component-async-tests = { path = "crates/misc/component-async-tests" }
322322

323323
# Bytecode Alliance maintained dependencies:
324324
# ---------------------------
325-
regalloc2 = "0.13.3"
325+
regalloc2 = "0.13.4"
326326
wasip1 = { version = "1.0.0", default-features = false }
327327

328328
# cap-std family:
@@ -414,7 +414,7 @@ tracing-subscriber = { version = "0.3.20", default-features = false, features =
414414
url = "2.5.7"
415415
postcard = { version = "1.1.3", default-features = false, features = ['alloc'] }
416416
criterion = { version = "0.7.0", default-features = false, features = ["html_reports", "rayon"] }
417-
rustc-hash = "2.1.1"
417+
rustc-hash = { version = "2.1.1", default-features = false }
418418
libtest-mimic = "0.8.1"
419419
semver = { version = "1.0.27", default-features = false }
420420
ittapi = "0.4.0"

cranelift/codegen/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,21 @@ cranelift-entity = { workspace = true }
3030
cranelift-bforest = { workspace = true }
3131
cranelift-bitset = { workspace = true }
3232
cranelift-control = { workspace = true }
33-
hashbrown = { workspace = true }
33+
hashbrown = { workspace = true, features = ["default-hasher"] }
3434
target-lexicon = { workspace = true }
3535
log = { workspace = true }
3636
serde = { workspace = true, optional = true }
3737
serde_derive = { workspace = true, optional = true }
3838
pulley-interpreter = { workspace = true, optional = true }
3939
postcard = { workspace = true, optional = true }
40-
gimli = { workspace = true, features = ["write", "std"], optional = true }
40+
gimli = { workspace = true, features = ["write"], optional = true }
4141
smallvec = { workspace = true }
4242
regalloc2 = { workspace = true, features = ["checker"] }
4343
souper-ir = { version = "2.1.0", optional = true }
4444
sha2 = { version = "0.10.2", optional = true }
4545
rustc-hash = { workspace = true }
4646
wasmtime-math = { workspace = true }
47+
libm = { workspace = true }
4748
# It is a goal of the cranelift-codegen crate to have minimal external dependencies.
4849
# Please don't add any unless they are essential to the task of creating binary
4950
# machine code. Integration tests that need external dependencies can be
@@ -65,7 +66,7 @@ default = ["std", "unwind", "host-arch", "timing"]
6566
# The "std" feature enables use of libstd. The "core" feature enables use
6667
# of some minimal std-like replacement libraries. At least one of these two
6768
# features need to be enabled.
68-
std = ["serde?/std"]
69+
std = ["serde?/std", "rustc-hash/std", "gimli/std", "cranelift-control/fuzz"]
6970

7071
# The "core" feature used to enable a hashmap workaround, but is now
7172
# deprecated (we (i) always use hashbrown, and (ii) don't support a

cranelift/codegen/src/alias_analysis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
//! must be correct likely reduce the potential benefit, we don't yet
6262
//! do this.
6363
64+
use crate::{FxHashMap, FxHashSet};
6465
use crate::{
6566
cursor::{Cursor, FuncCursor},
6667
dominator_tree::DominatorTree,
@@ -71,7 +72,6 @@ use crate::{
7172
trace,
7273
};
7374
use cranelift_entity::{EntityRef, packed_option::PackedOption};
74-
use rustc_hash::{FxHashMap, FxHashSet};
7575

7676
/// For a given program point, the vector of last-store instruction
7777
/// indices for each disjoint category of abstract state.

cranelift/codegen/src/egraph/elaborate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use crate::ir::{Block, Function, Inst, Value, ValueDef};
1111
use crate::loop_analysis::{Loop, LoopAnalysis};
1212
use crate::scoped_hash_map::ScopedHashMap;
1313
use crate::trace;
14+
use crate::{FxHashMap, FxHashSet};
1415
use alloc::vec::Vec;
1516
use cranelift_control::ControlPlane;
1617
use cranelift_entity::{EntitySet, SecondaryMap, packed_option::ReservedValue};
17-
use rustc_hash::{FxHashMap, FxHashSet};
1818
use smallvec::{SmallVec, smallvec};
1919

2020
pub(crate) struct Elaborator<'a> {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Support for egraphs represented in the DataFlowGraph.
22
3+
use crate::FxHashSet;
34
use crate::alias_analysis::{AliasAnalysis, LastStores};
45
use crate::ctxhash::{CtxEq, CtxHash, NullCtx};
56
use crate::cursor::{Cursor, CursorPosition, FuncCursor};
@@ -24,7 +25,6 @@ use core::hash::Hasher;
2425
use cranelift_control::ControlPlane;
2526
use cranelift_entity::SecondaryMap;
2627
use cranelift_entity::packed_option::ReservedValue;
27-
use rustc_hash::FxHashSet;
2828
use smallvec::SmallVec;
2929

3030
mod cost;

cranelift/codegen/src/ir/immediates.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use core::fmt::{self, Display, Formatter};
1010
use core::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Sub};
1111
use core::str::FromStr;
1212
use core::{i32, u32};
13+
use libm::Libm;
1314
#[cfg(feature = "enable-serde")]
1415
use serde_derive::{Deserialize, Serialize};
1516

@@ -682,27 +683,29 @@ macro_rules! ieee_float {
682683
$(
683684
/// Returns the square root of `self`.
684685
pub fn sqrt(self) -> Self {
685-
Self::with_float(self.$as_float().sqrt())
686+
Self::with_float(Libm::<$float_ty>::sqrt(self.$as_float()))
686687
}
687688

688689
/// Returns the smallest integer greater than or equal to `self`.
689690
pub fn ceil(self) -> Self {
690-
Self::with_float(self.$as_float().ceil())
691+
Self::with_float(Libm::<$float_ty>::ceil(self.$as_float()))
691692
}
692693

693694
/// Returns the largest integer less than or equal to `self`.
694695
pub fn floor(self) -> Self {
695-
Self::with_float(self.$as_float().floor())
696+
Self::with_float(Libm::<$float_ty>::floor(self.$as_float()))
696697
}
697698

698699
/// Returns the integer part of `self`. This means that non-integer numbers are always truncated towards zero.
699700
pub fn trunc(self) -> Self {
700-
Self::with_float(self.$as_float().trunc())
701+
Self::with_float(Libm::<$float_ty>::trunc(self.$as_float()))
701702
}
702703

703704
/// Returns the nearest integer to `self`. Rounds half-way cases to the number
704705
/// with an even least significant digit.
705706
pub fn round_ties_even(self) -> Self {
707+
// TODO: when libm v0.2.16 is published line below can be used instead
708+
// Self::with_float(Libm::<$float_ty>::roundeven(self.$as_float()))
706709
Self::with_float(self.$as_float().round_ties_even())
707710
}
708711
)?

cranelift/codegen/src/isa/unwind.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod winx64;
1414
#[cfg(feature = "unwind")]
1515
pub mod winarm64;
1616

17+
#[cfg(feature = "unwind")]
1718
/// CFA-based unwind information used on SystemV.
1819
pub type CfaUnwindInfo = systemv::UnwindInfo;
1920

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ mod lower;
2828
mod pcc;
2929
pub mod settings;
3030

31+
#[cfg(feature = "unwind")]
3132
pub use inst::unwind::systemv::create_cie;
3233

3334
/// An X64 backend.
@@ -217,6 +218,7 @@ pub fn emit_unwind_info(
217218
) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
218219
use crate::isa::unwind::{UnwindInfo, UnwindInfoKind};
219220
Ok(match kind {
221+
#[cfg(feature = "unwind")]
220222
UnwindInfoKind::SystemV => {
221223
let mapper = self::inst::unwind::systemv::RegisterMapper;
222224
Some(UnwindInfo::SystemV(
@@ -227,6 +229,7 @@ pub fn emit_unwind_info(
227229
)?,
228230
))
229231
}
232+
#[cfg(feature = "unwind")]
230233
UnwindInfoKind::Windows => Some(UnwindInfo::WindowsX64(
231234
crate::isa::unwind::winx64::create_unwind_info_from_insts::<
232235
self::inst::unwind::winx64::RegisterMapper,

cranelift/codegen/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
allow(dead_code, reason = "see comment above")
1515
)]
1616

17+
#[cfg_attr(not(feature = "std"), macro_use)]
1718
extern crate alloc;
1819

1920
#[cfg(feature = "std")]
@@ -23,7 +24,12 @@ extern crate std;
2324
#[cfg(not(feature = "std"))]
2425
use hashbrown::{HashMap, HashSet, hash_map};
2526
#[cfg(feature = "std")]
26-
use std::collections::{HashMap, hash_map};
27+
use std::collections::{HashMap, HashSet, hash_map};
28+
29+
/// Type alias for a hash map that uses the Fx hashing algorithm.
30+
pub type FxHashMap<K, V> = HashMap<K, V, rustc_hash::FxBuildHasher>;
31+
/// Type alias for a hash set that uses the Fx hashing algorithm.
32+
pub type FxHashSet<V> = HashSet<V, rustc_hash::FxBuildHasher>;
2733

2834
pub use crate::context::Context;
2935
pub use crate::value_label::{LabelValueLoc, ValueLabelsRanges, ValueLocRange};

0 commit comments

Comments
 (0)