-
Notifications
You must be signed in to change notification settings - Fork 52
/
xml.rs
227 lines (210 loc) · 6.34 KB
/
xml.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::traits::Backend;
use calyx_ir as ir;
use calyx_utils::CalyxResult;
use serde::Serialize;
/// Backend that generates XML that Xilinx needs to define the address
/// space for a kernel.
#[derive(Default)]
pub struct XilinxXmlBackend;
/// The root element of the `kernel.xml` file that describes an `.xo` package for the
/// Xilinx toolchain, as documented [in the Vitis user guide][ug].
///
/// [ug]: https://docs.xilinx.com/r/en-US/ug1393-vitis-application-acceleration/RTL-Kernel-XML-File
#[derive(Serialize)]
#[serde(rename = "root")]
struct Root<'a> {
#[serde(rename = "@versionMajor")]
version_major: u64,
#[serde(rename = "@versionMinor")]
version_minor: u64,
kernel: Kernel<'a>,
}
#[derive(Serialize)]
#[serde(rename = "kernel")]
struct Kernel<'a> {
#[serde(rename = "@name")]
name: &'a str,
#[serde(rename = "@language")]
language: &'a str,
#[serde(rename = "@vlnv")]
vlnv: &'a str,
#[serde(rename = "@preferredWorkGroupSizeMultiple")]
preferred_work_group_size_multiple: u64,
#[serde(rename = "@workGroupSize")]
work_group_size: u64,
#[serde(rename = "@interrupt")]
interrupt: bool,
#[serde(rename = "@hwControlProtocol")]
hw_control_protocol: &'a str,
ports: Ports<'a>,
args: Args<'a>,
}
#[derive(Serialize)]
struct Ports<'a> {
port: Vec<Port<'a>>,
}
impl<'a> From<Vec<Port<'a>>> for Ports<'a> {
fn from(port: Vec<Port<'a>>) -> Self {
Ports { port }
}
}
#[derive(Serialize)]
#[serde(rename = "port")]
struct Port<'a> {
#[serde(rename = "@name")]
name: &'a str,
#[serde(rename = "@mode")]
mode: &'a str,
#[serde(rename = "@range")]
range: &'a str,
#[serde(rename = "@dataWidth")]
data_width: u64,
#[serde(rename = "@portType")]
port_type: &'a str,
#[serde(rename = "@base")]
base: &'a str,
}
#[derive(Serialize)]
struct Args<'a> {
arg: Vec<Arg<'a>>,
}
impl<'a> From<Vec<Arg<'a>>> for Args<'a> {
fn from(arg: Vec<Arg<'a>>) -> Self {
Args { arg }
}
}
#[derive(Serialize)]
struct Arg<'a> {
#[serde(rename = "@name")]
name: &'a str,
#[serde(rename = "@addressQualifier")]
address_qualifier: u64,
#[serde(rename = "@id")]
id: u64,
#[serde(rename = "@port")]
port: &'a str,
#[serde(rename = "@size")]
size: &'a str,
#[serde(rename = "@offset")]
offset: &'a str,
#[serde(rename = "@type")]
typ: &'a str,
#[serde(rename = "@hostOffset")]
host_offset: &'a str,
#[serde(rename = "@hostSize")]
host_size: &'a str,
}
impl Backend for XilinxXmlBackend {
fn name(&self) -> &'static str {
"xilinx-xml"
}
fn validate(_ctx: &ir::Context) -> CalyxResult<()> {
Ok(())
}
fn link_externs(
_prog: &ir::Context,
_write: &mut calyx_utils::OutputFile,
) -> CalyxResult<()> {
Ok(())
}
fn emit(
prog: &ir::Context,
file: &mut calyx_utils::OutputFile,
) -> CalyxResult<()> {
let toplevel = prog
.components
.iter()
.find(|comp| comp.name == prog.entrypoint)
.unwrap();
let mut ports = vec![Port {
name: "s_axi_control",
mode: "slave",
range: "0x1000",
data_width: 32,
port_type: "addressable",
base: "0x0",
}];
let mut args = vec![Arg {
name: "timeout",
address_qualifier: 0,
id: 0,
port: "s_axi_control",
size: "0x4",
offset: "0x010",
typ: "uint",
host_offset: "0x0",
host_size: "0x4",
}];
let memories: Vec<(String, String)> = toplevel
.cells
.iter()
.filter(|cell_ref| {
cell_ref
.borrow()
.get_attribute(ir::BoolAttr::External)
.is_some()
})
.enumerate()
.map(|(i, cell_ref)| {
(cell_ref.borrow().name().to_string(), format!("m{}_axi", i))
})
.collect();
// make the lifetime of the &str long enough
let memories_ref: Vec<(&str, &str)> = memories
.iter()
.map(|(x, y)| (x.as_ref(), y.as_ref()))
.collect();
let offsets: Vec<String> = (0..memories.len())
.map(|i| format!("{:#x}", 0x18 + (8 * i)))
.collect();
for (i, (name, axi_name)) in memories_ref.iter().enumerate() {
ports.push(Port {
name: axi_name,
mode: "master",
range: "0xFFFFFFFFFFFFFFFF",
// Width should match the bus data width of memory modules
// described in hardware, for example see
// https://github.com/calyxir/calyx/blob/c2b12a0fe6b1ee3aaaae0c66e7c4619ee6c82614/src/backend/xilinx/toplevel.rs#L58
data_width: 512,
port_type: "addressable",
base: "0x0",
});
args.push(Arg {
name,
address_qualifier: 1,
id: (i + 1) as u64,
port: axi_name,
// XXX(nathanielnrn): This should probably be assigned dynamically
// and not hardcoded, need to figure out where this comes from
// One theory: this is an 8-byte pointer to our argument arrays
size: "0x8",
offset: &offsets[i],
typ: "int*",
host_offset: "0x0",
host_size: "0x8",
});
}
let root = Root {
version_major: 1,
version_minor: 6,
kernel: Kernel {
name: "Toplevel",
language: "ip_c",
// XXX(rachit): This hardcoding seems bad.
vlnv: "capra.cs.cornell.edu:kernel:Toplevel:1.0",
preferred_work_group_size_multiple: 0,
work_group_size: 1,
interrupt: false,
hw_control_protocol: "ap_ctrl_hs",
ports: ports.into(),
args: args.into(),
},
};
write!(
file.get_write(),
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n{}",
quick_xml::se::to_string(&root).expect("XML Serialization failed")
)?;
Ok(())
}
}