[PATCH]Fix Bug of Bitcast from Vectors of Small Element Size

李奇平 liqiping1991 at gmail.com
Thu May 8 01:36:56 PDT 2014


When doing bitcast from vectors of small element size in LLVM IR, we will
get wrong results.

For example, %a = bitcast <2 x i1><i1 0, i1 1> to i2 will set %a to 0 after

The cause of this bug is that in LLVM, the above bitcast is accomplished by
store and load. It first store the vector v2i1 to memory then load from
memory as an i2 type. But during the LegalizeVectorOps phase, it will store
each element of the vector separately.
When the element size is smaller than 8 bits, as in our case i1, it will
store each element in the **same** location. The code that cause this
problem is at  LegalizeVectorOps.cpp::ExpandStore:

// Store Stride in bytes
unsigned Stride = ScalarSize/8;
// Extract each of the elements from the original vector
// and save them into memory individually.
SmallVector<SDValue, 8> Stores;
for (unsigned Idx = 0; Idx < NumElem; Idx++) {
  SDValue Ex = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
   RegSclVT, Value, DAG.getConstant(Idx, TLI.getVectorIdxTy()));

  // This scalar TruncStore may be illegal, but we legalize it later.
  SDValue Store = DAG.getTruncStore(Chain, dl, Ex, BasePTR,
   ST->getPointerInfo().getWithOffset(Idx*Stride), MemSclVT,
   isVolatile, isNonTemporal, Alignment, TBAAInfo);

  BasePTR = DAG.getNode(ISD::ADD, dl, BasePTR.getValueType(), BasePTR,

         DAG.getConstant(Stride, BasePTR.getValueType()));


If ScalarSize is less than 8, the Stride is 0,
 ST->getPointerInfo().getWithOffset(Idx*Stride) will always get the same

The patch first convert vector of small element size to an integer using
shift and or before it is saved to the memory. Please see the attachment
for detail.

Thanks for your reviews.

Best Regards,

Qiping Li, Programmer
Email: liqiping1991 at gmail.com
Mobile: 15700080842
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140508/74583e84/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: bitcast.patch
Type: text/x-patch
Size: 2758 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140508/74583e84/attachment.bin>

More information about the llvm-commits mailing list