<div style="line-height:1.7;color:#000000;font-size:14px;font-family:Arial"><div>I find method SelectionDAG::FoldConstantArithmetic contains following code (path: <span style="line-height: 1.7;">lib/CodeGen/SelectionDAG/SelectionDAG.cpp)</span></div><div><br></div><div><div>SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, SDLoc DL, EVT VT,</div><div>                                             SDNode *Cst1, SDNode *Cst2) {</div><div>  // If the opcode is a target-specific ISD node, there's nothing we can</div><div>  // do here and the operand rules may not line up with the below, so</div><div>  // bail early.</div><div>  if (Opcode >= ISD::BUILTIN_OP_END)</div><div>    return SDValue();</div><div><br></div><div>  // Handle the case of two scalars.</div><div>  if (const ConstantSDNode *Scalar1 = dyn_cast<ConstantSDNode>(Cst1)) {</div><div>    if (const ConstantSDNode *Scalar2 = dyn_cast<ConstantSDNode>(Cst2)) {</div><div>      if (SDValue Folded =</div><div>          FoldConstantArithmetic(Opcode, DL, VT, Scalar1, Scalar2)) {</div><div>        if (!VT.isVector())</div><div>          return Folded;</div><div>        <span style="color: rgb(255, 0, 0);">SmallVector<SDValue, 4> Outputs;</span></div><div><span style="color: rgb(255, 0, 0);">        // We may have a vector type but a scalar result. Create a splat.</span></div><div><span style="color: rgb(255, 0, 0);">        Outputs.resize(VT.getVectorNumElements(), Outputs.back());</span></div><div><span style="color: rgb(255, 0, 0);">        // Build a big vector out of the scalar elements we generated.</span></div><div><span style="color: rgb(255, 0, 0);">        return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);</span></div><div>      } else {</div><div>        return SDValue();</div><div>      }</div><div>    }</div><div>  }</div></div><div><br></div><div>The <b>IF</b> block enclosing the code marked in red tries to do constant folding for the two constant operands.</div><div>If it finds out that we get a scalar result, but the expected type is a vector, the code marked in red will be </div><div>executed.</div><div>But it confuses me, for the <b>Outputs </b>is empty, first <span style="line-height: 23.8px;">even get last element for an empty SmallVector </span></div><div><span style="line-height: 23.8px;">will trigger assert, second </span><span style="line-height: 1.7;">we will use an array of empty SDValue instances as </span><span style="line-height: 23.8px;">the operands for the </span></div><div><span style="line-height: 23.8px;">BUILD_VECTOR operator.</span><span style="line-height: 23.8px;"> </span>It may trigger crash when try to access the NULL SDNode <span style="line-height: 23.8px;">pointer inside these</span><span style="line-height: 23.8px;"> </span></div><div>SDValue instances (tons of points not checking the NULL pointer before access).</div><div><br></div><div>Should it be like this, make the scalar result just elements of the vector result (I don't fully understand what should do in this situation):</div><div><br></div><div><div>        SmallVector<SDValue, 4> Outputs;</div><div><span style="color: rgb(255, 0, 0);">        EVT SVT = VT.getScalarType();</span></div><div><span style="color: rgb(255, 0, 0);">        <span style="color: rgb(255, 0, 0); line-height: 23.8px;">Outputs.push_back(</span>getConstant(Folded.first, DL, SVT)<span style="color: rgb(255, 0, 0); line-height: 23.8px;">);</span></span></div><div>        // We may have a vector type but a scalar result. Create a splat.</div><div>        Outputs.resize(VT.getVectorNumElements(), Outputs.back());</div><div>        // Build a big vector out of the scalar elements we generated.</div><div>        return getNode(ISD::BUILD_VECTOR, SDLoc(), VT, Outputs);</div></div><div><br></div><div>Or may it just the case will not actually happen and can be removed?</div><div><br></div><div>Regards</div><div>Hui Wu</div></div><br><br><span title="neteasefooter"><p> </p></span>