Skip to content

Commit 246b80b

Browse files
proppycopybara-github
authored andcommitted
xls/codegen: emit logic instead of reg for sv signals
Fixes #3104 PiperOrigin-RevId: 810146567
1 parent 9f5ccdf commit 246b80b

File tree

2 files changed

+84
-60
lines changed

2 files changed

+84
-60
lines changed

xls/codegen/vast/vast.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ void LineInfoIncrease(LineInfo* line_info, int64_t delta) {
8181
// Converts a `DataKind` to its SystemVerilog name, if any. Emitting a data type
8282
// in most contexts requires the containing entity to emit both the `DataKind`
8383
// and the `DataType`, at least one of which should emit as nonempty.
84-
std::string DataKindToString(DataKind kind) {
84+
std::string DataKindToString(DataKind kind, bool system_verilog) {
8585
switch (kind) {
8686
case DataKind::kReg:
87-
return "reg";
87+
return system_verilog ? "logic" : "reg";
8888
case DataKind::kWire:
8989
return "wire";
9090
case DataKind::kLogic:
@@ -615,8 +615,9 @@ std::string VerilogFunction::Emit(LineInfo* line_info) const {
615615
if (return_value_def_->data_type()->IsScalar() &&
616616
file()->use_system_verilog()) {
617617
// Preface the return type with "logic", so there's always a type provided.
618-
return_type =
619-
absl::StrCat(DataKindToString(DataKind::kLogic), " ", return_type);
618+
return_type = absl::StrCat(
619+
DataKindToString(DataKind::kLogic, file()->use_system_verilog()), " ",
620+
return_type);
620621
}
621622
if (!return_type.empty()) {
622623
return_type = absl::StrCat(" ", return_type);
@@ -705,7 +706,8 @@ absl::StatusOr<LogicRef*> Module::AddReg(std::string_view name, DataType* type,
705706
const SourceInfo& loc,
706707
Expression* init,
707708
ModuleSection* section) {
708-
XLS_RETURN_IF_ERROR(NoteDefined(&defined_names_, name, "reg"));
709+
XLS_RETURN_IF_ERROR(NoteDefined(
710+
&defined_names_, name, file()->use_system_verilog() ? "logic" : "reg"));
709711
return AddRegInternal(name, type, loc, init, section);
710712
}
711713

@@ -1064,7 +1066,8 @@ std::string Def::Emit(LineInfo* line_info) const {
10641066

10651067
std::string Def::EmitNoSemi(LineInfo* line_info) const {
10661068
LineInfoStart(line_info, this);
1067-
std::string kind_str = DataKindToString(data_kind());
1069+
std::string kind_str =
1070+
DataKindToString(data_kind(), file()->use_system_verilog());
10681071
std::string data_type_str =
10691072
data_type()->EmitWithIdentifier(line_info, GetName());
10701073
std::string result = CombineKindAndDataType(kind_str, data_type_str);
@@ -1501,7 +1504,8 @@ std::string Enum::Emit(LineInfo* line_info) const {
15011504
LineInfoStart(line_info, this);
15021505
std::string result = "enum {\n";
15031506
if (kind_ != DataKind::kUntypedEnum) {
1504-
std::string kind_str = DataKindToString(kind_);
1507+
std::string kind_str =
1508+
DataKindToString(kind_, file()->use_system_verilog());
15051509
std::string data_type_str = BaseType()->Emit(line_info);
15061510
std::string underlying_type_str =
15071511
CombineKindAndDataType(kind_str, data_type_str);

xls/codegen/vast/vast_test.cc

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "absl/status/status_matchers.h"
2828
#include "absl/strings/str_cat.h"
2929
#include "absl/strings/str_format.h"
30+
#include "absl/strings/substitute.h"
3031
#include "absl/types/span.h"
3132
#include "xls/common/status/matchers.h"
3233
#include "xls/ir/bits.h"
@@ -420,24 +421,25 @@ TEST_P(VastTest, ModuleWithManyVariableDefinitions) {
420421
f.Concat({a_ref, a_ref, a_ref, a_ref}, SourceInfo()));
421422
LineInfo line_info;
422423
EXPECT_EQ(module->Emit(&line_info),
423-
R"(module my_module(
424+
absl::Substitute(R"(module my_module(
424425
input wire a,
425426
output wire [3:0] b,
426427
input wire [7:0][41:0][2:0] array
427428
);
428-
reg r1;
429-
reg [1:0] r2;
430-
reg [0:0] r1_init = 1;
431-
reg [41:0] s;
432-
reg [41:0] s_init = 42'h000_0000_007b;
433-
reg [41:0][7:0][8 + 42 - 1:0] t;
434-
reg signed [7:0] signed_foo;
429+
$0 r1;
430+
$0 [1:0] r2;
431+
$0 [0:0] r1_init = 1;
432+
$0 [41:0] s;
433+
$0 [41:0] s_init = 42'h000_0000_007b;
434+
$0 [41:0][7:0][8 + 42 - 1:0] t;
435+
$0 signed [7:0] signed_foo;
435436
wire x;
436437
wire y;
437438
wire signed [3 * 3 - 1:0][7:0][8 + 42 - 1:0] z;
438439
integer i;
439440
assign b = {a, a, a, a};
440-
endmodule)");
441+
endmodule)",
442+
UseSystemVerilog() ? "logic" : "reg"));
441443

442444
EXPECT_EQ(line_info.LookupNode(module).value(),
443445
std::vector<LineSpan>{LineSpan(0, 17)});
@@ -489,20 +491,22 @@ TEST_P(VastTest, ModuleWithUnpackedArrayRegWithSize) {
489491
f.Index(f.Index(arr_ref, 2, SourceInfo()), 1, SourceInfo()));
490492
if (UseSystemVerilog()) {
491493
EXPECT_EQ(module->Emit(nullptr),
492-
R"(module my_module(
494+
absl::Substitute(R"(module my_module(
493495
output wire [63:0] out
494496
);
495-
reg [3:0] arr[8][64];
497+
$0 [3:0] arr[8][64];
496498
assign out = arr[2][1];
497-
endmodule)");
499+
endmodule)",
500+
UseSystemVerilog() ? "logic" : "reg"));
498501
} else {
499502
EXPECT_EQ(module->Emit(nullptr),
500-
R"(module my_module(
503+
absl::Substitute(R"(module my_module(
501504
output wire [63:0] out
502505
);
503-
reg [3:0] arr[0:7][0:63];
506+
$0 [3:0] arr[0:7][0:63];
504507
assign out = arr[2][1];
505-
endmodule)");
508+
endmodule)",
509+
UseSystemVerilog() ? "logic" : "reg"));
506510
}
507511
}
508512

