diff --git a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp --- a/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ b/mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -216,6 +216,16 @@ if (type.getNumElements() == 0) return nullptr; + // Check that the raw data size matches what is expected for the scalar size. + // TODO: in theory, we could repack the data here to keep constructing from + // raw data. + // TODO: we may also need to consider endianness when cross-compiling to an + // architecture where it is different. + unsigned elementByteSize = denseElementsAttr.getRawData().size() / + denseElementsAttr.getNumElements(); + if (8 * elementByteSize != innermostLLVMType->getScalarSizeInBits()) + return nullptr; + // Compute the shape of all dimensions but the innermost. Note that the // innermost dimension may be that of the vector element type. bool hasVectorElementType = type.getElementType().isa(); diff --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir --- a/mlir/test/Target/LLVMIR/llvmir.mlir +++ b/mlir/test/Target/LLVMIR/llvmir.mlir @@ -1251,6 +1251,20 @@ llvm.return %1 : !llvm.array<2 x !llvm.array<2 x !llvm.struct<(i32, i32)>>> } +// CHECK-LABEL: @indexconstantsplat +llvm.func @indexconstantsplat() -> vector<3xi32> { + %1 = llvm.mlir.constant(dense<42> : vector<3xindex>) : vector<3xi32> + // CHECK: ret <3 x i32> + llvm.return %1 : vector<3xi32> +} + +// CHECK-LABEL: @indexconstantarray +llvm.func @indexconstantarray() -> vector<3xi32> { + %1 = llvm.mlir.constant(dense<[0, 1, 2]> : vector<3xindex>) : vector<3xi32> + // CHECK: ret <3 x i32> + llvm.return %1 : vector<3xi32> +} + llvm.func @noreach() { // CHECK: unreachable llvm.unreachable