Skip to content

Commit 7a0ad42

Browse files
authored
[HW] Extend ElementType parsing to support union types (#9318)
This PR fixes a bug where !hw.union types nested inside !hw.array failed to parse back in after being printed (round-trip failure). When printing nested types, printHWElementType elides the dialect prefix to produce a more readable output (e.g., union<...> instead of !hw.union<...>). However, the corresponding parser helper, parseHWElementType, relies on a hardcoded list of string prefixes to detect when a bare keyword is being used. This list currently includes struct, array, enum, etc., but is missing union. As a result, the parser fails to recognize union<...> as a valid HW type when parsing the output of a valid IR print.
1 parent 678dc94 commit 7a0ad42

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/Dialect/HW/HWTypes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ static ParseResult parseHWElementType(AsmParser &p, Type &result) {
158158
if (typeString.starts_with("array<") || typeString.starts_with("inout<") ||
159159
typeString.starts_with("uarray<") || typeString.starts_with("struct<") ||
160160
typeString.starts_with("typealias<") || typeString.starts_with("int<") ||
161-
typeString.starts_with("enum<")) {
161+
typeString.starts_with("enum<") || typeString.starts_with("union<")) {
162162
llvm::StringRef mnemonic;
163163
if (auto parseResult = generatedTypeParser(p, &mnemonic, result);
164164
parseResult.has_value())

test/Dialect/HW/types.mlir

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,11 @@ module {
5151
%1 = hw.union_create "a", %i : !hw.typealias<@ns::@bar, !hw.union<a: i1, b: i1>>
5252
return
5353
}
54+
55+
// CHECK-LABEL: func @nestedUnionArray
56+
func.func @nestedUnionArray(
57+
// CHECK: %arg0: !hw.array<4xunion<u8: i8, u16: i16>>
58+
%arg0: !hw.array<4x!hw.union<u8: i8, u16: i16>>) {
59+
return
60+
}
5461
}

0 commit comments

Comments
 (0)