Skip to content

Commit cbf306e

Browse files
committed
[CIR] Refactor pointers to RecordType constraints
1 parent d5527cc commit cbf306e

File tree

5 files changed

+43
-38
lines changed

5 files changed

+43
-38
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,20 @@ def DynamicCastOp : CIR_Op<"dyn_cast"> {
246246
cast-to-complete operation.
247247
}];
248248

249-
let arguments = (ins DynamicCastKind:$kind,
250-
RecordPtr:$src,
251-
OptionalAttr<DynamicCastInfoAttr>:$info,
252-
UnitAttr:$relative_layout);
253-
let results = (outs CIR_PointerType:$result);
249+
let arguments = (ins
250+
DynamicCastKind:$kind,
251+
CIR_PtrToRecordType:$src,
252+
OptionalAttr<DynamicCastInfoAttr>:$info,
253+
UnitAttr:$relative_layout
254+
);
255+
256+
let results = (outs
257+
CIR_PtrToAnyOf<[CIR_VoidType, CIR_RecordType]>:$result
258+
);
254259

255260
let assemblyFormat = [{
256261
`(`
257-
$kind `,` $src `:` type($src)
262+
$kind `,` $src `:` qualified(type($src))
258263
(`,` qualified($info)^)?
259264
(`relative_layout` $relative_layout^)?
260265
`)`
@@ -273,8 +278,6 @@ def DynamicCastOp : CIR_Op<"dyn_cast"> {
273278
return getType().isVoidPtr();
274279
}
275280
}];
276-
277-
let hasVerifier = 1;
278281
}
279282

280283
//===----------------------------------------------------------------------===//
@@ -3037,7 +3040,7 @@ def GetRuntimeMemberOp : CIR_Op<"get_runtime_member"> {
30373040
}];
30383041

30393042
let arguments = (ins
3040-
Arg<RecordPtr, "address of the record object", [MemRead]>:$addr,
3043+
Arg<CIR_PtrToRecordType, "address of the record object", [MemRead]>:$addr,
30413044
Arg<CIR_DataMemberType, "pointer to the target member">:$member);
30423045

30433046
let results = (outs Res<CIR_PointerType, "">:$result);
@@ -3091,7 +3094,7 @@ def GetMethodOp : CIR_Op<"get_method"> {
30913094
method.
30923095
}];
30933096

3094-
let arguments = (ins CIR_MethodType:$method, RecordPtr:$object);
3097+
let arguments = (ins CIR_MethodType:$method, CIR_PtrToRecordType:$object);
30953098
let results = (outs FuncPtr:$callee, CIR_VoidPtrType:$adjusted_this);
30963099

