Skip to content

Commit e78a7af

Browse files
committed
Move all verification code into TableGen
1 parent c6515fe commit e78a7af

File tree

5 files changed

+96
-148
lines changed

5 files changed

+96
-148
lines changed

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

+26-35
Original file line numberDiff line numberDiff line change
@@ -993,16 +993,17 @@ def CmpOp : CIR_Op<"cmp", [Pure, SameTypeOperands]> {
993993
// BitsOp
994994
//===----------------------------------------------------------------------===//
995995

996-
class CIR_BitOp<string mnemonic> : CIR_Op<mnemonic, [Pure]> {
997-
let arguments = (ins CIR_IntType:$input);
998-
let results = (outs CIR_IntType:$result);
996+
class CIR_BitOp<string mnemonic, TypeConstraint inputTy>
997+
: CIR_Op<mnemonic, [Pure]> {
998+
let arguments = (ins inputTy:$input);
999+
let results = (outs SInt32:$result);
9991000

10001001
let assemblyFormat = [{
10011002
`(` $input `:` type($input) `)` `:` type($result) attr-dict
10021003
}];
10031004
}
10041005

1005-
def BitClrsbOp : CIR_BitOp<"bit.clrsb"> {
1006+
def BitClrsbOp : CIR_BitOp<"bit.clrsb", SIntOfWidths<[32, 64]>> {
10061007
let summary = "Get the number of leading redundant sign bits in the input";
10071008
let description = [{
10081009
Compute the number of leading redundant sign bits in the input integer.
@@ -1012,6 +1013,8 @@ def BitClrsbOp : CIR_BitOp<"bit.clrsb"> {
10121013
number of redundant sign bits in the input, that is, the number of bits
10131014
following the most significant bit that are identical to it.
10141015

1016+
The bit width of the input integer must be either 32 or 64.
1017+
10151018
Examples:
10161019

10171020
```mlir
@@ -1029,17 +1032,15 @@ def BitClrsbOp : CIR_BitOp<"bit.clrsb"> {
10291032
%3 = cir.bit.clrsb(%2 : !s32i) : !s32i
10301033
```
10311034
}];
1032-
1033-
let hasVerifier = 1;
10341035
}
10351036

1036-
def BitCtzOp : CIR_BitOp<"bit.ctz"> {
1037-
let summary = "Get the number of trailing 0-bits in the input";
1037+
def BitClzOp : CIR_BitOp<"bit.clz", UIntOfWidths<[16, 32, 64]>> {
1038+
let summary = "Get the number of leading 0-bits in the input";
10381039
let description = [{
1039-
Compute the number of trailing 0-bits in the input.
1040+
Compute the number of leading 0-bits in the input.
10401041

1041-
The input integer must be an unsigned integer. The `cir.bit.ctz` operation
1042-
returns the number of consecutive 0-bits at the least significant bit
1042+
The input integer must be an unsigned integer. The `cir.bit.clz` operation
1043+
returns the number of consecutive 0-bits at the most significant bit
10431044
position in the input.
10441045

10451046
This operation invokes undefined behavior if the input value is 0.
@@ -1050,23 +1051,21 @@ def BitCtzOp : CIR_BitOp<"bit.ctz"> {
10501051
!s32i = !cir.int<s, 32>
10511052
!u32i = !cir.int<u, 32>
10521053

1053-
// %0 = 0b1000
1054+
// %0 = 0b0000_0000_0000_0000_0000_0000_0000_1000
10541055
%0 = cir.const(#cir.int<8> : !u32i) : !u32i
1055-
// %1 will be 3
1056-
%1 = cir.bit.ctz(%0 : !u32i) : !s32i
1056+
// %1 will be 28
1057+
%1 = cir.bit.clz(%0 : !u32i) : !s32i
10571058
```
10581059
}];
1059-
1060-
let hasVerifier = 1;
10611060
}
10621061

1063-
def BitClzOp : CIR_BitOp<"bit.clz"> {
1064-
let summary = "Get the number of leading 0-bits in the input";
1062+
def BitCtzOp : CIR_BitOp<"bit.ctz", UIntOfWidths<[16, 32, 64]>> {
1063+
let summary = "Get the number of trailing 0-bits in the input";
10651064
let description = [{
1066-
Compute the number of leading 0-bits in the input.
1065+
Compute the number of trailing 0-bits in the input.
10671066

1068-
The input integer must be an unsigned integer. The `cir.bit.clz` operation
1069-
returns the number of consecutive 0-bits at the most significant bit
1067+
The input integer must be an unsigned integer. The `cir.bit.ctz` operation
1068+
returns the number of consecutive 0-bits at the least significant bit
10701069
position in the input.
10711070

10721071
This operation invokes undefined behavior if the input value is 0.
@@ -1077,17 +1076,15 @@ def BitClzOp : CIR_BitOp<"bit.clz"> {
10771076
!s32i = !cir.int<s, 32>
10781077
!u32i = !cir.int<u, 32>
10791078

1080-
// %0 = 0b0000_0000_0000_0000_0000_0000_0000_1000
1079+
// %0 = 0b1000
10811080
%0 = cir.const(#cir.int<8> : !u32i) : !u32i
1082-
// %1 will be 28
1083-
%1 = cir.bit.clz(%0 : !u32i) : !s32i
1081+
// %1 will be 3
1082+
%1 = cir.bit.ctz(%0 : !u32i) : !s32i
10841083
```
10851084
}];
1086-
1087-
let hasVerifier = 1;
10881085
}
10891086

1090-
def BitFfsOp : CIR_BitOp<"bit.ffs"> {
1087+
def BitFfsOp : CIR_BitOp<"bit.ffs", SIntOfWidths<[32, 64]>> {
10911088
let summary = "Get the position of the least significant 1-bit of input";
10921089
let description = [{
10931090
Compute the position of the least significant 1-bit of the input.
@@ -1108,11 +1105,9 @@ def BitFfsOp : CIR_BitOp<"bit.ffs"> {
11081105
%1 = cir.bit.ffs(%0 : !s32i) : !s32i
11091106
```
11101107
}];
1111-
1112-
let hasVerifier = 1;
11131108
}
11141109

1115-
def BitParityOp : CIR_BitOp<"bit.parity"> {
1110+
def BitParityOp : CIR_BitOp<"bit.parity", UIntOfWidths<[32, 64]>> {
11161111
let summary = "Get the parity of input";
11171112
let description = [{
11181113
Compute the parity of the input. The parity of an integer is the number of
@@ -1132,11 +1127,9 @@ def BitParityOp : CIR_BitOp<"bit.parity"> {
11321127
%1 = cir.bit.parity(%0 : !u32i) : !s32i
11331128
```
11341129
}];
1135-
1136-
let hasVerifier = 1;
11371130
}
11381131

1139-
def BitPopcountOp : CIR_BitOp<"bit.popcount"> {
1132+
def BitPopcountOp : CIR_BitOp<"bit.popcount", UIntOfWidths<[16, 32, 64]>> {
11401133
let summary = "Get the number of 1-bits in input";
11411134
let description = [{
11421135
Compute the number of 1-bits in the input.
@@ -1155,8 +1148,6 @@ def BitPopcountOp : CIR_BitOp<"bit.popcount"> {
11551148
%1 = cir.bit.popcount(%0 : !u32i) : !s32i
11561149
```
11571150
}];
1158-
1159-
let hasVerifier = 1;
11601151
}
11611152

11621153
//===----------------------------------------------------------------------===//

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

+30
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,36 @@ def SInt16 : SInt<16>;
9494
def SInt32 : SInt<32>;
9595
def SInt64 : SInt<64>;
9696

97+
// A type constraint that allows unsigned integer type whose width is among the
98+
// specified list of possible widths.
99+
class UIntOfWidths<list<int> widths>
100+
: Type<And<[
101+
CPred<"$_self.isa<::mlir::cir::IntType>()">,
102+
CPred<"$_self.cast<::mlir::cir::IntType>().isUnsigned()">,
103+
Or<!foreach(
104+
w, widths,
105+
CPred<"$_self.cast<::mlir::cir::IntType>().getWidth() == " # w>
106+
)>
107+
]>,
108+
!interleave(!foreach(w, widths, w # "-bit"), " or ") # " uint",
109+
"::mlir::cir::IntType"
110+
> {}
111+
112+
// A type constraint that allows unsigned integer type whose width is among the
113+
// specified list of possible widths.
114+
class SIntOfWidths<list<int> widths>
115+
: Type<And<[
116+
CPred<"$_self.isa<::mlir::cir::IntType>()">,
117+
CPred<"$_self.cast<::mlir::cir::IntType>().isSigned()">,
118+
Or<!foreach(
119+
w, widths,
120+
CPred<"$_self.cast<::mlir::cir::IntType>().getWidth() == " # w>
121+
)>
122+
]>,
123+
!interleave(!foreach(w, widths, w # "-bit"), " or ") # " sint",
124+
"::mlir::cir::IntType"
125+
> {}
126+
97127
//===----------------------------------------------------------------------===//
98128
// PointerType
99129
//===----------------------------------------------------------------------===//

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

-55
Original file line numberDiff line numberDiff line change
@@ -843,61 +843,6 @@ Block *BrCondOp::getSuccessorForOperands(ArrayRef<Attribute> operands) {
843843
return nullptr;
844844
}
845845

846-
//===----------------------------------------------------------------------===//
847-
// CIR_BitOp family of operations
848-
//===----------------------------------------------------------------------===//
849-
850-
LogicalResult BitClrsbOp::verify() {
851-
if (!getInput().getType().isSigned())
852-
return emitOpError("input of cir.bit.clrsb must be a signed integer");
853-
if (!getType().isSigned() || getType().getWidth() != 32)
854-
return emitOpError(
855-
"result of cir.bit.clrsb must be a 32-bit signed integer");
856-
return success();
857-
}
858-
859-
LogicalResult BitClzOp::verify() {
860-
if (!getInput().getType().isUnsigned())
861-
return emitOpError("input of cir.bit.clz must be an unsigned integer");
862-
if (!getType().isSigned() || getType().getWidth() != 32)
863-
return emitOpError("result of cir.bit.clz must be a 32-bit signed integer");
864-
return success();
865-
}
866-
867-
LogicalResult BitCtzOp::verify() {
868-
if (!getInput().getType().isUnsigned())
869-
return emitOpError("input of cir.bit.ctz must be an unsigned integer");
870-
if (!getType().isSigned() || getType().getWidth() != 32)
871-
return emitOpError("result of cir.bit.ctz must be a 32-bit signed integer");
872-
return success();
873-
}
874-
875-
LogicalResult BitFfsOp::verify() {
876-
if (!getInput().getType().isSigned())
877-
return emitOpError("input of cir.bit.ffs must be a signed integer");
878-
if (!getType().isSigned() || getType().getWidth() != 32)
879-
return emitOpError("result of cir.bit.ffs must be a 32-bit signed integer");
880-
return success();
881-
}
882-
883-
LogicalResult BitParityOp::verify() {
884-
if (!getInput().getType().isUnsigned())
885-
return emitOpError("input of cir.bit.parity must be an unsigned integer");
886-
if (!getType().isSigned() || getType().getWidth() != 32)
887-
return emitOpError(
888-
"result of cir.bit.parity must be a 32-bit signed integer");
889-
return success();
890-
}
891-
892-
LogicalResult BitPopcountOp::verify() {
893-
if (!getInput().getType().isUnsigned())
894-
return emitOpError("input of cir.bit.popcount must be an unsigned integer");
895-
if (!getType().isSigned() || getType().getWidth() != 32)
896-
return emitOpError(
897-
"result of cir.bit.popcount must be a 32-bit signed integer");
898-
return success();
899-
}
900-
901846
//===----------------------------------------------------------------------===//
902847
// SwitchOp
903848
//===----------------------------------------------------------------------===//

clang/test/CIR/IR/bit.cir

+28-46
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,26 @@ module {
2020
%u32 = cir.const(#cir.int<1> : !u32i) : !u32i
2121
%u64 = cir.const(#cir.int<1> : !u64i) : !u64i
2222

23-
%0 = cir.bit.clrsb(%s8 : !s8i) : !s32i
24-
%1 = cir.bit.clrsb(%s16 : !s16i) : !s32i
2523
%2 = cir.bit.clrsb(%s32 : !s32i) : !s32i
2624
%3 = cir.bit.clrsb(%s64 : !s64i) : !s32i
2725

28-
%4 = cir.bit.clz(%u8 : !u8i) : !s32i
29-
%5 = cir.bit.clz(%u16 : !u16i) : !s32i
30-
%6 = cir.bit.clz(%u32 : !u32i) : !s32i
31-
%7 = cir.bit.clz(%u64 : !u64i) : !s32i
26+
%4 = cir.bit.clz(%u16 : !u16i) : !s32i
27+
%5 = cir.bit.clz(%u32 : !u32i) : !s32i
28+
%6 = cir.bit.clz(%u64 : !u64i) : !s32i
3229

33-
%8 = cir.bit.ctz(%u8 : !u8i) : !s32i
34-
%9 = cir.bit.ctz(%u16 : !u16i) : !s32i
35-
%10 = cir.bit.ctz(%u32 : !u32i) : !s32i
36-
%11 = cir.bit.ctz(%u64 : !u64i) : !s32i
30+
%7 = cir.bit.ctz(%u16 : !u16i) : !s32i
31+
%8 = cir.bit.ctz(%u32 : !u32i) : !s32i
32+
%9 = cir.bit.ctz(%u64 : !u64i) : !s32i
3733

38-
%12 = cir.bit.ffs(%s8 : !s8i) : !s32i
39-
%13 = cir.bit.ffs(%s16 : !s16i) : !s32i
40-
%14 = cir.bit.ffs(%s32 : !s32i) : !s32i
41-
%15 = cir.bit.ffs(%s64 : !s64i) : !s32i
34+
%10 = cir.bit.ffs(%s32 : !s32i) : !s32i
35+
%11 = cir.bit.ffs(%s64 : !s64i) : !s32i
4236

43-
%16 = cir.bit.parity(%u8 : !u8i) : !s32i
44-
%17 = cir.bit.parity(%u16 : !u16i) : !s32i
45-
%18 = cir.bit.parity(%u32 : !u32i) : !s32i
46-
%19 = cir.bit.parity(%u64 : !u64i) : !s32i
37+
%12 = cir.bit.parity(%u32 : !u32i) : !s32i
38+
%13 = cir.bit.parity(%u64 : !u64i) : !s32i
4739

48-
%20 = cir.bit.popcount(%u8 : !u8i) : !s32i
49-
%21 = cir.bit.popcount(%u16 : !u16i) : !s32i
50-
%22 = cir.bit.popcount(%u32 : !u32i) : !s32i
51-
%23 = cir.bit.popcount(%u64 : !u64i) : !s32i
40+
%14 = cir.bit.popcount(%u16 : !u16i) : !s32i
41+
%15 = cir.bit.popcount(%u32 : !u32i) : !s32i
42+
%16 = cir.bit.popcount(%u64 : !u64i) : !s32i
5243

5344
cir.return
5445
}
@@ -64,30 +55,21 @@ module {
6455
// CHECK-NEXT: %5 = cir.const(#cir.int<1> : !u16i) : !u16i
6556
// CHECK-NEXT: %6 = cir.const(#cir.int<1> : !u32i) : !u32i
6657
// CHECK-NEXT: %7 = cir.const(#cir.int<1> : !u64i) : !u64i
67-
// CHECK-NEXT: %8 = cir.bit.clrsb(%0 : !s8i) : !s32i
68-
// CHECK-NEXT: %9 = cir.bit.clrsb(%1 : !s16i) : !s32i
69-
// CHECK-NEXT: %10 = cir.bit.clrsb(%2 : !s32i) : !s32i
70-
// CHECK-NEXT: %11 = cir.bit.clrsb(%3 : !s64i) : !s32i
71-
// CHECK-NEXT: %12 = cir.bit.clz(%4 : !u8i) : !s32i
72-
// CHECK-NEXT: %13 = cir.bit.clz(%5 : !u16i) : !s32i
73-
// CHECK-NEXT: %14 = cir.bit.clz(%6 : !u32i) : !s32i
74-
// CHECK-NEXT: %15 = cir.bit.clz(%7 : !u64i) : !s32i
75-
// CHECK-NEXT: %16 = cir.bit.ctz(%4 : !u8i) : !s32i
76-
// CHECK-NEXT: %17 = cir.bit.ctz(%5 : !u16i) : !s32i
77-
// CHECK-NEXT: %18 = cir.bit.ctz(%6 : !u32i) : !s32i
78-
// CHECK-NEXT: %19 = cir.bit.ctz(%7 : !u64i) : !s32i
79-
// CHECK-NEXT: %20 = cir.bit.ffs(%0 : !s8i) : !s32i
80-
// CHECK-NEXT: %21 = cir.bit.ffs(%1 : !s16i) : !s32i
81-
// CHECK-NEXT: %22 = cir.bit.ffs(%2 : !s32i) : !s32i
82-
// CHECK-NEXT: %23 = cir.bit.ffs(%3 : !s64i) : !s32i
83-
// CHECK-NEXT: %24 = cir.bit.parity(%4 : !u8i) : !s32i
84-
// CHECK-NEXT: %25 = cir.bit.parity(%5 : !u16i) : !s32i
85-
// CHECK-NEXT: %26 = cir.bit.parity(%6 : !u32i) : !s32i
86-
// CHECK-NEXT: %27 = cir.bit.parity(%7 : !u64i) : !s32i
87-
// CHECK-NEXT: %28 = cir.bit.popcount(%4 : !u8i) : !s32i
88-
// CHECK-NEXT: %29 = cir.bit.popcount(%5 : !u16i) : !s32i
89-
// CHECK-NEXT: %30 = cir.bit.popcount(%6 : !u32i) : !s32i
90-
// CHECK-NEXT: %31 = cir.bit.popcount(%7 : !u64i) : !s32i
58+
// CHECK-NEXT: %8 = cir.bit.clrsb(%2 : !s32i) : !s32i
59+
// CHECK-NEXT: %9 = cir.bit.clrsb(%3 : !s64i) : !s32i
60+
// CHECK-NEXT: %10 = cir.bit.clz(%5 : !u16i) : !s32i
61+
// CHECK-NEXT: %11 = cir.bit.clz(%6 : !u32i) : !s32i
62+
// CHECK-NEXT: %12 = cir.bit.clz(%7 : !u64i) : !s32i
63+
// CHECK-NEXT: %13 = cir.bit.ctz(%5 : !u16i) : !s32i
64+
// CHECK-NEXT: %14 = cir.bit.ctz(%6 : !u32i) : !s32i
65+
// CHECK-NEXT: %15 = cir.bit.ctz(%7 : !u64i) : !s32i
66+
// CHECK-NEXT: %16 = cir.bit.ffs(%2 : !s32i) : !s32i
67+
// CHECK-NEXT: %17 = cir.bit.ffs(%3 : !s64i) : !s32i
68+
// CHECK-NEXT: %18 = cir.bit.parity(%6 : !u32i) : !s32i
69+
// CHECK-NEXT: %19 = cir.bit.parity(%7 : !u64i) : !s32i
70+
// CHECK-NEXT: %20 = cir.bit.popcount(%5 : !u16i) : !s32i
71+
// CHECK-NEXT: %21 = cir.bit.popcount(%6 : !u32i) : !s32i
72+
// CHECK-NEXT: %22 = cir.bit.popcount(%7 : !u64i) : !s32i
9173
// CHECK-NEXT: cir.return
9274
// CHECK-NEXT: }
9375
// CHECK-NEXT: }

0 commit comments

Comments
 (0)