@@ -532,20 +536,22 @@ TEST_P(VastTest, ModuleWithUnpackedArrayRegWithPackedDims) {
532536
f.Index(f.Index(arr_ref, 2, SourceInfo()), 1, SourceInfo()));
533537
if (UseSystemVerilog()) {
534538
EXPECT_EQ(module->Emit(nullptr),
535-
R"(module my_module(
539+
absl::Substitute(R"(module my_module(
536540
output wire [63:0] out
537541
);
538-
reg [3:0][41:0][6:0] arr[8][64];
542+
$0 [3:0][41:0][6:0] arr[8][64];
539543
assign out = arr[2][1];
540-
endmodule)");
544+
endmodule)",
545+
UseSystemVerilog() ? "logic" : "reg"));
541546
} else {
542547
EXPECT_EQ(module->Emit(nullptr),
543-
R"(module my_module(
548+
absl::Substitute(R"(module my_module(
544549
output wire [63:0] out
545550
);
546-
reg [3:0][41:0][6:0] arr[0:7][0:63];
551+
$0 [3:0][41:0][6:0] arr[0:7][0:63];
547552
assign out = arr[2][1];
548-
endmodule)");
553+
endmodule)",
554+
UseSystemVerilog() ? "logic" : "reg"));
549555
}
550556
}
551557

