Skip to content

Commit a36c258

Browse files
committed
[Moore] Remove enum type
Remove the enum type from the Moore dialect. No passes or lowerings currently make use of this type, but imemdiately resolve it to its underlying integer type instead. This simplifies the dialect. Once the need for an enum type arises again in the future, we can define a new one.
1 parent 2769f65 commit a36c258

File tree

6 files changed

+10
-220
lines changed

6 files changed

+10
-220
lines changed

include/circt/Dialect/Moore/MooreTypes.h

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ struct UnsizedDimStorage;
241241
struct RangeDimStorage;
242242
struct SizedDimStorage;
243243
struct AssocDimStorage;
244-
struct EnumTypeStorage;
245244
struct StructTypeStorage;
246245
} // namespace detail
247246

@@ -396,7 +395,6 @@ class VoidType;
396395
class IntType;
397396
class PackedIndirectType;
398397
class PackedDim;
399-
class EnumType;
400398
class PackedStructType;
401399

402400
/// A packed SystemVerilog type.
@@ -425,7 +423,7 @@ class PackedType : public UnpackedType {
425423
static bool classof(Type type) {
426424
return llvm::isa<VoidType>(type) || llvm::isa<IntType>(type) ||
427425
llvm::isa<PackedIndirectType>(type) || llvm::isa<PackedDim>(type) ||
428-
llvm::isa<EnumType>(type) || llvm::isa<PackedStructType>(type);
426+
llvm::isa<PackedStructType>(type);
429427
}
430428

431429
/// Resolve one level of name or type reference indirection.
@@ -994,39 +992,6 @@ class UnpackedQueueDim : public Type::TypeBase<UnpackedQueueDim, UnpackedDim,
994992
friend struct detail::DimStorage;
995993
};
996994

997-
//===----------------------------------------------------------------------===//
998-
// Enumerations
999-
//===----------------------------------------------------------------------===//
1000-
1001-
/// An enum type.
1002-
class EnumType
1003-
: public Type::TypeBase<EnumType, PackedType, detail::EnumTypeStorage> {
1004-
public:
1005-
static EnumType get(StringAttr name, Location loc, PackedType base = {});
1006-
1007-
/// Get the base type of the enumeration.
1008-
PackedType getBase() const;
1009-
/// Returns whether the base type was explicitly specified by the user. This
1010-
/// allows us to distinguish `enum` from `enum int`.
1011-
bool isBaseExplicit() const;
1012-
/// Get the name of the surrounding typedef, if this enum is embedded in a
1013-
/// typedef. Otherwise this returns a null attribute.
1014-
StringAttr getName() const;
1015-
/// Get the location in the source text where the enum was declared. This
1016-
/// shall be the location of the `enum` keyword or, if the enum is embedded in
1017-
/// a typedef, the location of the typedef name.
1018-
Location getLoc() const;
1019-
1020-
/// Format this enum in SystemVerilog syntax. Useful to present the enum back
1021-
/// to the user in diagnostics.
1022-
void format(llvm::raw_ostream &os) const;
1023-
1024-
static constexpr StringLiteral name = "moore.enum";
1025-
1026-
protected:
1027-
using Base::Base;
1028-
};
1029-
1030995
//===----------------------------------------------------------------------===//
1031996
// Packed and Unpacked Structs
1032997
//===----------------------------------------------------------------------===//

