Skip to content

Commit

Permalink
Add proc-scoped channel support to channel legalization pass.
Browse files Browse the repository at this point in the history
Incidentally change the way in which channels are named. The data, predicate and completion channels are now numbered consistently. For example, the channels associated with the 42nd op instance of channel `foo` are: `foo__data_42`, `foo__pred_42` and `foo__condition_42`,

PiperOrigin-RevId: 683721323
  • Loading branch information
meheffernan authored and copybara-github committed Oct 8, 2024
1 parent 600dfac commit 825f277
Show file tree
Hide file tree
Showing 8 changed files with 826 additions and 287 deletions.
2 changes: 2 additions & 0 deletions xls/ir/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,7 @@ cc_library(
"//xls/common/status:ret_check",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -1896,6 +1897,7 @@ cc_library(
"//xls/common/status:status_macros",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
Expand Down
44 changes: 44 additions & 0 deletions xls/ir/channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <variant>
#include <vector>

#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
Expand Down Expand Up @@ -229,6 +230,38 @@ std::ostream& operator<<(std::ostream& os, Direction direction) {
return os;
}

ChannelRef AsChannelRef(SendChannelRef ref) {
if (std::holds_alternative<SendChannelReference*>(ref)) {
return std::get<SendChannelReference*>(ref);
}
return std::get<Channel*>(ref);
}

ChannelRef AsChannelRef(ReceiveChannelRef ref) {
if (std::holds_alternative<ReceiveChannelReference*>(ref)) {
return std::get<ReceiveChannelReference*>(ref);
}
return std::get<Channel*>(ref);
}

SendChannelRef AsSendChannelRefOrDie(ChannelRef ref) {
if (std::holds_alternative<ChannelReference*>(ref)) {
ChannelReference* cref = std::get<ChannelReference*>(ref);
CHECK_EQ(cref->direction(), Direction::kSend);
return down_cast<SendChannelReference*>(cref);
}
return std::get<Channel*>(ref);
}

ReceiveChannelRef AsReceiveChannelRefOrDie(ChannelRef ref) {
if (std::holds_alternative<ChannelReference*>(ref)) {
ChannelReference* cref = std::get<ChannelReference*>(ref);
CHECK_EQ(cref->direction(), Direction::kReceive);
return down_cast<ReceiveChannelReference*>(cref);
}
return std::get<Channel*>(ref);
}

std::string_view ChannelRefName(ChannelRef ref) {
return absl::visit([](const auto& ch) { return ch->name(); }, ref);
}
Expand All @@ -247,6 +280,17 @@ ChannelKind ChannelRefKind(ChannelRef ref) {
return std::get<Channel*>(ref)->kind();
}

std::optional<ChannelStrictness> ChannelRefStrictness(ChannelRef ref) {
if (std::holds_alternative<ChannelReference*>(ref)) {
return std::get<ChannelReference*>(ref)->strictness();
}
if (auto streaming_channel =
down_cast<StreamingChannel*>(std::get<Channel*>(ref))) {
return streaming_channel->GetStrictness();
}
return std::nullopt;
}

std::string ChannelReference::ToString() const {
std::vector<std::string> keyword_strs;
keyword_strs.push_back(
Expand Down
10 changes: 10 additions & 0 deletions xls/ir/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,20 @@ using ChannelRef = std::variant<Channel*, ChannelReference*>;
using SendChannelRef = std::variant<Channel*, SendChannelReference*>;
using ReceiveChannelRef = std::variant<Channel*, ReceiveChannelReference*>;

// Converts a send/receive ChannelRef into a generic ChannelRef.
ChannelRef AsChannelRef(SendChannelRef ref);
ChannelRef AsChannelRef(ReceiveChannelRef ref);

// Converts a base ChannelRef into a send/receive form. CHECK fails if the
// ChannelRef is not of the appropriate direction.
SendChannelRef AsSendChannelRefOrDie(ChannelRef ref);
ReceiveChannelRef AsReceiveChannelRefOrDie(ChannelRef ref);

// Return the name/type/kind of a channel reference.
std::string_view ChannelRefName(ChannelRef ref);
Type* ChannelRefType(ChannelRef ref);
ChannelKind ChannelRefKind(ChannelRef ref);
std::optional<ChannelStrictness> ChannelRefStrictness(ChannelRef ref);

} // namespace xls

Expand Down
3 changes: 3 additions & 0 deletions xls/ir/package.cc
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ absl::Status Package::AddChannel(std::unique_ptr<Channel> channel, Proc* proc) {
}

absl::StatusOr<Channel*> Package::GetChannel(int64_t id) const {
XLS_RET_CHECK(!ChannelsAreProcScoped());
for (Channel* ch : channels()) {
if (ch->id() == id) {
return ch;
Expand All @@ -777,6 +778,7 @@ absl::StatusOr<Channel*> Package::GetChannel(int64_t id) const {
}

std::vector<std::string> Package::GetChannelNames() const {
CHECK(!ChannelsAreProcScoped());
std::vector<std::string> names;
names.reserve(channels().size());
for (Channel* ch : channels()) {
Expand All @@ -786,6 +788,7 @@ std::vector<std::string> Package::GetChannelNames() const {
}

absl::StatusOr<Channel*> Package::GetChannel(std::string_view name) const {
XLS_RET_CHECK(!ChannelsAreProcScoped());
auto it = channels_.find(name);
if (it != channels_.end()) {
return it->second.get();
Expand Down
1 change: 1 addition & 0 deletions xls/ir/proc_elaboration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
Expand Down
3 changes: 3 additions & 0 deletions xls/passes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2909,13 +2909,15 @@ cc_library(
"//xls/ir:name_uniquer",
"//xls/ir:node_util",
"//xls/ir:op",
"//xls/ir:proc_elaboration",
"//xls/ir:source_location",
"//xls/ir:value",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
Expand Down Expand Up @@ -2993,6 +2995,7 @@ cc_test(
"//xls/ir:ir_matcher",
"//xls/ir:ir_parser",
"//xls/ir:ir_test_base",
"//xls/ir:proc_elaboration",
"//xls/ir:value",
"//xls/ir:verifier",
"@com_google_absl//absl/container:flat_hash_map",
Expand Down
Loading

0 comments on commit 825f277

Please sign in to comment.