[Mlir-commits] [mlir] [mlir][vector][spirv] Handle 1-element vector.{load|store} lowering. (PR #126294)

Md Abdullah Shahneous Bari llvmlistbot at llvm.org
Fri Feb 7 12:48:30 PST 2025


================
@@ -770,10 +770,20 @@ struct VectorLoadOpConverter final
 
     spirv::StorageClass storageClass = attr.getValue();
     auto vectorType = loadOp.getVectorType();
-    auto vectorPtrType = spirv::PointerType::get(vectorType, storageClass);
-    Value castedAccessChain =
-        rewriter.create<spirv::BitcastOp>(loc, vectorPtrType, accessChain);
-    rewriter.replaceOpWithNewOp<spirv::LoadOp>(loadOp, vectorType,
+    // Use the converted vector type instead of original (single element vector
+    // would get converted to scalar).
+    auto spirvVectorType = typeConverter.convertType(vectorType);
+    auto vectorPtrType = spirv::PointerType::get(spirvVectorType, storageClass);
+
+    // For single element vectors, we don't need to bitcast the access chain to
+    // the original vector type. Both is going to be the same, a pointer
+    // to a scalar.
+    Value castedAccessChain = (vectorType.getNumElements() == 1)
+                                  ? accessChain
+                                  : rewriter.create<spirv::BitcastOp>(
+                                        loc, vectorPtrType, accessChain);
----------------
mshahneo wrote:

Actually it's part of the original code and test. Here is the test case: https://github.com/llvm/llvm-project/blob/main/mlir/test/Conversion/VectorToSPIRV/vector-to-spirv.mlir#L997

In this PR, I am actually making this bitcast conditional, since, we don't need it for single element vector which gets converted to a scalar.

Also, doing the bitcast to single-element vector was causing it to fail in the first place (not a SPIR-V type). 

https://github.com/llvm/llvm-project/pull/126294


More information about the Mlir-commits mailing list