Skip to content

Commit c1f5383

Browse files
sitio-coutolanza
authored andcommitted
[CIR] Refactor StructType printing/parsing
This change simplifies the StructType printing/parsing methods. It also updates the representations to group members between braces and to remove commas between type attributes (e.g. id, packed, etc.). ghstack-source-id: 094d506 Pull Request resolved: #226
1 parent 6dc852c commit c1f5383

File tree

26 files changed

+127
-136
lines changed

26 files changed

+127
-136
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTypes.td

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,6 @@ def CIR_StructType : CIR_Type<"Struct", "struct",
113113
"std::optional<::mlir::cir::ASTRecordDeclAttr>":$ast
114114
);
115115

116-
let builders = [
117-
TypeBuilder<(ins
118-
"ArrayRef<mlir::Type>":$members, "StringRef":$typeName,
119-
"bool":$body
120-
), [{
121-
auto id = mlir::StringAttr::get(context, typeName);
122-
auto sTy = StructType::get(context, members, id, body,
123-
/*packed=*/false, std::nullopt);
124-
return sTy;
125-
}]>
126-
];
127-
128116
let hasCustomAssemblyFormat = 1;
129117

130118
let extraClassDeclaration = [{

clang/lib/CIR/Dialect/IR/CIRDialect.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
1515
#include "clang/CIR/Dialect/IR/CIROpsEnums.h"
1616
#include "clang/CIR/Dialect/IR/CIRTypes.h"
17+
#include <optional>
1718

1819
#include "mlir/Dialect/Func/IR/FuncOps.h"
1920
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
@@ -2104,9 +2105,10 @@ void SignedOverflowBehaviorAttr::print(::mlir::AsmPrinter &printer) const {
21042105

21052106
::mlir::Attribute ASTFunctionDeclAttr::parse(::mlir::AsmParser &parser,
21062107
::mlir::Type type) {
2107-
// We cannot really parse anything AST related at this point
2108-
// since we have no serialization/JSON story.
2109-
return ASTFunctionDeclAttr::get(parser.getContext(), nullptr);
2108+
// We cannot really parse anything AST related at this point since we have no
2109+
// serialization/JSON story. Even if the attr is parsed, it just holds nullptr
2110+
// instead of the AST node.
2111+
return get(parser.getContext(), nullptr);
21102112
}
21112113

21122114
void ASTFunctionDeclAttr::print(::mlir::AsmPrinter &printer) const {
@@ -2121,9 +2123,10 @@ LogicalResult ASTFunctionDeclAttr::verify(
21212123

21222124
::mlir::Attribute ASTVarDeclAttr::parse(::mlir::AsmParser &parser,
21232125
::mlir::Type type) {
2124-
// We cannot really parse anything AST related at this point
2125-
// since we have no serialization/JSON story.
2126-
return ASTVarDeclAttr::get(parser.getContext(), nullptr);
2126+
// We cannot really parse anything AST related at this point since we have no
2127+
// serialization/JSON story. Even if the attr is parsed, it just holds nullptr
2128+
// instead of the AST node.
2129+
return get(parser.getContext(), nullptr);
21272130
}
21282131

21292132
void ASTVarDeclAttr::print(::mlir::AsmPrinter &printer) const {
@@ -2138,9 +2141,10 @@ LogicalResult ASTVarDeclAttr::verify(
21382141

21392142
::mlir::Attribute ASTRecordDeclAttr::parse(::mlir::AsmParser &parser,
21402143
::mlir::Type type) {
2141-
// We cannot really parse anything AST related at this point
2142-
// since we have no serialization/JSON story.
2143-
return ASTRecordDeclAttr::get(parser.getContext(), nullptr);
2144+
// We cannot really parse anything AST related at this point since we have no
2145+
// serialization/JSON story. Even if the attr is parsed, it just holds nullptr
2146+
// instead of the AST node.
2147+
return get(parser.getContext(), nullptr);
21442148
}
21452149

21462150
void ASTRecordDeclAttr::print(::mlir::AsmPrinter &printer) const {

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "clang/CIR/Dialect/IR/CIRTypes.h"
14+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
1415
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1516

1617
#include "mlir/IR/Attributes.h"
18+
#include "mlir/IR/BuiltinAttributes.h"
1719
#include "mlir/IR/BuiltinTypes.h"
1820
#include "mlir/IR/DialectImplementation.h"
1921
#include "mlir/Support/LogicalResult.h"
@@ -22,6 +24,7 @@
2224
#include "llvm/ADT/SmallVector.h"
2325
#include "llvm/ADT/TypeSwitch.h"
2426
#include "llvm/Support/ErrorHandling.h"
27+
#include <optional>
2528

2629
//===----------------------------------------------------------------------===//
2730
// CIR Custom Parser/Printer Signatures
@@ -87,72 +90,69 @@ Type BoolType::parse(mlir::AsmParser &parser) {
8790

8891
void BoolType::print(mlir::AsmPrinter &printer) const {}
8992

93+
//===----------------------------------------------------------------------===//
94+
// StructType Definitions
95+
//===----------------------------------------------------------------------===//
96+
9097
Type StructType::parse(mlir::AsmParser &parser) {
98+
llvm::SmallVector<mlir::Type> members;
99+
mlir::StringAttr id;
100+
bool body = false;
101+
bool packed = false;
102+
mlir::cir::ASTRecordDeclAttr ast = nullptr;
103+
91104
if (parser.parseLess())
92-
return Type();
93-
std::string typeName;
94-
if (parser.parseString(&typeName))
95-
return Type();
105+
return {};
96106

97-
llvm::SmallVector<Type> members;
98-
bool parsedBody = false;
99-
100-
auto parseASTAttribute = [&](Attribute &attr) {
101-
auto optAttr = parser.parseOptionalAttribute(attr);
102-
if (optAttr.has_value()) {
103-
if (failed(*optAttr))
104-
return false;
105-
if (attr.isa<ASTFunctionDeclAttr>() || attr.isa<ASTRecordDeclAttr>() ||
106-
attr.isa<ASTVarDeclAttr>())
107-
return true;
108-
parser.emitError(parser.getCurrentLocation(),
109-
"Unknown cir.struct attribute");
110-
return false;
111-
}
112-
return false;
113-
};
114-
115-
while (mlir::succeeded(parser.parseOptionalComma())) {
116-
if (mlir::succeeded(parser.parseOptionalKeyword("incomplete")))
117-
continue;
118-
119-
parsedBody = true;
120-
Type nextMember;
121-
auto optTy = parser.parseOptionalType(nextMember);
122-
if (optTy.has_value()) {
123-
if (failed(*optTy))
124-
return Type();
125-
members.push_back(nextMember);
126-
continue;
127-
}
107+
if (parser.parseAttribute(id))
108+
return {};
128109

129-
// Maybe it's an AST attribute: always last member, break.
130-
Attribute astAttr;
131-
if (parseASTAttribute(astAttr))
132-
break;
110+
if (parser.parseOptionalKeyword("packed").succeeded())
111+
packed = true;
112+
113+
if (parser.parseOptionalKeyword("incomplete").failed()) {
114+
body = true;
115+
const auto delim = AsmParser::Delimiter::Braces;
116+
auto result = parser.parseCommaSeparatedList(delim, [&]() -> ParseResult {
117+
mlir::Type ty;
118+
if (parser.parseType(ty))
119+
return mlir::failure();
120+
members.push_back(ty);
121+
return mlir::success();
122+
});
123+
124+
if (result.failed())
125+
return {};
133126
}
134127

128+
parser.parseOptionalAttribute(ast);
129+
135130
if (parser.parseGreater())
136-
return Type();
137-
auto sTy = get(parser.getContext(), members, typeName, parsedBody);
138-
return sTy;
131+
return {};
132+
133+
return StructType::get(parser.getContext(), members, id, body, packed,
134+
std::nullopt);
139135
}
140136

141137
void StructType::print(mlir::AsmPrinter &printer) const {
142-
printer << '<' << getTypeName();
138+
printer << '<' << getTypeName() << " ";
139+
140+
if (getPacked())
141+
printer << "packed ";
142+
143143
if (!getBody()) {
144-
printer << ", incomplete";
144+
printer << "incomplete";
145145
} else {
146-
auto members = getMembers();
147-
if (!members.empty()) {
148-
printer << ", ";
149-
llvm::interleaveComma(getMembers(), printer);
150-
}
146+
printer << "{";
147+
llvm::interleaveComma(getMembers(), printer);
148+
printer << "}";
151149
}
152-
if (getAst()) {
153-
printer << ", ";
154-
printer.printAttributeWithoutType(*getAst());
150+
151+
if (getAst().has_value()) {
152+
printer << " ";
153+
printer.printAttribute(getAst().value());
155154
}
155+
156156
printer << '>';
157157
}
158158

clang/test/CIR/CodeGen/agg-init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++17 -fclangir -Wno-unused-value -emit-cir %s -o %t.cir
22
// RUN: FileCheck --input-file=%t.cir %s
33

4-
// CHECK: !ty_22struct2EZero22 = !cir.struct<"struct.Zero", !u8i>
5-
// CHECK: !ty_22struct2Eyep_22 = !cir.struct<"struct.yep_", !u32i, !u32i>
4+
// CHECK: !ty_22struct2EZero22 = !cir.struct<"struct.Zero" {!u8i}>
5+
// CHECK: !ty_22struct2Eyep_22 = !cir.struct<"struct.yep_" {!u32i, !u32i}>
66

77
struct Zero {
88
void yolo();

clang/test/CIR/CodeGen/atomic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ typedef struct _a {
77

88
void m() { at y; }
99

10-
// CHECK: !ty_22struct2E_a22 = !cir.struct<"struct._a", !s32i>
10+
// CHECK: !ty_22struct2E_a22 = !cir.struct<"struct._a" {!s32i}>

clang/test/CIR/CodeGen/bitfields.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ void m() {
1414
__long l;
1515
}
1616

17-
// CHECK: !ty_22struct2Eanon22 = !cir.struct<"struct.anon", !u32i, #cir.recdecl.ast>
18-
// CHECK: !ty_22struct2E__long22 = !cir.struct<"struct.__long", !ty_22struct2Eanon22, !u32i, !cir.ptr<!u32i>>
17+
// CHECK: !ty_22struct2Eanon22 = !cir.struct<"struct.anon" {!u32i} #cir.recdecl.ast>
18+
// CHECK: !ty_22struct2E__long22 = !cir.struct<"struct.__long" {!ty_22struct2Eanon22, !u32i, !cir.ptr<!u32i>}>

clang/test/CIR/CodeGen/coro-task.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ co_invoke_fn co_invoke;
126126

127127
}} // namespace folly::coro
128128

129-
// CHECK: ![[VoidTask:ty_.*]] = !cir.struct<"struct.folly::coro::Task", !u8i>
130-
// CHECK: ![[IntTask:ty_.*]] = !cir.struct<"struct.folly::coro::Task", !u8i>
131-
// CHECK: ![[VoidPromisse:ty_.*]] = !cir.struct<"struct.folly::coro::Task<void>::promise_type", !u8i>
132-
// CHECK: ![[CoroHandleVoid:ty_.*]] = !cir.struct<"struct.std::coroutine_handle", !u8i>
133-
// CHECK: ![[CoroHandlePromise:ty_.*]] = !cir.struct<"struct.std::coroutine_handle", !u8i>
134-
// CHECK: ![[StdString:ty_.*]] = !cir.struct<"struct.std::string", !u8i
135-
// CHECK: ![[SuspendAlways:ty_.*]] = !cir.struct<"struct.std::suspend_always", !u8i>
129+
// CHECK: ![[VoidTask:ty_.*]] = !cir.struct<"struct.folly::coro::Task" {!u8i}>
130+
// CHECK: ![[IntTask:ty_.*]] = !cir.struct<"struct.folly::coro::Task" {!u8i}>
131+
// CHECK: ![[VoidPromisse:ty_.*]] = !cir.struct<"struct.folly::coro::Task<void>::promise_type" {!u8i}>
132+
// CHECK: ![[CoroHandleVoid:ty_.*]] = !cir.struct<"struct.std::coroutine_handle" {!u8i}>
133+
// CHECK: ![[CoroHandlePromise:ty_.*]] = !cir.struct<"struct.std::coroutine_handle" {!u8i}>
134+
// CHECK: ![[StdString:ty_.*]] = !cir.struct<"struct.std::string" {!u8i}
135+
// CHECK: ![[SuspendAlways:ty_.*]] = !cir.struct<"struct.std::suspend_always" {!u8i}>
136136

137137
// CHECK: module {{.*}} {
138138
// CHECK-NEXT: cir.global external @_ZN5folly4coro9co_invokeE = #cir.zero : !ty_22struct2Efolly3A3Acoro3A3Aco_invoke_fn22

clang/test/CIR/CodeGen/ctor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void baz() {
1111
Struk s;
1212
}
1313

14-
// CHECK: !ty_22struct2EStruk22 = !cir.struct<"struct.Struk", !s32i>
14+
// CHECK: !ty_22struct2EStruk22 = !cir.struct<"struct.Struk" {!s32i}>
1515

1616
// CHECK: cir.func linkonce_odr @_ZN5StrukC2Ev(%arg0: !cir.ptr<!ty_22struct2EStruk22>
1717
// CHECK-NEXT: %0 = cir.alloca !cir.ptr<!ty_22struct2EStruk22>, cir.ptr <!cir.ptr<!ty_22struct2EStruk22>>, ["this", init] {alignment = 8 : i64}

clang/test/CIR/CodeGen/derived-to-base.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ void C3::Layer::Initialize() {
7575
}
7676
}
7777

78-
// CHECK: !ty_22class2EC23A3ALayer22 = !cir.struct<"class.C2::Layer", !ty_22class2EC13A3ALayer22, !cir.ptr<!ty_22class2EC222>
79-
// CHECK: !ty_22struct2EC33A3ALayer22 = !cir.struct<"struct.C3::Layer", !ty_22class2EC23A3ALayer22
78+
// CHECK: !ty_22class2EC23A3ALayer22 = !cir.struct<"class.C2::Layer" {!ty_22class2EC13A3ALayer22, !cir.ptr<!ty_22class2EC222>
79+
// CHECK: !ty_22struct2EC33A3ALayer22 = !cir.struct<"struct.C3::Layer" {!ty_22class2EC23A3ALayer22
8080

8181
// CHECK: cir.func @_ZN2C35Layer10InitializeEv
8282

clang/test/CIR/CodeGen/dtors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class B : public A
3838
};
3939

4040
// Class A
41-
// CHECK: ![[ClassA:ty_.*]] = !cir.struct<"class.A", !cir.ptr<!cir.ptr<!cir.func<!u32i ()>>>, #cir.recdecl.ast>
41+
// CHECK: ![[ClassA:ty_.*]] = !cir.struct<"class.A" {!cir.ptr<!cir.ptr<!cir.func<!u32i ()>>>} #cir.recdecl.ast>
4242

4343
// Class B
44-
// CHECK: ![[ClassB:ty_.*]] = !cir.struct<"class.B", ![[ClassA]]>
44+
// CHECK: ![[ClassB:ty_.*]] = !cir.struct<"class.B" {![[ClassA]]}>
4545

4646
// CHECK: cir.func @_Z4bluev()
4747
// CHECK: %0 = cir.alloca !ty_22class2EPSEvent22, cir.ptr <!ty_22class2EPSEvent22>, ["p", init] {alignment = 8 : i64}

0 commit comments

Comments
 (0)