@@ -1498,16 +1504,17 @@ TEST_P(VastTest, ParameterAndLocalParam) {
14981504
(void)state; // unused
14991505

15001506
EXPECT_EQ(m->Emit(nullptr),
1501-
R"(module top;
1507+
absl::Substitute(R"(module top;
15021508
parameter ClocksPerBaud = `DEFAULT_CLOCKS_PER_BAUD;
15031509
parameter logic [15:0] ParamWithDef = 5;
15041510
localparam
15051511
StateIdle = 2'h0,
15061512
StateGotByte = 2'h1,
15071513
StateError = 2'h2;
15081514
localparam StateBits = 2'h2;
1509-
reg [StateBits - 1:0] state = StateIdle;
1510-
endmodule)");
1515+
$0 [StateBits - 1:0] state = StateIdle;
1516+
endmodule)",
1517+
UseSystemVerilog() ? "logic" : "reg"));
15111518
}
15121519

15131520
TEST_P(VastTest, SimpleConditional) {
@@ -1641,12 +1648,12 @@ TEST_P(VastTest, NestedConditional) {
16411648
f.Literal(1, 1, SourceInfo()));
16421649

16431650
EXPECT_EQ(m->Emit(nullptr),
1644-
R"(module top(
1651+
absl::Substitute(R"(module top(
16451652
input wire input1,
16461653
input wire input2
16471654
);
1648-
reg output1;
1649-
reg output2;
1655+
$0 output1;
1656+
$0 output2;
16501657
always_comb begin
16511658
if (input1) begin
16521659
output1 = 1'h1;
@@ -1659,7 +1666,8 @@ TEST_P(VastTest, NestedConditional) {
16591666
end
16601667
end
16611668
end
1662-
endmodule)");
1669+
endmodule)",
1670+
UseSystemVerilog() ? "logic" : "reg"));
16631671
}
16641672

16651673
TEST_P(VastTest, TestbenchClock) {
@@ -1683,14 +1691,15 @@ TEST_P(VastTest, TestbenchClock) {
16831691
f.LogicalNot(clk, SourceInfo()))));
16841692

16851693
EXPECT_EQ(f.Emit(nullptr),
1686-
R"(module testbench;
1687-
reg clk;
1694+
absl::Substitute(R"(module testbench;
1695+
$0 clk;
16881696
initial begin
16891697
#1 clk = 0;
16901698
forever #1 clk = !clk;
16911699
end
16921700
endmodule
1693-
)");
1701+
)",
1702+
UseSystemVerilog() ? "logic" : "reg"));
16941703
}
16951704

16961705
TEST_P(VastTest, TestbenchDisplayAndMonitor) {
@@ -1847,17 +1856,18 @@ TEST_P(VastTest, ModuleSections) {
18471856

18481857
LineInfo line_info;
18491858
EXPECT_EQ(module->Emit(&line_info),
1850-
R"(module my_module;
1859+
absl::Substitute(R"(module my_module;
18511860
// section 0
18521861
// more stuff in section 0
18531862
`SOME_MACRO(42);
1854-
reg [41:0] section_0_reg;
1855-
reg foo = 1;
1863+
$0 [41:0] section_0_reg;
1864+
$0 foo = 1;
18561865
// section 1
18571866
// nested in section 1
18581867
// more stuff in section 1
18591868
// random comment at end
1860-
endmodule)");
1869+
endmodule)",
1870+
UseSystemVerilog() ? "logic" : "reg"));
18611871

18621872
EXPECT_EQ(line_info.LookupNode(module).value(),
18631873
std::vector<LineSpan>{LineSpan(0, 10)});
@@ -1883,10 +1893,16 @@ TEST_P(VastTest, VerilogFunction) {
18831893
SourceInfo());
18841894
func->AddStatement<BlockingAssignment>(SourceInfo(), func->return_value_ref(),
18851895
f.Shll(foo, bar, SourceInfo()));
1886-
EXPECT_EQ(func->return_value_def()->Emit(nullptr), "reg [41:0] func;");
1896+
EXPECT_EQ(func->return_value_def()->Emit(nullptr),
1897+
absl::Substitute("$0 [41:0] func;",
1898+
UseSystemVerilog() ? "logic" : "reg"));
18871899
ASSERT_EQ(func->arguments().size(), 3);
1888-
EXPECT_EQ(func->arguments()[0]->Emit(nullptr), "reg [31:0] foo;");
1889-
EXPECT_EQ(func->arguments()[1]->Emit(nullptr), "reg [2:0] bar;");
1900+
EXPECT_EQ(
1901+
func->arguments()[0]->Emit(nullptr),
1902+
absl::Substitute("$0 [31:0] foo;", UseSystemVerilog() ? "logic" : "reg"));
1903+
EXPECT_EQ(
1904+
func->arguments()[1]->Emit(nullptr),
1905+
absl::Substitute("$0 [2:0] bar;", UseSystemVerilog() ? "logic" : "reg"));
18901906
EXPECT_EQ(func->arguments()[2]->Emit(nullptr), "integer baz;");
18911907
EXPECT_EQ(func->statement_block()->Emit(nullptr), R"(begin
18921908
func = foo << bar;
@@ -1901,15 +1917,16 @@ end)");
19011917
std::vector<Expression*>{f.Literal(UBits(12, 32), SourceInfo()),
19021918
f.Literal(UBits(2, 3), SourceInfo())}));
19031919
EXPECT_EQ(m->Emit(nullptr),
1904-
R"(module top;
1905-
function automatic [41:0] func (input reg [31:0] foo, input reg [2:0] bar, input integer baz);
1920+
absl::Substitute(R"(module top;
1921+
function automatic [41:0] func (input $0 [31:0] foo, input $0 [2:0] bar, input integer baz);
19061922
begin
19071923
func = foo << bar;
19081924
end
19091925
endfunction
19101926
wire [31:0] qux;
19111927
assign qux = func(32'h0000_000c, 3'h2);
1912-
endmodule)");
1928+
endmodule)",
1929+
UseSystemVerilog() ? "logic" : "reg"));
19131930
}
19141931

