Skip to content

Commit e860197

Browse files
ericastorcopybara-github
authored andcommitted
[codegen 1.5] Enable the simplest proc test for new block conversion
This required small patches to channel & state lowering, as well as to how we transfer Nodes into ScheduledBlocks: 1. Channel lowering now handles single-value channnels without crashing in some circumstances. (Whoops.) 2. State lowering now cleans up after itself. 3. ScheduledBlock conversion now takes ownership of the node name as well as the node. We also add dataflow simplification to the pipeline (matching codegen 1.0). PiperOrigin-RevId: 849163225
1 parent b9ba251 commit e860197

25 files changed

+3323
-123
lines changed

xls/codegen_v_1_5/BUILD

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,20 @@ cc_library(
4141
hdrs = ["block_conversion_pass_pipeline.h"],
4242
deps = [
4343
":block_conversion_pass",
44+
":block_conversion_wrapper_pass",
4445
":block_finalization_pass",
4546
":channel_to_port_io_lowering_pass",
4647
":flow_control_insertion_pass",
4748
":function_io_lowering_pass",
4849
":idle_insertion_pass",
4950
":pipeline_register_insertion_pass",
51+
":register_cleanup_pass",
5052
":scheduled_block_conversion_pass",
5153
":scheduling_pass",
5254
":state_to_register_io_lowering_pass",
55+
"//xls/passes:dataflow_simplification_pass",
56+
"//xls/passes:dce_pass",
57+
"//xls/passes:optimization_pass",
5358
],
5459
)
5560

@@ -93,15 +98,127 @@ cc_test(
9398
],
9499
)
95100

