Skip to content

Commit

Permalink
[vast] Represent single-bit signed bitvectors as scalars.
Browse files Browse the repository at this point in the history
Previously, they would be `wire signed [0:0]`.

PiperOrigin-RevId: 682511756
  • Loading branch information
grebe authored and copybara-github committed Oct 5, 2024
1 parent d8a8ca0 commit a933ce3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
11 changes: 8 additions & 3 deletions xls/codegen/vast/vast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,12 @@ std::string ToString(Direction direction) {

std::string ScalarType::Emit(LineInfo* line_info) const {
// The `DataKind` preceding the type is enough.
return EmitNothing(this, line_info);
if (!is_signed_) {
return EmitNothing(this, line_info);
}
LineInfoStart(line_info, this);
LineInfoEnd(line_info, this);
return " signed";
}

std::string IntegerType::Emit(LineInfo* line_info) const {
Expand Down Expand Up @@ -345,8 +350,8 @@ BitVectorType* VerilogFile::BitVectorTypeNoScalar(int64_t bit_count,
DataType* VerilogFile::BitVectorType(int64_t bit_count, const SourceInfo& loc,
bool is_signed) {
CHECK_GT(bit_count, 0);
if (bit_count == 1 && !is_signed) {
return ScalarType(loc);
if (bit_count == 1) {
return Make<verilog::ScalarType>(loc, is_signed);
}
return BitVectorTypeNoScalar(bit_count, loc, is_signed);
}
Expand Down
9 changes: 8 additions & 1 deletion xls/codegen/vast/vast.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,20 @@ class DataType : public VastNode {
// wire foo;
class ScalarType final : public DataType {
public:
ScalarType(VerilogFile* file, const SourceInfo& loc) : DataType(file, loc) {}
ScalarType(VerilogFile* file, const SourceInfo& loc)
: DataType(file, loc), is_signed_(false) {}
ScalarType(bool is_signed, VerilogFile* file, const SourceInfo& loc)
: DataType(file, loc), is_signed_(is_signed) {}

bool IsScalar() const final { return true; }
bool is_signed() const final { return is_signed_; }
absl::StatusOr<int64_t> WidthAsInt64() const final { return 1; }
absl::StatusOr<int64_t> FlatBitCountAsInt64() const final { return 1; }
std::optional<Expression*> width() const final { return std::nullopt; }
std::string Emit(LineInfo* line_info) const final;

private:
bool is_signed_;
};

// Represents an integer type. Example:
Expand Down
2 changes: 1 addition & 1 deletion xls/codegen/vast/vast_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ TEST_P(VastTest, DataTypes) {

DataType* s1 = f.BitVectorType(1, SourceInfo(), /*is_signed=*/true);
EXPECT_FALSE(s1->IsUserDefined());
EXPECT_EQ(s1->EmitWithIdentifier(nullptr, "foo"), " signed [0:0] foo");
EXPECT_EQ(s1->EmitWithIdentifier(nullptr, "foo"), " signed foo");
EXPECT_THAT(s1->WidthAsInt64(), IsOkAndHolds(1));
EXPECT_THAT(s1->FlatBitCountAsInt64(), IsOkAndHolds(1));
EXPECT_TRUE(s1->is_signed());
Expand Down

0 comments on commit a933ce3

Please sign in to comment.