Skip to content

Commit 5b17612

Browse files
committed
[CIR] Refactor IntType constraints
1 parent c5f0f97 commit 5b17612

File tree

3 files changed

+116
-82
lines changed

3 files changed

+116
-82
lines changed

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

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
include "clang/CIR/Dialect/IR/CIRDialect.td"
1818
include "clang/CIR/Dialect/IR/CIRTypes.td"
19+
include "clang/CIR/Dialect/IR/CIRTypeConstraints.td"
1920
include "clang/CIR/Dialect/IR/CIRAttrs.td"
2021

2122
include "clang/CIR/Interfaces/ASTAttrInterfaces.td"
@@ -297,7 +298,7 @@ def ObjSizeOp : CIR_Op<"objsize", [Pure]> {
297298

298299
let arguments = (ins CIR_PointerType:$ptr, SizeInfoType:$kind,
299300
UnitAttr:$dynamic);
300-
let results = (outs PrimitiveInt:$result);
301+
let results = (outs CIR_IntType:$result);
301302

302303
let assemblyFormat = [{
303304
`(`
@@ -333,7 +334,7 @@ def PtrDiffOp : CIR_Op<"ptr_diff", [Pure, SameTypeOperands]> {
333334
```
334335
}];
335336

336-
let results = (outs PrimitiveInt:$result);
337+
let results = (outs CIR_IntType:$result);
337338
let arguments = (ins CIR_PointerType:$lhs, CIR_PointerType:$rhs);
338339

339340
let assemblyFormat = [{
@@ -361,7 +362,7 @@ def PtrStrideOp : CIR_Op<"ptr_stride",
361362
```
362363
}];
363364

364-
let arguments = (ins CIR_PointerType:$base, PrimitiveInt:$stride);
365+
let arguments = (ins CIR_PointerType:$base, CIR_IntType:$stride);
365366
let results = (outs CIR_PointerType:$result);
366367

367368
let assemblyFormat = [{
@@ -492,7 +493,7 @@ def AllocaOp : CIR_Op<"alloca", [
492493
}];
493494

494495
let arguments = (ins
495-
Optional<PrimitiveInt>:$dynAllocSize,
496+
Optional<CIR_IntType>:$dynAllocSize,
496497
TypeAttr:$allocaType,
497498
StrAttr:$name,
498499
UnitAttr:$init,
@@ -1033,7 +1034,7 @@ def ResumeOp : CIR_Op<"resume", [ReturnLike, Terminator,
10331034
}];
10341035

10351036
let arguments = (ins Optional<VoidPtr>:$exception_ptr,
1036-
Optional<UInt32>:$type_id,
1037+
Optional<CIR_UInt32>:$type_id,
10371038
UnitAttr:$rethrow);
10381039
let assemblyFormat = [{
10391040
($rethrow^)?
@@ -1585,7 +1586,7 @@ class CIR_CountZerosBitOp<string mnemonic, TypeConstraint inputTy>
15851586
}];
15861587
}
15871588

1588-
def BitClrsbOp : CIR_BitOp<"bit.clrsb", AnyTypeOf<[SInt32, SInt64]>> {
1589+
def BitClrsbOp : CIR_BitOp<"bit.clrsb", CIR_SIntOfWidths<[32, 64]>> {
15891590
let summary = "Get the number of leading redundant sign bits in the input";
15901591
let description = [{
15911592
Compute the number of leading redundant sign bits in the input integer.
@@ -1616,7 +1617,7 @@ def BitClrsbOp : CIR_BitOp<"bit.clrsb", AnyTypeOf<[SInt32, SInt64]>> {
16161617
}];
16171618
}
16181619

1619-
def BitClzOp : CIR_CountZerosBitOp<"bit.clz", AnyTypeOf<[UInt16, UInt32, UInt64]>> {
1620+
def BitClzOp : CIR_CountZerosBitOp<"bit.clz", CIR_UIntOfWidths<[16, 32, 64]>> {
16201621
let summary = "Get the number of leading 0-bits in the input";
16211622
let description = [{
16221623
Compute the number of leading 0-bits in the input.
@@ -1641,7 +1642,7 @@ def BitClzOp : CIR_CountZerosBitOp<"bit.clz", AnyTypeOf<[UInt16, UInt32, UInt64]
16411642
}];
16421643
}
16431644

1644-
def BitCtzOp : CIR_CountZerosBitOp<"bit.ctz", AnyTypeOf<[UInt16, UInt32, UInt64]>> {
1645+
def BitCtzOp : CIR_CountZerosBitOp<"bit.ctz", CIR_UIntOfWidths<[16, 32, 64]>> {
16451646
let summary = "Get the number of trailing 0-bits in the input";
16461647
let description = [{
16471648
Compute the number of trailing 0-bits in the input.
@@ -1667,7 +1668,7 @@ def BitCtzOp : CIR_CountZerosBitOp<"bit.ctz", AnyTypeOf<[UInt16, UInt32, UInt64]
16671668
}];
16681669
}
16691670

1670-
def BitFfsOp : CIR_BitOp<"bit.ffs", AnyTypeOf<[SInt32, SInt64]>> {
1671+
def BitFfsOp : CIR_BitOp<"bit.ffs", CIR_SIntOfWidths<[32, 64]>> {
16711672
let summary = "Get the position of the least significant 1-bit of input";
16721673
let description = [{
16731674
Compute the position of the least significant 1-bit of the input.
@@ -1690,7 +1691,7 @@ def BitFfsOp : CIR_BitOp<"bit.ffs", AnyTypeOf<[SInt32, SInt64]>> {
16901691
}];
16911692
}
16921693

1693-
def BitParityOp : CIR_BitOp<"bit.parity", AnyTypeOf<[UInt32, UInt64]>> {
1694+
def BitParityOp : CIR_BitOp<"bit.parity", CIR_UIntOfWidths<[32, 64]>> {
16941695
let summary = "Get the parity of input";
16951696
let description = [{
16961697
Compute the parity of the input. The parity of an integer is the number of
@@ -1713,7 +1714,7 @@ def BitParityOp : CIR_BitOp<"bit.parity", AnyTypeOf<[UInt32, UInt64]>> {
17131714
}
17141715

17151716
def BitPopcountOp
1716-
: CIR_BitOp<"bit.popcount", AnyTypeOf<[UInt16, UInt32, UInt64]>> {
1717+
: CIR_BitOp<"bit.popcount", CIR_UIntOfWidths<[16, 32, 64]>> {
17171718
let summary = "Get the number of 1-bits in input";
17181719
let description = [{
17191720
Compute the number of 1-bits in the input.
@@ -1760,7 +1761,7 @@ def ByteswapOp : CIR_Op<"bswap", [Pure, SameOperandsAndResultType]> {
17601761
}];
17611762

17621763
let results = (outs CIR_IntType:$result);
1763-
let arguments = (ins AnyTypeOf<[UInt16, UInt32, UInt64]>:$input);
1764+
let arguments = (ins CIR_UIntOfWidths<[16, 32, 64]>:$input);
17641765

17651766
let assemblyFormat = [{
17661767
`(` $input `:` type($input) `)` `:` type($result) attr-dict
@@ -1792,7 +1793,7 @@ def RotateOp : CIR_Op<"rotate", [Pure, SameOperandsAndResultType]> {
17921793
}];
17931794

17941795
let results = (outs CIR_IntType:$result);
1795-
let arguments = (ins PrimitiveInt:$src, PrimitiveInt:$amt,
1796+
let arguments = (ins CIR_IntType:$src, CIR_IntType:$amt,
17961797
UnitAttr:$left);
17971798

17981799
let assemblyFormat = [{
@@ -1821,8 +1822,8 @@ def BitReverseOp : CIR_Op<"bit_reverse", [Pure, SameOperandsAndResultType]> {
18211822
```
18221823
}];
18231824

1824-
let arguments = (ins AnyTypeOf<[UInt8, UInt16, UInt32, UInt64]>:$src);
1825-
let results = (outs AnyTypeOf<[UInt8, UInt16, UInt32, UInt64]>:$result);
1825+
let arguments = (ins CIR_UIntOfWidths<[8, 16, 32, 64]>:$src);
1826+
let results = (outs CIR_UIntOfWidths<[8, 16, 32, 64]>:$result);
18261827

18271828
let assemblyFormat = [{
18281829
$src `:` type($result) attr-dict
@@ -1868,7 +1869,7 @@ def CmpThreeWayOp : CIR_Op<"cmp3way", [Pure, SameTypeOperands]> {
18681869
```
18691870
}];
18701871

1871-
let results = (outs PrimitiveSInt:$result);
1872+
let results = (outs CIR_SIntType:$result);
18721873
let arguments = (ins CIR_AnyType:$lhs, CIR_AnyType:$rhs,
18731874
CmpThreeWayInfoAttr:$info);
18741875

@@ -3134,7 +3135,7 @@ def VecInsertOp : CIR_Op<"vec.insert", [Pure,
31343135
element is returned.
31353136
}];
31363137

3137-
let arguments = (ins CIR_VectorType:$vec, AnyType:$value, PrimitiveInt:$index);
3138+
let arguments = (ins CIR_VectorType:$vec, AnyType:$value, CIR_IntType:$index);
31383139
let results = (outs CIR_VectorType:$result);
31393140

31403141
let assemblyFormat = [{
@@ -3161,7 +3162,7 @@ def VecExtractOp : CIR_Op<"vec.extract", [Pure,
31613162
from a vector object.
31623163
}];
31633164

3164-
let arguments = (ins CIR_VectorType:$vec, PrimitiveInt:$index);
3165+
let arguments = (ins CIR_VectorType:$vec, CIR_IntType:$index);
31653166
let results = (outs CIR_AnyType:$result);
31663167

31673168
let assemblyFormat = [{
@@ -4367,7 +4368,7 @@ def EhInflightOp : CIR_Op<"eh.inflight_exception"> {
43674368

43684369
let arguments = (ins UnitAttr:$cleanup,
43694370
OptionalAttr<FlatSymbolRefArrayAttr>:$sym_type_list);
4370-
let results = (outs VoidPtr:$exception_ptr, UInt32:$type_id);
4371+
let results = (outs VoidPtr:$exception_ptr, CIR_UInt32:$type_id);
43714372
let assemblyFormat = [{
43724373
(`cleanup` $cleanup^)?
43734374
($sym_type_list^)?
@@ -4386,7 +4387,7 @@ def EhTypeIdOp : CIR_Op<"eh.typeid",
43864387
}];
43874388

43884389
let arguments = (ins FlatSymbolRefAttr:$type_sym);
4389-
let results = (outs UInt32:$type_id);
4390+
let results = (outs CIR_UInt32:$type_id);
43904391
let assemblyFormat = [{
43914392
$type_sym attr-dict
43924393
}];
@@ -4469,7 +4470,7 @@ def MemCpyOp : CIR_MemOp<"libc.memcpy"> {
44694470
```
44704471
}];
44714472

4472-
let arguments = !con(commonArgs, (ins PrimitiveUInt:$len));
4473+
let arguments = !con(commonArgs, (ins CIR_UIntType:$len));
44734474

44744475
let assemblyFormat = [{
44754476
$len `bytes` `from` $src `to` $dst attr-dict
@@ -4499,7 +4500,7 @@ def MemMoveOp : CIR_MemOp<"libc.memmove"> {
44994500
```
45004501
}];
45014502

4502-
let arguments = !con(commonArgs, (ins PrimitiveUInt:$len));
4503+
let arguments = !con(commonArgs, (ins CIR_UIntType:$len));
45034504

45044505
let assemblyFormat = [{
45054506
$len `bytes` `from` $src `to` $dst attr-dict
@@ -4548,8 +4549,8 @@ def MemCpyInlineOp : CIR_MemOp<"memcpy_inline"> {
45484549

45494550
def MemSetOp : CIR_Op<"libc.memset"> {
45504551
let arguments = (ins Arg<VoidPtr, "", [MemWrite]>:$dst,
4551-
SInt32:$val,
4552-
PrimitiveUInt:$len);
4552+
CIR_SInt32:$val,
4553+
CIR_UIntType:$len);
45534554
let summary = "Equivalent to libc's `memset`";
45544555
let description = [{
45554556
Given the CIR pointer, `dst`, `cir.libc.memset` will set the first `len`
@@ -4579,7 +4580,7 @@ def MemSetOp : CIR_Op<"libc.memset"> {
45794580

45804581
def MemSetInlineOp : CIR_Op<"memset_inline"> {
45814582
let arguments = (ins Arg<VoidPtr, "", [MemWrite]>:$dst,
4582-
SInt32:$val,
4583+
CIR_SInt32:$val,
45834584
I64Attr:$len);
45844585
let summary = "Fill a block of memory with constant length without calling"
45854586
"any external function";
@@ -4612,8 +4613,8 @@ def MemChrOp : CIR_Op<"libc.memchr"> {
46124613
// TODO: instead of using UInt64 for len, we could make it constrained on
46134614
// size_t (64 or 32) and have a builder that does the right job.
46144615
let arguments = (ins Arg<VoidPtr, "", [MemRead]>:$src,
4615-
SInt32:$pattern,
4616-
UInt64:$len);
4616+
CIR_SInt32:$pattern,
4617+
CIR_UInt64:$len);
46174618
let summary = "libc's `memchr`";
46184619
let results = (outs Res<VoidPtr, "">:$result);
46194620

@@ -4641,7 +4642,7 @@ def MemChrOp : CIR_Op<"libc.memchr"> {
46414642
//===----------------------------------------------------------------------===//
46424643

46434644
class FuncAddrBuiltinOp<string mnemonic> : CIR_Op<mnemonic, []> {
4644-
let arguments = (ins UInt32:$level);
4645+
let arguments = (ins CIR_UInt32:$level);
46454646
let results = (outs Res<VoidPtr, "">:$result);
46464647
let assemblyFormat = [{
46474648
`(` $level `)` attr-dict
@@ -4951,10 +4952,10 @@ def ExpectOp : CIR_Op<"expect",
49514952
where probability = $prob.
49524953
}];
49534954

4954-
let arguments = (ins PrimitiveInt:$val,
4955-
PrimitiveInt:$expected,
4955+
let arguments = (ins CIR_IntType:$val,
4956+
CIR_IntType:$expected,
49564957
OptionalAttr<F64Attr>:$prob);
4957-
let results = (outs PrimitiveInt:$result);
4958+
let results = (outs CIR_IntType:$result);
49584959
let assemblyFormat = [{
49594960
`(` $val`,` $expected (`,` $prob^)? `)` `:` type($val) attr-dict
49604961
}];
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines the CIR dialect type constraints.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD
14+
#define CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD
15+
16+
include "mlir/IR/Constraints.td"
17+
18+
class CIR_IsTypePred<code type> : CPred<"::mlir::isa<" # type # ">($_self)">;
19+
20+
class CIR_TypeBase<string type, string summary = "">
21+
: Type<CIR_IsTypePred<type>, summary, type>;
22+
23+
class CIR_CastSelfToType<string type, Pred pred>
24+
: SubstLeaves<"$_self", "::mlir::cast<" # type # ">($_self)", pred>;
25+
26+
class CIR_CastedSelfsToType<string type, list<Pred> preds>
27+
: And<!foreach(pred, preds, CIR_CastSelfToType<type, pred>)>;
28+
29+
class CIR_ConfinedType<Type type, list<Pred> preds, string summary = "">
30+
: Type<And<[type.predicate, CIR_CastedSelfsToType<type.cppType, preds>]>,
31+
summary, type.cppType>;
32+
33+
//===----------------------------------------------------------------------===//
34+
// IntType predicates
35+
//===----------------------------------------------------------------------===//
36+
37+
def CIR_AnyIntType : CIR_TypeBase<"::cir::IntType", "any integer type">;
38+
39+
def CIR_UIntType : CIR_ConfinedType<CIR_AnyIntType, [
40+
CPred<"$_self.isUnsigned()">], "Unsigned integer type">;
41+
42+
def CIR_SIntType : CIR_ConfinedType<CIR_AnyIntType, [
43+
CPred<"$_self.isSigned()">], "signed integer type">;
44+
45+
class CIR_HasWidthPred<int width> : CPred<"$_self.getWidth() == " # width>;
46+
47+
class CIR_IntOfWidthsPred<list<int> widths>
48+
: Or<!foreach(width, widths, CIR_HasWidthPred<width>)>;
49+
50+
class CIR_SIntOfWidths<list<int> widths>
51+
: CIR_ConfinedType<CIR_SIntType, [CIR_IntOfWidthsPred<widths>],
52+
"signed integer type of widths" # !interleave(widths, "/" )>;
53+
54+
class CIR_UIntOfWidths<list<int> widths>
55+
: CIR_ConfinedType<CIR_UIntType, [CIR_IntOfWidthsPred<widths>],
56+
"unsigned integer type of widths" # !interleave(widths, "/" )>;
57+
58+
class CIR_UInt<int width>
59+
: CIR_ConfinedType<CIR_UIntType, [CIR_HasWidthPred<width>],
60+
width # "-bit unsigned integer">,
61+
BuildableType<"$_builder.getType<" # cppType # ">(" #
62+
width # ", /*isSigned=*/false)">;
63+
64+
def CIR_UInt1 : CIR_UInt<1>;
65+
def CIR_UInt8 : CIR_UInt<8>;
66+
def CIR_UInt16 : CIR_UInt<16>;
67+
def CIR_UInt32 : CIR_UInt<32>;
68+
def CIR_UInt64 : CIR_UInt<64>;
69+
70+
class CIR_SInt<int width>
71+
: CIR_ConfinedType<CIR_SIntType, [CIR_HasWidthPred<width>],
72+
width # "-bit signed integer">,
73+
BuildableType<"$_builder.getType<" # cppType # ">(" #
74+
width # ", /*isSigned=*/true)">;
75+
76+
def CIR_SInt1 : CIR_SInt<1>;
77+
def CIR_SInt8 : CIR_SInt<8>;
78+
def CIR_SInt16 : CIR_SInt<16>;
79+
def CIR_SInt32 : CIR_SInt<32>;
80+
def CIR_SInt64 : CIR_SInt<64>;
81+
82+
#endif // CLANG_CIR_DIALECT_IR_CIRTYPECONSTRAINTS_TD

0 commit comments

Comments
 (0)