Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs_src/dev_passes_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}


Expand Down
38 changes: 38 additions & 0 deletions xls/dev_tools/dev_passes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down Expand Up @@ -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",
],
)
63 changes: 63 additions & 0 deletions xls/dev_tools/dev_passes/remove_identifiers_pass.cc
Original file line number Diff line number Diff line change
@@ -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 <memory>
#include <optional>
#include <string_view>
#include <vector>

#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<bool> RemoveIdentifiersPass::RunInternal(
Package* p, const OptimizationPassOptions& options, PassResults* results,
OptimizationContext& context) const {
XLS_ASSIGN_OR_RETURN(std::unique_ptr<Package> new_p,
StripPackage(p, options_));
std::vector<FunctionBase*> old_funcs = p->GetFunctionBases();
std::vector<Channel*> 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
68 changes: 68 additions & 0 deletions xls/dev_tools/dev_passes/remove_identifiers_pass.h
Original file line number Diff line number Diff line change
@@ -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 <string_view>

#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<bool> 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_
90 changes: 90 additions & 0 deletions xls/dev_tools/dev_passes/remove_identifiers_pass_test.cc
Original file line number Diff line number Diff line change
@@ -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 <array>
#include <string_view>

#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<bool> Run(Package* p) {
RemoveIdentifiersPass pass;
PassResults results;
OptimizationContext context;
return pass.Run(p, {}, &results, context);
}
};

TEST_F(RemoveIdentifiersPassTest, BasicFunction) {
static constexpr std::array<std::string_view, 4> 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<std::string_view, 3> 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
7 changes: 7 additions & 0 deletions xls/ir/package.cc
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,13 @@ absl::StatusOr<SingleValueChannel*> 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);
Expand Down
3 changes: 3 additions & 0 deletions xls/ir/package.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading