Skip to content

Commit b06b78f

Browse files
meheffernancopybara-github
authored andcommitted
Use elaboration instances in the interpreter and JIT.
Rather than creating one continuation (channel queue) per Proc (Channel) IR object, one continuation (channel queue) is created per _instance_ of these IR objects. No functional change, but this lays the groundwork for the interpreter and JIT working with new-style procs with proc-scoped channels where channels and procs can be instantiated multiple times. PiperOrigin-RevId: 595811756
1 parent a32cbfd commit b06b78f

25 files changed

+758
-350
lines changed

xls/interpreter/BUILD

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,14 @@ cc_library(
110110
":ir_interpreter",
111111
":proc_evaluator",
112112
"@com_google_absl//absl/container:flat_hash_map",
113+
"@com_google_absl//absl/status",
113114
"@com_google_absl//absl/status:statusor",
114115
"@com_google_absl//absl/types:span",
115116
"//xls/common/status:ret_check",
116117
"//xls/common/status:status_macros",
117118
"//xls/ir",
119+
"//xls/ir:bits",
120+
"//xls/ir:elaboration",
118121
"//xls/ir:events",
119122
"//xls/ir:value",
120123
"//xls/ir:value_helpers",
@@ -141,11 +144,18 @@ cc_test(
141144
deps = [
142145
":channel_queue",
143146
":channel_queue_test_base",
147+
"@com_google_absl//absl/status",
148+
"@com_google_absl//absl/status:statusor",
149+
"@com_google_absl//absl/types:span",
144150
"//xls/common:xls_gunit",
145151
"//xls/common:xls_gunit_main",
146152
"//xls/common/status:matchers",
153+
"//xls/common/status:status_macros",
147154
"//xls/ir",
148155
"//xls/ir:channel",
156+
"//xls/ir:channel_ops",
157+
"//xls/ir:elaboration",
158+
"//xls/ir:ir_test_base",
149159
],
150160
)
151161

@@ -225,12 +235,20 @@ cc_library(
225235
srcs = ["channel_queue.cc"],
226236
hdrs = ["channel_queue.h"],
227237
deps = [
238+
"@com_google_absl//absl/base:core_headers",
239+
"@com_google_absl//absl/container:flat_hash_map",
240+
"@com_google_absl//absl/container:flat_hash_set",
241+
"@com_google_absl//absl/memory",
228242
"@com_google_absl//absl/status",
229243
"@com_google_absl//absl/status:statusor",
244+
"@com_google_absl//absl/strings:str_format",
245+
"@com_google_absl//absl/synchronization",
246+
"@com_google_absl//absl/types:span",
247+
"//xls/common/logging",
230248
"//xls/common/status:status_macros",
231249
"//xls/ir",
232250
"//xls/ir:channel",
233-
"//xls/ir:type",
251+
"//xls/ir:elaboration",
234252
"//xls/ir:value",
235253
"//xls/ir:value_helpers",
236254
],
@@ -279,11 +297,17 @@ cc_library(
279297
hdrs = ["channel_queue_test_base.h"],
280298
deps = [
281299
":channel_queue",
300+
"@com_google_absl//absl/status",
282301
"@com_google_absl//absl/status:statusor",
283302
"//xls/common/status:matchers",
303+
"//xls/ir",
304+
"//xls/ir:bits",
284305
"//xls/ir:channel",
285306
"//xls/ir:channel_cc_proto",
307+
"//xls/ir:channel_ops",
308+
"//xls/ir:elaboration",
286309
"//xls/ir:ir_test_base",
310+
"//xls/ir:value",
287311
"@com_google_benchmark//:benchmark",
288312
"@com_google_googletest//:gtest",
289313
],
@@ -295,9 +319,11 @@ cc_library(
295319
hdrs = ["proc_evaluator.h"],
296320
deps = [
297321
"@com_google_absl//absl/status:statusor",
322+
"@com_google_absl//absl/strings:str_format",
298323
"//xls/common/logging",
299324
"//xls/ir",
300325
"//xls/ir:channel",
326+
"//xls/ir:elaboration",
301327
"//xls/ir:events",
302328
"//xls/ir:value",
303329
],
@@ -316,6 +342,8 @@ cc_library(
316342
"//xls/ir",
317343
"//xls/ir:bits",
318344
"//xls/ir:channel",
345+
"//xls/ir:channel_ops",
346+
"//xls/ir:elaboration",
319347
"//xls/ir:function_builder",
320348
"//xls/ir:ir_test_base",
321349
"//xls/ir:value",
@@ -331,10 +359,20 @@ cc_library(
331359
deps = [
332360
":channel_queue",
333361
":proc_evaluator",
362+
"@com_google_absl//absl/container:flat_hash_map",
363+
"@com_google_absl//absl/status",
334364
"@com_google_absl//absl/status:statusor",
335365
"@com_google_absl//absl/strings",
366+
"@com_google_absl//absl/strings:str_format",
367+
"//xls/common/logging",
368+
"//xls/common/logging:vlog_is_on",
369+
"//xls/common/status:status_macros",
336370
"//xls/ir",
371+
"//xls/ir:channel",
372+
"//xls/ir:channel_ops",
373+
"//xls/ir:elaboration",
337374
"//xls/ir:events",
375+
"//xls/ir:value",
338376
"//xls/jit:jit_channel_queue",
339377
],
340378
)
@@ -347,9 +385,15 @@ cc_library(
347385
":channel_queue",
348386
":proc_evaluator",
349387
":proc_runtime",
388+
"@com_google_absl//absl/container:flat_hash_map",
389+
"@com_google_absl//absl/memory",
350390
"@com_google_absl//absl/status:statusor",
391+
"@com_google_absl//absl/strings:str_format",
392+
"//xls/common/logging",
351393
"//xls/common/status:ret_check",
394+
"//xls/common/status:status_macros",
352395
"//xls/ir",
396+
"//xls/ir:elaboration",
353397
],
354398
)
355399

@@ -376,15 +420,20 @@ cc_library(
376420
srcs = ["proc_runtime_test_base.cc"],
377421
hdrs = ["proc_runtime_test_base.h"],
378422
deps = [
423+
":channel_queue",
379424
":proc_runtime",
425+
"@com_google_absl//absl/status",
380426
"@com_google_absl//absl/status:statusor",
381427
"@com_google_absl//absl/strings",
382428
"//xls/common/status:matchers",
383429
"//xls/ir",
430+
"//xls/ir:bits",
384431
"//xls/ir:channel",
432+
"//xls/ir:channel_ops",
385433
"//xls/ir:function_builder",
386434
"//xls/ir:ir_parser",
387435
"//xls/ir:ir_test_base",
436+
"//xls/ir:value",
388437
"@com_google_benchmark//:benchmark",
389438
"@com_google_googletest//:gtest",
390439
],
@@ -399,7 +448,10 @@ cc_library(
399448
":proc_evaluator",
400449
":proc_interpreter",
401450
":serial_proc_runtime",
451+
"@com_google_absl//absl/status:statusor",
402452
"//xls/common/status:status_macros",
403453
"//xls/ir",
454+
"//xls/ir:elaboration",
455+
"//xls/ir:value",
404456
],
405457
)

xls/interpreter/channel_queue.cc

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@
1515
#include "xls/interpreter/channel_queue.h"
1616

1717
#include <algorithm>
18+
#include <cstdint>
1819
#include <memory>
1920
#include <optional>
2021
#include <string_view>
2122
#include <utility>
2223
#include <vector>
2324

25+
#include "absl/container/flat_hash_set.h"
26+
#include "absl/memory/memory.h"
2427
#include "absl/status/status.h"
2528
#include "absl/status/statusor.h"
29+
#include "absl/strings/str_format.h"
30+
#include "absl/synchronization/mutex.h"
31+
#include "xls/common/logging/logging.h"
2632
#include "xls/common/status/status_macros.h"
27-
#include "xls/ir/type.h"
33+
#include "xls/ir/channel.h"
34+
#include "xls/ir/elaboration.h"
35+
#include "xls/ir/package.h"
36+
#include "xls/ir/value.h"
2837
#include "xls/ir/value_helpers.h"
2938

3039
namespace xls {
@@ -34,7 +43,7 @@ absl::Status ChannelQueue::AttachGenerator(GeneratorFn generator) {
3443
if (generator_.has_value()) {
3544
return absl::InternalError("ChannelQueue already has a generator attached");
3645
}
37-
if (channel_->kind() == ChannelKind::kSingleValue) {
46+
if (channel()->kind() == ChannelKind::kSingleValue) {
3847
return absl::InternalError(
3948
absl::StrFormat("ChannelQueues for single-value channels cannot have a "
4049
"generator. Channel: %s",
@@ -45,17 +54,18 @@ absl::Status ChannelQueue::AttachGenerator(GeneratorFn generator) {
4554
}
4655

4756
absl::Status ChannelQueue::Write(const Value& value) {
48-
XLS_VLOG(4) << absl::StreamFormat("Writing value to channel %s: { %s }",
49-
channel_->name(), value.ToString());
57+
XLS_VLOG(4) << absl::StreamFormat(
58+
"Writing value to channel instance `%s`: { %s }",
59+
channel_instance()->ToString(), value.ToString());
5060
absl::MutexLock lock(&mutex_);
5161
if (generator_.has_value()) {
5262
return absl::InternalError(
5363
"Cannot write to ChannelQueue because it has a generator function.");
5464
}
55-
if (!ValueConformsToType(value, channel_->type())) {
65+
if (!ValueConformsToType(value, channel()->type())) {
5666
return absl::InvalidArgumentError(absl::StrFormat(
57-
"Channel %s expects values to have type %s, got: %s", channel_->name(),
58-
channel_->type()->ToString(), value.ToString()));
67+
"Channel `%s` expects values to have type %s, got: %s",
68+
channel()->name(), channel()->type()->ToString(), value.ToString()));
5969
}
6070

6171
WriteInternal(value);
@@ -90,7 +100,8 @@ std::optional<Value> ChannelQueue::Read() {
90100
}
91101
std::optional<Value> value = ReadInternal();
92102
XLS_VLOG(4) << absl::StreamFormat(
93-
"Reading data from channel %s: %s", channel_->name(),
103+
"Reading data from channel instance %s: %s",
104+
channel_instance()->ToString(),
94105
value.has_value() ? value->ToString() : "(none)");
95106
XLS_VLOG(4) << absl::StreamFormat("Channel now has %d elements",
96107
queue_.size());
@@ -110,62 +121,76 @@ std::optional<Value> ChannelQueue::ReadInternal() {
110121
return std::move(value);
111122
}
112123

124+
/* static */ absl::StatusOr<std::unique_ptr<ChannelQueueManager>>
125+
ChannelQueueManager::Create(Package* package) {
126+
XLS_ASSIGN_OR_RETURN(Elaboration elaboration,
127+
Elaboration::ElaborateOldStylePackage(package));
128+
return Create(std::move(elaboration));
129+
}
130+
113131
/* static */
114132
absl::StatusOr<std::unique_ptr<ChannelQueueManager>>
115133
ChannelQueueManager::Create(std::vector<std::unique_ptr<ChannelQueue>>&& queues,
116-
Package* package) {
134+
Elaboration elaboration) {
117135
// Verify there is exactly one queue per channel.
118-
absl::flat_hash_set<Channel*> proc_channels(package->channels().begin(),
119-
package->channels().end());
120-
absl::flat_hash_set<Channel*> queue_channels;
136+
absl::flat_hash_set<ChannelInstance*> channel_instances(
137+
elaboration.channel_instances().begin(),
138+
elaboration.channel_instances().end());
139+
absl::flat_hash_set<ChannelInstance*> queue_chan_instances;
121140
for (const std::unique_ptr<ChannelQueue>& queue : queues) {
122-
if (!proc_channels.contains(queue->channel())) {
141+
if (!channel_instances.contains(queue->channel_instance())) {
123142
return absl::InvalidArgumentError(absl::StrFormat(
124-
"Channel `%s` for queue does not exist in package `%s`",
125-
queue->channel()->name(), package->name()));
143+
"Channel instance `%s` for queue does not exist in package `%s`",
144+
queue->channel_instance()->ToString(),
145+
elaboration.package()->name()));
126146
}
127-
auto [ir, inserted] = queue_channels.insert(queue->channel());
147+
auto [ir, inserted] =
148+
queue_chan_instances.insert(queue->channel_instance());
128149
if (!inserted) {
129150
return absl::InvalidArgumentError(
130-
absl::StrFormat("Multiple queues specified for channel `%s`",
131-
queue->channel()->name()));
151+
absl::StrFormat("Multiple queues specified for channel instance `%s`",
152+
queue->channel_instance()->ToString()));
132153
}
133154
}
134-
for (Channel* channel : package->channels()) {
135-
if (!queue_channels.contains(channel)) {
136-
return absl::InvalidArgumentError(absl::StrFormat(
137-
"No queue specified for channel `%s`", channel->name()));
155+
for (ChannelInstance* instance : elaboration.channel_instances()) {
156+
if (!queue_chan_instances.contains(instance)) {
157+
return absl::InvalidArgumentError(
158+
absl::StrFormat("No queue specified for channel instance `%s`",
159+
instance->ToString()));
138160
}
139161
}
140162

141-
return absl::WrapUnique(new ChannelQueueManager(package, std::move(queues)));
163+
return absl::WrapUnique(
164+
new ChannelQueueManager(std::move(elaboration), std::move(queues)));
142165
}
143166

144167
/* static */
145168
absl::StatusOr<std::unique_ptr<ChannelQueueManager>>
146-
ChannelQueueManager::Create(Package* package) {
169+
ChannelQueueManager::Create(Elaboration elaboration) {
147170
std::vector<std::unique_ptr<ChannelQueue>> queues;
148171

149-
// Create a queue per channel in the package.
150-
for (Channel* channel : package->channels()) {
151-
if (channel->kind() != ChannelKind::kStreaming &&
152-
channel->kind() != ChannelKind::kSingleValue) {
172+
// Create a queue per channel instance in the elaboration.
173+
for (ChannelInstance* channel_instance : elaboration.channel_instances()) {
174+
if (channel_instance->channel->kind() != ChannelKind::kStreaming &&
175+
channel_instance->channel->kind() != ChannelKind::kSingleValue) {
153176
return absl::UnimplementedError(
154177
"Only streaming and single-value channels are supported.");
155178
}
156-
queues.push_back(std::make_unique<ChannelQueue>(channel));
179+
queues.push_back(std::make_unique<ChannelQueue>(channel_instance));
157180
}
158181

159-
return absl::WrapUnique(new ChannelQueueManager(package, std::move(queues)));
182+
return absl::WrapUnique(
183+
new ChannelQueueManager(std::move(elaboration), std::move(queues)));
160184
}
161185

162186
ChannelQueueManager::ChannelQueueManager(
163-
Package* package, std::vector<std::unique_ptr<ChannelQueue>>&& queues)
164-
: package_(package) {
187+
Elaboration elaboration,
188+
std::vector<std::unique_ptr<ChannelQueue>>&& queues)
189+
: elaboration_(std::move(elaboration)) {
165190
for (std::unique_ptr<ChannelQueue>& queue : queues) {
166-
Channel* channel = queue->channel();
167-
queues_[channel] = std::move(queue);
168-
queue_vec_.push_back(queues_[channel].get());
191+
ChannelInstance* instance = queue->channel_instance();
192+
queues_[instance] = std::move(queue);
193+
queue_vec_.push_back(queues_[instance].get());
169194
}
170195
// Stably sort the queues by channel ID.
171196
std::sort(queue_vec_.begin(), queue_vec_.end(),
@@ -176,14 +201,18 @@ ChannelQueueManager::ChannelQueueManager(
176201

177202
absl::StatusOr<ChannelQueue*> ChannelQueueManager::GetQueueById(
178203
int64_t channel_id) {
179-
XLS_ASSIGN_OR_RETURN(Channel * channel, package_->GetChannel(channel_id));
180-
return queues_.at(channel).get();
204+
XLS_ASSIGN_OR_RETURN(Channel * channel, package()->GetChannel(channel_id));
205+
XLS_ASSIGN_OR_RETURN(ChannelInstance * instance,
206+
elaboration().GetUniqueInstance(channel));
207+
return queues_.at(instance).get();
181208
}
182209

183210
absl::StatusOr<ChannelQueue*> ChannelQueueManager::GetQueueByName(
184211
std::string_view name) {
185-
XLS_ASSIGN_OR_RETURN(Channel * channel, package_->GetChannel(name));
186-
return queues_.at(channel).get();
212+
XLS_ASSIGN_OR_RETURN(Channel * channel, package()->GetChannel(name));
213+
XLS_ASSIGN_OR_RETURN(ChannelInstance * instance,
214+
elaboration().GetUniqueInstance(channel));
215+
return queues_.at(instance).get();
187216
}
188217

189218
} // namespace xls

0 commit comments

Comments
 (0)