Skip to content

Commit 88d8b78

Browse files
xlaukolanza
authored andcommitted
[CIR] Refactor VoidPtr constraint to CIR_VoidPtrType (llvm#1601)
1 parent 75293db commit 88d8b78

File tree

4 files changed

+80
-36
lines changed

4 files changed

+80
-36
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def ResumeOp : CIR_Op<"resume", [ReturnLike, Terminator,
10371037
```
10381038
}];
10391039

1040-
let arguments = (ins Optional<VoidPtr>:$exception_ptr,
1040+
let arguments = (ins Optional<CIR_VoidPtrType>:$exception_ptr,
10411041
Optional<CIR_UInt32>:$type_id,
10421042
UnitAttr:$rethrow);
10431043
let assemblyFormat = [{
@@ -2661,7 +2661,7 @@ def VTTAddrPointOp : CIR_Op<"vtt.address_point",
26612661
let arguments = (ins OptionalAttr<FlatSymbolRefAttr>:$name,
26622662
Optional<CIR_AnyType>:$sym_addr,
26632663
I32Attr:$offset);
2664-
let results = (outs Res<CIR_PointerType, "", []>:$addr);
2664+
let results = (outs CIR_PointerType:$addr);
26652665

26662666
let assemblyFormat = [{
26672667
($name^)?
@@ -3114,7 +3114,7 @@ def GetMethodOp : CIR_Op<"get_method"> {
31143114
}];
31153115

31163116
let arguments = (ins CIR_MethodType:$method, RecordPtr:$object);
3117-
let results = (outs FuncPtr:$callee, VoidPtr:$adjusted_this);
3117+
let results = (outs FuncPtr:$callee, CIR_VoidPtrType:$adjusted_this);
31183118

31193119
let assemblyFormat = [{
31203120
$method `,` $object
@@ -4345,7 +4345,7 @@ def CatchParamOp : CIR_Op<"catch_param"> {
43454345
```
43464346
}];
43474347

