diff --git a/docs_src/dev_passes_list.md b/docs_src/dev_passes_list.md index a408777cfa..58630a9b27 100644 --- a/docs_src/dev_passes_list.md +++ b/docs_src/dev_passes_list.md @@ -80,6 +80,33 @@ the optimization pass pipeline for modernizing procs. +## remove_identifiers_unsafe - UNSAFE: Remove all identifiers {#remove_identifiers_unsafe} + + +A pass version of 'remove_identifers_main'. Running this pass will rewrite +the package to remove the names of all functions, procs, nodes, channels, +etc. + +This should only be used while investigating optimizer behavior using the +ir-visualizer or other tools. + +!!! WARNING + This pass does not preserve externally visible names. Once run on a + design the various io-constraint and other configs which rely on naming + pieces of the design will not work. This pass is only for dev/ investigatory + purposes. + +!!! NOTE + This pass **does not** preserve the id number of nodes. + + +[Header](http://github.com/google/xls/tree/main/xls/dev_tools/dev_passes/remove_identifiers_pass.h) + + + + + + ## remove_one_hot_sel - One Hot Select Removal Pass {#remove_one_hot_sel} diff --git a/xls/dev_tools/dev_passes/BUILD b/xls/dev_tools/dev_passes/BUILD index 7fcf0f00d0..2dcab8eda7 100644 --- a/xls/dev_tools/dev_passes/BUILD +++ b/xls/dev_tools/dev_passes/BUILD @@ -44,6 +44,7 @@ xls_pass_registry( ":to_zero_ext_pass", ":decompose_dataflow_pass", ":remove_one_hot_select_pass", + ":remove_identifiers_pass", ], tags = ["keep_dep"], ) @@ -306,3 +307,40 @@ cc_test( "@googletest//:gtest", ], ) + +xls_pass( + name = "remove_identifiers_pass", + srcs = ["remove_identifiers_pass.cc"], + hdrs = ["remove_identifiers_pass.h"], + pass_class = "RemoveIdentifiersPass", + deps = [ + "//xls/common/status:status_macros", + "//xls/dev_tools:remove_identifiers", + "//xls/ir", + "//xls/passes:optimization_pass", + "//xls/passes:pass_base", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/status:statusor", + ], +) + +cc_test( + name = "remove_identifiers_pass_test", + srcs = ["remove_identifiers_pass_test.cc"], + deps = [ + ":remove_identifiers_pass", + "//xls/common:xls_gunit_main", + "//xls/common/status:matchers", + "//xls/ir", + "//xls/ir:bits", + "//xls/ir:function_builder", + "//xls/ir:ir_test_base", + "//xls/ir:value", + "//xls/passes:dfe_pass", + "//xls/passes:optimization_pass", + "//xls/passes:pass_base", + "@com_google_absl//absl/status:status_matchers", + "@com_google_absl//absl/status:statusor", + "@googletest//:gtest", + ], +) diff --git a/xls/dev_tools/dev_passes/remove_identifiers_pass.cc b/xls/dev_tools/dev_passes/remove_identifiers_pass.cc new file mode 100644 index 0000000000..2a3c9de04f --- /dev/null +++ b/xls/dev_tools/dev_passes/remove_identifiers_pass.cc @@ -0,0 +1,63 @@ +// Copyright 2026 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. + +#include "xls/dev_tools/dev_passes/remove_identifiers_pass.h" + +#include +#include +#include +#include + +#include "absl/container/flat_hash_map.h" +#include "absl/status/statusor.h" +#include "xls/common/status/status_macros.h" +#include "xls/dev_tools/remove_identifiers.h" +#include "xls/ir/package.h" +#include "xls/passes/optimization_pass.h" +#include "xls/passes/pass_base.h" + +namespace xls { + +absl::StatusOr RemoveIdentifiersPass::RunInternal( + Package* p, const OptimizationPassOptions& options, PassResults* results, + OptimizationContext& context) const { + XLS_ASSIGN_OR_RETURN(std::unique_ptr new_p, + StripPackage(p, options_)); + std::vector old_funcs = p->GetFunctionBases(); + std::vector old_chans(p->channels().begin(), p->channels().end()); + XLS_RETURN_IF_ERROR(p->SetTop(std::nullopt)); + for (FunctionBase* f : old_funcs) { + XLS_RETURN_IF_ERROR(p->RemoveFunctionBase(f)); + } + for (Channel* c : old_chans) { + XLS_RETURN_IF_ERROR(p->RemoveChannel(c)); + } + XLS_RETURN_IF_ERROR(p->ClearFiles()); + XLS_ASSIGN_OR_RETURN(Package::PackageMergeResult merge, + p->ImportFromPackage(new_p.get())); + if (new_p->HasTop()) { + std::string_view new_top_name = + merge.name_updates.contains(new_p->GetTop().value()->name()) + ? merge.name_updates.at(new_p->GetTop().value()->name()) + : new_p->GetTop().value()->name(); + XLS_RETURN_IF_ERROR(p->SetTopByName(new_top_name)) + << "Unable to update top to " << new_top_name; + } + for (FunctionBase* f : p->GetFunctionBases()) { + XLS_RETURN_IF_ERROR(f->RebuildSideTables()); + } + return true; +} + +} // namespace xls diff --git a/xls/dev_tools/dev_passes/remove_identifiers_pass.h b/xls/dev_tools/dev_passes/remove_identifiers_pass.h new file mode 100644 index 0000000000..d843830f06 --- /dev/null +++ b/xls/dev_tools/dev_passes/remove_identifiers_pass.h @@ -0,0 +1,68 @@ +// Copyright 2026 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. + +#ifndef XLS_DEV_TOOLS_DEV_PASSES_REMOVE_IDENTIFIERS_PASS_H_ +#define XLS_DEV_TOOLS_DEV_PASSES_REMOVE_IDENTIFIERS_PASS_H_ + +#include + +#include "absl/status/statusor.h" +#include "xls/dev_tools/remove_identifiers.h" +#include "xls/ir/node.h" +#include "xls/passes/optimization_pass.h" +#include "xls/passes/pass_base.h" + +namespace xls { + +// A pass version of 'remove_identifers_main'. Running this pass will rewrite +// the package to remove the names of all functions, procs, nodes, channels, +// etc. +// +// This should only be used while investigating optimizer behavior using the +// ir-visualizer or other tools. +// +// WARNING: This pass does not preserve externally visible names. Once run on a +// design the various io-constraint and other configs which rely on naming +// pieces of the design will not work. This pass is only for dev/ investigatory +// purposes. +// +// NOTE: This pass **does not** preserve the id number of nodes. +class RemoveIdentifiersPass : public OptimizationPass { + public: + static constexpr std::string_view kName = "remove_identifiers_unsafe"; + explicit RemoveIdentifiersPass( + StripOptions options = StripOptions{.new_package_name = "subrosa"}) + : OptimizationPass(kName, "UNSAFE: Remove all identifiers"), + options_(options) {} + ~RemoveIdentifiersPass() override = default; + + RedundancyGuard GetRedundancyGuard( + const OptimizationPassOptions& options, + OptimizationContext& context) const override { + return RedundancyGuard::Never(); + } + + protected: + absl::StatusOr RunInternal(Package* p, + const OptimizationPassOptions& options, + PassResults* results, + OptimizationContext& context) const override; + + private: + StripOptions options_; +}; + +} // namespace xls + +#endif // XLS_DEV_TOOLS_DEV_PASSES_REMOVE_IDENTIFIERS_PASS_H_ diff --git a/xls/dev_tools/dev_passes/remove_identifiers_pass_test.cc b/xls/dev_tools/dev_passes/remove_identifiers_pass_test.cc new file mode 100644 index 0000000000..c3c670c254 --- /dev/null +++ b/xls/dev_tools/dev_passes/remove_identifiers_pass_test.cc @@ -0,0 +1,90 @@ +// Copyright 2026 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. + +#include "xls/dev_tools/dev_passes/remove_identifiers_pass.h" + +#include +#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" +#include "absl/status/status_matchers.h" +#include "absl/status/statusor.h" +#include "xls/common/status/matchers.h" +#include "xls/ir/bits.h" +#include "xls/ir/function.h" +#include "xls/ir/function_builder.h" +#include "xls/ir/ir_test_base.h" +#include "xls/ir/package.h" +#include "xls/ir/value.h" +#include "xls/passes/optimization_pass.h" +#include "xls/passes/pass_base.h" + +namespace xls { +namespace { + +using absl_testing::IsOkAndHolds; + +class RemoveIdentifiersPassTest : public IrTestBase { + protected: + absl::StatusOr Run(Package* p) { + RemoveIdentifiersPass pass; + PassResults results; + OptimizationContext context; + return pass.Run(p, {}, &results, context); + } +}; + +TEST_F(RemoveIdentifiersPassTest, BasicFunction) { + static constexpr std::array kSecrets{ + "the_answer_func", "foo", "bar", "ultimate_question"}; + auto p = CreatePackage(); + FunctionBuilder fb("the_answer_func", p.get()); + fb.Add(fb.Param("foo", p->GetBitsType(32)), + fb.Param("bar", p->GetBitsType(32)), SourceInfo(), + "ultimate_question"); + XLS_ASSERT_OK_AND_ASSIGN(Function * f, fb.Build()); + XLS_ASSERT_OK(p->SetTop(f)); + + EXPECT_THAT(Run(p.get()), IsOkAndHolds(true)); + + EXPECT_THAT(p->functions(), testing::SizeIs(1)); + + for (auto secret : kSecrets) { + EXPECT_THAT(p->DumpIr(), testing::Not(testing::ContainsRegex(secret))); + } +} + +TEST_F(RemoveIdentifiersPassTest, BasicProc) { + static constexpr std::array kSecrets{ + "the_answer_proc", "secret_tunnel", "astounding"}; + auto p = CreatePackage(); + ProcBuilder pb(NewStyleProc{}, "the_answer_proc", p.get()); + XLS_ASSERT_OK(pb.AddChannel("secret_tunnel", p->GetBitsType(32)).status()); + auto start_param = pb.StateElement("astounding", Value(UBits(42, 32))); + pb.Next(start_param, start_param); + XLS_ASSERT_OK_AND_ASSIGN(auto* orig_proc, pb.Build()); + XLS_ASSERT_OK(p->SetTop(orig_proc)); + + EXPECT_THAT(Run(p.get()), IsOkAndHolds(true)); + + EXPECT_THAT(p->procs(), testing::SizeIs(1)); + + for (auto secret : kSecrets) { + EXPECT_THAT(p->DumpIr(), testing::Not(testing::ContainsRegex(secret))); + } +} + +} // namespace +} // namespace xls diff --git a/xls/ir/package.cc b/xls/ir/package.cc index fa120e4775..ba2d9d9a35 100644 --- a/xls/ir/package.cc +++ b/xls/ir/package.cc @@ -700,6 +700,13 @@ absl::StatusOr Package::CreateSingleValueChannelInProc( return channel_ptr; } +absl::Status Package::ClearFiles() { + fileno_to_filename_.clear(); + filename_to_fileno_.clear(); + maximum_fileno_.reset(); + return absl::OkStatus(); +} + absl::Status Package::RemoveChannel(Channel* channel) { // First check that the channel is owned by this package. auto it = std::find(channel_vec_.begin(), channel_vec_.end(), channel); diff --git a/xls/ir/package.h b/xls/ir/package.h index f8fb58905b..1c6625bae4 100644 --- a/xls/ir/package.h +++ b/xls/ir/package.h @@ -381,6 +381,9 @@ class Package { // nodes an error is returned. absl::Status RemoveChannel(Channel* channel); + // Clears all files from the package. + absl::Status ClearFiles(); + // Builder to collect overrides when cloning channels. // Each field is optional where std::nullopt indicates that the cloned channel // should share the same value as the original. If a field contains a value,