Skip to content

Commit d8792fe

Browse files
[mlir][LLVMIR] Check number of elements in mlir.constant verifier
Check that the number of elements in the result type and the attribute of an `llvm.mlir.constant` op matches. Also fix a broken test where that was not the case.
1 parent 4915fdd commit d8792fe

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,20 @@ OpFoldResult LLVM::ZeroOp::fold(FoldAdaptor) {
26662666
// ConstantOp.
26672667
//===----------------------------------------------------------------------===//
26682668

2669+
/// Compute the total number of elements in the given type, also taking into
2670+
/// account nested types. Supported types are `VectorType`, `LLVMArrayType` and
2671+
/// `LLVMFixedVectorType`. Everything else is treated as a scalar.
2672+
static int64_t getNumElements(Type t) {
2673+
if (auto vecType = dyn_cast<VectorType>(t))
2674+
return vecType.getNumElements() * getNumElements(vecType.getElementType());
2675+
if (auto arrayType = dyn_cast<LLVM::LLVMArrayType>(t))
2676+
return arrayType.getNumElements() *
2677+
getNumElements(arrayType.getElementType());
2678+
if (auto vecType = dyn_cast<LLVMFixedVectorType>(t))
2679+
return vecType.getNumElements() * getNumElements(vecType.getElementType());
2680+
return 1;
2681+
}
2682+
26692683
LogicalResult LLVM::ConstantOp::verify() {
26702684
if (StringAttr sAttr = llvm::dyn_cast<StringAttr>(getValue())) {
26712685
auto arrayType = llvm::dyn_cast<LLVMArrayType>(getType());
@@ -2708,14 +2722,12 @@ LogicalResult LLVM::ConstantOp::verify() {
27082722
if (auto targetExtType = dyn_cast<LLVMTargetExtType>(getType())) {
27092723
return emitOpError() << "does not support target extension type.";
27102724
}
2711-
if (!llvm::isa<IntegerAttr, ArrayAttr, FloatAttr, ElementsAttr>(getValue()))
2712-
return emitOpError()
2713-
<< "only supports integer, float, string or elements attributes";
2725+
2726+
// Verification of IntegerAttr, FloatAttr, ElementsAttr, ArrayAttr.
27142727
if (auto intAttr = dyn_cast<IntegerAttr>(getValue())) {
27152728
if (!llvm::isa<IntegerType>(getType()))
27162729
return emitOpError() << "expected integer type";
2717-
}
2718-
if (auto floatAttr = dyn_cast<FloatAttr>(getValue())) {
2730+
} else if (auto floatAttr = dyn_cast<FloatAttr>(getValue())) {
27192731
const llvm::fltSemantics &sem = floatAttr.getValue().getSemantics();
27202732
unsigned floatWidth = APFloat::getSizeInBits(sem);
27212733
if (auto floatTy = dyn_cast<FloatType>(getType())) {
@@ -2728,13 +2740,30 @@ LogicalResult LLVM::ConstantOp::verify() {
27282740
if (isa<IntegerType>(getType()) && !getType().isInteger(floatWidth)) {
27292741
return emitOpError() << "expected integer type of width " << floatWidth;
27302742
}
2731-
}
2732-
if (auto splatAttr = dyn_cast<SplatElementsAttr>(getValue())) {
2733-
if (!isa<VectorType>(getType()) && !isa<LLVM::LLVMArrayType>(getType()) &&
2734-
!isa<LLVM::LLVMFixedVectorType>(getType()) &&
2735-
!isa<LLVM::LLVMScalableVectorType>(getType()))
2743+
} else if (isa<ElementsAttr, ArrayAttr>(getValue())) {
2744+
if (isa<LLVM::LLVMScalableVectorType>(getType())) {
2745+
// The exact number of elements of a scalable vector is unknown, so there
2746+
// is nothing more to verify.
2747+
return success();
2748+
}
2749+
if (!isa<VectorType, LLVM::LLVMArrayType, LLVM::LLVMFixedVectorType>(
2750+
getType()))
27362751
return emitOpError() << "expected vector or array type";
2752+
// The number of elements of the attribute and the type must match.
2753+
int64_t attrNumElements;
2754+
if (auto elementsAttr = dyn_cast<ElementsAttr>(getValue()))
2755+
attrNumElements = elementsAttr.getNumElements();
2756+
else
2757+
attrNumElements = cast<ArrayAttr>(getValue()).size();
2758+
if (getNumElements(getType()) != attrNumElements)
2759+
return emitOpError()
2760+
<< "type and attribute have a different number of elements: "
2761+
<< getNumElements(getType()) << " vs. " << attrNumElements;
2762+
} else {
2763+
return emitOpError()
2764+
<< "only supports integer, float, string or elements attributes";
27372765
}
2766+
27382767
return success();
27392768
}
27402769

mlir/test/Dialect/LLVMIR/invalid.mlir

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,14 @@ llvm.func @struct_wrong_element_types() -> !llvm.struct<(!llvm.array<2 x f64>, !
414414

415415
// -----
416416

417+
llvm.func @struct_wrong_element_types() -> vector<5xf64> {
418+
// expected-error @+1{{type and attribute have a different number of elements: 5 vs. 2}}
419+
%0 = llvm.mlir.constant(dense<[1.0, 1.0]> : tensor<2xf64>) : vector<5xf64>
420+
llvm.return %0 : vector<5xf64>
421+
}
422+
423+
// -----
424+
417425
func.func @insertvalue_non_llvm_type(%a : i32, %b : i32) {
418426
// expected-error@+2 {{expected LLVM IR Dialect type}}
419427
llvm.insertvalue %a, %b[0] : tensor<*xi32>

mlir/test/Target/LLVMIR/llvmir.mlir

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1295,11 +1295,17 @@ llvm.func @complexintconstant() -> !llvm.struct<(i32, i32)> {
12951295
}
12961296

12971297
llvm.func @complexintconstantsplat() -> !llvm.array<2 x !llvm.struct<(i32, i32)>> {
1298-
%1 = llvm.mlir.constant(dense<(0, 1)> : tensor<complex<i32>>) : !llvm.array<2 x !llvm.struct<(i32, i32)>>
1298+
%1 = llvm.mlir.constant(dense<(0, 1)> : tensor<2xcomplex<i32>>) : !llvm.array<2 x !llvm.struct<(i32, i32)>>
12991299
// CHECK: ret [2 x { i32, i32 }] [{ i32, i32 } { i32 0, i32 1 }, { i32, i32 } { i32 0, i32 1 }]
13001300
llvm.return %1 : !llvm.array<2 x !llvm.struct<(i32, i32)>>
13011301
}
13021302

1303+
llvm.func @complexintconstantsingle() -> !llvm.array<1 x !llvm.struct<(i32, i32)>> {
1304+
%1 = llvm.mlir.constant(dense<(0, 1)> : tensor<complex<i32>>) : !llvm.array<1 x !llvm.struct<(i32, i32)>>
1305+
// CHECK: ret [1 x { i32, i32 }] [{ i32, i32 } { i32 0, i32 1 }]
1306+
llvm.return %1 : !llvm.array<1 x !llvm.struct<(i32, i32)>>
1307+
}
1308+
13031309
llvm.func @complexintconstantarray() -> !llvm.array<2 x !llvm.array<2 x !llvm.struct<(i32, i32)>>> {
13041310
%1 = llvm.mlir.constant(dense<[[(0, 1), (2, 3)], [(4, 5), (6, 7)]]> : tensor<2x2xcomplex<i32>>) : !llvm.array<2 x!llvm.array<2 x !llvm.struct<(i32, i32)>>>
13051311
// CHECK{LITERAL}: ret [2 x [2 x { i32, i32 }]] [[2 x { i32, i32 }] [{ i32, i32 } { i32 0, i32 1 }, { i32, i32 } { i32 2, i32 3 }], [2 x { i32, i32 }] [{ i32, i32 } { i32 4, i32 5 }, { i32, i32 } { i32 6, i32 7 }]]

0 commit comments

Comments
 (0)