Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules/zstd: add buffer module #1167

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
modules/zstd: Add Buffer use-case example
This commit adds a simple test that shows, how one can use the Buffer
struct inside a Proc.

Internal-tag: [#50221]
Signed-off-by: Robert Winkler <rwinkler@antmicro.com>
  • Loading branch information
rw1nkler authored and lpawelcz committed Feb 21, 2024
commit 782887046bb78bdb2c8237dcde789546776287d0
16 changes: 16 additions & 0 deletions xls/modules/zstd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,19 @@ xls_dslx_test(
name = "buffer_dslx_test",
library = ":buffer_dslx",
)

xls_dslx_library(
name = "window_buffer_dslx",
srcs = [
"window_buffer.x",
],
deps = [
":buffer_dslx",
],
)

xls_dslx_test(
name = "window_buffer_dslx_test",
library = ":window_buffer_dslx",
)

134 changes: 134 additions & 0 deletions xls/modules/zstd/window_buffer.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2024 The XLS Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file contains the implementation of a Proc which can be used to
// receive data through transactions of one width and output that data
// in transactions of other width.

import std;
import xls.modules.zstd.buffer as buff;

type Buffer = buff::Buffer;

// WindowBuffer is a simple Proc that uses the Buffer structure to aggregate data
// in transactions of <INPUT_WIDTH> length and output it in transactions of
// <OUTPUT_WIDTH> length. <BUFFER_SIZE> defines the maximal size of the buffer.

proc WindowBuffer<BUFFER_SIZE: u32, INPUT_WIDTH: u32, OUTPUT_WIDTH: u32> {
Copy link
Member

@proppy proppy Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a relation between BUFFER_SIZE, INPUT_WIDTH and OUTPUT_WIDTH that we can const_assert?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can for sure assert BUFFER_SIZE >= INPUT WIDTH and BUFFER_SIZE >= OUTPUT_WIDTH.

input_r: chan<uN[INPUT_WIDTH]> in;
output_s: chan<uN[OUTPUT_WIDTH]> out;

config(
input_r: chan<uN[INPUT_WIDTH]> in,
output_s: chan<uN[OUTPUT_WIDTH]> out
) { (input_r, output_s) }

init { buff::buffer_new<BUFFER_SIZE>() }

next(tok: token, buffer: Buffer<BUFFER_SIZE>) {
const_assert!(BUFFER_SIZE >= INPUT_WIDTH);
const_assert!(BUFFER_SIZE >= OUTPUT_WIDTH);
let (tok, recv_data, valid) = recv_non_blocking(tok, input_r, uN[INPUT_WIDTH]:0);
let buffer = if (valid) {
buff::buffer_append(buffer, recv_data)
} else {
buffer
};

if buffer.length >= OUTPUT_WIDTH {
let (buffer, data_to_send) = buff::buffer_fixed_pop<OUTPUT_WIDTH>(buffer);
let tok = send(tok, output_s, data_to_send);
buffer
} else {
buffer
}
}
}

#[test_proc]
proc WindowBufferTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a test with INPUT_WIDTH > OUTPUT_WIDTH for completeness?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

terminator: chan<bool> out;
data32_s: chan<u32> out;
data48_r: chan<u48> in;

config(terminator: chan<bool> out) {
let (data32_s, data32_r) = chan<u32>;
let (data48_s, data48_r) = chan<u48>;
spawn WindowBuffer<u32:64, u32:32, u32:48>(data32_r, data48_s);
(terminator, data32_s, data48_r)
}

init {}

next(tok: token, state: ()) {
let tok = send(tok, data32_s, u32:0xDEADBEEF);
let tok = send(tok, data32_s, u32:0xBEEFCAFE);
let tok = send(tok, data32_s, u32:0xCAFEDEAD);

let (tok, received_data) = recv(tok, data48_r);
assert_eq(received_data, u48:0xCAFE_DEAD_BEEF);
let (tok, received_data) = recv(tok, data48_r);
assert_eq(received_data, u48:0xCAFE_DEAD_BEEF);

send(tok, terminator, true);
}
}

#[test_proc]
proc WindowBufferReverseTest {
terminator: chan<bool> out;
data48_s: chan<u48> out;
data32_r: chan<u32> in;

config(terminator: chan<bool> out) {
let (data48_s, data48_r) = chan<u48>;
let (data32_s, data32_r) = chan<u32>;
spawn WindowBuffer<u32:64, u32:48, u32:32>(data48_r, data32_s);
(terminator, data48_s, data32_r)
}

init {}

next(tok: token, state: ()) {
let tok = send(tok, data48_s, u48:0xCAFEDEADBEEF);
let tok = send(tok, data48_s, u48:0xCAFEDEADBEEF);

let (tok, received_data) = recv(tok, data32_r);
assert_eq(received_data, u32:0xDEADBEEF);
let (tok, received_data) = recv(tok, data32_r);
assert_eq(received_data, u32:0xBEEFCAFE);
let (tok, received_data) = recv(tok, data32_r);
assert_eq(received_data, u32:0xCAFEDEAD);

send(tok, terminator, true);
}
}

// Sample for codegen
proc WindowBuffer64 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding this!

input_r: chan<u32> in;
output_s: chan<u48> out;

config(
input_r: chan<u32> in,
output_s: chan<u48> out
) {
spawn WindowBuffer<u32:64, u32:32, u32:48>(input_r, output_s);
(input_r, output_s)
}

init {}

next(tok: token, state: ()) {}
}