lib/Conversion/ImportVerilog/Types.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,8 @@ struct TypeVisitor {
148148

149149
// Handle enums.
150150
Type visit(const slang::ast::EnumType &type) {
151-
auto baseType = type.baseType.visit(*this);
152-
if (!baseType)
153-
return {};
154-
return moore::EnumType::get(StringAttr{}, loc,
155-
cast<moore::PackedType>(baseType));
151+
// Simply return the underlying type.
152+
return type.baseType.visit(*this);
156153
}
157154

158155
// Collect the members in a struct or union.

lib/Dialect/Moore/MooreTypes.cpp

Lines changed: 4 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void MooreDialect::registerTypes() {
3737
addTypes<IntType, RealType, PackedNamedType, PackedRefType, UnpackedNamedType,
3838
UnpackedRefType, PackedUnsizedDim, PackedRangeDim,
3939
UnpackedUnsizedDim, UnpackedArrayDim, UnpackedRangeDim,
40-
UnpackedAssocDim, UnpackedQueueDim, EnumType, PackedStructType,
40+
UnpackedAssocDim, UnpackedQueueDim, PackedStructType,
4141
UnpackedStructType>();
4242

4343
addTypes<
@@ -260,7 +260,6 @@ Domain PackedType::getDomain() const {
260260
.Case<IntType>([&](auto type) { return type.getDomain(); })
261261
.Case<PackedIndirectType, PackedDim>(
262262
[&](auto type) { return type.getInner().getDomain(); })
263-
.Case<EnumType>([](auto type) { return type.getBase().getDomain(); })
264263
.Case<PackedStructType>(
265264
[](auto type) { return type.getStruct().domain; });
266265
}
@@ -271,8 +270,7 @@ Sign PackedType::getSign() const {
271270
.Case<IntType, PackedStructType>(
272271
[&](auto type) { return type.getSign(); })
273272
.Case<PackedIndirectType, PackedDim>(
274-
[&](auto type) { return type.getInner().getSign(); })
275-
.Case<EnumType>([](auto type) { return type.getBase().getSign(); });
273+
[&](auto type) { return type.getInner().getSign(); });
276274
}
277275

278276
std::optional<unsigned> PackedType::getBitSize() const {
@@ -287,16 +285,15 @@ std::optional<unsigned> PackedType::getBitSize() const {
287285
})
288286
.Case<PackedIndirectType>(
289287
[](auto type) { return type.getInner().getBitSize(); })
290-
.Case<EnumType>([](auto type) { return type.getBase().getBitSize(); })
291288
.Case<PackedStructType>(
292289
[](auto type) { return type.getStruct().bitSize; });
293290
}
294291

