<div><div>Hello.</div><div><br></div><div>Sorry I am not sure this question should go to llvm or mesa3d-dev mailing list, so I post it to both.</div><div><br></div><div>I am writing a llvm backend for a modern graphics processor which has a ISA very similar to that of Direct 3D.</div>
<div><br></div><div>I am reading the code in Gallium-3D driver in a mesa3d branch, which converts the shader programs (TGSI tokens) to LLVM IR.</div><div><br></div><div>For the shader instruction also found in LLVM IR, the conversion is trivial:</div>
<div><br></div><div><code></div><div>llvm::Value * Instructions::mul(llvm::Value *in1, llvm::Value *in2) {</div><div>   return m_builder.CreateMul(in1, in2, name("mul")); // m_builder is a llvm::IRBuilder</div>
<div>}</div><div></code></div><div><br></div><div>However, the special instrucions cannot directly be mapped to LLVM IR, like "min", the conversion involves in 'extract' the vector, create less-than-compare, create 'select' instruction, and create 'insert-element' instruction. </div>
<div><br></div><div><code></div><div>llvm::Value * Instructions::min(llvm::Value *in1, llvm::Value *in2)</div><div>{</div><div>   std::vector<llvm::Value*> vec1 = extractVector(in1); // generate LLVM extract element</div>
<div>   std::vector<llvm::Value*> vec2 = extractVector(in2);</div><div><br></div><div>   Value *xcmp  = m_builder.CreateFCmpOLT(vec1[0], vec2[0], name("xcmp"));</div><div>   Value *selx = m_builder.CreateSelect(xcmp, vec1[0], vec2[0],</div>
<div>                                        name("selx"));</div><div><br></div><div>   Value *ycmp  = m_builder.CreateFCmpOLT(vec1[1], vec2[1], name("ycmp"));</div><div>   Value *sely = m_builder.CreateSelect(ycmp, vec1[1], vec2[1],</div>
<div>                                        name("sely"));</div><div><br></div><div>   Value *zcmp  = m_builder.CreateFCmpOLT(vec1[2], vec2[2], name("zcmp"));</div><div>   Value *selz = m_builder.CreateSelect(zcmp, vec1[2], vec2[2],</div>
<div>                                        name("selz"));</div><div><br></div><div>   Value *wcmp  = m_builder.CreateFCmpOLT(vec1[3], vec2[3], name("wcmp"));</div><div>   Value *selw = m_builder.CreateSelect(wcmp, vec1[3], vec2[3],</div>
<div>                                        name("selw"));</div><div>   return vectorFromVals(selx, sely, selz, selw); // generate LLVM 'insert-element' </div><div>}</div><div></code></div><div><br>
</div><div>Eventually all these should be folded to a 'min' instruction in the codegen, so I wonder if the conversion only generates a simple 'call' instruction to a 'min Function' will make the instruction selection easier (no folding and complicated pattern-matching in the instruction selection DAG). </div>
<div><br></div><div>I don't have experience of the new vector instructions in LLVM, and perhaps that's why it makes me feel it's complicated to fold the swizzle and writemask.</div><div><br></div><div>Thanks.</div>
</div>