[clang] [llvm] Explicit types in cbuffer layouts (PR #156919)

Steven Perron via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 11 12:35:48 PDT 2025


================
@@ -4678,6 +4678,26 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
         emitArraySubscriptGEP(*this, Int8Ty, Addr.emitRawPointer(*this),
                               ScaledIdx, false, SignedIndices, E->getExprLoc());
     Addr = Address(EltPtr, OrigBaseElemTy, EltAlign);
+  } else if (E->getType().getAddressSpace() == LangAS::hlsl_constant) {
+    // This is an array inside of a cbuffer.
+    Addr = EmitPointerWithAlignment(E->getBase(), &EltBaseInfo, &EltTBAAInfo);
+    auto *Idx = EmitIdxAfterBase(/*Promote*/true);
+
+    // ...
+    CharUnits RowAlignedSize = getContext()
+                                   .getTypeSizeInChars(E->getType())
+                                   .alignTo(CharUnits::fromQuantity(16));
+
+    llvm::Value *RowAlignedSizeVal =
+        llvm::ConstantInt::get(Idx->getType(), RowAlignedSize.getQuantity());
+    llvm::Value *ScaledIdx = Builder.CreateMul(Idx, RowAlignedSizeVal);
+
+    CharUnits EltAlign =
+      getArrayElementAlign(Addr.getAlignment(), Idx, RowAlignedSize);
+    llvm::Value *EltPtr =
+        emitArraySubscriptGEP(*this, Int8Ty, Addr.emitRawPointer(*this),
+                              ScaledIdx, false, SignedIndices, E->getExprLoc());
----------------
s-perron wrote:

Doing an i8 gep does work for SPIR-V. We want to avoid that as much as possible. Can this be turned into a typed GEP with the padded type when needed? I tried writing it myself so I could make a suggestion, but I can't get it right.

See https://discourse.llvm.org/t/type-based-gep-plans/87183/14

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


More information about the llvm-commits mailing list