[clang] [CIR] SubscriptExpr for VariableLengthArray (PR #175370)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 12 13:26:13 PST 2026


================
@@ -1181,6 +1182,36 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
     return makeAddrLValue(addr, elementType, lv.getBaseInfo());
   }
 
+  if (const VariableArrayType *vla =
+          getContext().getAsVariableArrayType(e->getType())) {
+    // The base must be a pointer, which is not an aggregate.  Emit
+    // it.  It needs to be emitted first in case it's what captures
+    // the VLA bounds.
+    Address addr = emitPointerWithAlignment(e->getBase());
+
+    // The element count here is the total number of non-VLA elements.
+    mlir::Value numElements = getVLASize(vla).numElts;
+    idx = builder.createIntCast(idx, numElements.getType());
+
+    // Effectively, the multiply by the VLA size is part of the GEP.
+    // GEP indexes are signed, and scaling an index isn't permitted to
+    // signed-overflow, so we use the same semantics for our explicit
+    // multiply.  We suppress this if overflow is not undefined behavior.
+    OverflowBehavior overflowBehavior = getLangOpts().PointerOverflowDefined
+                                            ? OverflowBehavior::None
+                                            : OverflowBehavior::NoSignedWrap;
+    idx = builder.createMul(cgm.getLoc(e->getExprLoc()), idx, numElements,
+                            overflowBehavior);
+
+    assert(!cir::MissingFeatures::emitCheckedInBoundsGEP());
----------------
andykaylor wrote:

This is already marked inside `emitArraySubscriptPtr`. Is there anything else needed here?

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


More information about the cfe-commits mailing list