101+
cc_library(
102+
name = "register_cleanup_pass",
103+
srcs = ["register_cleanup_pass.cc"],
104+
hdrs = ["register_cleanup_pass.h"],
105+
deps = [
106+
":block_conversion_pass",
107+
"//xls/common/status:status_macros",
108+
"//xls/data_structures:transitive_closure",
109+
"//xls/ir",
110+
"//xls/ir:register",
111+
"//xls/ir:value",
112+
"//xls/ir:value_utils",
113+
"//xls/passes:node_dependency_analysis",
114+
"//xls/passes:partial_info_query_engine",
115+
"//xls/passes:pass_base",
116+
"//xls/passes:query_engine",
117+
"@com_google_absl//absl/algorithm:container",
118+
"@com_google_absl//absl/container:flat_hash_map",
119+
"@com_google_absl//absl/container:flat_hash_set",
120+
"@com_google_absl//absl/status:statusor",
121+
"@com_google_absl//absl/types:span",
122+
],
123+
)
124+
125+
cc_library(
126+
name = "block_conversion_wrapper_pass",
127+
srcs = ["block_conversion_wrapper_pass.cc"],
128+
hdrs = ["block_conversion_wrapper_pass.h"],
129+
deps = [
130+
":block_conversion_pass",
131+
"//xls/ir",
132+
"//xls/passes:optimization_pass",
133+
"//xls/passes:pass_base",
134+
"@com_google_absl//absl/status:statusor",
135+
"@com_google_absl//absl/strings:str_format",
136+
"@com_google_absl//absl/types:span",
137+
],
138+
)
139+
140+
cc_test(
141+
name = "block_conversion_wrapper_pass_test",
142+
srcs = ["block_conversion_wrapper_pass_test.cc"],
143+
deps = [
144+
":block_conversion_pass",
145+
":block_conversion_wrapper_pass",
146+
"//xls/common:xls_gunit_main",
147+
"//xls/common/status:matchers",
148+
"//xls/ir",
149+
"//xls/ir:function_builder",
150+
"//xls/ir:ir_matcher",
151+
"//xls/ir:ir_test_base",
152+
"//xls/passes:dce_pass",
153+
"//xls/passes:optimization_pass",
154+
"//xls/passes:pass_base",
155+
"@com_google_absl//absl/status:statusor",
156+
"@googletest//:gtest",
157+
],
158+
)
159+
96160
cc_library(
97161
name = "channel_to_port_io_lowering_pass",
98162
srcs = ["channel_to_port_io_lowering_pass.cc"],
99163
hdrs = ["channel_to_port_io_lowering_pass.h"],
100164
deps = [
101165
":block_conversion_pass",
166+
"//xls/codegen:conversion_utils",
167+
"//xls/codegen:ram_configuration",
168+
"//xls/common:casts",
169+
"//xls/common/status:ret_check",
170+
"//xls/common/status:status_macros",
171+
"//xls/ir",
172+
"//xls/ir:bits",
173+
"//xls/ir:channel",
174+
"//xls/ir:node_util",
175+
"//xls/ir:op",
176+
"//xls/ir:register",
177+
"//xls/ir:source_location",
178+
"//xls/ir:value",
179+
"//xls/ir:value_utils",
180+
"//xls/passes:bdd_query_engine",
181+
"//xls/passes:pass_base",
182+
"//xls/public:function_builder",
183+
"@com_google_absl//absl/algorithm:container",
184+
"@com_google_absl//absl/base:core_headers",
185+
"@com_google_absl//absl/container:btree",
186+
"@com_google_absl//absl/container:flat_hash_map",
187+
"@com_google_absl//absl/container:flat_hash_set",
188+
"@com_google_absl//absl/container:inlined_vector",
189+
"@com_google_absl//absl/log:check",
190+
"@com_google_absl//absl/status",
191+
"@com_google_absl//absl/status:statusor",
192+
"@com_google_absl//absl/strings",
193+
"@com_google_absl//absl/strings:str_format",
194+
"@com_google_absl//absl/types:span",
195+
"@cppitertools",
196+
],
197+
)
198+
199+
cc_test(
200+
name = "channel_to_port_io_lowering_pass_test",
201+
srcs = ["channel_to_port_io_lowering_pass_test.cc"],
202+
deps = [
203+
":block_conversion_pass",
204+
":channel_to_port_io_lowering_pass",
205+
":scheduled_block_conversion_pass",
206+
"//xls/codegen:codegen_options",
207+
"//xls/common:xls_gunit_main",
208+
"//xls/common/status:matchers",
209+
"//xls/common/status:status_macros",
102210
"//xls/ir",
211+
"//xls/ir:bits",
212+
"//xls/ir:channel",
213+
"//xls/ir:channel_ops",
214+
"//xls/ir:function_builder",
215+
"//xls/ir:ir_matcher",
216+
"//xls/ir:ir_test_base",
217+
"//xls/ir:op",
218+
"//xls/ir:value",
103219
"//xls/passes:pass_base",
104220
"@com_google_absl//absl/status:statusor",
221+
"@googletest//:gtest",
105222
],
106223
)
107224

