Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zkasm hackaton #1

Closed
wants to merge 68 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
2af4f60
zkasm: copy riscv64 backend as zkasm starting point
nagisa Aug 24, 2023
366fcc3
rename
nagisa Aug 24, 2023
4ca385a
Fix the wasm command
nagisa Aug 24, 2023
95d9bf3
todo-out riscv64 codegen
Ekleog-NEAR Aug 24, 2023
c721b1e
split todo for emit further
Ekleog-NEAR Aug 24, 2023
07338e9
Use fixed registers for AluRRR
aborg-dev Aug 24, 2023
ced9775
Add prints to emit
aborg-dev Aug 24, 2023
932c527
add some call prints
nagisa Aug 24, 2023
21f2a56
Allow unused vars in some places
aborg-dev Aug 24, 2023
387da7c
add printlns for other things we need to implement
Ekleog-NEAR Aug 24, 2023
68c42d1
Add an AddImm32 MInst variant
nagisa Aug 24, 2023
2b02dcc
remove rust-analyzer spew
nagisa Aug 24, 2023
cc40f68
Update the set of registers
aborg-dev Aug 24, 2023
a5e74b0
implement jump-to-label
Ekleog-NEAR Aug 24, 2023
9382193
Use allocated physical registers where appropriate...
nagisa Aug 24, 2023
aea57ad
This target does not support unwinding
nagisa Aug 24, 2023
e602536
MSTORE: make it more general
nagisa Aug 24, 2023
12977ba
Migrate from println to sink
aborg-dev Aug 24, 2023
4e5aa2b
handle jumps and labels
Ekleog-NEAR Aug 24, 2023
3a23cef
Add a few more instructions for counter
aborg-dev Aug 24, 2023
c431024
Allocate registers during Mov
aborg-dev Aug 28, 2023
bf4f7cc
Use proper register names
aborg-dev Aug 28, 2023
ff8855d
Fix a few more emit statements
aborg-dev Aug 29, 2023
17927bf
Rename a few more registers
aborg-dev Aug 29, 2023
fac63c6
Allocate call return address
aborg-dev Aug 29, 2023
ee1147b
Fix ADD instruction
aborg-dev Aug 29, 2023
f5c868b
Implement ASSERT calls
aborg-dev Aug 29, 2023
3db90c7
Format integer in LOAD
aborg-dev Aug 29, 2023
a6d8a9c
Rewrite function calling conventions
aborg-dev Aug 29, 2023
d88a1ce
Remove space after colon
aborg-dev Aug 29, 2023
bae400d
LOAD -> MLOAD
aborg-dev Aug 29, 2023
26da8e1
Add preamble and postamble
aborg-dev Aug 29, 2023
e0290bd
Run cargo fmt
aborg-dev Aug 29, 2023
89ea5a3
Fix number representations
aborg-dev Aug 29, 2023
68c7c40
Indent instructions
aborg-dev Aug 29, 2023
844dade
Add methods for counter testcase
aborg-dev Aug 29, 2023
069a586
Fix formatting issue
aborg-dev Aug 29, 2023
efb6d32
Implement conditionals
aborg-dev Aug 29, 2023
5398fa2
Tweak the set of available registers
aborg-dev Aug 30, 2023
18f3d50
Fix MLOAD/MSTORE with negative immediates
aborg-dev Aug 30, 2023
ee70c32
Fix clobbering logic
aborg-dev Aug 30, 2023
0742845
Hack extend implementation
aborg-dev Aug 30, 2023
5edf8ff
Format abi.rs
aborg-dev Aug 30, 2023
557d5ea
Add results of compilation to git
aborg-dev Aug 30, 2023
db67946
Remove empty file
aborg-dev Aug 30, 2023
6b2c993
Print code conditionally
aborg-dev Aug 31, 2023
3ff0f24
Handle VMContext register
aborg-dev Aug 31, 2023
7256201
Add a function for context_register
aborg-dev Aug 31, 2023
d6ddbf8
Support Icmp instruction
aborg-dev Aug 31, 2023
6f18a58
Update generated zkasm with conditionals
aborg-dev Aug 31, 2023
33748c6
Fix inversion in jump comparison
aborg-dev Aug 31, 2023
9d2bff7
Implement function calls
aborg-dev Sep 4, 2023
ee6bed4
Fix function indexing
aborg-dev Sep 4, 2023
e044dfb
Add a newline between functions
aborg-dev Sep 4, 2023
a382e44
Property result extfuncnames
aborg-dev Sep 4, 2023
140d38c
Disambiguate labels across functions
aborg-dev Sep 5, 2023
9c6677e
Remove redundant labels
aborg-dev Sep 5, 2023
e9ad965
Disable clobbering of CTX register
aborg-dev Sep 5, 2023
f37587d
Fix return point after function call
aborg-dev Sep 5, 2023
d72c060
Fix warnings
aborg-dev Sep 5, 2023
957c528
Add recursive fibonacci zkasm
aborg-dev Sep 6, 2023
4ab3505
Fix typo in ISLE
aborg-dev Sep 7, 2023
eafc91d
Add support for 32-bit addresses
aborg-dev Sep 12, 2023
70f6491
Add example working with memory
aborg-dev Sep 15, 2023
9588014
training changes
MCJOHN974 Sep 18, 2023
a1a42aa
mul for Imm32
MCJOHN974 Sep 19, 2023
caa24c3
revert args.rs
MCJOHN974 Sep 20, 2023
c9481d0
cargo fmt
MCJOHN974 Sep 20, 2023
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
4 changes: 3 additions & 1 deletion cranelift/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ x86 = []
arm64 = []
s390x = []
riscv64 = []
zkasm = []
# Enable the ISA target for the host machine
host-arch = []

