Skip to content

Commit b3268b8

Browse files
committed
Synthesis: Adds new synthesis_binary rule
The new rules allows you to get a binary version of the synthesize_rtl rule. This version is self contained, and can be run outside the context of bazel PiperOrigin-RevId: 496973180
1 parent c6ba9ff commit b3268b8

File tree

2 files changed

+113
-13
lines changed

2 files changed

+113
-13
lines changed

place_and_route/open_road.bzl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,22 @@ def clock_commands(ctx):
107107
Returns:
108108
Struct with params inputs and commands. Both return values are lists.
109109
"""
110-
if not clock_commands:
111-
return struct(inputs = [], commands = ["create_clock [get_ports clk] -period {period}".format(period = ctx.attr.clock_period)])
112-
113110
sdc = ctx.file.sdc
111+
114112
if sdc:
115113
return struct(inputs = [sdc], commands = ["read_sdc {}".format(sdc.path)])
116114

115+
# If no name is passed, the clock is assumed to be named "clk".
116+
if ctx.attr.clock_period:
117+
return struct(
118+
inputs = [],
119+
commands = [
120+
"create_clock [get_ports clk] -period {period}".format(
121+
period = ctx.attr.clock_period,
122+
),
123+
],
124+
)
125+
117126
return struct(
118127
inputs = [],
119128
commands = [

synthesis/build_defs.bzl

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ UhdmInfo = provider(
2626
},
2727
)
2828

29+
ExternalSynthesisInfo = provider(
30+
"Surelog/UHDM based RTL representation",
31+
fields = {
32+
"env": "Map of env variables",
33+
"yosys_script": "yosys script file",
34+
"inputs": "File inputs",
35+
"verilog_files": "Verilog Inputs",
36+
"uhdm_files": "UHDM files",
37+
},
38+
)
39+
2940
# Args:
3041
# standard_cell_info: The StandardCellInfo provider this target was synthesized against.
3142
# synthesized_netlist: The structural verilog syntheized with standard_cell_info
@@ -42,9 +53,12 @@ def _transitive_srcs(deps):
4253
transitive = [dep[VerilogInfo].dag for dep in deps],
4354
)
4455

45-
def _create_flist(ctx, flist_tag, files):
56+
def _create_flist(ctx, flist_tag, files, short_path = False):
4657
flist = ctx.actions.declare_file("{}_{}.flist".format(flist_tag, ctx.attr.name))
47-
ctx.actions.write(flist, "\n".join([f.path for f in files]) + "\n")
58+
if short_path:
59+
ctx.actions.write(flist, "\n".join([f.short_path for f in files]) + "\n")
60+
else:
61+
ctx.actions.write(flist, "\n".join([f.path for f in files]) + "\n")
4862
return flist
4963

5064
def _synthesize_design_impl(ctx):
@@ -80,6 +94,7 @@ def _synthesize_design_impl(ctx):
8094
inputs.append(uhdm_flist)
8195
inputs.extend(uhdm_files)
8296
inputs.append(synth_tcl)
97+
inputs.append(default_liberty_file)
8398

8499
(tool_inputs, input_manifests) = ctx.resolve_tools(tools = [ctx.attr.yosys_tool])
85100

@@ -94,22 +109,31 @@ def _synthesize_design_impl(ctx):
94109
args.add_all("-l", [log_file]) # put output in log file
95110
args.add_all("-c", [synth_tcl]) # run synthesis tcl script
96111

97-
env = {
98-
"FLIST": verilog_flist.path,
99-
"UHDM_FLIST": uhdm_flist.path,
112+
script_env_files = {
113+
"FLIST": verilog_flist,
114+
"UHDM_FLIST": uhdm_flist,
100115
"TOP": ctx.attr.top_module,
101-
"OUTPUT": output_file.path,
102-
"LIBERTY": default_liberty_file.path,
116+
"OUTPUT": output_file,
117+
"LIBERTY": default_liberty_file,
118+
}
119+
120+
if ctx.attr.target_clock_period_pico_seconds:
121+
script_env_files["CLOCK_PERIOD"] = str(ctx.attr.target_clock_period_pico_seconds)
122+
123+
env = {
103124
"YOSYS_DATDIR": yosys_runfiles_dir + "/at_clifford_yosys/techlibs/",
104125
"ABC": yosys_runfiles_dir + "/edu_berkeley_abc/abc",
105126
}
106127

107-
if ctx.attr.target_clock_period_pico_seconds:
108-
env["CLOCK_PERIOD"] = str(ctx.attr.target_clock_period_pico_seconds)
128+
for k, v in script_env_files.items():
129+
if type(v) == "File":
130+
env[k] = v.path
131+
else:
132+
env[k] = v
109133

110134
ctx.actions.run(
111135
outputs = [output_file, log_file],
112-
inputs = inputs + tool_inputs.to_list() + [default_liberty_file],
136+
inputs = inputs + tool_inputs.to_list(),
113137
arguments = [args],
114138
executable = ctx.executable.yosys_tool,
115139
tools = tool_inputs,
@@ -129,8 +153,75 @@ def _synthesize_design_impl(ctx):
129153
top_module = ctx.attr.top_module,
130154
log_file = log_file,
131155
),
156+
ExternalSynthesisInfo(
157+
env = script_env_files,
158+
yosys_script = synth_tcl,
159+
inputs = inputs,
160+
verilog_files = verilog_files,
161+
uhdm_files = uhdm_files,
162+
),
132163
]
133164

165+
def _synthesize_binary_impl(ctx):
166+
script = ""
167+
external_info = ctx.attr.synthesize_rtl_rule[ExternalSynthesisInfo]
168+
169+
env = dict(external_info.env)
170+
env["FLIST"] = _create_flist(
171+
ctx,
172+
flist_tag = "verilog",
173+
files = external_info.verilog_files,
174+
short_path = True,
175+
)
176+
177+
env["UHDM_FLIST"] = _create_flist(
178+
ctx,
179+
flist_tag = "uhdm",
180+
files = external_info.uhdm_files,
181+
short_path = True,
182+
)
183+
184+
env["OUTPUT"] = "/tmp/{}.v".format(ctx.attr.name)
185+
186+
script += "#!/bin/bash\n"
187+
188+
for k, v in env.items():
189+
script += "export {}='{}'\n".format(k, v.short_path if type(v) == "File" else v)
190+
191+
yosys_runfiles_dir = ctx.executable.yosys_tool.short_path + ".runfiles"
192+
193+
script += "export YOSYS_DATDIR='{}/at_clifford_yosys/techlibs/'\n".format(yosys_runfiles_dir)
194+
yosys = ctx.attr.yosys_tool[DefaultInfo]
195+
script += "${{PREFIX_COMMAND}} {} -c {}\n".format(ctx.executable.yosys_tool.short_path, external_info.yosys_script.short_path)
196+
197+
binary = ctx.actions.declare_file("{}_synth_binary.sh".format(ctx.attr.name))
198+
ctx.actions.write(binary, script, is_executable = True)
199+
200+
return [
201+
DefaultInfo(
202+
executable = binary,
203+
runfiles = yosys.default_runfiles.merge_all(
204+
[
205+
ctx.runfiles(external_info.inputs),
206+
ctx.runfiles([env["UHDM_FLIST"], env["FLIST"]]),
207+
],
208+
),
209+
),
210+
]
211+
212+
synthesis_binary = rule(
213+
implementation = _synthesize_binary_impl,
214+
attrs = {
215+
"synthesize_rtl_rule": attr.label(providers = [ExternalSynthesisInfo]),
216+
"yosys_tool": attr.label(
217+
default = Label("@at_clifford_yosys//:yosys"),
218+
executable = True,
219+
cfg = "target",
220+
),
221+
},
222+
executable = True,
223+
)
224+
134225
synthesize_rtl = rule(
135226
implementation = _synthesize_design_impl,
136227
attrs = {

0 commit comments

Comments
 (0)