@@ -117,6 +234,7 @@ cc_library(
117234
"//xls/common/status:status_macros",
118235
"//xls/estimators/delay_model:delay_estimator",
119236
"//xls/ir",
237+
"//xls/passes:optimization_pass",
120238
"//xls/passes:pass_base",
121239
"//xls/scheduling:pipeline_schedule_cc_proto",
122240
"//xls/scheduling:scheduling_options",

xls/codegen_v_1_5/block_conversion_pass_pipeline.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,25 @@
2020
#include <memory>
2121

2222
#include "xls/codegen_v_1_5/block_conversion_pass.h"
23+
#include "xls/codegen_v_1_5/block_conversion_wrapper_pass.h"
2324
#include "xls/codegen_v_1_5/block_finalization_pass.h"
2425
#include "xls/codegen_v_1_5/channel_to_port_io_lowering_pass.h"
2526
#include "xls/codegen_v_1_5/flow_control_insertion_pass.h"
2627
#include "xls/codegen_v_1_5/function_io_lowering_pass.h"
2728
#include "xls/codegen_v_1_5/idle_insertion_pass.h"
2829
#include "xls/codegen_v_1_5/pipeline_register_insertion_pass.h"
30+
#include "xls/codegen_v_1_5/register_cleanup_pass.h"
2931
#include "xls/codegen_v_1_5/scheduled_block_conversion_pass.h"
3032
#include "xls/codegen_v_1_5/scheduling_pass.h"
3133
#include "xls/codegen_v_1_5/state_to_register_io_lowering_pass.h"
34+
#include "xls/passes/dataflow_simplification_pass.h"
35+
#include "xls/passes/dce_pass.h"
36+
#include "xls/passes/optimization_pass.h"
3237

3338
namespace xls::codegen {
3439

35-
std::unique_ptr<BlockConversionCompoundPass>
36-
CreateBlockConversionPassPipeline() {
40+
std::unique_ptr<BlockConversionCompoundPass> CreateBlockConversionPassPipeline(
41+
OptimizationContext& opt_context) {
3742
auto top = std::make_unique<BlockConversionCompoundPass>(
3843
"block_conversion", "Top level codegen v1.5 block conversion pipeline");
3944

@@ -64,6 +69,18 @@ CreateBlockConversionPassPipeline() {
6469
// Lower scheduled block to standard block, inlining each stage.
6570
top->Add<BlockFinalizationPass>();
6671

72+
// Clean up unused registers & load-enable bits (including flow-control
73+
// registers).
74+
top->Add<RegisterCleanupPass>();
75+
76+
// Clean up unnecessary array/tuple manipulation.
77+
top->Add<BlockConversionWrapperPass>(
78+
std::make_unique<DataflowSimplificationPass>(), opt_context);
79+
80+
// Remove anything we created & then left dead.
81+
top->Add<BlockConversionWrapperPass>(
82+
std::make_unique<DeadCodeEliminationPass>(), opt_context);
83+
6784
return top;
6885
}
6986

xls/codegen_v_1_5/block_conversion_pass_pipeline.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
#include <memory>
1919

2020
#include "xls/codegen_v_1_5/block_conversion_pass.h"
21+
#include "xls/passes/optimization_pass.h"
2122

2223
namespace xls::codegen {
2324

2425
// Returns a pipeline which converts an unscheduled IR package into a standard
2526
// block.
26-
std::unique_ptr<BlockConversionCompoundPass>
27-
CreateBlockConversionPassPipeline();
27+
std::unique_ptr<BlockConversionCompoundPass> CreateBlockConversionPassPipeline(
28+
OptimizationContext& opt_context);
2829

2930
} // namespace xls::codegen
3031

xls/codegen_v_1_5/block_conversion_pass_pipeline_test.cc

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
#include "xls/common/status/matchers.h"
5555
#include "xls/common/status/ret_check.h"
5656
#include "xls/common/status/status_macros.h"
57-
#include "xls/estimators/delay_model/delay_estimator.h"
5857
#include "xls/interpreter/block_evaluator.h"
5958
#include "xls/interpreter/block_interpreter.h"
6059
#include "xls/ir/bits.h"
@@ -97,6 +96,7 @@ using ::testing::ElementsAre;
9796
using ::testing::Eq;
9897
using ::testing::Ge;
9998
using ::testing::HasSubstr;
99+
using ::testing::IsEmpty;
100100
using ::testing::Optional;
101101
using ::testing::Pair;
102102
using ::testing::SizeIs;
@@ -282,7 +282,7 @@ TEST_F(BlockConversionTest, SimpleFunction) {
282282
m::OutputPort("out", m::Add(m::InputPort("x"), m::InputPort("y"))));
283283
}
284284

285-
TEST_F(BlockConversionTest, DISABLED_SimpleFunctionWithNamedOutput) {
285+
TEST_F(BlockConversionTest, SimpleFunctionWithNamedOutput) {
286286
auto p = CreatePackage();
287287
FunctionBuilder fb(TestName(), p.get());
288288
BValue x = fb.Param("x", p->GetBitsType(32));
@@ -331,7 +331,7 @@ TEST_F(BlockConversionTest, ZeroWidthInputsAndOutput) {
331331
EXPECT_EQ(block->GetPorts().size(), 4);
332332
}
333333

334-
TEST_F(BlockConversionTest, DISABLED_SimplePipelinedFunction) {
334+
TEST_F(BlockConversionTest, SimplePipelinedFunction) {
335335
auto p = CreatePackage();
336336
FunctionBuilder fb(TestName(), p.get());
337337
BValue x = fb.Param("x", p->GetBitsType(32));
@@ -348,11 +348,13 @@ TEST_F(BlockConversionTest, DISABLED_SimplePipelinedFunction) {
348348
SchedulingOptions().pipeline_stages(3)));
349349

350350
EXPECT_THAT(GetOutputPort(block),
351-
m::OutputPort(m::Neg(m::Register(m::Not(m::Register(
352-
m::Add(m::InputPort("x"), m::InputPort("y"))))))));
351+
m::OutputPort(m::Neg(m::Register(m::Not(
352+
m::Register(m::Add(m::InputPort("x"), m::InputPort("y"))))))))
353+
<< "\n\nIR:\n"
354+
<< block->DumpIr();
353355
}
354356

355-
TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionNoFlopping) {
357+
TEST_F(BlockConversionTest, TrivialPipelinedFunctionNoFlopping) {
356358
auto p = CreatePackage();
357359
FunctionBuilder fb(TestName(), p.get());
358360
BValue x = fb.Param("x", p->GetBitsType(32));
@@ -373,7 +375,7 @@ TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionNoFlopping) {
373375
m::Add(m::InputPort("x"), m::InputPort("y"))))))));
374376
}
375377

