[PATCH] D153894: [mlir][llvm] Fix conversion of scalable vectors of rank > 1

Cullen Rhodes via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 27 09:58:00 PDT 2023


c-rhodes created this revision.
c-rhodes added reviewers: awarzynski, jsetoain.
c-rhodes added a project: MLIR.
Herald added subscribers: gysit, Dinistro, bviyer, Moerafaat, zero9178, bzcheeseman, sdasgup3, wenzhicui, wrengr, cota, teijeong, rdzhabarov, tatianashp, msifontes, jurahul, Kayjukh, grosul1, Joonsoo, liufengdb, aartbik, mgester, arpith-jacob, antiagainst, shauheen, rriddle, mehdi_amini.
Herald added a reviewer: aartbik.
Herald added a reviewer: ftynse.
Herald added a reviewer: dcaballe.
Herald added a project: All.
c-rhodes requested review of this revision.
Herald added subscribers: alextsao1999, stephenneuendorffer, nicolasvasilache.

Vector types of rank > 1 are converted to array(s) of vector type(s).
Arrays in LLVM cannot contain scalable vectors since the size is unknown
at compile-time, but it's currently trying to do so and creating 1-D
vector types where the number of scalable dims is greater than the rank,
triggering an assert in `llvm-project/llvm/include/llvm/ADT/ArrayRef.h:255`:

  Assertion `Index < Length && "Invalid index!"' failed.

This patch fixes this so there's only a single scalable dim. This needs
fixing properly by adding support for converting scalable vectors of
rank > 1 to LLVM but at least now it will fail expectedly during
conversion to LLVM with:

  Assertion `isValidElementType(ElementType) && "Invalid type for array
  element!"' failed.

rather than with a cryptic assert.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153894

Files:
  mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
  mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir


Index: mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
===================================================================
--- mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
+++ mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
@@ -112,6 +112,21 @@
 
 // -----
 
+func.func @broadcast_scalable_vec2d_from_scalar(%arg0: f32) -> vector<[2x3]xf32> {
+  %0 = vector.broadcast %arg0 : f32 to vector<[2x3]xf32>
+  return %0 : vector<[2x3]xf32>
+}
+// CHECK-LABEL: @broadcast_scalable_vec2d_from_scalar(
+// CHECK-SAME:  %[[A:.*]]: f32)
+// CHECK:       %[[T0:.*]] = llvm.insertelement %[[A]]
+// CHECK:       %[[T1:.*]] = llvm.shufflevector %[[T0]]
+// CHECK:       %[[T2:.*]] = llvm.insertvalue %[[T1]], %{{.*}}[0] : !llvm.array<2 x vector<[3]xf32>>
+// CHECK:       %[[T3:.*]] = llvm.insertvalue %[[T1]], %{{.*}}[1] : !llvm.array<2 x vector<[3]xf32>>
+// CHECK:       %[[T4:.*]] = builtin.unrealized_conversion_cast %[[T3]] : !llvm.array<2 x vector<[3]xf32>> to vector<[2x3]xf32>
+// CHECK:       return %[[T4]] : vector<[2x3]xf32>
+
+// -----
+
 func.func @broadcast_vec3d_from_scalar(%arg0: f32) -> vector<2x3x4xf32> {
   %0 = vector.broadcast %arg0 : f32 to vector<2x3x4xf32>
   return %0 : vector<2x3x4xf32>
Index: mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
===================================================================
--- mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
+++ mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
@@ -464,7 +464,7 @@
   if (type.getShape().empty())
     return VectorType::get({1}, elementType);
   Type vectorType = VectorType::get(type.getShape().back(), elementType,
-                                    type.getNumScalableDims());
+                                    type.isScalable());
   assert(LLVM::isCompatibleVectorType(vectorType) &&
          "expected vector type compatible with the LLVM dialect");
   auto shape = type.getShape();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153894.535034.patch
Type: text/x-patch
Size: 1905 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230627/a1d7897b/attachment.bin>


More information about the llvm-commits mailing list