4348-
let arguments = (ins Optional<VoidPtr>:$exception_ptr,
4348+
let arguments = (ins Optional<CIR_VoidPtrType>:$exception_ptr,
43494349
OptionalAttr<CatchParamKind>:$kind);
43504350
let results = (outs Optional<CIR_AnyType>:$param);
43514351
let assemblyFormat = [{
@@ -4384,7 +4384,7 @@ def EhInflightOp : CIR_Op<"eh.inflight_exception"> {
43844384

43854385
let arguments = (ins UnitAttr:$cleanup,
43864386
OptionalAttr<FlatSymbolRefArrayAttr>:$sym_type_list);
4387-
let results = (outs VoidPtr:$exception_ptr, CIR_UInt32:$type_id);
4387+
let results = (outs CIR_VoidPtrType:$exception_ptr, CIR_UInt32:$type_id);
43884388
let assemblyFormat = [{
43894389
(`cleanup` $cleanup^)?
43904390
($sym_type_list^)?
@@ -4462,9 +4462,10 @@ def CopyOp : CIR_Op<"copy",
44624462

44634463
class CIR_MemOp<string mnemonic>
44644464
: CIR_Op<mnemonic, [AllTypesMatch<["dst", "src"]>]> {
4465-
dag commonArgs = (ins Arg<VoidPtr, "", [MemWrite]>:$dst,
4466-
Arg<VoidPtr, "", [MemRead]>:$src);
4467-
let hasVerifier = 0;
4465+
dag commonArgs = (ins
4466+
Arg<CIR_VoidPtrType, "", [MemWrite]>:$dst,
4467+
Arg<CIR_VoidPtrType, "", [MemRead]>:$src
4468+
);
44684469
}
44694470

44704471
def MemCpyOp : CIR_MemOp<"libc.memcpy"> {
@@ -4581,7 +4582,7 @@ def MemSetOp : CIR_Op<"libc.memset"> {
45814582
}];
45824583

45834584
let arguments = (ins
4584-
Arg<VoidPtr, "", [MemWrite]>:$dst,
4585+
Arg<CIR_VoidPtrType, "", [MemWrite]>:$dst,
45854586
CIR_SInt32:$val,
45864587
CIR_AnyFundamentalUIntType:$len
45874588
);
@@ -4615,7 +4616,7 @@ def MemSetInlineOp : CIR_Op<"memset_inline"> {
46154616
}];
46164617

46174618
let arguments = (ins
4618-
Arg<VoidPtr, "", [MemWrite]>:$dst,
4619+
Arg<CIR_VoidPtrType, "", [MemWrite]>:$dst,
46194620
CIR_SInt32:$val,
46204621
I64Attr:$len
46214622
);
@@ -4647,12 +4648,12 @@ def MemChrOp : CIR_Op<"libc.memchr"> {
46474648
// TODO: instead of using UInt64 for len, we could make it constrained on
46484649
// size_t (64 or 32) and have a builder that does the right job.
46494650
let arguments = (ins
4650-
Arg<VoidPtr, "", [MemRead]>:$src,
4651+
Arg<CIR_VoidPtrType, "", [MemRead]>:$src,
46514652
CIR_SInt32:$pattern,
46524653
CIR_UInt64:$len
46534654
);
46544655

4655-
let results = (outs Res<VoidPtr, "">:$result);
4656+
let results = (outs CIR_VoidPtrType:$result);
46564657

46574658
let assemblyFormat = [{
46584659
`(`
@@ -4666,7 +4667,7 @@ def MemChrOp : CIR_Op<"libc.memchr"> {
46664667

46674668
class FuncAddrBuiltinOp<string mnemonic> : CIR_Op<mnemonic, []> {
46684669
let arguments = (ins CIR_UInt32:$level);
4669-
let results = (outs Res<VoidPtr, "">:$result);
4670+
let results = (outs CIR_VoidPtrType:$result);
46704671
let assemblyFormat = [{
46714672
`(` $level `)` attr-dict
46724673
}];
@@ -4931,7 +4932,7 @@ def AssumeSepStorageOp : CIR_Op<"assume.separate_storage", [SameTypeOperands]> {
49314932
builtin function.
49324933
}];
49334934

4934-
let arguments = (ins VoidPtr:$ptr1, VoidPtr:$ptr2);
4935+
let arguments = (ins CIR_VoidPtrType:$ptr1, CIR_VoidPtrType:$ptr2);
49354936

49364937
let assemblyFormat = [{
49374938
$ptr1 `,` $ptr2 `:` qualified(type($ptr1)) attr-dict
@@ -5079,7 +5080,7 @@ def FreeExceptionOp : CIR_Op<"free.exception"> {
50795080
```
50805081
}];
50815082

5082-
let arguments = (ins VoidPtr:$ptr);
5083+
let arguments = (ins CIR_VoidPtrType:$ptr);
50835084
let results = (outs);
50845085

50855086
let assemblyFormat = [{
@@ -5325,11 +5326,11 @@ def PrefetchOp : CIR_Op<"prefetch"> {
53255326
If $isWrite doesn't specified it means that prefetch is prepared for 'read'.
53265327
}];
53275328

5328-
let arguments = (
5329-
ins VoidPtr:$addr,
5330-
ConfinedAttr<I32Attr, [IntMinValue<0>,
5331-
IntMaxValue<3>]>:$locality,
5332-
UnitAttr:$isWrite);
5329+
let arguments = (ins
5330+
CIR_VoidPtrType:$addr,
5331+
ConfinedAttr<I32Attr, [IntMinValue<0>, IntMaxValue<3>]>:$locality,
5332+
UnitAttr:$isWrite
5333+
);
53335334

53345335
let assemblyFormat = [{
53355336
`(` $addr `:` qualified(type($addr)) `)`
@@ -5349,7 +5350,7 @@ def ClearCacheOp : CIR_Op<"clear_cache", [AllTypesMatch<["begin", "end"]>]> {
53495350
CIR representation for `__builtin___clear_cache`.
53505351
}];
53515352

5352-
let arguments = (ins VoidPtr:$begin, VoidPtr:$end);
5353+
let arguments = (ins CIR_VoidPtrType:$begin, CIR_VoidPtrType:$end);
53535354
let assemblyFormat = [{
53545355
$begin `:` qualified(type($begin)) `,`
53555356
$end `,`

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,37 @@ def CIR_AnyIntOrFloatType : AnyTypeOf<[CIR_AnyFloatType, CIR_AnyIntType],
147147

148148
def CIR_AnyComplexType : CIR_TypeBase<"::cir::ComplexType", "complex type">;
149149

150+
//===----------------------------------------------------------------------===//
151+
// Pointer Type predicates
152+
//===----------------------------------------------------------------------===//
153+
154+
def CIR_AnyPtrType : CIR_TypeBase<"::cir::PointerType", "pointer type">;
155+
156+
// Pointer to type constraint bases
157+
class CIR_IsPtrToPred<code type> : CPred<"$_self.isPtrTo<" # type # ">()">;
158+
159+
class CIR_PtrTo<code type, string summary>
160+
: CIR_ConfinedType<CIR_AnyPtrType, [CIR_IsPtrToPred<type>],
161+
"pointer to " # summary>;
162+
163+
// Pointer to pointer constraint bases
164+
class CIR_IsPtrToPtrToPred<code type>
165+
: CPred<"$_self.isPtrToPtrTo<" # type # ">()">;
166+
167+
class CIR_PtrToPtrTo<code type, string summary>
168+
: CIR_ConfinedType<CIR_AnyPtrType, [CIR_IsPtrToPtrToPred<type>],
169+
"pointer to pointer to " # summary>;
170+
171+
// Void pointer type constraints
172+
def CIR_VoidPtrType
173+
: CIR_PtrTo<"::cir::VoidType", "void type">,
174+
BuildableType<"$_builder.getType<" # cppType # ">("
175+
"cir::VoidType::get($_builder.getContext()))">;
176+
177+
def CIR_PtrToVoidPtrType
178+
: CIR_PtrToPtrTo<"::cir::VoidType", "void type">,
179+
BuildableType<"$_builder.getType<" # cppType # ">("
180+
"$_builder.getType<" # cppType # ">("
181+
"cir::VoidType::get($_builder.getContext())))">;
182+
150183
#endif // CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,30 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr",
233233
let skipDefaultBuilders = 1;
234234

235235
let extraClassDeclaration = [{
236+
template <typename ...Types>
237+
bool isPtrTo() const {
238+
return mlir::isa< Types... >(getPointee());
239+
}
240+
236241
bool isVoidPtr() const {
237-
return mlir::isa<cir::VoidType>(getPointee());
242+
return isPtrTo<cir::VoidType>();
243+
}
244+
245+
template <typename ...Types>
246+
bool isPtrToPtrTo() const {
247+
if (auto ptrType = mlir::dyn_cast<cir::PointerType>(getPointee()))
248+
return ptrType.isPtrTo<Types...>();
249+
return false;
250+
}
251+
252+
bool isPtrTo(mlir::Type type) const {
253+
return getPointee() == type;
254+
}
255+
256+
bool isPtrToPtrTo(mlir::Type type) const {
257+
if (auto ptrType = mlir::dyn_cast<cir::PointerType>(getPointee()))
258+
return ptrType.isPtrTo(type);
259+
return false;
238260
}
239261
}];
240262
}
@@ -483,18 +505,6 @@ def CIR_VoidType : CIR_Type<"Void", "void"> {
483505

484506
// Constraints
485507

486-
// Pointer to void
487-
def VoidPtr : Type<
488-
And<[
489-
CPred<"::mlir::isa<::cir::PointerType>($_self)">,
490-
CPred<"::mlir::isa<::cir::VoidType>("
491-
"::mlir::cast<::cir::PointerType>($_self).getPointee())">,
492-
]>, "void*">,
493-
BuildableType<
494-
"cir::PointerType::get($_builder.getContext(),"
495-
"cir::VoidType::get($_builder.getContext()))"> {
496-
}
497-
498508
// Pointer to a primitive int, float or double
499509
def PrimitiveIntOrFPPtr : Type<
500510
And<[

clang/test/CIR/IR/invalid.cir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ module {
734734
module {
735735
// Should not memcpy non-void pointers.
736736
cir.func @invalid_memcpy_pointer_0(%arg0 : !cir.ptr<!s8i>, %arg1 : !u32i) {
737-
// expected-error@+1 {{'cir.libc.memcpy' op operand #0 must be void*, but got '!cir.ptr<!cir.int<s, 8>>'}}
737+
// expected-error@+1 {{'cir.libc.memcpy' op operand #0 must be pointer to void type, but got '!cir.ptr<!cir.int<s, 8>>'}}
738738
cir.libc.memcpy %arg1 bytes from %arg0 to %arg0 : !u32i, !cir.ptr<!s8i> -> !cir.ptr<!s8i>
739739
cir.return
740740
}
@@ -747,7 +747,7 @@ module {
747747
module {
748748
// Should not memcpy non-void pointers.
749749
cir.func @invalid_memcpy_pointer_1(%arg0 : !cir.ptr<!cir.void>, %arg1 : !cir.ptr<!s8i>, %arg2 : !u32i) {
750-
// expected-error@+1 {{'cir.libc.memcpy' op operand #1 must be void*, but got '!cir.ptr<!cir.int<s, 8>>'}}
750+
// expected-error@+1 {{'cir.libc.memcpy' op operand #1 must be pointer to void type, but got '!cir.ptr<!cir.int<s, 8>>'}}
751751
cir.libc.memcpy %arg2 bytes from %arg1 to %arg0 : !u32i, !cir.ptr<!s8i> -> !cir.ptr<!cir.void>
752752
cir.return
753753
}

0 commit comments

Comments
 (0)