@@ -826,26 +826,40 @@ class ScalarExprEmitter : public StmtVisitor<ScalarExprEmitter, mlir::Value> {
826826 QualType qualSrcTy = E->getSrcExpr ()->getType ();
827827 QualType qualDstTy = E->getType ();
828828
829- // Bitwidth check
830- unsigned srcBits = CGF.getContext ().getTypeSize (qualSrcTy);
831- unsigned dstBits = CGF.getContext ().getTypeSize (qualDstTy);
832- if (srcBits != dstBits) {
833- emitError (CGF.getLoc (E->getExprLoc ()),
834- " source and destination must have equal bitwidths: '" +
835- llvm::Twine (srcBits) + " ' vs '" + llvm::Twine (dstBits) +
836- " '" );
837- return nullptr ;
838- }
839-
840- // No-op if already same type
841829 mlir::Type srcTy = CGF.convertType (qualSrcTy);
842830 mlir::Type dstTy = CGF.convertType (qualDstTy);
831+ auto loc = CGF.getLoc (E->getExprLoc ());
832+
833+ unsigned numSrcElems = 0 , numDstElems = 0 ;
834+ if (auto v = dyn_cast<cir::VectorType>(srcTy))
835+ numSrcElems = v.getSize ();
836+ if (auto v = dyn_cast<cir::VectorType>(dstTy))
837+ numDstElems = v.getSize ();
838+
839+ // Use bit vector expansion for ext_vector_type boolean vectors.
840+ if (qualDstTy->isExtVectorBoolType ()) {
841+ llvm_unreachable (" NYI" );
842+ }
843+
844+ // Going from vec3 to non-vec3 is a special case and requires a shuffle
845+ // vector to get a vec4, then a bitcast if the target type is different.
846+ if (numSrcElems == 3 && numDstElems != 3 ) {
847+ llvm_unreachable (" NYI" );
848+ }
849+
850+ // Going from non-vec3 to vec3 is a special case and requires a bitcast
851+ // to vec4 if the original type is not vec4, then a shuffle vector to
852+ // get a vec3.
853+ if (numSrcElems != 3 && numDstElems == 3 ) {
854+ llvm_unreachable (" NYI" );
855+ }
856+
857+ // If types are identical, return the source
843858 if (srcTy == dstTy)
844859 return src;
845860
846- // Perform the bitcast
847- auto loc = CGF.getLoc (E->getExprLoc ());
848- return Builder.create <cir::CastOp>(loc, dstTy, cir::CastKind::bitcast, src);
861+ // Otherwise, fallback to CIR bitcast
862+ return cir::CastOp::create (Builder, loc, dstTy, cir::CastKind::bitcast, src);
849863 }
850864
851865 mlir::Value VisitAtomicExpr (AtomicExpr *E) {
0 commit comments