295292
void PackedType::format(llvm::raw_ostream &os) const {
296293
TypeSwitch<PackedType>(*this)
297294
.Case<VoidType>([&](auto) { os << "void"; })
298-
.Case<IntType, PackedRangeDim, PackedUnsizedDim, EnumType,
299-
PackedStructType>([&](auto type) { type.format(os); })
295+
.Case<IntType, PackedRangeDim, PackedUnsizedDim, PackedStructType>(
296+
[&](auto type) { type.format(os); })
300297
.Case<PackedNamedType>(
301298
[&](auto type) { os << type.getName().getValue(); })
302299
.Case<PackedRefType>(
@@ -938,66 +935,6 @@ std::optional<unsigned> UnpackedQueueDim::getBound() const {
938935
return bound;
939936
}
940937

941-
//===----------------------------------------------------------------------===//
942-
// Enumerations
943-
//===----------------------------------------------------------------------===//
944-
945-
namespace circt {
946-
namespace moore {
947-
namespace detail {
948-
949-
struct EnumTypeStorage : TypeStorage {
950-
using KeyTy = std::tuple<StringAttr, Location, PackedType, char>;
951-
952-
EnumTypeStorage(KeyTy key)
953-
: name(std::get<0>(key)), loc(std::get<1>(key)), base(std::get<2>(key)),
954-
explicitBase(std::get<3>(key)) {}
955-
bool operator==(const KeyTy &key) const {
956-
return std::get<0>(key) == name && std::get<1>(key) == loc &&
957-
std::get<2>(key) == base && std::get<3>(key) == explicitBase;
958-
}
959-
static EnumTypeStorage *construct(TypeStorageAllocator &allocator,
960-
const KeyTy &key) {
961-
return new (allocator.allocate<EnumTypeStorage>()) EnumTypeStorage(key);
962-
}
963-
964-
StringAttr name;
965-
Location loc;
966-
PackedType base;
967-
bool explicitBase;
968-
};
969-
970-
} // namespace detail
971-
} // namespace moore
972-
} // namespace circt
973-
974-
EnumType EnumType::get(StringAttr name, Location loc, PackedType base) {
975-
return Base::get(loc.getContext(), name, loc,
976-
base ? base : IntType::getInt(loc.getContext()), !!base);
977-
}
978-
979-
PackedType EnumType::getBase() const { return getImpl()->base; }
980-
981-
bool EnumType::isBaseExplicit() const { return getImpl()->explicitBase; }
982-
983-
StringAttr EnumType::getName() const { return getImpl()->name; }
984-
985-
Location EnumType::getLoc() const { return getImpl()->loc; }
986-
987-
void EnumType::format(llvm::raw_ostream &os) const {
988-
os << "enum";
989-
990-
// If the enum is part of a typedefm simply print it as `enum <name>`.
991-
if (auto name = getName()) {
992-
os << " " << name.getValue();
993-
return;
994-
}
995-
996-
// Otherwise print `enum <base-type>` or just `enum`.
997-
if (isBaseExplicit())
998-
os << " " << getBase();
999-
}
1000-
1001938
//===----------------------------------------------------------------------===//
1002939
// Packed and Unpacked Structs
1003940
//===----------------------------------------------------------------------===//
@@ -1227,31 +1164,6 @@ static OptionalParseResult customTypeParser(DialectAsmParser &parser,
12271164
if (auto kind = RealType::getKindFromKeyword(mnemonic))
12281165
return yieldUnpacked(RealType::get(context, *kind));
12291166

1230-
// Enums
1231-
if (mnemonic == "enum") {
1232-
if (parser.parseLess())
1233-
return failure();
1234-
StringAttr name;
1235-
auto result = parser.parseOptionalAttribute(name);
1236-
if (result.has_value())
1237-
if (*result || parser.parseComma())
1238-
return failure();
1239-
LocationAttr loc;
1240-
PackedType base;
1241-
result = parser.parseOptionalAttribute(loc);
1242-
if (result.has_value()) {
1243-
if (*result)
1244-
return failure();
1245-
} else {
1246-
if (parseMooreType(parser, {Subset::Packed, false}, base) ||
1247-
parser.parseComma() || parser.parseAttribute(loc))
1248-
return failure();
1249-
}
1250-
if (parser.parseGreater())
1251-
return failure();
1252-
return yieldPacked(EnumType::get(name, loc, base));
1253-
}
1254-
12551167
// Everything that follows can be packed or unpacked. The packing is inferred
12561168
// from the last `packed<...>` or `unpacked<...>` that we've seen. The
12571169
// `yieldImplied` function will call the first lambda to construct a packed
@@ -1441,19 +1353,6 @@ static LogicalResult customTypePrinter(Type type, DialectAsmPrinter &printer,
14411353
.Case<RealType>(
14421354
[&](auto type) { return printer << type.getKeyword(), success(); })
14431355

1444-
// Enums
1445-
.Case<EnumType>([&](auto type) {
1446-
printer << "enum<";
1447-
if (type.getName())
1448-
printer << type.getName() << ", ";
1449-
if (type.isBaseExplicit()) {
1450-
printMooreType(type.getBase(), printer, subset);
1451-
printer << ", ";
1452-
}
1453-
printer << type.getLoc() << ">";
1454-
return success();
1455-
})
1456-
14571356
// Type indirections
14581357
.Case<PackedNamedType, UnpackedNamedType>([&](auto type) {
14591358
printer << "named<" << type.getName() << ", ";

test/Conversion/ImportVerilog/types.sv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
module Enums;
99
typedef enum shortint { MAGIC } myEnum;
1010

11-
// CHECK-NEXT: %e0 = moore.variable : !moore.enum<int, loc(
12-
// CHECK-NEXT: %e1 = moore.variable : !moore.enum<byte, loc(
13-
// CHECK-NEXT: %e2 = moore.variable : !moore.packed<named<"myEnum", enum<shortint, loc(
11+
// CHECK-NEXT: %e0 = moore.variable : !moore.int
12+
// CHECK-NEXT: %e1 = moore.variable : !moore.byte
13+
// CHECK-NEXT: %e2 = moore.variable : !moore.packed<named<"myEnum", shortint, loc(
1414
enum { FOO, BAR } e0;
1515
enum byte { HELLO = 0, WORLD = 1 } e1;
1616
myEnum e2;

test/Dialect/Moore/types.mlir

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,6 @@ func.func @RealTypes(
8080
%arg2: !moore.realtime
8181
) { return }
8282

83-
// CHECK-LABEL: func @EnumType(
84-
func.func @EnumType(
85-
// CHECK-SAME: %arg0: !moore.enum<loc("foo.sv":42:9001)>
86-
// CHECK-SAME: %arg1: !moore.enum<int, loc("foo.sv":42:9001)>
87-
// CHECK-SAME: %arg2: !moore.enum<"Foo", loc("foo.sv":42:9001)>
88-
// CHECK-SAME: %arg3: !moore.enum<"Foo", int, loc("foo.sv":42:9001)>
89-
%arg0: !moore.enum<loc("foo.sv":42:9001)>,
90-
%arg1: !moore.enum<int, loc("foo.sv":42:9001)>,
91-
%arg2: !moore.enum<"Foo", loc("foo.sv":42:9001)>,
92-
%arg3: !moore.enum<"Foo", int, loc("foo.sv":42:9001)>
93-
) { return }
94-
9583
// CHECK-LABEL: func @IndirectTypes(
9684
func.func @IndirectTypes(
9785
// CHECK-SAME: %arg0: !moore.packed<named<"Foo", bit, loc("foo.sv":42:9001)>>

unittests/Dialect/Moore/TypesTest.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -340,65 +340,6 @@ TEST(TypesTest, Structs) {
340340
ASSERT_EQ(s3.getBitSize(), std::nullopt);
341341
}
342342

343-
TEST(TypesTest, Enums) {
344-
MLIRContext context;
345-
context.loadDialect<MooreDialect>();
346-
auto loc = UnknownLoc::get(&context);
347-
auto foo = StringAttr::get(&context, "Foo");
348-
auto intType = IntType::getInt(&context);
349-
auto bitType = IntType::get(&context, IntType::Bit);
350-
auto bit8Type = PackedRangeDim::get(bitType, 8);
351-
auto slogicType = IntType::get(&context, IntType::Logic, Sign::Signed);
352-
auto slogic8Type = PackedRangeDim::get(slogicType, 8);
353-
354-
auto e0 = EnumType::get({}, loc);
355-
auto e1 = EnumType::get(foo, loc);
356-
auto e2 = EnumType::get({}, loc, bit8Type);
357-
auto e3 = EnumType::get(foo, loc, bit8Type);
358-
auto e4 = EnumType::get({}, loc, slogic8Type);
359-
auto e5 = EnumType::get(foo, loc, slogic8Type);
360-
361-
// Formatting
362-
ASSERT_EQ(e0.toString(), "enum");
363-
ASSERT_EQ(e1.toString(), "enum Foo");
364-
ASSERT_EQ(e2.toString(), "enum bit [7:0]");
365-
ASSERT_EQ(e3.toString(), "enum Foo");
366-
ASSERT_EQ(e4.toString(), "enum logic signed [7:0]");
367-
ASSERT_EQ(e5.toString(), "enum Foo");
368-
369-
// Base types
370-
ASSERT_EQ(e0.getBase(), intType);
371-
ASSERT_EQ(e1.getBase(), intType);
372-
ASSERT_EQ(e2.getBase(), bit8Type);
373-
ASSERT_EQ(e3.getBase(), bit8Type);
374-
ASSERT_EQ(e4.getBase(), slogic8Type);
375-
ASSERT_EQ(e5.getBase(), slogic8Type);
376-
377-
// Sign
378-
ASSERT_EQ(e0.getSign(), Sign::Signed); // implicit int
379-
ASSERT_EQ(e1.getSign(), Sign::Signed); // implicit int
380-
ASSERT_EQ(e2.getSign(), Sign::Unsigned);
381-
ASSERT_EQ(e3.getSign(), Sign::Unsigned);
382-
ASSERT_EQ(e4.getSign(), Sign::Signed);
383-
ASSERT_EQ(e5.getSign(), Sign::Signed);
384-
385-
// Value domain
386-
ASSERT_EQ(e0.getDomain(), Domain::TwoValued);
387-
ASSERT_EQ(e1.getDomain(), Domain::TwoValued);
388-
ASSERT_EQ(e2.getDomain(), Domain::TwoValued);
389-
ASSERT_EQ(e3.getDomain(), Domain::TwoValued);
390-
ASSERT_EQ(e4.getDomain(), Domain::FourValued);
391-
ASSERT_EQ(e5.getDomain(), Domain::FourValued);
392-
393-
// Bit size
394-
ASSERT_EQ(e0.getBitSize(), 32u);
395-
ASSERT_EQ(e1.getBitSize(), 32u);
396-
ASSERT_EQ(e2.getBitSize(), 8u);
397-
ASSERT_EQ(e3.getBitSize(), 8u);
398-
ASSERT_EQ(e4.getBitSize(), 8u);
399-
ASSERT_EQ(e5.getBitSize(), 8u);
400-
}
401-
402343
TEST(TypesTest, SimpleBitVectorTypes) {
403344
MLIRContext context;
404345
context.loadDialect<MooreDialect>();

0 commit comments

Comments
 (0)