google / google/xls

[enhancement] Add proc-scoped constants

Open
#1,580 1 comment 0 reactions 0 assignees View on GitHub
dslx enhancement
Dominant language
C++
Stars
1.9k
Forks
283
Avg merge
2d 10h
Merged PRs (30d)
135

Description

### What's hard to do? (limit 100 words)

Currently, the only way to create a constant available in the entire proc's scope is by using a parameter with a default value. However, this exposes the parameter to user modification, which can lead to unintended behavior. It would be useful to define immutable constants that are accessible across the entire proc without risking alteration. The proc-scoped constants would be particularly useful when used for defining channels.

There are many cases in the examples when something that should be defined as a constant is defined as a parameter. Here is the `DelayInternal` proc from the [delay.x](https://github.com/google/xls/blob/main/xls/examples/delay.x#L76) file:
```
proc DelayInternal {
data_in: chan in;
data_out: chan out;
ram_req: chan> out;
ram_resp: chan> in;
ram_wr_comp: chan<()> in;
```
In this case at least `DOUBLE_DATA_WIDTH` and `HALF_FLOOR_DELAY` should be defined as constants:
```
proc DelayInternal {

const DOUBLE_DATA_WIDTH = double(DATA_WIDTH);
const HALF_FLOOR_DELAY = half_floor(DELAY);

data_in: chan in;
data_out: chan out;
ram_req: chan> out;
ram_resp: chan> in;
ram_wr_comp: chan<()> in;
```

### Current best alternative workaround (limit 100 words)

The desired behavior can be achieved by defining the same constant using the `const` keyword in the `init`, `next`, and `config` functions

### Your view of the "best case XLS enhancement" (limit 100 words)

Allow proc-scoped `const` declarations, similar to how proc-scoped type aliases are handled now

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.