Skip to content

Commit a57ebc1

Browse files
authored
[CIR][NFC] Upstream VectorType support in helper function (#142222)
This change upstream supports VectorType in the helper function and adds a test for the Vector type of FP binary operations Issue #136487
1 parent b88dfb0 commit a57ebc1

File tree

7 files changed

+95
-41
lines changed

7 files changed

+95
-41
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,9 +1464,13 @@ def ShiftOp : CIR_Op<"shift", [Pure]> {
14641464
```
14651465
}];
14661466

1467-
let results = (outs CIR_AnyIntOrVecOfInt:$result);
1468-
let arguments = (ins CIR_AnyIntOrVecOfInt:$value, CIR_AnyIntOrVecOfInt:$amount,
1469-
UnitAttr:$isShiftleft);
1467+
let arguments = (ins
1468+
CIR_AnyIntOrVecOfIntType:$value,
1469+
CIR_AnyIntOrVecOfIntType:$amount,
1470+
UnitAttr:$isShiftleft
1471+
);
1472+
1473+
let results = (outs CIR_AnyIntOrVecOfIntType:$result);
14701474

14711475
let assemblyFormat = [{
14721476
`(`
@@ -2050,7 +2054,7 @@ def VecCreateOp : CIR_Op<"vec.create", [Pure]> {
20502054
in the vector type.
20512055
}];
20522056

2053-
let arguments = (ins Variadic<CIR_AnyType>:$elements);
2057+
let arguments = (ins Variadic<CIR_VectorElementType>:$elements);
20542058
let results = (outs CIR_VectorType:$result);
20552059

20562060
let assemblyFormat = [{
@@ -2085,7 +2089,7 @@ def VecInsertOp : CIR_Op<"vec.insert", [Pure,
20852089

20862090
let arguments = (ins
20872091
CIR_VectorType:$vec,
2088-
AnyType:$value,
2092+
CIR_VectorElementType:$value,
20892093
CIR_AnyFundamentalIntType:$index
20902094
);
20912095

@@ -2118,7 +2122,7 @@ def VecExtractOp : CIR_Op<"vec.extract", [Pure,
21182122
}];
21192123

21202124
let arguments = (ins CIR_VectorType:$vec, CIR_AnyFundamentalIntType:$index);
2121-
let results = (outs CIR_AnyType:$result);
2125+
let results = (outs CIR_VectorElementType:$result);
21222126

21232127
let assemblyFormat = [{
21242128
$vec `[` $index `:` type($index) `]` attr-dict `:` qualified(type($vec))
@@ -2180,7 +2184,7 @@ def VecShuffleDynamicOp : CIR_Op<"vec.shuffle.dynamic",
21802184
```
21812185
}];
21822186

2183-
let arguments = (ins CIR_VectorType:$vec, IntegerVector:$indices);
2187+
let arguments = (ins CIR_VectorType:$vec, CIR_VectorOfIntType:$indices);
21842188
let results = (outs CIR_VectorType:$result);
21852189
let assemblyFormat = [{
21862190
$vec `:` qualified(type($vec)) `,` $indices `:` qualified(type($indices))

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

Lines changed: 60 additions & 2 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
// Bool Type predicates
3648
//===----------------------------------------------------------------------===//
@@ -184,6 +196,24 @@ def CIR_PtrToVoidPtrType
184196
// Vector Type predicates
185197
//===----------------------------------------------------------------------===//
186198

199+
def CIR_AnyVectorType : CIR_TypeBase<"::cir::VectorType", "vector type">;
200+
201+
def CIR_VectorElementType : AnyTypeOf<[CIR_AnyIntOrFloatType, CIR_AnyPtrType],
202+
"any cir integer, floating point or pointer type"
203+
> {
204+
let cppFunctionName = "isValidVectorTypeElementType";
205+
}
206+
207+
class CIR_ElementTypePred<Pred pred> : SubstLeaves<"$_self",
208+
"::mlir::cast<::cir::VectorType>($_self).getElementType()", pred>;
209+
210+
class CIR_VectorTypeOf<list<Type> types, string summary = "">
211+
: CIR_ConfinedType<CIR_AnyVectorType,
212+
[Or<!foreach(type, types, CIR_ElementTypePred<type.predicate>)>],
213+
!if(!empty(summary),
214+
"vector of " # CIR_TypeSummaries<types>.value,
215+
summary)>;
216+
187217
// Vector of integral type
188218
def IntegerVector : Type<
189219
And<[
@@ -196,8 +226,36 @@ def IntegerVector : Type<
196226
]>, "!cir.vector of !cir.int"> {
197227
}
198228

199-
// Any Integer or Vector of Integer Constraints
200-
def CIR_AnyIntOrVecOfInt: AnyTypeOf<[CIR_AnyIntType, IntegerVector]>;
229+
// Vector of type constraints
230+
def CIR_VectorOfIntType : CIR_VectorTypeOf<[CIR_AnyIntType]>;
231+
def CIR_VectorOfUIntType : CIR_VectorTypeOf<[CIR_AnyUIntType]>;
232+
def CIR_VectorOfSIntType : CIR_VectorTypeOf<[CIR_AnySIntType]>;
233+
def CIR_VectorOfFloatType : CIR_VectorTypeOf<[CIR_AnyFloatType]>;
234+
235+
// Vector or Scalar type constraints
236+
def CIR_AnyIntOrVecOfIntType
237+
: AnyTypeOf<[CIR_AnyIntType, CIR_VectorOfIntType],
238+
"integer or vector of integer type"> {
239+
let cppFunctionName = "isIntOrVectorOfIntType";
240+
}
241+
242+
def CIR_AnySIntOrVecOfSIntType
243+
: AnyTypeOf<[CIR_AnySIntType, CIR_VectorOfSIntType],
244+
"signed integer or vector of signed integer type"> {
245+
let cppFunctionName = "isSIntOrVectorOfSIntType";
246+
}
247+
248+
def CIR_AnyUIntOrVecOfUIntType
249+
: AnyTypeOf<[CIR_AnyUIntType, CIR_VectorOfUIntType],
250+
"unsigned integer or vector of unsigned integer type"> {
251+
let cppFunctionName = "isUIntOrVectorOfUIntType";
252+
}
253+
254+
def CIR_AnyFloatOrVecOfFloatType
255+
: AnyTypeOf<[CIR_AnyFloatType, CIR_VectorOfFloatType],
256+
"floating point or vector of floating point type"> {
257+
let cppFunctionName = "isFPOrVectorOfFPType";
258+
}
201259

202260
//===----------------------------------------------------------------------===//
203261
// Scalar Type predicates

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ struct RecordTypeStorage;
2626

2727
bool isValidFundamentalIntWidth(unsigned width);
2828

29-
bool isFPOrFPVectorTy(mlir::Type);
30-
3129
} // namespace cir
3230

3331
//===----------------------------------------------------------------------===//

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,31 @@ def CIR_VectorType : CIR_Type<"Vector", "vector",
275275

276276
let summary = "CIR vector type";
277277
let description = [{
278-
`!cir.vector' represents fixed-size vector types, parameterized
279-
by the element type and the number of elements.
278+
The `!cir.vector` type represents a fixed-size, one-dimensional vector.
279+
It takes two parameters: the element type and the number of elements.
280280

281-
Example:
281+
Syntax:
282282

283283
```mlir
284-
!cir.vector<!u64i x 2>
285-
!cir.vector<!cir.float x 4>
284+
vector-type ::= !cir.vector<size x element-type>
285+
element-type ::= float-type | integer-type | pointer-type
286+
```
287+
288+
The `element-type` must be a scalar CIR type. Zero-sized vectors are not
289+
allowed. The `size` must be a positive integer.
290+
291+
Examples:
292+
293+
```mlir
294+
!cir.vector<4 x !cir.int<u, 8>>
295+
!cir.vector<2 x !cir.float>
286296
```
287297
}];
288298

289-
let parameters = (ins "mlir::Type":$elementType, "uint64_t":$size);
299+
let parameters = (ins
300+
CIR_VectorElementType:$elementType,
301+
"uint64_t":$size
302+
);
290303

291304
let assemblyFormat = [{
292305
`<` $size `x` $elementType `>`

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ mlir::Value ScalarExprEmitter::emitMul(const BinOpInfo &ops) {
13111311
!canElideOverflowCheck(cgf.getContext(), ops))
13121312
cgf.cgm.errorNYI("unsigned int overflow sanitizer");
13131313

1314-
if (cir::isFPOrFPVectorTy(ops.lhs.getType())) {
1314+
if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
13151315
assert(!cir::MissingFeatures::cgFPOptionsRAII());
13161316
return builder.createFMul(loc, ops.lhs, ops.rhs);
13171317
}
@@ -1370,7 +1370,7 @@ mlir::Value ScalarExprEmitter::emitAdd(const BinOpInfo &ops) {
13701370
!canElideOverflowCheck(cgf.getContext(), ops))
13711371
cgf.cgm.errorNYI("unsigned int overflow sanitizer");
13721372

1373-
if (cir::isFPOrFPVectorTy(ops.lhs.getType())) {
1373+
if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
13741374
assert(!cir::MissingFeatures::cgFPOptionsRAII());
13751375
return builder.createFAdd(loc, ops.lhs, ops.rhs);
13761376
}
@@ -1418,7 +1418,7 @@ mlir::Value ScalarExprEmitter::emitSub(const BinOpInfo &ops) {
14181418
!canElideOverflowCheck(cgf.getContext(), ops))
14191419
cgf.cgm.errorNYI("unsigned int overflow sanitizer");
14201420

1421-
if (cir::isFPOrFPVectorTy(ops.lhs.getType())) {
1421+
if (cir::isFPOrVectorOfFPType(ops.lhs.getType())) {
14221422
assert(!cir::MissingFeatures::cgFPOptionsRAII());
14231423
return builder.createFSub(loc, ops.lhs, ops.rhs);
14241424
}

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -552,15 +552,6 @@ LongDoubleType::getABIAlignment(const mlir::DataLayout &dataLayout,
552552
.getABIAlignment(dataLayout, params);
553553
}
554554

555-
//===----------------------------------------------------------------------===//
556-
// Floating-point and Float-point Vector type helpers
557-
//===----------------------------------------------------------------------===//
558-
559-
bool cir::isFPOrFPVectorTy(mlir::Type t) {
560-
assert(!cir::MissingFeatures::vectorType());
561-
return isAnyFloatingPointType(t);
562-
}
563-
564555
//===----------------------------------------------------------------------===//
565556
// FuncType Definitions
566557
//===----------------------------------------------------------------------===//
@@ -693,17 +684,7 @@ mlir::LogicalResult cir::VectorType::verify(
693684
mlir::Type elementType, uint64_t size) {
694685
if (size == 0)
695686
return emitError() << "the number of vector elements must be non-zero";
696-
697-
// Check if it a valid FixedVectorType
698-
if (mlir::isa<cir::PointerType, cir::FP128Type>(elementType))
699-
return success();
700-
701-
// Check if it a valid VectorType
702-
if (mlir::isa<cir::IntType>(elementType) ||
703-
isAnyFloatingPointType(elementType))
704-
return success();
705-
706-
return emitError() << "unsupported element type for CIR vector";
687+
return success();
707688
}
708689

709690
//===----------------------------------------------------------------------===//

clang/test/CIR/IR/invalid-vector.cir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module {
66

7-
// expected-error @below {{unsupported element type for CIR vector}}
7+
// expected-error @below {{failed to verify 'elementType'}}
88
cir.global external @vec_b = #cir.zero : !cir.vector<4 x !cir.array<!s32i x 10>>
99

1010
}

0 commit comments

Comments
 (0)