lib/HLSL/HLMatrixLowerPass.cpp
// Flatten values inside init list to scalar elements.
Flatten values inside init list to scalar elements.
Flatten values inside init list to scalar elements .
static void IterateInitList(MutableArrayRef<Value *> elts, unsigned &idx,
                            Value *val,
                            DenseMap<Instruction *, Value *> &matToVecMap,
                            IRBuilder<> &Builder) {
  Type *valTy = val->getType();
  if (valTy->isPointerTy()) {
    if (HLMatrixLower::IsMatrixArrayPointer(valTy)) {
      if (matToVecMap.count(cast<Instruction>(val))) {
        val = matToVecMap[cast<Instruction>(val)];
      } else {
        Type *vecArrayPtrTy = HLMatrixLower::LowerMatrixArrayPointer(valTy);
        val = Builder.CreateBitCast(val, vecArrayPtrTy);
      }
    }
    Type *valEltTy = val->getType()->getPointerElementType();
    if (valEltTy->isVectorTy() || HLMatrixLower::IsMatrixType(valEltTy) ||
        valEltTy->isSingleValueType()) {
      Value *ldVal = Builder.CreateLoad(val);
      IterateInitList(elts, idx, ldVal, matToVecMap, Builder);
    } else {
      Type *i32Ty = Type::getInt32Ty(valTy->getContext());
      Value *zero = ConstantInt::get(i32Ty, 0);
      if (ArrayType *AT = dyn_cast<ArrayType>(valEltTy)) {
        for (unsigned i = 0; i < AT->getArrayNumElements(); i++) {
          Value *gepIdx = ConstantInt::get(i32Ty, i);
          Value *EltPtr = Builder.CreateInBoundsGEP(val, {zero, gepIdx});
          IterateInitList(elts, idx, EltPtr, matToVecMap, Builder);
        }
      } else {
        StructType *ST = cast<StructType>(valEltTy);
        for (unsigned i = 0; i < ST->getNumElements(); i++) {
          Value *gepIdx = ConstantInt::get(i32Ty, i);
          Value *EltPtr = Builder.CreateInBoundsGEP(val, {zero, gepIdx});
          IterateInitList(elts, idx, EltPtr, matToVecMap, Builder);
        }
      }
    }
  } else if (HLMatrixLower::IsMatrixType(valTy)) {
    unsigned col, row;
    HLMatrixLower::GetMatrixInfo(valTy, col, row);
    unsigned matSize = col * row;
    val = matToVecMap[cast<Instruction>(val)];
    for (unsigned i = 0; i < matSize; i++) {
      Value *Elt = Builder.CreateExtractElement(val, i);
      elts[idx + i] = Elt;
    }
    idx += matSize;
  } else {
    if (valTy->isVectorTy()) {
      unsigned vecSize = valTy->getVectorNumElements();
      for (unsigned i = 0; i < vecSize; i++) {
        Value *Elt = Builder.CreateExtractElement(val, i);
        elts[idx + i] = Elt;
      }
      idx += vecSize;
    } else {
      DXASSERT(valTy->isSingleValueType(), "must be single value type here");
      elts[idx++] = val;
    }
  }
}
static void IterateInitList ( MutableArrayRef < Value * > elts , unsigned & idx , Value * val , DenseMap < Instruction * , Value * > & matToVecMap , IRBuilder < > & Builder ) { Type * valTy = val -> getType ( ) ; if ( valTy -> isPointerTy ( ) ) { if ( HLMatrixLower :: IsMatrixArrayPointer ( valTy ) ) { if ( matToVecMap . count ( cast < Instruction > ( val ) ) ) { val = matToVecMap [ cast < Instruction > ( val ) ] ; } else { Type * vecArrayPtrTy = HLMatrixLower :: LowerMatrixArrayPointer ( valTy ) ; val = Builder . CreateBitCast ( val , vecArrayPtrTy ) ; } } Type * valEltTy = val -> getType ( ) -> getPointerElementType ( ) ; if ( valEltTy -> isVectorTy ( ) || HLMatrixLower :: IsMatrixType ( valEltTy ) || valEltTy -> isSingleValueType ( ) ) { Value * ldVal = Builder . CreateLoad ( val ) ; IterateInitList ( elts , idx , ldVal , matToVecMap , Builder ) ; } else { Type * i32Ty = Type :: getInt32Ty ( valTy -> getContext ( ) ) ; Value * zero = ConstantInt :: get ( i32Ty , 0 ) ; if ( ArrayType * AT = dyn_cast < ArrayType > ( valEltTy ) ) { for ( unsigned i = 0 ; i < AT -> getArrayNumElements ( ) ; i ++ ) { Value * gepIdx = ConstantInt :: get ( i32Ty , i ) ; Value * EltPtr = Builder . CreateInBoundsGEP ( val , { zero , gepIdx } ) ; IterateInitList ( elts , idx , EltPtr , matToVecMap , Builder ) ; } } else { StructType * ST = cast < StructType > ( valEltTy ) ; for ( unsigned i = 0 ; i < ST -> getNumElements ( ) ; i ++ ) { Value * gepIdx = ConstantInt :: get ( i32Ty , i ) ; Value * EltPtr = Builder . CreateInBoundsGEP ( val , { zero , gepIdx } ) ; IterateInitList ( elts , idx , EltPtr , matToVecMap , Builder ) ; } } } } else if ( HLMatrixLower :: IsMatrixType ( valTy ) ) { unsigned col , row ; HLMatrixLower :: GetMatrixInfo ( valTy , col , row ) ; unsigned matSize = col * row ; val = matToVecMap [ cast < Instruction > ( val ) ] ; for ( unsigned i = 0 ; i < matSize ; i ++ ) { Value * Elt = Builder . CreateExtractElement ( val , i ) ; elts [ idx + i ] = Elt ; } idx += matSize ; } else { if ( valTy -> isVectorTy ( ) ) { unsigned vecSize = valTy -> getVectorNumElements ( ) ; for ( unsigned i = 0 ; i < vecSize ; i ++ ) { Value * Elt = Builder . CreateExtractElement ( val , i ) ; elts [ idx + i ] = Elt ; } idx += vecSize ; } else { DXASSERT ( valTy -> isSingleValueType ( ) , " " ) ; elts [ idx ++ ] = val ; } } }
Flatten values inside init list to scalar elements.
Flatten values inside init list to scalar elements .
// Convert to vec array with bitcast. // Struct. // temp matrix all row major