cranelift_codegen_meta/isa/
riscv64.rs

1use crate::cdsl::isa::TargetIsa;
2use crate::cdsl::settings::{PredicateNode, SettingGroupBuilder};
3
4macro_rules! define_zvl_ext {
5    (DEF: $settings:expr, $size:expr) => {{
6        let name = concat!("has_zvl", $size, "b");
7        let desc = concat!("has extension Zvl", $size, "b?");
8        let comment = concat!(
9            "Zvl",
10            $size,
11            "b: Vector register has a minimum of ",
12            $size,
13            " bits"
14        );
15        $settings.add_bool(&name, &desc, &comment, false)
16    }};
17    ($settings:expr, $size:expr $(, $implies:expr)*) => {{
18        let has_feature = define_zvl_ext!(DEF: $settings, $size);
19
20        let name = concat!("zvl", $size, "b");
21        let desc = concat!("Has a vector register size of at least ", $size, " bits");
22
23        let preset = $settings.add_preset(&name, &desc, preset!(has_feature $( && $implies )*));
24        (has_feature, preset)
25    }};
26}
27
28pub(crate) fn define() -> TargetIsa {
29    let mut setting = SettingGroupBuilder::new("riscv64");
30
31    // We target a minimum of riscv64g. That means that we have the following extensions by default:
32    //
33    // * M (integer multiplication and division)
34    // * A (atomic instructions)
35    // * F (single-precision floating point)
36    // * D (double-precision floating point)
37    // * Zicsr (control and status register instructions)
38    // * Zifencei (instruction-fetch fence)
39
40    let has_m = setting.add_bool(
41        "has_m",
42        "has extension M?",
43        "Integer multiplication and division",
44        true,
45    );
46    let has_a = setting.add_bool("has_a", "has extension A?", "Atomic instructions", true);
47    let has_f = setting.add_bool(
48        "has_f",
49        "has extension F?",
50        "Single-precision floating point",
51        true,
52    );
53    let has_d = setting.add_bool(
54        "has_d",
55        "has extension D?",
56        "Double-precision floating point",
57        true,
58    );
59
60    let _has_zfa = setting.add_bool(
61        "has_zfa",
62        "has extension Zfa?",
63        "Zfa: Extension for Additional Floating-Point Instructions",
64        false,
65    );
66
67    let _has_zfh = setting.add_bool(
68        "has_zfh",
69        "has extension Zfh?",
70        "Zfh: Half-Precision Floating-Point Instructions",
71        false,
72    );
73
74    let _has_v = setting.add_bool(
75        "has_v",
76        "has extension V?",
77        "Vector instruction support",
78        false,
79    );
80
81    let has_zca = setting.add_bool(
82        "has_zca",
83        "has extension Zca?",
84        "Zca is the C extension without floating point loads",
85        false,
86    );
87    let has_zcd = setting.add_bool(
88        "has_zcd",
89        "has extension Zcd?",
90        "Zcd contains only the double precision floating point loads from the C extension",
91        false,
92    );
93    setting.add_preset(
94        "has_c",
95        "Support for compressed instructions",
96        preset!(has_zca && has_zcd),
97    );
98
99    let _has_zcb = setting.add_bool(
100        "has_zcb",
101        "has extension Zcb?",
102        "Zcb: Extra compressed instructions",
103        false,
104    );
105
106    let _has_zbkb = setting.add_bool(
107        "has_zbkb",
108        "has extension zbkb?",
109        "Zbkb: Bit-manipulation for Cryptography",
110        false,
111    );
112    let _has_zba = setting.add_bool(
113        "has_zba",
114        "has extension zba?",
115        "Zba: Address Generation",
116        false,
117    );
118    let _has_zbb = setting.add_bool(
119        "has_zbb",
120        "has extension zbb?",
121        "Zbb: Basic bit-manipulation",
122        false,
123    );
124    let _has_zbc = setting.add_bool(
125        "has_zbc",
126        "has extension zbc?",
127        "Zbc: Carry-less multiplication",
128        false,
129    );
130    let _has_zbs = setting.add_bool(
131        "has_zbs",
132        "has extension zbs?",
133        "Zbs: Single-bit instructions",
134        false,
135    );
136    let _has_zicond = setting.add_bool(
137        "has_zicond",
138        "has extension zicond?",
139        "ZiCond: Integer Conditional Operations",
140        false,
141    );
142
143    let has_zicsr = setting.add_bool(
144        "has_zicsr",
145        "has extension zicsr?",
146        "Zicsr: Control and Status Register (CSR) Instructions",
147        true,
148    );
149    let has_zifencei = setting.add_bool(
150        "has_zifencei",
151        "has extension zifencei?",
152        "Zifencei: Instruction-Fetch Fence",
153        true,
154    );
155
156    // Zvl*: Minimum Vector Length Standard Extensions
157    // These extension specify the minimum number of bits in a vector register.
158    // Since it is a minimum, Zvl64b implies Zvl32b, Zvl128b implies Zvl64b, etc.
159    // The V extension supports a maximum of 64K bits in a single register.
160    //
161    // See: https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#181-zvl-minimum-vector-length-standard-extensions
162    let (_, zvl32b) = define_zvl_ext!(setting, 32);
163    let (_, zvl64b) = define_zvl_ext!(setting, 64, zvl32b);
164    let (_, zvl128b) = define_zvl_ext!(setting, 128, zvl64b);
165    let (_, zvl256b) = define_zvl_ext!(setting, 256, zvl128b);
166    let (_, zvl512b) = define_zvl_ext!(setting, 512, zvl256b);
167    let (_, zvl1024b) = define_zvl_ext!(setting, 1024, zvl512b);
168    let (_, zvl2048b) = define_zvl_ext!(setting, 2048, zvl1024b);
169    let (_, zvl4096b) = define_zvl_ext!(setting, 4096, zvl2048b);
170    let (_, zvl8192b) = define_zvl_ext!(setting, 8192, zvl4096b);
171    let (_, zvl16384b) = define_zvl_ext!(setting, 16384, zvl8192b);
172    let (_, zvl32768b) = define_zvl_ext!(setting, 32768, zvl16384b);
173    let (_, _zvl65536b) = define_zvl_ext!(setting, 65536, zvl32768b);
174
175    setting.add_predicate(
176        "has_g",
177        predicate!(has_m && has_a && has_f && has_d && has_zicsr && has_zifencei),
178    );
179
180    TargetIsa::new("riscv64", setting.build())
181}