19151932
TEST_P(VastTest, VerilogFunctionNoArguments) {
@@ -1963,10 +1980,10 @@ TEST_P(VastTest, VerilogFunctionWithRegDefs) {
19631980
f.Make<VerilogFunctionCall>(SourceInfo(), func,
19641981
std::vector<Expression*>{}));
19651982
EXPECT_EQ(m->Emit(nullptr),
1966-
R"(module top;
1983+
absl::Substitute(R"(module top;
19671984
function automatic [41:0] func ();
1968-
reg [41:0] foo;
1969-
reg [41:0] bar;
1985+
$0 [41:0] foo;
1986+
$0 [41:0] bar;
19701987
begin
19711988
foo = 42'h000_0000_0042;
19721989
bar = foo;
@@ -1975,7 +1992,8 @@ TEST_P(VastTest, VerilogFunctionWithRegDefs) {
19751992
endfunction
19761993
wire [31:0] qux;
19771994
assign qux = func();
1978-
endmodule)");
1995+
endmodule)",
1996+
UseSystemVerilog() ? "logic" : "reg"));
19791997
}
19801998

19811999
TEST_P(VastTest, VerilogFunctionWithScalarReturn) {
@@ -2140,18 +2158,19 @@ TEST_P(VastTest, VerilogFunctionWithComplicatedTypes) {
21402158
std::vector<Expression*>{a, b, c}));
21412159
LineInfo line_info;
21422160
EXPECT_EQ(m->Emit(&line_info),
2143-
R"(module top;
2144-
function automatic signed [5:0][2:0][32:0] func (input reg foo, input reg signed [6 + 6 - 1:0][110:0] bar, input reg signed [32:0] baz);
2161+
absl::Substitute(R"(module top;
2162+
function automatic signed [5:0][2:0][32:0] func (input $0 foo, input $0 signed [6 + 6 - 1:0][110:0] bar, input $0 signed [32:0] baz);
21452163
begin
21462164
func = 0;
21472165
end
21482166
endfunction
2149-
reg a;
2167+
$0 a;
21502168
wire signed [6 + 6 - 1:0][110:0] b;
21512169
wire signed [32:0] c;
21522170
wire signed [5:0][2:0][32:0] qux;
21532171
assign qux = func(a, b, c);
2154-
endmodule)");
2172+
endmodule)",
2173+
UseSystemVerilog() ? "logic" : "reg"));
21552174

21562175
EXPECT_EQ(line_info.LookupNode(m).value(),
21572176
std::vector<LineSpan>{LineSpan(0, 11)});
@@ -2209,17 +2228,18 @@ TEST_P(VastTest, RegAndWireDefWithInit) {
22092228
LogicRef * qux, m->AddWire("qux", return_type, func_call, SourceInfo()));
22102229
LineInfo line_info;
22112230
EXPECT_EQ(m->Emit(&line_info),
2212-
R"(module top;
2213-
function automatic signed [5:0][2:0][32:0] func (input reg foo, input reg signed [6 + 6 - 1:0][110:0] bar, input reg signed [32:0] baz);
2231+
absl::Substitute(R"(module top;
2232+
function automatic signed [5:0][2:0][32:0] func (input $0 foo, input $0 signed [6 + 6 - 1:0][110:0] bar, input $0 signed [32:0] baz);
22142233
begin
22152234
func = 0;
22162235
end
22172236
endfunction
2218-
reg a = 1'h0;
2237+
$0 a = 1'h0;
22192238
wire signed [6 + 6 - 1:0][110:0] b = 0;
22202239
wire signed [32:0] c = 33'h0_0000_0000;
22212240
wire signed [5:0][2:0][32:0] qux = func(a, b, c);
2222-
endmodule)");
2241+
endmodule)",
2242+
UseSystemVerilog() ? "logic" : "reg"));
22232243

22242244
EXPECT_EQ(line_info.LookupNode(m).value(),
22252245
std::vector<LineSpan>{LineSpan(0, 10)});

0 commit comments

Comments
 (0)