Expand All @@ -81,7 +82,8 @@ all-arch = [
"x86",
"arm64",
"s390x",
"riscv64"
"riscv64",
"zkasm"
]

# For dependent crates that want to serialize some parts of cranelift
Expand Down
15 changes: 14 additions & 1 deletion cranelift/codegen/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
.cloned()
.filter(|isa| {
let env_key = format!("CARGO_FEATURE_{}", isa.to_string().to_uppercase());
env::var(env_key).is_ok()
dbg!(env::var(dbg!(env_key)).is_ok())
})
.collect::<Vec<_>>();

Expand Down Expand Up @@ -200,6 +200,8 @@ fn get_isle_compilations(

let src_isa_risc_v =
make_isle_source_path_relative(&cur_dir, crate_dir.join("src").join("isa").join("riscv64"));
let src_isa_zkasm =
make_isle_source_path_relative(&cur_dir, crate_dir.join("src").join("isa").join("zkasm"));
// This is a set of ISLE compilation units.
//
// The format of each entry is:
Expand Down Expand Up @@ -280,6 +282,17 @@ fn get_isle_compilations(
],
untracked_inputs: vec![clif_lower_isle.clone()],
},
IsleCompilation {
output: out_dir.join("isle_zkasm.rs"),
inputs: vec![
prelude_isle.clone(),
prelude_lower_isle.clone(),
src_isa_zkasm.join("inst.isle"),
src_isa_zkasm.join("inst_vector.isle"),
src_isa_zkasm.join("lower.isle"),
],
untracked_inputs: vec![clif_lower_isle.clone()],
},
],
})
}
Expand Down
7 changes: 6 additions & 1 deletion cranelift/codegen/meta/src/isa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod arm64;
mod riscv64;
mod s390x;
pub(crate) mod x86;
mod zkasm;

/// Represents known ISA target.
#[derive(PartialEq, Copy, Clone)]
Expand All @@ -14,6 +15,7 @@ pub enum Isa {
Arm64,
S390x,
Riscv64,
ZkAsm,
}

impl Isa {
Expand All @@ -29,6 +31,7 @@ impl Isa {
pub fn from_arch(arch: &str) -> Option<Self> {
match arch {
"aarch64" => Some(Isa::Arm64),
"sparc" | "zkasm" => Some(Isa::ZkAsm),
"s390x" => Some(Isa::S390x),
x if ["x86_64", "i386", "i586", "i686"].contains(&x) => Some(Isa::X86),
"riscv64" | "riscv64gc" | "riscv64imac" => Some(Isa::Riscv64),
Expand All @@ -38,7 +41,7 @@ impl Isa {

/// Returns all supported isa targets.
pub fn all() -> &'static [Isa] {
&[Isa::X86, Isa::Arm64, Isa::S390x, Isa::Riscv64]
&[Isa::X86, Isa::Arm64, Isa::S390x, Isa::Riscv64, Isa::ZkAsm]
}
}

Expand All @@ -50,6 +53,7 @@ impl fmt::Display for Isa {
Isa::Arm64 => write!(f, "arm64"),
Isa::S390x => write!(f, "s390x"),
Isa::Riscv64 => write!(f, "riscv64"),
Isa::ZkAsm => write!(f, "zkasm"),
}
}
}
Expand All @@ -61,6 +65,7 @@ pub(crate) fn define(isas: &[Isa]) -> Vec<TargetIsa> {
Isa::Arm64 => arm64::define(),
Isa::S390x => s390x::define(),
Isa::Riscv64 => riscv64::define(),
Isa::ZkAsm => zkasm::define(),
})
.collect()
}
101 changes: 101 additions & 0 deletions cranelift/codegen/meta/src/isa/zkasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::cdsl::isa::TargetIsa;
use crate::cdsl::settings::SettingGroupBuilder;

macro_rules! define_zvl_ext {
(DEF: $settings:expr, $size:expr) => {{
let name = concat!("has_zvl", $size, "b");
let desc = concat!("has extension Zvl", $size, "b?");
let comment = concat!(
"Zvl",
$size,
"b: Vector register has a minimum of ",
$size,
" bits"
);
$settings.add_bool(&name, &desc, &comment, false)
}};
($settings:expr, $size:expr $(, $implies:expr)*) => {{
let has_feature = define_zvl_ext!(DEF: $settings, $size);

let name = concat!("zvl", $size, "b");
let desc = concat!("Has a vector register size of at least ", $size, " bits");

let preset = $settings.add_preset(&name, &desc, preset!(has_feature $( && $implies )*));
(has_feature, preset)
}};
}

