[clang] [CIR] Fold load from constant alloca slots (PR #212284)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 28 07:04:18 PDT 2026


================
@@ -32,6 +33,68 @@ namespace mlir {
 
 namespace {
 
+/// Find the `cir.store` operation that stores to the given alloca and dominates
+/// the given load operation. Dominance calculation is done through the given
+/// DominanceInfo object.
+///
+/// Return nullptr if no such store operation exists. If multiple store
+/// operations satisfy the criteria, any of them may be returned.
+cir::StoreOp findDominatingInitOp(cir::AllocaOp alloca, cir::LoadOp load,
+                                  const DominanceInfo &domInfo) {
+  // Walk through all uses of the alloca and visit the store operations that
+  // store to the alloca
+  for (const mlir::OpOperand &use : alloca->getUses()) {
+    auto store = mlir::dyn_cast<cir::StoreOp>(use.getOwner());
+    if (!store)
+      continue;
+
+    // `cir.store` has two operands, we're only interested if the store is
+    // storing into the alloca, not if the store is storing the address of the
+    // alloca slot into somewhere else
+    if (use.getOperandNumber() != cir::StoreOp::odsIndex_addr)
----------------
erichkeane wrote:

Ah, this comment is very helpful.  I would like to hear from others (as my experience in writing this sort of pass is non-existant), but this seems a little bit of an awkward way of checking this?  

Also, do we care if THIS is an atomic or volatile store? 

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


More information about the cfe-commits mailing list