[clang] [HLSL] `constexpr` vector element conversions (PR #195173)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun May 3 21:57:43 PDT 2026
================
@@ -8111,6 +8123,50 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
return true;
}
+/// Cast each element of a source vector to the corresponding element type of a
+/// destination vector using a scalar primitive cast. A pointer to the
+/// destination must be on top of the interpreter stack when \p Initializing is
+/// true; otherwise a new local is allocated for the result.
+template <class Emitter>
+bool Compiler<Emitter>::emitVectorElementwiseCast(const CastExpr *E) {
+ const Expr *SubExpr = E->getSubExpr();
+ assert(SubExpr->getType()->isVectorType() && "expected vector source type");
+ assert(E->getType()->isVectorType() && "expected vector destination type");
+
+ const auto *DstVTy = E->getType()->getAs<VectorType>();
+ unsigned NumElts = DstVTy->getNumElements();
+ PrimType SrcElemT = classifyVectorElementType(SubExpr->getType());
+ PrimType DstElemT = classifyVectorElementType(E->getType());
+ QualType DstElemType = DstVTy->getElementType();
+
+ if (!Initializing) {
+ UnsignedOrNone LocalIndex = allocateLocal(E);
+ if (!LocalIndex)
+ return false;
+ if (!this->emitGetPtrLocal(*LocalIndex, E))
+ return false;
+ }
+
+ unsigned SrcOffset =
+ allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
+ if (!this->visit(SubExpr))
+ return false;
+ if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
+ return false;
+
+ for (unsigned I = 0; I < NumElts; ++I) {
+ if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
+ return false;
+ if (!this->emitArrayElemPop(SrcElemT, I, E))
+ return false;
+ if (!this->emitPrimCast(SrcElemT, DstElemT, DstElemType, E))
+ return false;
+ if (!this->emitInitElem(DstElemT, I, E))
+ return false;
+ }
+ return true;
+}
+
----------------
tbaederr wrote:
This is pretty much the same as what we already have for `ConvertVectorExpr` (which is why I said in the past that HLSL should use those instead of regular casts but alas), so I think we should try to merge those.
https://github.com/llvm/llvm-project/pull/195173
More information about the cfe-commits
mailing list