[clang] [C2y] Fix _Countof handling of VLAs (PR #141621)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue May 27 08:39:31 PDT 2025


================
@@ -3574,20 +3574,26 @@ ScalarExprEmitter::VisitUnaryExprOrTypeTraitExpr(
           CGF.EmitIgnoredExpr(E->getArgumentExpr());
         }
 
-        auto VlaSize = CGF.getVLASize(VAT);
-        llvm::Value *size = VlaSize.NumElts;
-
         // For sizeof and __datasizeof, we need to scale the number of elements
         // by the size of the array element type. For _Countof, we just want to
         // return the size directly.
+        llvm::Value *Size;
         if (Kind != UETT_CountOf) {
+          auto VlaSize = CGF.getVLASize(VAT);
+          Size = VlaSize.NumElts;
+
           // Scale the number of non-VLA elements by the non-VLA element size.
           CharUnits eltSize = CGF.getContext().getTypeSizeInChars(VlaSize.Type);
           if (!eltSize.isOne())
-            size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), size);
+            Size = CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), Size);
----------------
erichkeane wrote:

I find myself wondering whether 'early return 'would make this all easier.  Also, I would expect the builder's constant folder to make the `eltSize ==1` case a no-op anyway, so we might just be able to make this whole branch:

return CGF.Builder.CreateNUWMul(CGF.CGM.getSize(eltSize), VlaSize.NumElts).

Though, enough of this is pre-existing, *eh*.

https://github.com/llvm/llvm-project/pull/141621


More information about the cfe-commits mailing list