30973100
let assemblyFormat = [{

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class CIR_ConfinedType<Type type, list<Pred> preds, string summary = "">
3131
: Type<And<[type.predicate, CIR_CastedSelfsToType<type.cppType, preds>]>,
3232
summary, type.cppType>;
3333

34+
// Generates a type summary.
35+
// - For a single type: returns its summary.
36+
// - For multiple types: returns `any of <comma-separated summaries>`.
37+
class CIR_TypeSummaries<list<Type> types> {
38+
assert !not(!empty(types)), "expects non-empty list of types";
39+
40+
list<string> summaries = !foreach(type, types, type.summary);
41+
string joined = !interleave(summaries, ", ");
42+
43+
string value = !if(!eq(!size(types), 1), joined, "any of " # joined);
44+
}
45+
3446
//===----------------------------------------------------------------------===//
3547
// IntType predicates
3648
//===----------------------------------------------------------------------===//
@@ -151,6 +163,12 @@ def CIR_AnyIntOrFloatType : AnyTypeOf<[CIR_AnyFloatType, CIR_AnyIntType],
151163

152164
def CIR_AnyComplexType : CIR_TypeBase<"::cir::ComplexType", "complex type">;
153165

166+
//===----------------------------------------------------------------------===//
167+
// Record Type predicates
168+
//===----------------------------------------------------------------------===//
169+
170+
def CIR_AnyRecordType : CIR_TypeBase<"::cir::RecordType", "record type">;
171+
154172
//===----------------------------------------------------------------------===//
155173
// Pointer Type predicates
156174
//===----------------------------------------------------------------------===//
@@ -176,9 +194,14 @@ class CIR_PtrToPtrTo<code type, string summary>
176194
class CIR_PointeePred<Pred pred> : SubstLeaves<"$_self",
177195
"::mlir::cast<::cir::PointerType>($_self).getPointee()", pred>;
178196

179-
class CIR_PtrToType<Type type>
180-
: CIR_ConfinedType<CIR_AnyPtrType, [CIR_PointeePred<type.predicate>],
181-
"pointer to " # type.summary>;
197+
class CIR_PtrToAnyOf<list<Type> types, string summary = "">
198+
: CIR_ConfinedType<CIR_AnyPtrType,
199+
[Or<!foreach(type, types, CIR_PointeePred<type.predicate>)>],
200+
!if(!empty(summary),
201+
"pointer to " # CIR_TypeSummaries<types>.value,
202+
summary)>;
203+
204+
class CIR_PtrToType<Type type> : CIR_PtrToAnyOf<[type]>;
182205

183206
// Void pointer type constraints
184207
def CIR_VoidPtrType
@@ -197,4 +220,6 @@ def CIR_PtrToIntOrFloatType : CIR_PtrToType<CIR_AnyIntOrFloatType>;
197220

198221
def CIR_PtrToComplexType : CIR_PtrToType<CIR_AnyComplexType>;
199222

223+
def CIR_PtrToRecordType : CIR_PtrToType<CIR_AnyRecordType>;
224+
200225
#endif // CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,6 @@ def CIR_VoidType : CIR_Type<"Void", "void"> {
505505

506506
// Constraints
507507

508-
// Pointer to record
509-
def RecordPtr : Type<
510-
And<[
511-
CPred<"::mlir::isa<::cir::PointerType>($_self)">,
512-
CPred<"::mlir::isa<::cir::RecordType>("
513-
"::mlir::cast<::cir::PointerType>($_self).getPointee())">
514-
]>, "!cir.record*"> {
515-
}
516-
517508
// Pointer to exception info
518509
def ExceptionPtr : Type<
519510
And<[

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -843,19 +843,6 @@ OpFoldResult cir::UnaryOp::fold(FoldAdaptor adaptor) {
843843
return {};
844844
}
845845

846-
//===----------------------------------------------------------------------===//
847-
// DynamicCastOp
848-
//===----------------------------------------------------------------------===//
849-
850-
LogicalResult cir::DynamicCastOp::verify() {
851-
auto resultPointeeTy = mlir::cast<cir::PointerType>(getType()).getPointee();
852-
if (!mlir::isa<cir::VoidType, cir::RecordType>(resultPointeeTy))
853-
return emitOpError()
854-
<< "cir.dyn_cast must produce a void ptr or record ptr";
855-
856-
return mlir::success();
857-
}
858-
859846
//===----------------------------------------------------------------------===//
860847
// BaseDataMemberOp & DerivedDataMemberOp
861848
//===----------------------------------------------------------------------===//
@@ -3650,8 +3637,7 @@ LogicalResult cir::InsertMemberOp::verify() {
36503637
//===----------------------------------------------------------------------===//
36513638

36523639
LogicalResult cir::GetRuntimeMemberOp::verify() {
3653-
auto recordTy =
3654-
cast<RecordType>(cast<PointerType>(getAddr().getType()).getPointee());
3640+
auto recordTy = cast<RecordType>(getAddr().getType().getPointee());
36553641
auto memberPtrTy = getMember().getType();
36563642

36573643
if (recordTy != memberPtrTy.getClsTy()) {

clang/test/CIR/IR/invalid.cir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ module {
894894
module {
895895
cir.func @invalid_base_type(%arg0 : !cir.data_member<!u32i in !struct1>) {
896896
%0 = cir.alloca !u32i, !cir.ptr<!u32i>, ["tmp"] {alignment = 4 : i64}
897-
// expected-error@+1 {{'cir.get_runtime_member' op operand #0 must be !cir.record*}}
897+
// expected-error@+1 {{'cir.get_runtime_member' op operand #0 must be pointer to record type}}
898898
%1 = cir.get_runtime_member %0[%arg0 : !cir.data_member<!u32i in !struct1>] : !cir.ptr<!u32i> -> !cir.ptr<!u32i>
899899
cir.return
900900
}

0 commit comments

Comments
 (0)