8000 xls/scheduling: Generalize ScheduleBounds and ASAPScheduler. by copybara-service[bot] · Pull Request #5600 · google/xls · GitHub
[go: up one dir, main page]

Skip to content
Draft
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
8000 Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions xls/scheduling/BUILD
< 8000 td id="diff-0630eb78cb6a3dd5156663fe926e92fdd1b212e62a38da5ab9a9df4846da4bfeL245" data-line-number="245" class="blob-num blob-num-context js-linkable-line-number">
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,9 @@ cc_library(
"//xls/common/status:status_macros",
"//xls/estimators/delay_model:delay_estimator",
"//xls/ir",
"//xls/ir:channel",
"//xls/ir:op",
"//xls/ir:state_element",
"//xls/ir:type",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/container:btree",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/container:flat_hash_set",
"@abseil-cpp//absl/log",
Expand Down Expand Up @@ -242,11 +239,14 @@ cc_library(
srcs = ["schedule_util.cc"],
hdrs = ["schedule_util.h"],
deps = [
":schedule_graph",
":scheduling_options",
"//xls/common/status:status_macros",
"//xls/ir",
"//xls/ir:channel",
"//xls/ir:op",
"//xls/passes:node_dependency_analysis",
"@abseil-cpp//absl/container:btree",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/container:flat_hash_set",
"@abseil-cpp//absl/log",
Expand Down Expand Up @@ -318,7 +318,6 @@ cc_library(
":min_cut_scheduler",
":pipeline_schedule",
":random_scheduler",
":schedule_bounds",
":schedule_graph",
":schedule_util",
":scheduler",
Expand Down Expand Up @@ -413,6 +412,7 @@ cc_test(
"//xls/ir:ir_matcher",
"//xls/ir:ir_test_base",
"//xls/ir:op",
"//xls/ir:proc_elaboration",
"//xls/ir:source_location",
"//xls/ir:value",
"@abseil-cpp//absl/container:flat_hash_map",
Expand Down Expand Up @@ -458,17 +458,15 @@ cc_library(
":schedule_util",
":scheduling_options",
"//xls/common:iterator_range",
"//xls/common:math_util",
"//xls/common:visitor",
"//xls/common/status:ret_check",
"//xls/common/status:status_macros",
"//xls/estimators/delay_model:delay_estimator",
"//xls/ir",
"//xls/ir:channel",
"//xls/passes:node_dependency_analysis",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/container:flat_hash_set",
"@abseil-cpp//absl/container:inlined_vector",
"@abseil-cpp//absl/log",
"@abseil-cpp//absl/log:check",
"@abseil-cpp//absl/status",
Expand Down Expand Up @@ -781,7 +779,6 @@ cc_library(
"@abseil-cpp//absl/strings:str_format",
"@abseil-cpp//absl/types:span",
"@cppitertools",
"@or-tools//ortools/graph:topologicalsorter",
],
)

Expand Down Expand Up @@ -878,6 +875,7 @@ cc_library(
"//xls/common/status:status_macros",
"//xls/estimators/delay_model:delay_estimator",
"//xls/ir",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/log",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:status_builder",
Expand All @@ -897,12 +895,18 @@ cc_test(
":scheduling_options",
"//xls/common:xls_gunit_main",
"//xls/common/status:matchers",
"//xls/ir",
"//xls/ir:bits",
"//xls/ir:channel",
"//xls/ir:channel_ops",
"//xls/ir:function_builder",
"//xls/ir:ir_test_base",
"//xls/ir:proc_elaboration",
"//xls/ir:type",
"//xls/ir:value",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/status",
"@abseil-cpp//absl/status:status_matchers",
"@googletest//:gtest",
],
)
Expand All @@ -917,11 +921,11 @@ cc_library(
":schedule_graph",
":scheduler",
":scheduling_options",
"//xls/common/status:ret_check",
"//xls/common/status:status_macros",
"//xls/estimators/delay_model:delay_estimator",
"//xls/ir",
"@abseil-cpp//absl/algorithm:container",
"@abseil-cpp//absl/container:flat_hash_map",
"@abseil-cpp//absl/log",
"@abseil-cpp//absl/random",
"@abseil-cpp//absl/status",
Expand Down
111 changes: 40 additions & 71 deletions xls/scheduling/asap_scheduler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include <cstdint>
#include <optional>
#include <utility>
#include <variant>
#include <vector>

#include "absl/log/log.h"
#include "absl/status/status.h"
Expand All @@ -28,44 +26,12 @@
#include "absl/strings/str_join.h"
#include "xls/common/status/ret_check.h"
#include "xls/common/status/status_macros.h"
#include "xls/ir/function.h"
#include "xls/ir/function_base.h"
#include "xls/ir/node.h"
#include "xls/ir/nodes.h"
#include "xls/scheduling/schedule_bounds.h"
#include "xls/scheduling/schedule_graph.h"
#include "xls/scheduling/scheduling_options.h"

namespace xls {
namespace {

// Returns the nodes of `f` which must be scheduled in the first stage of a
// pipeline. For functions this is parameters.
std::vector<Node*> FirstStageNodes(FunctionBase* f) {
if (Function* function = dynamic_cast<Function*>(f)) {
return std::vector<Node*>(function->params().begin(),
function->params().end());
}

return {};
}
// Returns the nodes of `f` which must be scheduled in the final stage of a
// pipeline. For functions this is the return value.
std::vector<Node*> FinalStageNodes(FunctionBase* f) {
if (Function* function = dynamic_cast<Function*>(f)) {
// If the return value is a parameter, then we do not force the return value
// to be scheduled in the final stage because, as a parameter, the node must
// be in the first stage.
if (function->return_value()->Is<Param>()) {
return {};
}
return {function->return_value()};
}

return {};
}

} // namespace

// Tighten `bounds` to the ASAP/ALAP bounds for each node. If `schedule_length`
// is given, then the ALAP bounds are computed with the given length. Otherwise,
Expand All @@ -74,29 +40,32 @@ std::vector<Node*> FinalStageNodes(FunctionBase* f) {
// Both schedules will be feasible if no other scheduling constraints are
// applied.
/* static */ absl::Status ASAPScheduler::TightenBounds(
sched::ScheduleBounds& boun 8 ds, Func 10BC8 tionBase* f,
std::optional<int64_t> schedule_length) {
sched::ScheduleBounds& bounds, std::optional<int64_t> schedule_length) {
// If we have a schedule length give everything that upper bound.
if (schedule_length.has_value()) {
for (Node* node : f->nodes()) {
for (const ScheduleNode& sn : bounds.graph().nodes()) {
XLS_RETURN_IF_ERROR(
bounds.TightenNodeUb(node, schedule_length.value() - 1));
bounds.TightenNodeUb(sn.node, schedule_length.value() - 1));
}
}
// If we have a schedule_length (and therefore a small-ish max stage) we can
// try to propagate forever since we will hit that schedule length before
// long.
std::optional<int64_t> propagation_fuel =
schedule_length ? std::nullopt : std::make_optional<int64_t>(6);
XLS_RETURN_IF_ERROR(bounds.PropagateBounds(propagation_fuel))
<< "Failed to schedule bounds for " << f->name() << ".";
VLOG(5) << "ASAP bounds: "
<< f->DumpIr(sched::ScheduleBoundsAnnotator(bounds));
XLS_RETURN_IF_ERROR(bounds.PropagateBounds())
<< "Failed to schedule bounds for " << bounds.graph().name() << ".";
if (std::optional<FunctionBase*> fb = bounds.graph().function_base();
fb.has_value()) {
VLOG(5) << "ASAP bounds: "
<< (*fb)->DumpIr(sched::ScheduleBoundsAnnotator(bounds));
} else {
VLOG(5) << "ASAP bounds:\n" << bounds.ToString();
}
return absl::OkStatus();
}
absl::Status ASAPScheduler::GenerateHelpfulError(
absl::Status&& orig_status, std::optional<int64_t> pipeline_stages,
int64_t clock_period_ps, std::optional<int64_t> worst_case_throughput) {
if (!absl::IsResourceExhausted(orig_status) &&
!absl::IsInternal(orig_status)) {
return std::move(orig_status);
}
absl::StatusBuilder status(std::move(orig_status));
// Try to figure out what the actual required stages are.
if (pipeline_stages.has_value()) {
Expand All @@ -105,19 +74,18 @@ absl::Status ASAPScheduler::GenerateHelpfulError(
no_length.IgnoreError();
if (no_length.ok()) {
return (status << absl::StrFormat(
"Function %s cannot be scheduled in %d "
" Function %s cannot be scheduled in %d "
"stages. Computed minimum is --pipeline_stages=%d",
graph().name(), pipeline_stages.value(),
no_length->max_lower_bound() + 1))
.SetCode(absl::StatusCode::kInvalidArgument);
}
return (status << "Function " << graph().name()
<< " cannot be scheduled in any number of stages via ASAP "
"bounds. Some constraints may be unsatisfiable or "
"require a full SDC schedule to resolve.")
.SetCode(absl::StatusCode::kResourceExhausted);
}
return status;
return (status << " Function " << graph().name()
<< " cannot be scheduled in any number of stages via ASAP "
"bounds. Some constraints may be unsatisfiable or "
"require a full SDC schedule to resolve.")
.SetCode(absl::StatusCode::kResourceExhausted);
}

absl::StatusOr<ScheduleCycleMap> ASAPScheduler::Schedule(
Expand Down Expand Up @@ -145,34 +113,35 @@ absl::StatusOr<ScheduleCycleMap> ASAPScheduler::Schedule(
absl::StatusOr<sched::ScheduleBounds> ASAPScheduler::ComputeBounds(
std::optional<int64_t> pipeline_stages, int64_t clock_period_ps,
std::optional<int64_t> worst_case_throughput) {
XLS_RET_CHECK(std::holds_alternative<FunctionBase*>(graph_.ir_scope()));
auto* f = std::get<FunctionBase*>(graph_.ir_scope());
// TODO(allight): This actually creates a copy of graph_ since it needs to
// have its topo-sort changed as constraints are added. Since that list is
// held with the graph it needs its own mutable copy. Since the graph is not
// actually terribly large and this copy is only done once per call this is
// fine.
VLOG(5) << "ASAP scheduler: graph: " << graph_.name();
VLOG(5) << " clock_period_ps: " << clock_period_ps;
VLOG(5) << " worst_case_throughput: "
<< worst_case_throughput.value_or(-1);
VLOG(5) << " constraints: "
<< absl::StrJoin(constraints_, ", ");
// TODO(allight): ScheduleBounds::Create takes `graph_` by value and copies
// it, even though ScheduleBounds no longer mutates `graph_`. Since the graph
// is not terribly large and this copy is only done once per call, keep the
// value interface for now (also relied on by ComputeAsapAndAlapBounds) and
// avoid the copy in a follow-up.
XLS_ASSIGN_OR_RETURN(
auto bounds,
sched::ScheduleBounds::Create(graph_, clock_period_ps, delay_estimator_,
worst_case_throughput, constraints_));
sched::ScheduleBounds bounds,
sched::ScheduleBounds::Create(
graph_, clock_period_ps, delay_estimator_, worst_case_throughput,
constraints_, sched::ScheduleBounds::kDefaultMaxUpperBound,
arc_worst_case_throughput_, default_arc_worst_case_throughput_));
// Add first and last stage constraints.
using LastStageConstraint =
sched::ScheduleBounds::NodeSchedulingConstraint::LastStageConstraint;
for (Node* n : FirstStageNodes(f)) {
bounds.AddConstraint(NodeInCycleConstraint{n, 0});
}
for (Node* n : FinalStageNodes(f)) {
bounds.AddConstraint(LastStageConstraint{n});
for (const ScheduleNode& sn : graph_.nodes()) {
if (sn.schedule_in_first_stage) {
bounds.AddConstraint(NodeInCycleConstraint{sn.node, 0});
}
if (sn.schedule_in_last_stage) {
bounds.AddConstraint(LastStageConstraint{sn.node});
}
}
absl::Status tighten_bounds_status =
TightenBounds(bounds, f, pipeline_stages);
absl::Status tighten_bounds_status = TightenBounds(bounds, pipeline_stages);
if (!tighten_bounds_status.ok()) {
return GenerateHelpfulError(std::move(tighten_bounds_status),
pipeline_stages, clock_period_ps,
Expand Down
28 changes: 21 additions & 7 deletions xls/scheduling/asap_scheduler.h
6A87
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#include <utility>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/types/span.h"
#include "xls/estimators/delay_model/delay_estimator.h"
#include "xls/ir/function_base.h"
#include "xls/scheduling/schedule_bounds.h"
#include "xls/scheduling/schedule_graph.h"
#include "xls/scheduling/scheduler.h"
Expand All @@ -35,8 +35,14 @@ namespace xls {

class ASAPScheduler : public Scheduler {
public:
ASAPScheduler(const ScheduleGraph& graph, DelayEstimator& delay_estimator)
: ASAPScheduler("ASAPScheduler", graph, delay_estimator) {}
ASAPScheduler(
const ScheduleGraph& graph, DelayEstimator& delay_estimator,
absl::flat_hash_map<std::pair<std::string, std::string>, int64_t>
arc_worst_case_throughput = {},
std::optional<int64_t> default_arc_worst_case_throughput = std::nullopt)
: ASAPScheduler("ASAPScheduler", graph, delay_estimator,
std::move(arc_worst_case_throughput),
default_arc_worst_case_throughput) {}
~ASAPScheduler() override = default;

absl::Status AddConstraints(
Expand All @@ -58,11 +64,17 @@ class ASAPScheduler : public Scheduler {
}

protected:
ASAPScheduler(std::string name, const ScheduleGraph& graph,
DelayEstimator& delay_estimator)
ASAPScheduler(
std::string name, const ScheduleGraph& graph,
DelayEstimator& delay_estimator,
absl::flat_hash_map<std::pair<std::string, std::string>, int64_t>
arc_worst_case_throughput = {},
std::optional<int64_t> default_arc_worst_case_throughput = std::nullopt)
: Scheduler(std::move(name)),
graph_(graph),
delay_estimator_(delay_estimator) {}
delay_estimator_(delay_estimator),
arc_worst_case_throughput_(std::move(arc_worst_case_throughput)),
default_arc_worst_case_throughput_(default_arc_worst_case_throughput) {}

absl::Status GenerateHelpfulError(
absl::Status&& orig_status, std::optional<int64_t> pipeline_stages,
Expand All @@ -75,11 +87,13 @@ class ASAPScheduler : public Scheduler {

// Helper to tighten bounds using the ASAP/ALAP bounds.
static absl::Status TightenBounds(sched::ScheduleBounds& bounds,
FunctionBase* f,
std::optional<int64_t> schedule_length);
const ScheduleGraph& graph_;
DelayEstimator& delay_estimator_;
std::vector<SchedulingConstraint> constraints_;
absl::flat_hash_map<std::pair<std::string, std::string>, int64_t>
arc_worst_case_throughput_;
std::optional<int64_t> default_arc_worst_case_throughput_;
};

} // namespace xls
Expand Down
Loading
Loading
0