-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract IntervalSet -> basic Ternary code to common location
This is/will be useful in other places. Put it in a dedicated interval_ops:: namespace. PiperOrigin-RevId: 553612501
- Loading branch information
1 parent
f5621b3
commit 3214556
Showing
6 changed files
with
221 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 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/ir/interval_ops.h" | ||
|
||
#include <cstdint> | ||
#include <optional> | ||
|
||
#include "xls/ir/bits.h" | ||
#include "xls/ir/bits_ops.h" | ||
#include "xls/ir/node.h" | ||
#include "xls/ir/ternary.h" | ||
|
||
namespace xls::interval_ops { | ||
|
||
TernaryVector ExtractTernaryVector(const IntervalSet& intervals, | ||
std::optional<Node*> source) { | ||
KnownBits bits = ExtractKnownBits(intervals, source); | ||
return ternary_ops::FromKnownBits(bits.known_bits, bits.known_bit_values); | ||
} | ||
|
||
KnownBits ExtractKnownBits(const IntervalSet& intervals, | ||
std::optional<Node*> source) { | ||
XLS_CHECK(intervals.IsNormalized()) | ||
<< (source.has_value() ? source.value()->ToString() : ""); | ||
XLS_CHECK(!intervals.Intervals().empty()) | ||
<< (source.has_value() ? source.value()->ToString() : ""); | ||
Bits lcp = bits_ops::LongestCommonPrefixMSB( | ||
{intervals.Intervals().front().LowerBound(), | ||
intervals.Intervals().back().UpperBound()}); | ||
int64_t size = intervals.BitCount(); | ||
Bits remainder = Bits(size - lcp.bit_count()); | ||
return KnownBits{ | ||
.known_bits = | ||
bits_ops::Concat({Bits::AllOnes(lcp.bit_count()), remainder}), | ||
.known_bit_values = bits_ops::Concat({lcp, remainder}), | ||
}; | ||
} | ||
} // namespace xls::interval_ops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 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_IR_INTERVAL_OPS_H_ | ||
#define XLS_IR_INTERVAL_OPS_H_ | ||
|
||
#include <optional> | ||
|
||
#include "xls/ir/bits.h" | ||
#include "xls/ir/interval_set.h" | ||
#include "xls/ir/node.h" | ||
#include "xls/ir/ternary.h" | ||
|
||
namespace xls::interval_ops { | ||
|
||
// Extract the ternary vector embedded in the interval-sets. | ||
// TODO(allight): Currently this only searches for the longest common MSB | ||
// prefix. More complex analysis is possible though of questionable usefulness | ||
// given they can be extracted by other analyses. | ||
TernaryVector ExtractTernaryVector(const IntervalSet& intervals, | ||
std::optional<Node*> source = std::nullopt); | ||
|
||
struct KnownBits { | ||
Bits known_bits; | ||
Bits known_bit_values; | ||
}; | ||
|
||
KnownBits ExtractKnownBits(const IntervalSet& intervals, | ||
std::optional<Node*> source = std::nullopt); | ||
|
||
} // namespace xls::interval_ops | ||
|
||
#endif // XLS_IR_INTERVAL_OPS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2023 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/ir/interval_ops.h" | ||
|
||
#include <vector> | ||
|
||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "absl/algorithm/container.h" | ||
#include "absl/types/span.h" | ||
#include "xls/ir/bits.h" | ||
#include "xls/ir/interval.h" | ||
#include "xls/ir/interval_set.h" | ||
#include "xls/ir/ternary.h" | ||
|
||
namespace xls::interval_ops { | ||
|
||
namespace { | ||
|
||
IntervalSet SetOf(absl::Span<const Interval> intervals) { | ||
IntervalSet is(intervals.front().BitCount()); | ||
absl::c_for_each(intervals, [&](auto v) { is.AddInterval(v); }); | ||
is.Normalize(); | ||
return is; | ||
} | ||
TEST(IntervalOpsTest, BitsPrecise) { | ||
IntervalSet is = SetOf({Interval::Precise(UBits(21, 8))}); | ||
auto known = ExtractKnownBits(is); | ||
EXPECT_EQ(known.known_bits, Bits::AllOnes(8)); | ||
EXPECT_EQ(known.known_bit_values, UBits(21, 8)); | ||
} | ||
TEST(IntervalOpsTest, BitsMaximal) { | ||
IntervalSet is = SetOf({Interval::Maximal(8)}); | ||
auto known = ExtractKnownBits(is); | ||
EXPECT_EQ(known.known_bits, Bits(8)); | ||
EXPECT_EQ(known.known_bit_values, Bits(8)); | ||
} | ||
TEST(IntervalOpsTest, BitsHalfFull) { | ||
IntervalSet is = SetOf({Interval::Maximal(4).ZeroExtend(8)}); | ||
auto known = ExtractKnownBits(is); | ||
EXPECT_EQ(known.known_bits, UBits(0xf0, 8)); | ||
EXPECT_EQ(known.known_bit_values, Bits(8)); | ||
} | ||
TEST(IntervalOpsTest, MiddleOut) { | ||
IntervalSet is = SetOf({Interval(UBits(0, 8), UBits(0x4, 8)), | ||
Interval(UBits(0x10, 8), UBits(0x14, 8))}); | ||
auto known = ExtractKnownBits(is); | ||
EXPECT_EQ(known.known_bits, UBits(0xe0, 8)); | ||
EXPECT_EQ(known.known_bit_values, Bits(8)); | ||
} | ||
TEST(IntervalOpsTest, MiddleOutHigh) { | ||
IntervalSet is = SetOf({Interval(UBits(0xe0, 8), UBits(0xe4, 8)), | ||
Interval(UBits(0xf0, 8), UBits(0xf4, 8))}); | ||
auto known = ExtractKnownBits(is); | ||
EXPECT_EQ(known.known_bits, UBits(0xe0, 8)); | ||
EXPECT_EQ(known.known_bit_values, UBits(0xe0, 8)); | ||
} | ||
TEST(IntervalOpsTest, MiddleOutTernary) { | ||
IntervalSet is = SetOf({Interval(UBits(0, 8), UBits(0x4, 8)), | ||
Interval(UBits(0x10, 8), UBits(0x14, 8))}); | ||
auto known = ExtractTernaryVector(is); | ||
TernaryVector expected{ | ||
TernaryValue::kUnknown, TernaryValue::kUnknown, | ||
TernaryValue::kUnknown, TernaryValue::kUnknown, | ||
TernaryValue::kUnknown, TernaryValue::kKnownZero, | ||
TernaryValue::kKnownZero, TernaryValue::kKnownZero, | ||
}; | ||
EXPECT_EQ(known, expected); | ||
} | ||
|
||
} // namespace | ||
} // namespace xls::interval_ops |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters