Skip to content

Commit 29e5587

Browse files
meheffernancopybara-github
authored andcommitted
Avoid name and keyword collisions in generated verilog.
Add a name uniquer on ModuleBuilder to prevent accidental collisions of identifiers in verilog modules (regs, wires, etc). Other fixes, cleanups: * xls::verilog::SanitizeIdentifier renamed to SanitizeVerilogIdentifier to distinguish it from other uses of sanitize. * SanitizeVerilogIdentifier now sanitizes keywords. * Raise an error if port names collide rather than silently renaming because port names are part of the interface and should not be changed. * Don't call SanitizeVerilogIdentifer during block creation. We have a separate pass which handes that (NameLeagalizationPass). Fixes #2209. Fixes #805 PiperOrigin-RevId: 761267477
1 parent 1c0629e commit 29e5587

28 files changed

+580
-365
lines changed

xls/codegen/BUILD

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ cc_library(
410410
"//xls/ir:bits_ops",
411411
"//xls/ir:format_preference",
412412
"//xls/ir:format_strings",
413+
"//xls/ir:name_uniquer",
413414
"//xls/ir:op",
414415
"//xls/ir:source_location",
415416
"//xls/ir:type",
@@ -521,7 +522,6 @@ cc_library(
521522
":concurrent_stage_groups",
522523
":conversion_utils",
523524
":mark_channel_fifos_pass",
524-
"//xls/codegen/vast",
525525
"//xls/common:casts",
526526
"//xls/common/logging:log_lines",
527527
"//xls/common/status:ret_check",
@@ -696,12 +696,12 @@ cc_library(
696696
hdrs = ["name_legalization_pass.h"],
697697
deps = [
698698
":codegen_pass",
699+
"//xls/codegen/vast:verilog_keywords",
699700
"//xls/common/status:ret_check",
700701
"//xls/common/status:status_macros",
701702
"//xls/ir",
702703
"//xls/ir:register",
703704
"//xls/passes:pass_base",
704-
"@com_google_absl//absl/base:no_destructor",
705705
"@com_google_absl//absl/container:flat_hash_set",
706706
"@com_google_absl//absl/status",
707707
"@com_google_absl//absl/status:statusor",
@@ -718,6 +718,7 @@ cc_library(
718718
":codegen_pass",
719719
":module_signature",
720720
":module_signature_cc_proto",
721+
"//xls/codegen/vast",
721722
"//xls/common:casts",
722723
"//xls/common/logging:log_lines",
723724
"//xls/common/status:ret_check",

xls/codegen/block_conversion.cc

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
#include "xls/codegen/conversion_utils.h"
4545
#include "xls/codegen/mark_channel_fifos_pass.h"
4646
#include "xls/codegen/proc_block_conversion.h"
47-
#include "xls/codegen/vast/vast.h"
4847
#include "xls/common/logging/log_lines.h"
4948
#include "xls/common/status/ret_check.h"
5049
#include "xls/common/status/status_macros.h"
@@ -649,21 +648,17 @@ absl::StatusOr<CodegenContext> PackageToPipelinedBlocks(
649648

650649
// Make `unit` optional because we haven't created the top block yet. We will
651650
// create it on the first iteration and emplace `unit`.
652-
std::string module_name(
653-
SanitizeIdentifier(options.module_name().value_or(top->name())));
651+
std::string module_name(options.module_name().value_or(top->name()));
654652
Block* top_block =
655653
package->AddBlock(std::make_unique<Block>(module_name, package));
656654
top_block->SetFunctionBaseProvenance(top);
657655

658656
// We use a uniquer here because the top block name comes from the codegen
659657
// option's `module_name` field (if set). A non-top proc could have the same
660658
// name, so the name uniquer will ensure that the sub-block gets a suffix if
661-
// needed. Note that the NameUniquer's sanitize performs a different function
662-
// from `SanitizeIdentifier()`, which is used to ensure that identifiers are
663-
// OK for RTL.
659+
// needed.
664660
NameUniquer block_name_uniquer("__");
665-
XLS_RET_CHECK_EQ(block_name_uniquer.GetSanitizedUniqueName(module_name),
666-
module_name);
661+
block_name_uniquer.GetSanitizedUniqueName(module_name);
667662
CodegenContext context(top_block);
668663

669664
// Run codegen passes as appropriate
@@ -682,8 +677,8 @@ absl::StatusOr<CodegenContext> PackageToPipelinedBlocks(
682677
"Missing schedule for functionbase `%s`", fb->name());
683678

684679
const PipelineSchedule& schedule = schedules.at(fb);
685-
std::string sub_block_name = block_name_uniquer.GetSanitizedUniqueName(
686-
SanitizeIdentifier(fb->name()));
680+
std::string sub_block_name =
681+
block_name_uniquer.GetSanitizedUniqueName(fb->name());
687682
Block* sub_block;
688683
if (fb == top) {
689684
sub_block = top_block;
@@ -737,8 +732,7 @@ absl::StatusOr<CodegenContext> FunctionToCombinationalBlock(
737732
Function* f, const CodegenOptions& options) {
738733
XLS_RET_CHECK(!options.valid_control().has_value())
739734
<< "Combinational block generator does not support valid control.";
740-
std::string module_name(
741-
options.module_name().value_or(SanitizeIdentifier(f->name())));
735+
std::string module_name(options.module_name().value_or(f->name()));
742736
Block* block = f->package()->AddBlock(
743737
std::make_unique<Block>(module_name, f->package()));
744738
block->SetFunctionBaseProvenance(f);
@@ -816,9 +810,9 @@ absl::StatusOr<CodegenContext> ProcToCombinationalBlock(
816810
})));
817811
}
818812

819-
std::string module_name = SanitizeIdentifier(std::string{
820-
options.module_name().has_value() ? options.module_name().value()
821-
: proc->name()});
813+
std::string module_name = std::string{options.module_name().has_value()
814+
? options.module_name().value()
815+
: proc->name()};
822816
Block* block = proc->package()->AddBlock(
823817
std::make_unique<Block>(module_name, proc->package()));
824818
block->SetFunctionBaseProvenance(proc);

xls/codegen/block_conversion_pass_pipeline.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ absl::StatusOr<CodegenContext> CreateBlocksFor(
4747
FunctionBase* top = *package->GetTop();
4848

4949
std::string module_name(
50-
SanitizeIdentifier(options.module_name().value_or(top->name())));
50+
SanitizeVerilogIdentifier(options.module_name().value_or(top->name())));
5151

5252
Block* top_block =
5353
package->AddBlock(std::make_unique<Block>(module_name, package));
@@ -58,8 +58,8 @@ absl::StatusOr<CodegenContext> CreateBlocksFor(
5858
// option's `module_name` field (if set). A non-top proc could have the same
5959
// name, so the name uniquer will ensure that the sub-block gets a suffix if
6060
// needed. Note that the NameUniquer's sanitize performs a different function
61-
// from `SanitizeIdentifier()`, which is used to ensure that identifiers are
62-
// OK for RTL.
61+
// from `SanitizeVerilogIdentifier()`, which is used to ensure that
62+
// identifiers are OK for RTL.
6363
NameUniquer block_name_uniquer("__");
6464
XLS_RET_CHECK_EQ(block_name_uniquer.GetSanitizedUniqueName(module_name),
6565
module_name);
@@ -77,7 +77,7 @@ absl::StatusOr<CodegenContext> CreateBlocksFor(
7777
std::string sub_block_name =
7878
(fb == top) ? module_name
7979
: block_name_uniquer.GetSanitizedUniqueName(
80-
SanitizeIdentifier(fb->name()));
80+
SanitizeVerilogIdentifier(fb->name()));
8181
Block* block = (fb == top) ? top_block
8282
: package->AddBlock(std::make_unique<Block>(
8383
sub_block_name, package));

xls/codegen/block_stitching_pass.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ absl::StatusOr<bool> BlockStitchingPass::RunInternal(
603603
"Splitting outputs is not supported by block stitching.");
604604
}
605605
std::string top_block_name(options.codegen_options.module_name().value_or(
606-
SanitizeIdentifier(context.top_block()->name())));
606+
context.top_block()->name()));
607607

608608
XLS_ASSIGN_OR_RETURN(
609609
Block * top_block,

xls/codegen/conversion_utils.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ std::string PipelineSignalName(std::string_view root, int64_t stage) {
134134
if (!RE2::PartialMatch(root, *kPipelinePrefix, &base)) {
135135
base = root;
136136
}
137-
return absl::StrFormat("p%d_%s", stage, SanitizeIdentifier(base));
137+
return absl::StrFormat("p%d_%s", stage, base);
138138
}
139139

140140
absl::StatusOr<std::vector<Node*>> MakeInputReadyPortsForOutputChannels(

xls/codegen/ffi_instantiation_pass.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ absl::StatusOr<bool> FfiInstantiationPass::RunInternal(
170170
}
171171

172172
// TODO(hzeller): Better ways to generate a name ?
173-
const std::string inst_name = SanitizeIdentifier(
173+
const std::string inst_name = SanitizeVerilogIdentifier(
174174
absl::StrCat(fun->name(), "_", invocation->GetName(), "_inst"));
175175
XLS_ASSIGN_OR_RETURN(xls::Instantiation * instantiation,
176176
block->AddInstantiation(

0 commit comments

Comments
 (0)