Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion cranelift/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ edition.workspace = true
anyhow = { workspace = true, optional = true }
bumpalo = "3"
capstone = { workspace = true, optional = true }
cranelift-codegen-shared = { path = "./shared", version = "0.99.0" }
cranelift-entity = { workspace = true }
cranelift-bforest = { workspace = true }
cranelift-control = { workspace = true }
Expand Down
1 change: 0 additions & 1 deletion cranelift/codegen/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ edition.workspace = true
rustdoc-args = [ "--document-private-items" ]

[dependencies]
cranelift-codegen-shared = { path = "../shared", version = "0.99.0" }
29 changes: 28 additions & 1 deletion cranelift/codegen/meta/src/cdsl/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,34 @@
use std::fmt;

use crate::shared::types as shared_types;
use cranelift_codegen_shared::constants;

pub mod constants {
// Numbering scheme for value types:
//
// 0: Void
// 0x01-0x6f: Special types
// 0x70-0x7d: Lane types
// 0x7e-0x7f: Reference types
// 0x80-0xff: Vector types
// 0x100-0x17f: Dynamic Vector types
//
// Vector types are encoded with the lane type in the low 4 bits and log2(lanes)
// in the next highest 4 bits, giving a range of 2-256 lanes.

// Dynamic vector types are encoded similarily.

/// Start of the lane types.
pub const LANE_BASE: u16 = 0x70;

/// Base for reference types.
pub const REFERENCE_BASE: u16 = 0x7E;

/// Start of the 2-lane vector types.
pub const VECTOR_BASE: u16 = 0x80;

/// Start of the dynamic vector types.
pub const DYNAMIC_VECTOR_BASE: u16 = 0x100;
}

// Rust name prefix used for the `rust_name` method.
static RUST_NAME_PREFIX: &str = "ir::types::";
Expand Down
30 changes: 28 additions & 2 deletions cranelift/codegen/meta/src/constant_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,27 @@

use std::iter;

/// A primitive hash function for matching opcodes.
pub fn simple_hash(s: &str) -> usize {
let mut h: u32 = 5381;
for c in s.chars() {
h = (h ^ c as u32).wrapping_add(h.rotate_right(6));
}
h as usize
}

// Note: Keep in sync with the above code.
pub static SIMPLE_HASH_SOURCE: &str = r#"
/// A primitive hash function for matching opcodes.
pub fn simple_hash(s: &str) -> usize {
let mut h: u32 = 5381;
for c in s.chars() {
h = (h ^ c as u32).wrapping_add(h.rotate_right(6));
}
h as usize
}
"#;

/// Compute an open addressed, quadratically probed hash table containing
/// `items`. The returned table is a list containing the elements of the
/// iterable `items` and `None` in unused slots.
Expand Down Expand Up @@ -44,8 +65,7 @@ pub fn generate_table<'cont, T, I: iter::Iterator<Item = &'cont T>, H: Fn(&T) ->

#[cfg(test)]
mod tests {
use super::generate_table;
use cranelift_codegen_shared::constant_hash::simple_hash;
use super::{generate_table, simple_hash};

#[test]
fn test_generate_table() {
Expand All @@ -61,4 +81,10 @@ mod tests {
]
);
}

#[test]
fn basic() {
assert_eq!(simple_hash("Hello"), 0x2fa70c01);
assert_eq!(simple_hash("world"), 0x5b0c31d5);
}
}
10 changes: 4 additions & 6 deletions cranelift/codegen/meta/src/gen_inst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
use std::fmt;
use std::rc::Rc;

use cranelift_codegen_shared::constant_hash;

use crate::cdsl::camel_case;
use crate::cdsl::formats::InstructionFormat;
use crate::cdsl::instructions::{AllInstructions, Instruction};
use crate::cdsl::operands::Operand;
use crate::cdsl::typevar::{TypeSet, TypeVar};

use crate::constant_hash::{generate_table, simple_hash};
use crate::error;
use crate::srcgen::{Formatter, Match};
use crate::unique_table::{UniqueSeqTable, UniqueTable};
Expand Down Expand Up @@ -658,10 +657,9 @@ fn gen_opcodes(all_inst: &AllInstructions, fmt: &mut Formatter) {
fmt.empty_line();

// Generate an opcode hash table for looking up opcodes by name.
let hash_table =
crate::constant_hash::generate_table(all_inst.iter(), all_inst.len(), |inst| {
constant_hash::simple_hash(&inst.name)
});
let hash_table = generate_table(all_inst.iter(), all_inst.len(), |inst| {
simple_hash(&inst.name)
});
fmtln!(
fmt,
"const OPCODE_HASH_TABLE: [Option<Opcode>; {}] = [",
Expand Down
3 changes: 1 addition & 2 deletions cranelift/codegen/meta/src/gen_settings.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Generate the ISA-specific settings.
use std::collections::HashMap;

use crate::constant_hash::generate_table;
use cranelift_codegen_shared::constant_hash::simple_hash;
use crate::constant_hash::{generate_table, simple_hash};

use crate::cdsl::camel_case;
use crate::cdsl::settings::{
Expand Down
28 changes: 27 additions & 1 deletion cranelift/codegen/meta/src/gen_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! This ensures that the metaprogram and the generated program see the same
//! type numbering.

use crate::cdsl::types as cdsl_types;
use crate::cdsl::types::{self as cdsl_types, constants};
use crate::error;
use crate::srcgen;

Expand Down Expand Up @@ -48,6 +48,32 @@ fn emit_dynamic_vectors(bits: u64, fmt: &mut srcgen::Formatter) {

/// Emit types using the given formatter object.
fn emit_types(fmt: &mut srcgen::Formatter) {
fmt.line("#[allow(dead_code)]");
fmt.line("mod constants {");
fmt.indent(|fmt| {
fmtln!(
fmt,
"pub const LANE_BASE: u16 = {:#X};\n",
constants::LANE_BASE
);
fmtln!(
fmt,
"pub const REFERENCE_BASE: u16 = {:#X};\n",
constants::REFERENCE_BASE
);
fmtln!(
fmt,
"pub const VECTOR_BASE: u16 = {:#X};\n",
constants::VECTOR_BASE
);
fmtln!(
fmt,
"pub const DYNAMIC_VECTOR_BASE: u16 = {:#X};\n",
constants::DYNAMIC_VECTOR_BASE
);
Comment on lines +54 to +73
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel strongly about this, but I'd be tempted to use a macro here to ensure that the name of the constant is always the same in the generated source as it is in the meta crate. Something like constants!(fmt, LANE_BASE, REFERENCE_BASE, ...) I guess.

I'm happy to merge this as-is, but I thought I'd mention it since I think publish.rs needs to be fixed anyway. Let me know whether you want to change this too.

});
fmt.line("}");

// Emit all of the lane types, such integers, floats, and booleans.
for ty in cdsl_types::ValueType::all_lane_types().map(cdsl_types::ValueType::from) {
emit_type(&ty, fmt);
Expand Down
4 changes: 4 additions & 0 deletions cranelift/codegen/meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ pub fn generate(isas: &[isa::Isa], out_dir: &str, isle_dir: &str) -> Result<(),
isle_dir,
)?;

let mut fmt = srcgen::Formatter::new();
fmt.line(crate::constant_hash::SIMPLE_HASH_SOURCE);
fmt.update_file("constant_hash.rs", out_dir)?;

// Per ISA definitions.
for isa in isa::define(isas) {
gen_settings::generate(
Expand Down
13 changes: 0 additions & 13 deletions cranelift/codegen/shared/Cargo.toml

This file was deleted.

Loading