[clang] [CIR] Handle boolean expression as array indexes (PR #193814)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 23 13:00:49 PDT 2026


================
@@ -1248,13 +1248,19 @@ CIRGenFunction::emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e) {
          "index was neither LHS nor RHS");
 
   auto emitIdxAfterBase = [&](bool promote) -> mlir::Value {
-    const mlir::Value idx = emitScalarExpr(e->getIdx());
+    mlir::Value idx = emitScalarExpr(e->getIdx());
+
+    assert(!cir::MissingFeatures::sanitizers());
+
+    // Extend or truncate the index type to pointer-sized integer.
+    if (promote && idx.getType() != ptrDiffTy) {
+      cir::CastKind kind = mlir::isa<cir::BoolType>(idx.getType())
+                               ? cir::CastKind::bool_to_int
+                               : cir::CastKind::integral;
+      idx =
+          builder.createOrFold<cir::CastOp>(idx.getLoc(), ptrDiffTy, kind, idx);
----------------
erichkeane wrote:

Hmm, on godbolt I'm able to get a uint64_max as an index at least : https://godbolt.org/z/xPTfY7nMG

Though I will say it seems to be doing the roll-over as well.  Though the problem would be if it was a pointer indirection, but I guess this is for arrays of known bounds anyway.

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


More information about the cfe-commits mailing list