[clang] [CIR] Upstream ArraySubscriptExpr for fixed size array (PR #134536)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 9 14:48:43 PDT 2025


================
@@ -390,6 +391,148 @@ LValue CIRGenFunction::emitUnaryOpLValue(const UnaryOperator *e) {
   llvm_unreachable("Unknown unary operator kind!");
 }
 
+/// If the specified expr is a simple decay from an array to pointer,
+/// return the array subexpression.
+/// FIXME: this could be abstracted into a common AST helper.
+static const Expr *getSimpleArrayDecayOperand(const Expr *e) {
+  // If this isn't just an array->pointer decay, bail out.
+  const auto *castExpr = dyn_cast<CastExpr>(e);
+  if (!castExpr || castExpr->getCastKind() != CK_ArrayToPointerDecay)
+    return nullptr;
+
+  // If this is a decay from variable width array, bail out.
+  const Expr *subExpr = castExpr->getSubExpr();
+  if (subExpr->getType()->isVariableArrayType())
+    return nullptr;
+
+  return subExpr;
+}
+
+static mlir::IntegerAttr getConstantIndexOrNull(mlir::Value idx) {
+  // TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
+  if (auto constantOp = dyn_cast<cir::ConstantOp>(idx.getDefiningOp()))
+    return mlir::dyn_cast<mlir::IntegerAttr>(constantOp.getValue());
----------------
andykaylor wrote:

If I understand correctly, cgf.isInPreservedAIRegion will never be true until we support the associated builtin function (`__builtin_preserve_access_index`), which isn't supported yet in the incubator and isn't a high priority and might not be relevant to CIR at all. I don't see any reason to upstream any of that yet.

But you're right that the check in emitArraySubscriptPtr is wrong as it currently appears in this PR. It appears that there is no reason to be checking for a constant index as we are there and we should be unconditionally calling the code in the true case. If you can support a variable index with no other changes, go ahead. Otherwise, leave it for a subsequent patch.

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


More information about the cfe-commits mailing list