[clang] [CIR] Handle boolean expression as array indexes (PR #193814)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 23 13:46:15 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);
----------------
andykaylor wrote:
I see. The thing I was saying it wouldn't let me do is `char arr[0xFFFFFFFFFFFFFFFF];`
I wasn't sure if I should use `ptrDiffTy` here, which is signed, or `uIntPtrTy`, which is unsigned. I notice that if I use an index of -1, classic codegen drops the `nuw` flag on the GEP. Classic codegen seems to be checking the type of the index value to see if it's signed and passing an argument to `emitArraySubscriptGEP` to indicate that.
Perhaps I should be checking the sign of the index to choose between `ptrDiffTy` and `uIntPtrTy` (which is in a union with `sizeTy`)?
https://github.com/llvm/llvm-project/pull/193814
More information about the cfe-commits
mailing list