Skip to content

Commit b861cab

Browse files
allightcopybara-github
authored andcommitted
Mark channel flop types in a pass
This is more in line with best practices than doing it during block conversion itself. Bug: #1803 PiperOrigin-RevId: 707951892
1 parent 16602fc commit b861cab

6 files changed

+529
-8
lines changed

xls/codegen/BUILD

+40
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ cc_library(
256256
":codegen_options",
257257
":codegen_pass",
258258
":convert_ir_to_blocks_passes",
259+
":mark_channel_fifos_pass",
259260
":update_channel_metadata_pass",
260261
"//xls/codegen/vast",
261262
"//xls/common/status:ret_check",
@@ -430,6 +431,7 @@ cc_library(
430431
":codegen_pass",
431432
":codegen_wrapper_pass",
432433
":concurrent_stage_groups",
434+
":mark_channel_fifos_pass",
433435
":register_legalization_pass",
434436
"//xls/codegen/vast",
435437
"//xls/common:casts",
@@ -1834,3 +1836,41 @@ cc_test(
18341836
"@com_google_googletest//:gtest",
18351837
],
18361838
)
1839+
1840+
cc_library(
1841+
name = "mark_channel_fifos_pass",
1842+
srcs = ["mark_channel_fifos_pass.cc"],
1843+
hdrs = ["mark_channel_fifos_pass.h"],
1844+
deps = [
1845+
":codegen_options",
1846+
":codegen_pass",
1847+
"//xls/common:casts",
1848+
"//xls/ir:channel",
1849+
"@com_google_absl//absl/status",
1850+
"@com_google_absl//absl/status:statusor",
1851+
],
1852+
)
1853+
1854+
cc_test(
1855+
name = "mark_channel_fifos_pass_test",
1856+
srcs = ["mark_channel_fifos_pass_test.cc"],
1857+
deps = [
1858+
":codegen_options",
1859+
":codegen_pass",
1860+
":mark_channel_fifos_pass",
1861+
"//xls/common:xls_gunit_main",
1862+
"//xls/common/fuzzing:fuzztest",
1863+
"//xls/common/status:matchers",
1864+
"//xls/common/status:ret_check",
1865+
"//xls/common/status:status_macros",
1866+
"//xls/ir",
1867+
"//xls/ir:bits",
1868+
"//xls/ir:channel",
1869+
"//xls/ir:channel_ops",
1870+
"//xls/ir:function_builder",
1871+
"//xls/ir:ir_test_base",
1872+
"//xls/ir:value",
1873+
"@com_google_absl//absl/status:status_matchers",
1874+
"@com_google_googletest//:gtest",
1875+
],
1876+
)

xls/codegen/block_conversion.cc

+16-8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "xls/codegen/codegen_pass.h"
4747
#include "xls/codegen/codegen_wrapper_pass.h"
4848
#include "xls/codegen/concurrent_stage_groups.h"
49+
#include "xls/codegen/mark_channel_fifos_pass.h"
4950
#include "xls/codegen/register_legalization_pass.h"
5051
#include "xls/codegen/vast/vast.h"
5152
#include "xls/common/casts.h"
@@ -1474,13 +1475,12 @@ static absl::Status AddInputOutputFlops(
14741475
for (auto& vec : streaming_io.inputs) {
14751476
for (StreamingInput& input : vec) {
14761477
StreamingChannel* channel = down_cast<StreamingChannel*>(input.channel);
1478+
XLS_RET_CHECK(channel->channel_config().input_flop_kind())
1479+
<< "No input flop kind";
14771480
// TODO(https://github.com/google/xls/issues/1803): This is super hacky.
14781481
// We really should have a different pass that configures all the channels
14791482
// in a separate lowering step.
1480-
FlopKind kind = channel->channel_config().input_flop_kind().value_or(
1481-
options.flop_inputs()
1482-
? CodegenOptions::IOKindToFlopKind(options.flop_inputs_kind())
1483-
: FlopKind::kNone);
1483+
FlopKind kind = *channel->channel_config().input_flop_kind();
14841484
XLS_RETURN_IF_ERROR(AddRegisterAfterStreamingInput(
14851485
input, kind, options.ResetBehavior(), block, valid_nodes));
14861486

@@ -1496,13 +1496,12 @@ static absl::Status AddInputOutputFlops(
14961496
for (auto& vec : streaming_io.outputs) {
14971497
for (StreamingOutput& output : vec) {
14981498
StreamingChannel* channel = down_cast<StreamingChannel*>(output.channel);
1499+
XLS_RET_CHECK(channel->channel_config().output_flop_kind())
1500+
<< "No output flop kind";
14991501
// TODO(https://github.com/google/xls/issues/1803): This is super hacky.
15001502
// We really should have a different pass that configures all the channels
15011503
// in a separate lowering step.
1502-
FlopKind kind = channel->channel_config().output_flop_kind().value_or(
1503-
options.flop_outputs()
1504-
? CodegenOptions::IOKindToFlopKind(options.flop_outputs_kind())
1505-
: FlopKind::kNone);
1504+
FlopKind kind = *channel->channel_config().output_flop_kind();
15061505
XLS_RETURN_IF_ERROR(AddRegisterBeforeStreamingOutput(
15071506
output, kind, options.ResetBehavior(), block, valid_nodes));
15081507

@@ -3401,6 +3400,15 @@ absl::StatusOr<CodegenPassUnit> PackageToPipelinedBlocks(
34013400
module_name);
34023401
CodegenPassUnit unit(package, top_block);
34033402

3403+
// Run codegen passes as appropriate
3404+
{
3405+
MarkChannelFifosPass mark_chans;
3406+
CodegenPassOptions cg_options;
3407+
cg_options.codegen_options = options;
3408+
CodegenPassResults results;
3409+
XLS_RETURN_IF_ERROR(mark_chans.Run(&unit, cg_options, &results).status());
3410+
}
3411+
34043412
for (const auto& [fb, schedule] : sorted_schedules) {
34053413
std::string sub_block_name = block_name_uniquer.GetSanitizedUniqueName(
34063414
SanitizeIdentifier(fb->name()));

xls/codegen/block_conversion_pass_pipeline.cc

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "xls/codegen/codegen_options.h"
2828
#include "xls/codegen/codegen_pass.h"
2929
#include "xls/codegen/convert_ir_to_blocks_passes.h"
30+
#include "xls/codegen/mark_channel_fifos_pass.h"
3031
#include "xls/codegen/update_channel_metadata_pass.h"
3132
#include "xls/codegen/vast/vast.h"
3233
#include "xls/common/status/ret_check.h"
@@ -100,6 +101,7 @@ std::unique_ptr<CodegenCompoundPass> CreateBlockConversionPassPipeline(
100101
top->Add<ConvertFuncsToCombinationalBlocksPass>();
101102
top->Add<ConvertProcsToCombinationalBlocksPass>();
102103
} else {
104+
top->Add<MarkChannelFifosPass>();
103105
top->Add<ConvertFuncsToPipelinedBlocksPass>();
104106
top->Add<ConvertProcsToPipelinedBlocksPass>();
105107
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2024 The XLS Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "xls/codegen/mark_channel_fifos_pass.h"
16+
17+
#include "absl/status/status.h"
18+
#include "absl/status/statusor.h"
19+
#include "xls/codegen/codegen_options.h"
20+
#include "xls/codegen/codegen_pass.h"
21+
#include "xls/common/casts.h"
22+
#include "xls/ir/channel.h"
23+
24+
namespace xls::verilog {
25+
namespace {
26+
FlopKind GetRealFlopKind(bool enabled, CodegenOptions::IOKind kind) {
27+
if (!enabled) {
28+
return FlopKind::kNone;
29+
}
30+
return CodegenOptions::IOKindToFlopKind(kind);
31+
}
32+
} // namespace
33+
34+
absl::StatusOr<bool> MarkChannelFifosPass::RunInternal(
35+
CodegenPassUnit* unit, const CodegenPassOptions& options,
36+
CodegenPassResults* results) const {
37+
if (unit->package->ChannelsAreProcScoped()) {
38+
return absl::UnimplementedError("Proc scoped channels not yet supported");
39+
}
40+
bool changed = false;
41+
for (Channel* chan : unit->package->channels()) {
42+
if (chan->kind() != ChannelKind::kStreaming) {
43+
continue;
44+
}
45+
StreamingChannel* schan = down_cast<StreamingChannel*>(chan);
46+
if (!schan->channel_config().input_flop_kind()) {
47+
schan->channel_config(schan->channel_config().WithInputFlopKind(
48+
GetRealFlopKind(options.codegen_options.flop_inputs(),
49+
options.codegen_options.flop_inputs_kind())));
50+
changed = true;
51+
}
52+
if (!schan->channel_config().output_flop_kind()) {
53+
schan->channel_config(schan->channel_config().WithOutputFlopKind(
54+
GetRealFlopKind(options.codegen_options.flop_outputs(),
55+
options.codegen_options.flop_outputs_kind())));
56+
changed = true;
57+
}
58+
}
59+
return changed;
60+
}
61+
62+
} // namespace xls::verilog

xls/codegen/mark_channel_fifos_pass.h

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2024 The XLS Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef XLS_CODEGEN_MARK_CHANNEL_FIFOS_PASS_H_
16+
#define XLS_CODEGEN_MARK_CHANNEL_FIFOS_PASS_H_
17+
18+
#include "absl/status/statusor.h"
19+
#include "xls/codegen/codegen_pass.h"
20+
namespace xls::verilog {
21+
22+
class MarkChannelFifosPass : public CodegenPass {
23+
public:
24+
MarkChannelFifosPass()
25+
: CodegenPass("mark_channel_fifos",
26+
"Mark Channel with fifo options from codegen") {}
27+
~MarkChannelFifosPass() override = default;
28+
29+
protected:
30+
absl::StatusOr<bool> RunInternal(CodegenPassUnit* unit,
31+
const CodegenPassOptions& options,
32+
CodegenPassResults* results) const override;
33+
};
34+
35+
} // namespace xls::verilog
36+
37+
#endif // XLS_CODEGEN_MARK_CHANNEL_FIFOS_PASS_H_

0 commit comments

Comments
 (0)