Skip to content

Commit 38b7fdb

Browse files
author
Stephen Tridgell
committed
Add vitis rules for HLS.
1 parent 085ae76 commit 38b7fdb

File tree

295 files changed

+293968
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

295 files changed

+293968
-0
lines changed

vitis/BUILD

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
exports_files(["vitis_generate.tcl.template"])
2+
3+
# Define versions of vitis_hls/vivado_hls as needed.
4+
# These are copied from the installation.
5+
cc_library(
6+
name = "v2021_2_cc",
7+
srcs = glob([
8+
"v2021_2/**/*.h",
9+
]),
10+
strip_include_prefix = "external/rules_hdl/",
11+
visibility = ["//visibility:public"],
12+
)
13+
14+
cc_library(
15+
name = "v2020_1_cc",
16+
srcs = glob([
17+
"v2020_1/**/*.h",
18+
]),
19+
strip_include_prefix = "external/rules_hdl/",
20+
visibility = ["//visibility:public"],
21+
)

vitis/defs.bzl

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""Defs for generating verilog using HLS"""
2+
3+
HlsFileInfo = provider(
4+
"HLS files required by vitis",
5+
fields = {
6+
"files": "a list of files",
7+
},
8+
)
9+
10+
def _vitis_hls_files_aspect_impl(target, ctx):
11+
"""Filter out the vitis header deps."""
12+
files = []
13+
14+
for f in target[CcInfo].compilation_context.headers.to_list():
15+
if "vitis/v" not in f.dirname:
16+
files.append(f)
17+
18+
if hasattr(ctx.rule.attr, "srcs"):
19+
for src in ctx.rule.attr.srcs:
20+
for f in src.files.to_list():
21+
if f not in files and "vitis/v" not in f.dirname:
22+
files.append(f)
23+
24+
if hasattr(ctx.rule.attr, "deps"):
25+
for dep in ctx.rule.attr.deps:
26+
files = files + dep[HlsFileInfo].files
27+
28+
return [HlsFileInfo(files = files)]
29+
30+
vitis_hls_files_aspect = aspect(
31+
implementation = _vitis_hls_files_aspect_impl,
32+
attr_aspects = ["deps"],
33+
)
34+
35+
def _vitis_generate_impl(ctx):
36+
all_files = []
37+
cflags = "-D__SYNTHESIS__=1 --std=c++17"
38+
for dep in ctx.attr.deps:
39+
for file in dep[HlsFileInfo].files:
40+
external_path = "/".join([file.root.path, file.owner.workspace_root]) if file.root.path else file.owner.workspace_root
41+
cflags += " -I" + external_path
42+
43+
source_file_str = ""
44+
for dep in ctx.attr.deps:
45+
for file in dep[HlsFileInfo].files:
46+
all_files.append(file)
47+
source_file_str += "add_file " + file.path + " -cflags \"" + cflags + "\"\n"
48+
49+
vitis_tcl = ctx.actions.declare_file("{}_run_hls.tcl".format(ctx.label.name))
50+
vitis_log = ctx.actions.declare_file("{}_hls.log".format(ctx.label.name))
51+
52+
substitutions = {
53+
"{{PROJECT_NAME}}": ctx.label.name,
54+
"{{SOURCE_FILES}}": source_file_str,
55+
"{{TOP_LEVEL_FUNCTION}}": ctx.attr.top_func,
56+
"{{PART_NUMBER}}": ctx.attr.part_number,
57+
"{{CLOCK_PERIOD}}": ctx.attr.clock_period,
58+
}
59+
60+
ctx.actions.expand_template(
61+
template = ctx.file._vitis_generate_template,
62+
output = vitis_tcl,
63+
substitutions = substitutions,
64+
)
65+
66+
vitis_command = "source " + ctx.file.xilinx_env.path + " && "
67+
if ctx.attr.use_vivado_hls:
68+
vitis_command += "vivado_hls " + vitis_tcl.path
69+
else:
70+
vitis_command += "vitis_hls " + vitis_tcl.path
71+
vitis_command += " -l " + vitis_log.path
72+
vitis_command += " && tar -czvf " + ctx.outputs.out.path + " -C "
73+
vitis_command += ctx.label.name + "/sol1/impl/verilog ."
74+
75+
outputs = [vitis_log, ctx.outputs.out]
76+
77+
if ctx.attr.use_vivado_hls:
78+
progress_message = "Running with vivado_hls: {}".format(ctx.label.name)
79+
else:
80+
progress_message = "Running with vitis_hls: {}".format(ctx.label.name)
81+
82+
ctx.actions.run_shell(
83+
outputs = outputs,
84+
inputs = all_files + [vitis_tcl, ctx.file.xilinx_env],
85+
progress_message = progress_message,
86+
command = vitis_command,
87+
)
88+
89+
return [
90+
DefaultInfo(files = depset(outputs)),
91+
]
92+
93+
vitis_generate = rule(
94+
implementation = _vitis_generate_impl,
95+
attrs = {
96+
"top_func": attr.string(doc = "The name of the top level function.", mandatory = True),
97+
"clock_period": attr.string(doc = "The clock period for the module.", mandatory = True),
98+
"part_number": attr.string(doc = "The part number to use. Default is ZCU111", default = "xczu28dr-ffvg1517-2-e"),
99+
"deps": attr.label_list(doc = "The file to generate from", aspects = [vitis_hls_files_aspect], mandatory = True),
100+
"out": attr.output(doc = "The generated verilog files", mandatory = True),
101+
"use_vivado_hls": attr.bool(doc = "Use vivado HLS instead of vitis hls.", default = False),
102+
"_vitis_generate_template": attr.label(
103+
doc = "The tcl template to run with vitis.",
104+
default = "@rules_hdl//vitis:vitis_generate.tcl.template",
105+
allow_single_file = [".template"],
106+
),
107+
"xilinx_env": attr.label(
108+
doc = "Environment variables for xilinx tools.",
109+
allow_single_file = [".sh"],
110+
mandatory = True,
111+
),
112+
},
113+
)

vitis/tests/BUILD

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
load("//vitis:defs.bzl", "vitis_generate")
2+
3+
# This is to test the path of generated headers.
4+
genrule(
5+
name = "consts",
6+
outs = [
7+
"consts.h",
8+
],
9+
cmd_bash = "touch $(OUTS)",
10+
)
11+
12+
cc_library(
13+
name = "consts_cc",
14+
hdrs = [
15+
"consts.h",
16+
],
17+
)
18+
19+
cc_library(
20+
name = "hls_adder",
21+
srcs = [
22+
"hls_adder.cc",
23+
],
24+
hdrs = [
25+
"hls_adder.h",
26+
],
27+
deps = [
28+
"consts_cc",
29+
"@rules_hdl//vitis:v2021_2_cc",
30+
],
31+
)
32+
33+
cc_test(
34+
name = "hls_adder_test",
35+
srcs = [
36+
"hls_adder_test.cc",
37+
],
38+
deps = [
39+
":hls_adder",
40+
"@com_google_googletest//:gtest_main",
41+
],
42+
)
43+
44+
cc_library(
45+
name = "hls_adder_vivado",
46+
srcs = [
47+
"hls_adder.cc",
48+
],
49+
hdrs = [
50+
"hls_adder.h",
51+
],
52+
defines = ["WITH_VIVADO_HLS=1"],
53+
deps = [
54+
"consts_cc",
55+
"@rules_hdl//vitis:v2020_1_cc",
56+
],
57+
)
58+
59+
cc_test(
60+
name = "hls_adder_vivado_test",
61+
srcs = [
62+
"hls_adder_test.cc",
63+
],
64+
deps = [
65+
":hls_adder_vivado",
66+
"@com_google_googletest//:gtest_main",
67+
],
68+
)
69+
70+
vitis_generate(
71+
name = "adder",
72+
out = "adder.tar.gz",
73+
clock_period = "10.0",
74+
tags = ["manual"],
75+
top_func = "adder",
76+
xilinx_env = ":xilinx_env.sh",
77+
deps = [":hls_adder"],
78+
)
79+
80+
vitis_generate(
81+
name = "adder_vivado",
82+
out = "adder_vivado.tar.gz",
83+
clock_period = "10.0",
84+
tags = ["manual"],
85+
# Note need namespace with vivado_hls.
86+
top_func = "vitis::adder",
87+
use_vivado_hls = True,
88+
xilinx_env = ":xilinx_env_vivado.sh",
89+
deps = [":hls_adder_vivado"],
90+
)

vitis/tests/hls_adder.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "hls_adder.h"
2+
3+
namespace vitis {
4+
5+
void adder(fixed &a, fixed &b, fixed &c) {
6+
#pragma HLS pipeline II = 1
7+
c = a + b;
8+
}
9+
10+
} // namespace vitis

vitis/tests/hls_adder.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
#ifdef __SYNTHESIS__
3+
#include <ap_fixed.h>
4+
#else
5+
#ifdef WITH_VIVADO_HLS
6+
#include "vitis/v2020_1/ap_fixed.h"
7+
#else
8+
#include "vitis/v2021_2/ap_fixed.h"
9+
#endif
10+
#endif
11+
12+
#include "vitis/tests/consts.h"
13+
namespace vitis {
14+
15+
typedef ap_fixed<16, 9> fixed;
16+
17+
void adder(fixed &a, fixed &b, fixed &c);
18+
19+
} // namespace vitis

vitis/tests/hls_adder_test.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "hls_adder.h"
2+
3+
#include "gtest/gtest.h"
4+
5+
namespace vitis {
6+
namespace {
7+
8+
class HlsAdderTest : public testing::Test {};
9+
10+
TEST_F(HlsAdderTest, one_plus_one) {
11+
fixed a = 1;
12+
fixed b = 1;
13+
fixed c;
14+
adder(a, b, c);
15+
EXPECT_EQ(c, 2);
16+
}
17+
18+
TEST_F(HlsAdderTest, quarter_plus_quarter) {
19+
fixed a = 0.25;
20+
fixed b = 0.25;
21+
fixed c;
22+
adder(a, b, c);
23+
EXPECT_EQ(c, 0.5);
24+
}
25+
26+
} // namespace
27+
} // namespace vitis

vitis/tests/xilinx_env.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
export HOME=/tmp
4+
source /opt/xilinx/Vitis_HLS/2021.2/settings64.sh
5+
export XILINXD_LICENSE_FILE=2100@localhost

vitis/tests/xilinx_env_vivado.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#! /bin/bash
2+
3+
export HOME=/tmp
4+
source /opt/xilinx/Vivado/2020.1/settings64.sh
5+
export XILINXD_LICENSE_FILE=2100@localhost

0 commit comments

Comments
 (0)