376-
TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionFloppedInputs) {
378+
TEST_F(BlockConversionTest, TrivialPipelinedFunctionFloppedInputs) {
377379
auto p = CreatePackage();
378380
FunctionBuilder fb(TestName(), p.get());
379381
BValue x = fb.Param("x", p->GetBitsType(32));
@@ -395,7 +397,7 @@ TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionFloppedInputs) {
395397
m::Register(m::InputPort("x")), m::Register(m::InputPort("y")))))))));
396398
}
397399

398-
TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionFloppedOutputs) {
400+
TEST_F(BlockConversionTest, TrivialPipelinedFunctionFloppedOutputs) {
399401
auto p = CreatePackage();
400402
FunctionBuilder fb(TestName(), p.get());
401403
BValue x = fb.Param("x", p->GetBitsType(32));
@@ -416,7 +418,7 @@ TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionFloppedOutputs) {
416418
m::Add(m::InputPort("x"), m::InputPort("y")))))))));
417419
}
418420

419-
TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionFloppedIo) {
421+
TEST_F(BlockConversionTest, TrivialPipelinedFunctionFloppedIo) {
420422
auto p = CreatePackage();
421423
FunctionBuilder fb(TestName(), p.get());
422424
BValue x = fb.Param("x", p->GetBitsType(32));
@@ -438,7 +440,7 @@ TEST_F(BlockConversionTest, DISABLED_TrivialPipelinedFunctionFloppedIo) {
438440
m::Register(m::InputPort("y"))))))))));
439441
}
440442

441-
TEST_F(BlockConversionTest, DISABLED_ZeroWidthPipeline) {
443+
TEST_F(BlockConversionTest, ZeroWidthPipeline) {
442444
auto p = CreatePackage();
443445
FunctionBuilder fb(TestName(), p.get());
444446
BValue x = fb.Param("x", p->GetTupleType({}));
@@ -454,7 +456,28 @@ TEST_F(BlockConversionTest, DISABLED_ZeroWidthPipeline) {
454456
"clk"),
455457
SchedulingOptions().pipeline_stages(3)));
456458

