Skip to content

Commit 9c39337

Browse files
James Molloycopybara-github
authored andcommitted
[xls] RangeQueryEngine: Add fast path for ArrayIndex
If all array indices are precise, i.e. the IntervalSet has only one value, then we don't need to scan all array indices and can simply look up our result. PiperOrigin-RevId: 808137825
1 parent 65aa30d commit 9c39337

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

xls/passes/range_query_engine.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "xls/passes/range_query_engine.h"
1616

17+
#include <algorithm>
1718
#include <cstddef>
1819
#include <cstdint>
1920
#include <functional>
@@ -674,6 +675,27 @@ absl::Status RangeQueryVisitor::HandleArrayIndex(ArrayIndex* array_index) {
674675

675676
IntervalSetTree result = EmptyIntervalSetTree(array_index->GetType());
676677

678+
// Fast path: if all of the index intervals are precise, we can just perform
679+
// a lookup.
680+
if (absl::c_all_of(index_intervals,
681+
[](const IntervalSet& is) { return is.IsPrecise(); }) &&
682+
array_index->GetType()->IsBits()) {
683+
std::vector<int64_t> indexes;
684+
indexes.reserve(index_intervals.size());
685+
for (int64_t i = 0; i < index_intervals.size(); ++i) {
686+
const IntervalSet& intervals = index_intervals[i];
687+
XLS_ASSIGN_OR_RETURN(uint64_t index,
688+
intervals.GetPreciseValue()->ToUint64());
689+
int64_t bound = dimension[i] - 1;
690+
// Clamp to boundary if out of bounds.
691+
indexes.push_back(std::clamp<uint64_t>(index, 0, bound));
692+
}
693+
result.Set({}, array_interval_set_tree->Get(indexes));
694+
695+
SetIntervalSetTree(array_index, std::move(result));
696+
return absl::OkStatus();
697+
}
698+
677699
// Returns true if the given interval set covers the given index value for an
678700
// array of dimension `dim`. Includes handling of OOB conditions.
679701
auto intervals_cover_index = [&](const IntervalSet& intervals, int64_t index,

0 commit comments

Comments
 (0)