Skip to content

Commit

Permalink
examples: Add parameterized design example
Browse files Browse the repository at this point in the history
This example showcases the usage of proto_to_dslx tool in DSLX design
parameterization.
It defines a protobuf message containing design parameters:

* input width,
* output width

A textproto file with a variant of the design is provided.
Textproto file can be converted to DSLX file which is then imported
by top-level design in order to use the parameters.

The design itself has 2 inputs and one output. The widths of those are
controlled by parameters.

Verilog file for the variant can be generated with:

```
bazel build //xls/examples/parameterized:design_verilog
```

Signed-off-by: Pawel Czarnecki <pczarnecki@antmicro.com>
  • Loading branch information
lpawelcz committed Dec 14, 2023
1 parent c37e207 commit 9e27804
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
58 changes: 58 additions & 0 deletions xls/examples/parameterized/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
load("//xls/build_rules:xls_utilities.bzl", "proto_to_dslx")
load(
"//xls/build_rules:xls_build_defs.bzl",
"xls_dslx_ir",
"xls_dslx_library",
"xls_ir_opt_ir",
"xls_ir_verilog",
)

proto_library(
name = "proto_schema_lib",
srcs = [":param.proto"],
)

proto_to_dslx(
name = "parameters",
proto_schema = ":proto_schema_lib",
proto_name = "xls.DesignVariants",
textproto = ":parameters.textproto",
)

xls_dslx_library(
name = "parameters_lib",
srcs = [":parameters"],
)

xls_dslx_library(
name = "design_lib",
srcs = [
"design.x"
],
deps = [":parameters_lib"],
)

xls_dslx_ir(
name = "design_ir",
dslx_top = "top",
ir_file = "desing.ir",
library = "design_lib",
)

xls_ir_opt_ir(
name = "design_opt_ir",
src = "desing.ir",
top = "__design__top",
)

xls_ir_verilog(
name = "design_verilog",
src = ":design_opt_ir.opt.ir",
codegen_args = {
"module_name": "top",
"delay_model": "unit",
"pipeline_stages": "1",
"use_system_verilog": "false",
},
verilog_file = "design.v",
)
19 changes: 19 additions & 0 deletions xls/examples/parameterized/design.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import xls.examples.parameterized.parameters

type DesignParameters = parameters::DesignParameters;

fn add<INPUT_WIDTH: u32, OUTPUT_WIDTH: u32>(x: uN[INPUT_WIDTH], y: uN[INPUT_WIDTH]) -> uN[OUTPUT_WIDTH] {
let lhs = x as uN[OUTPUT_WIDTH];
let rhs = y as uN[OUTPUT_WIDTH];
lhs + rhs
}

fn top(x: u64, y: u64) -> u64 {
let variant: DesignParameters = parameters::parameters.variant[0];
let variant_input_width: u32 = variant.input_width;
let variant_output_width: u32 = variant.output_width;
let arg_x = x as uN[variant_input_width];
let arg_y = y as uN[variant_input_width];

add<variant_input_width, variant_output_width>(arg_x, arg_y) as u64
}
12 changes: 12 additions & 0 deletions xls/examples/parameterized/param.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto2";

package xls;

message DesignParameters {
optional uint32 input_width = 1;
optional uint32 output_width = 2;
}

message DesignVariants {
repeated DesignParameters variant = 1;
}
5 changes: 5 additions & 0 deletions xls/examples/parameterized/parameters.textproto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
variant {
input_width: 1
output_width: 2
}

0 comments on commit 9e27804

Please sign in to comment.