pub(crate) fn define() -> TargetIsa {
let mut setting = SettingGroupBuilder::new("zkasm");

let _has_m = setting.add_bool("has_m", "has extension M?", "", false);
let _has_a = setting.add_bool("has_a", "has extension A?", "", false);
let _has_f = setting.add_bool("has_f", "has extension F?", "", false);
let _has_d = setting.add_bool("has_d", "has extension D?", "", false);
let _has_v = setting.add_bool("has_v", "has extension V?", "", false);
let _has_c = setting.add_bool("has_c", "has extension C?", "", false);
let _has_zbkb = setting.add_bool(
"has_zbkb",
"has extension zbkb?",
"Zbkb: Bit-manipulation for Cryptography",
false,
);
let _has_zba = setting.add_bool(
"has_zba",
"has extension zba?",
"Zba: Address Generation",
false,
);
let _has_zbb = setting.add_bool(
"has_zbb",
"has extension zbb?",
"Zbb: Basic bit-manipulation",
false,
);
let _has_zbc = setting.add_bool(
"has_zbc",
"has extension zbc?",
"Zbc: Carry-less multiplication",
false,
);
let _has_zbs = setting.add_bool(
"has_zbs",
"has extension zbs?",
"Zbs: Single-bit instructions",
false,
);

let _has_zicsr = setting.add_bool(
"has_zicsr",
"has extension zicsr?",
"Zicsr: Control and Status Register (CSR) Instructions",
false,
);
let _has_zifencei = setting.add_bool(
"has_zifencei",
"has extension zifencei?",
"Zifencei: Instruction-Fetch Fence",
false,
);

// Zvl*: Minimum Vector Length Standard Extensions
// These extension specifiy the minimum number of bits in a vector register.
// Since it is a minimum, Zvl64b implies Zvl32b, Zvl128b implies Zvl64b, etc.
// The V extension supports a maximum of 64K bits in a single register.
//
// See: https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#181-zvl-minimum-vector-length-standard-extensions
let (_, zvl32b) = define_zvl_ext!(setting, 32);
let (_, zvl64b) = define_zvl_ext!(setting, 64, zvl32b);
let (_, zvl128b) = define_zvl_ext!(setting, 128, zvl64b);
let (_, zvl256b) = define_zvl_ext!(setting, 256, zvl128b);
let (_, zvl512b) = define_zvl_ext!(setting, 512, zvl256b);
let (_, zvl1024b) = define_zvl_ext!(setting, 1024, zvl512b);
let (_, zvl2048b) = define_zvl_ext!(setting, 2048, zvl1024b);
let (_, zvl4096b) = define_zvl_ext!(setting, 4096, zvl2048b);
let (_, zvl8192b) = define_zvl_ext!(setting, 8192, zvl4096b);
let (_, zvl16384b) = define_zvl_ext!(setting, 16384, zvl8192b);
let (_, zvl32768b) = define_zvl_ext!(setting, 32768, zvl16384b);
let (_, _zvl65536b) = define_zvl_ext!(setting, 65536, zvl32768b);

TargetIsa::new("zkasm", setting.build())
}
1 change: 1 addition & 0 deletions cranelift/codegen/src/isa/aarch64/inst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,7 @@ impl MachInst for Inst {
}

fn gen_block_start(
_block_index: usize,
is_indirect_branch_target: bool,
is_forward_edge_cfi_enabled: bool,
) -> Option<Self> {
Expand Down
6 changes: 5 additions & 1 deletion cranelift/codegen/src/isa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub mod riscv64;
#[cfg(feature = "s390x")]
mod s390x;

#[cfg(feature = "zkasm")]
pub mod zkasm;

pub mod unwind;

mod call_conv;
Expand Down Expand Up @@ -103,14 +106,15 @@ pub fn lookup(triple: Triple) -> Result<Builder, LookupError> {
Architecture::Aarch64 { .. } => isa_builder!(aarch64, (feature = "arm64"), triple),
Architecture::S390x { .. } => isa_builder!(s390x, (feature = "s390x"), triple),
Architecture::Riscv64 { .. } => isa_builder!(riscv64, (feature = "riscv64"), triple),
Architecture::Sparc { .. } => isa_builder!(zkasm, (feature = "zkasm"), triple),
_ => Err(LookupError::Unsupported),
}
}

/// The string names of all the supported, but possibly not enabled, architectures. The elements of
/// this slice are suitable to be passed to the [lookup_by_name] function to obtain the default
/// configuration for that architecture.
pub const ALL_ARCHITECTURES: &[&str] = &["x86_64", "aarch64", "s390x", "riscv64"];
pub const ALL_ARCHITECTURES: &[&str] = &["x86_64", "aarch64", "s390x", "riscv64", "sparc"];

/// Look for a supported ISA with the given `name`.
/// Return a builder that can create a corresponding `TargetIsa`.
Expand Down
Loading