457-
EXPECT_EQ(block->GetRegisters().size(), 4);
459+
EXPECT_THAT(block->GetRegisters(), IsEmpty());
460+
}
461+
462+
TEST_F(BlockConversionTest, ZeroWidthPipelineWithValidControl) {
463+
auto p = CreatePackage();
464+
FunctionBuilder fb(TestName(), p.get());
465+
BValue x = fb.Param("x", p->GetTupleType({}));
466+
BValue y = fb.Param("y", p->GetBitsType(0));
467+
XLS_ASSERT_OK_AND_ASSIGN(Function * f,
468+
fb.BuildWithReturnValue(fb.Tuple({x, y})));
469+
470+
XLS_ASSERT_OK_AND_ASSIGN(
471+
Block * block,
472+
ConvertToBlock(f,
473+
CodegenOptions()
474+
.flop_inputs(false)
475+
.flop_outputs(false)
476+
.clock_name("clk")
477+
.valid_control("inputs_valid", "output_valid"),
478+
SchedulingOptions().pipeline_stages(3)));
479+
480+
EXPECT_THAT(block->GetRegisters(), SizeIs(2));
458481
}
459482

460483
// Verifies that an implicit token, as generated by the DSLX IR converter, is
@@ -473,11 +496,10 @@ fn __itok__implicit_token__main(__token: token, __activated: bits[1]) ->
473496
fn __implicit_token__main() -> () {
474497
after_all.9: token = after_all(id=9)
475498
literal.10: bits[1] = literal(value=1, id=10)
476-
invoke.11: (token, ()) = invoke(after_all.9, literal.10,
477-
to_apply=__itok__implicit_token__main, id=11) tuple_index.12: token =
478-
tuple_index(invoke.11, index=0, id=12) invoke.13: (token, ()) =
479-
invoke(tuple_index.12, literal.10, to_apply=__itok__implicit_token__main,
480-
id=13) ret tuple_index.14: () = tuple_index(invoke.13, index=1, id=14)
499+
invoke.11: (token, ()) = invoke(after_all.9, literal.10, to_apply=__itok__implicit_token__main, id=11)
500+
tuple_index.12: token = tuple_index(invoke.11, index=0, id=12)
501+
invoke.13: (token, ()) = invoke(tuple_index.12, literal.10, to_apply=__itok__implicit_token__main, id=13)
502+
ret tuple_index.14: () = tuple_index(invoke.13, index=1, id=14)
481503
}
482504
)";
483505
XLS_ASSERT_OK_AND_ASSIGN(auto p, Parser::ParsePackage(kIrText));
@@ -489,7 +511,7 @@ fn __implicit_token__main() -> () {
489511
XLS_ASSERT_OK(VerifyBlock(block));
490512
}
491513

492-
TEST_F(BlockConversionTest, DISABLED_SimpleProc) {
514+
TEST_F(BlockConversionTest, SimpleProc) {
493515
const std::string ir_text = R"(package test
494516
495517
chan in(bits[32], id=0, kind=single_value, ops=receive_only)
@@ -513,7 +535,9 @@ proc my_proc(my_state: (), init={()}) {
513535
Block * block,
514536
ConvertToBlock(proc, codegen_options().generate_combinational(true)));
515537
EXPECT_THAT(FindNode("out", block),
516-
m::OutputPort("out", m::Neg(m::InputPort("in"))));
538+
m::OutputPort("out", m::Neg(m::InputPort("in"))))
539+
<< "\n\nIR:\n"
540+
<< block->DumpIr();
517541
}
518542

519543
TEST_F(BlockConversionTest, DISABLED_StreamingChannelMetadataForSimpleProc) {
@@ -6394,7 +6418,7 @@ TEST_F(ProcConversionTestFixture,
63946418
}
63956419
}
63966420

6397-
TEST_F(ProcConversionTestFixture, DISABLED_SimpleFunctionWithProcsPresent) {
6421+
TEST_F(ProcConversionTestFixture, SimpleFunctionWithProcsPresent) {
63986422
XLS_ASSERT_OK_AND_ASSIGN(std::unique_ptr<Package> p,
63996423
CreateMultiProcPackage(/*with_functions=*/true));
64006424
XLS_ASSERT_OK_AND_ASSIGN(Function * f0, p->GetFunction("f0"));

0 commit comments

Comments
 (0)