diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 89e3791036c6..ca5385a5b5f7 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -391,6 +391,8 @@ def CIR_VectorType : CIR_Type<"Vector", "vector", let assemblyFormat = [{ `<` $elementType `x` $size `>` }]; + + let genVerifyDecl = 1; } //===----------------------------------------------------------------------===// diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp index 0a7fa365a742..a41bbb695732 100644 --- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp +++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp @@ -410,6 +410,23 @@ cir::VectorType::getABIAlignment(const ::mlir::DataLayout &dataLayout, return llvm::NextPowerOf2(dataLayout.getTypeSizeInBits(*this)); } +mlir::LogicalResult cir::VectorType::verify( + llvm::function_ref emitError, + mlir::Type elementType, uint64_t size) { + if (size == 0) + return emitError() << "the number of vector elements must be non-zero"; + + // Check if it a valid FixedVectorType + if (mlir::isa(elementType)) + return success(); + + // Check if it a valid VectorType + if (mlir::isa(elementType) || + isAnyFloatingPointType(elementType)) + return success(); + + return emitError() << "unsupported element type for CIR vector"; +} // TODO(cir): Implement a way to cache the datalayout info calculated below. llvm::TypeSize diff --git a/clang/test/CIR/IR/invalid-vector-zero-size.cir b/clang/test/CIR/IR/invalid-vector-zero-size.cir new file mode 100644 index 000000000000..d7472f28fad8 --- /dev/null +++ b/clang/test/CIR/IR/invalid-vector-zero-size.cir @@ -0,0 +1,10 @@ +// RUN: cir-opt %s -verify-diagnostics -split-input-file + +!s32i = !cir.int + +module { + +// expected-error @below {{the number of vector elements must be non-zero}} +cir.global external @vec_a = #cir.zero : !cir.vector + +} diff --git a/clang/test/CIR/IR/invalid-vector.cir b/clang/test/CIR/IR/invalid-vector.cir new file mode 100644 index 000000000000..2d26cade2348 --- /dev/null +++ b/clang/test/CIR/IR/invalid-vector.cir @@ -0,0 +1,10 @@ +// RUN: cir-opt %s -verify-diagnostics -split-input-file + +!s32i = !cir.int + +module { + +// expected-error @below {{unsupported element type for CIR vector}} +cir.global external @vec_b = #cir.zero : !cir.vector x 4> + +}