vector type legalization

Hi Nadav, I believe the implementation to keep on widening the vector to the next power of two must be in TargetLowering.h because that is where we decide whether to Widen the vector or not, and the size to which we widen it. In this case, we stop at 4xi8 and do not check if it is legal or not. But the comment says ‘try to widen vector elements until a legal type is found’.

Also, there is a minor issue with the comment <3 x float> to <4 x float>, where the float vectors won’t even enter this block of code, since we check the element Vector type (EltVT) to be integer.

The patch breaks a whole lot of test-cases, so it is obviously not the ideal solution. It simply reflects what the comment says.

diff --git a/include/llvm/Target/TargetLowering.h b/include/llvm/Target/TargetLowering.h
index c3fa3cc…181f951 100644
— a/include/llvm/Target/TargetLowering.h
+++ b/include/llvm/Target/TargetLowering.h
@@ -1474,10 +1474,14 @@ public:
// Try to widen vector elements until a legal type is found.
if (EltVT.isInteger()) {
// Vectors with a number of elements that is not a power of two are always

  • // widened, for example <3 x float> → <4 x float>.
  • // widened, for example <3 x i8> → <4 x i8>.
    if (!VT.isPow2VectorType()) {
    NumElts = (unsigned)NextPowerOf2(NumElts);
    EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
  • while (!isTypeLegal(NVT)) {
  • NumElts = (unsigned)NextPowerOf2(NumElts);
  • NVT = EVT::getVectorVT(Context, EltVT, NumElts);
  • }
    return LegalizeKind(TypeWidenVector, NVT);
    }

Hi Nadav, I believe the implementation to keep on widening the vector to the next power of two must be in TargetLowering.h because that is where we decide whether to Widen the vector or not, and the size to which we widen it.

The decision on which legalization kind to do is implemented in TargetLowering.h. The actual legalization methods are called from LegalizeTypes.cpp (TargetLowering::TypeXXXIntger and TypeXXXFloat).

In this case, we stop at 4xi8 and do not check if it is legal or not. But the comment says ‘try to widen vector elements until a legal type is found’.

The comment is incorrect. Type-legalization is an iterative process. We first widen the vector to a power of two, and after that we split, promote, scalarize, etc.

Also, there is a minor issue with the comment <3 x float> to <4 x float>, where the float vectors won’t even enter this block of code, since we check the element Vector type (EltVT) to be integer.

The code is okay. The comment should be changed to something with integers.

The patch breaks a whole lot of test-cases, so it is obviously not the ideal solution. It simply reflects what the comment says.

diff --git a/include/llvm/Target/TargetLowering.h b/include/llvm/Target/TargetLowering.h
index c3fa3cc..181f951 100644
--- a/include/llvm/Target/TargetLowering.h
+++ b/include/llvm/Target/TargetLowering.h
@@ -1474,10 +1474,14 @@ public:
     // Try to widen vector elements until a legal type is found.
     if (EltVT.isInteger()) {
       // Vectors with a number of elements that is not a power of two are always
- // widened, for example <3 x float> -> <4 x float>.
+ // widened, for example <3 x i8> -> <4 x i8>.
       if (!VT.isPow2VectorType()) {
         NumElts = (unsigned)NextPowerOf2(NumElts);
         EVT NVT = EVT::getVectorVT(Context, EltVT, NumElts);
+ while (!isTypeLegal(NVT)) {
+ NumElts = (unsigned)NextPowerOf2(NumElts);
+ NVT = EVT::getVectorVT(Context, EltVT, NumElts);
+ }
         return LegalizeKind(TypeWidenVector, NVT);
       }

Your patch is incorrect. We first need to widen to the next power of two. Regardless if this is a legal type or not.

Thanks,
Nadav