https://github.com/bcardosolopes updated 
https://github.com/llvm/llvm-project/pull/244294

>From 2c2726c2c4f90d798fd8a1fd8c091f388d9b26ec Mon Sep 17 00:00:00 2001
From: Bruno Cardoso Lopes <[email protected]>
Date: Wed, 18 Nov 2026 05:02:31 -0800
Subject: [PATCH] [CIR] Fix mem2reg promotion of copies and unions, drop
 lifetime markers

Promoting a cir.copy could lose its effect in four ways; lifetime markers
blocked promotion of every local at -O1 and up.

* Volatile copies are not promoted, like volatile loads and stores.
* A copy that reaches the slot through a cast is no longer erased: it must
  name the slot directly.
* Copies of a slot holding a union keep it in memory: the typed load/store
  they become drops bytes another member keeps in the padding.
* The load/store a promoted copy becomes keeps the copy's alignment.
* cir.lifetime.start/end are erased with a promoted slot, as in LLVM.

Each fix has a test that fails without it. On SemaExpr.cpp at -O2, 5.6K
more allocas promote (62.6K -> 57.0K left); the union check keeps 33 in
memory.

Assisted-by: Claude Code (Opus 5.5).
---
 clang/include/clang/CIR/Dialect/IR/CIROps.td |   8 +-
 clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp   |  64 ++++++-
 clang/test/CIR/Transforms/mem2reg.cir        | 175 +++++++++++++++++++
 3 files changed, 241 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 87842e875e7874..b5c49a0cc7ab42 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -6105,7 +6105,9 @@ def CIR_StackRestoreOp : CIR_Op<"stackrestore"> {
 // LifetimeStartOp & LifetimeEndOp
 
//===----------------------------------------------------------------------===//
 
-def CIR_LifetimeStartOp : CIR_Op<"lifetime.start"> {
+def CIR_LifetimeStartOp : CIR_Op<"lifetime.start", [
+  DeclareOpInterfaceMethods<PromotableOpInterface>
+]> {
   let summary = "Marks the start of the lifetime of a variable produced by a 
cir.alloca operation";
   let description = [{
     The `cir.lifetime.start` operation marks the beginning of the lifetime
@@ -6145,7 +6147,9 @@ def CIR_LifetimeStartOp : CIR_Op<"lifetime.start"> {
   let llvmOp = "LifetimeStartOp";
 }
 
-def CIR_LifetimeEndOp : CIR_Op<"lifetime.end"> {
+def CIR_LifetimeEndOp : CIR_Op<"lifetime.end", [
+  DeclareOpInterfaceMethods<PromotableOpInterface>
+]> {
   let summary = "Marks the end of the lifetime of a variable produced by a 
cir.alloca operation";
   let description = [{
     The `cir.lifetime.end` operation marks the end of the lifetime of the
diff --git a/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp 
b/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp
index 0f5944c11730cb..8d897424c4c5b4 100644
--- a/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRMemorySlot.cpp
@@ -140,7 +140,12 @@ bool cir::CopyOp::storesTo(const MemorySlot &slot) {
 
 Value cir::CopyOp::getStored(const MemorySlot &slot, OpBuilder &builder,
                              Value reachingDef, const DataLayout &dataLayout) {
-  return cir::LoadOp::create(builder, getLoc(), slot.valueType, getSrc());
+  return cir::LoadOp::create(builder, getLoc(), slot.valueType, getSrc(),
+                             /*isDeref=*/false, /*is_volatile=*/false,
+                             /*is_nontemporal=*/false, getSrcAlignmentAttr(),
+                             /*sync_scope=*/cir::SyncScopeKindAttr(),
+                             /*mem_order=*/cir::MemOrderAttr(),
+                             /*invariant=*/false);
 }
 
 DeletionKind cir::CopyOp::removeBlockingUses(
@@ -150,18 +155,39 @@ DeletionKind cir::CopyOp::removeBlockingUses(
   if (loadsFrom(slot))
     cir::StoreOp::create(builder, getLoc(), reachingDefinition, getDst(),
                          /*is_volatile=*/false,
-                         /*is_nontemporal=*/false,
-                         /*alignment=*/mlir::IntegerAttr{},
+                         /*is_nontemporal=*/false, getDstAlignmentAttr(),
                          /*sync_scope=*/cir::SyncScopeKindAttr(),
                          /*mem-order=*/cir::MemOrderAttr());
   return DeletionKind::Delete;
 }
 
+// Whether the type holds a union by value; only structs and arrays can.
+static bool containsUnion(mlir::Type ty) {
+  if (mlir::isa<cir::UnionType>(ty))
+    return true;
+  if (auto structTy = mlir::dyn_cast<cir::StructType>(ty))
+    return llvm::any_of(structTy.getMembers(), containsUnion);
+  if (auto arrayTy = mlir::dyn_cast<cir::ArrayType>(ty))
+    return containsUnion(arrayTy.getElementType());
+  return false;
+}
+
 bool cir::CopyOp::canUsesBeRemoved(
     const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
     SmallVectorImpl<OpOperand *> &newBlockingUses,
     const DataLayout &dataLayout) {
-  if (getDst() == getSrc())
+  // loadsFrom/storesTo only recognize the slot pointer itself, so a copy
+  // through a cast would be erased without its effect.
+  if (blockingUses.size() != 1 || (*blockingUses.begin())->get() != slot.ptr)
+    return false;
+
+  // Volatile copies must stay in memory, like volatile loads and stores.
+  if (getIsVolatile())
+    return false;
+
+  // A union lowers to one member plus padding: a typed load or store of the
+  // value would drop bytes another member keeps in that padding.
+  if (containsUnion(slot.valueType))
     return false;
 
   return getCopySizeInBytes(dataLayout) ==
@@ -354,3 +380,33 @@ Value cir::TernaryOp::finalizePromotion(
   assert(!hasValueStores && "cir.ternary cannot yield a new definition");
   return reachingDef;
 }
+
+//===----------------------------------------------------------------------===//
+// Interfaces for LifetimeStartOp and LifetimeEndOp
+//===----------------------------------------------------------------------===//
+
+// A promoted slot is no longer on the stack, so its lifetime markers go away
+// with it.
+bool cir::LifetimeStartOp::canUsesBeRemoved(
+    const SmallPtrSetImpl<OpOperand *> &blockingUses,
+    SmallVectorImpl<OpOperand *> &newBlockingUses,
+    const DataLayout &dataLayout) {
+  return true;
+}
+
+DeletionKind cir::LifetimeStartOp::removeBlockingUses(
+    const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
+  return DeletionKind::Delete;
+}
+
+bool cir::LifetimeEndOp::canUsesBeRemoved(
+    const SmallPtrSetImpl<OpOperand *> &blockingUses,
+    SmallVectorImpl<OpOperand *> &newBlockingUses,
+    const DataLayout &dataLayout) {
+  return true;
+}
+
+DeletionKind cir::LifetimeEndOp::removeBlockingUses(
+    const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
+  return DeletionKind::Delete;
+}
diff --git a/clang/test/CIR/Transforms/mem2reg.cir 
b/clang/test/CIR/Transforms/mem2reg.cir
index 3162a3f5e09a5d..bc882c810f3437 100644
--- a/clang/test/CIR/Transforms/mem2reg.cir
+++ b/clang/test/CIR/Transforms/mem2reg.cir
@@ -5,6 +5,11 @@
 !u16i = !cir.int<u, 16>
 !rec_Whole = !cir.struct<"Whole" {data !u16i, data !u16i}>
 !rec_AllPad = !cir.struct<"AllPad" {pad !u8i}>
+!rec_A = !cir.struct<"A" {data !s32i}>
+!rec_B = !cir.struct<"B" {data !s32i}>
+!rec_U = !cir.union<"U" {data !s32i, data !u8i}>
+!rec_HasU = !cir.struct<"HasU" {data !s32i, data !rec_U}>
+!rec_HasUPtr = !cir.struct<"HasUPtr" {data !s32i, data !cir.ptr<!rec_U>}>
 
 module {
   // Promote CIR stack slots through the generic mem2reg pass after CFG 
flattening.
@@ -94,4 +99,174 @@ module {
     %v = cir.load %slot : !cir.ptr<!rec_AllPad>, !rec_AllPad
     cir.return %v : !rec_AllPad
   }
+
+  // CHECK-LABEL: cir.func @do_not_promote_copy_to_bitcast_slot
+  cir.func @do_not_promote_copy_to_bitcast_slot(%src: !cir.ptr<!rec_B>) -> 
!rec_A {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    // CHECK: %[[BC:.*]] = cir.cast bitcast %[[SLOT]] : !cir.ptr<!rec_A> -> 
!cir.ptr<!rec_B>
+    // CHECK: cir.copy %{{.+}} to %[[BC]] : !cir.ptr<!rec_B>
+    // CHECK: cir.load %[[SLOT]] : !cir.ptr<!rec_A>, !rec_A
+    %slot = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    %bc = cir.cast bitcast %slot : !cir.ptr<!rec_A> -> !cir.ptr<!rec_B>
+    cir.copy %src to %bc : !cir.ptr<!rec_B>
+    %v = cir.load %slot : !cir.ptr<!rec_A>, !rec_A
+    cir.return %v : !rec_A
+  }
+
+  // CHECK-LABEL: cir.func @do_not_promote_copy_from_bitcast_slot
+  cir.func @do_not_promote_copy_from_bitcast_slot(%v: !rec_A, %dst: 
!cir.ptr<!rec_B>) {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    // CHECK: cir.store %{{.+}}, %[[SLOT]] : !rec_A, !cir.ptr<!rec_A>
+    // CHECK: %[[BC:.*]] = cir.cast bitcast %[[SLOT]] : !cir.ptr<!rec_A> -> 
!cir.ptr<!rec_B>
+    // CHECK: cir.copy %[[BC]] to %{{.+}} : !cir.ptr<!rec_B>
+    %slot = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    cir.store %v, %slot : !rec_A, !cir.ptr<!rec_A>
+    %bc = cir.cast bitcast %slot : !cir.ptr<!rec_A> -> !cir.ptr<!rec_B>
+    cir.copy %bc to %dst : !cir.ptr<!rec_B>
+    cir.return
+  }
+
+  // Promoting the bitcast copy would compute the copied value in ^store but
+  // read the slot's default at ^merge.
+  // CHECK-LABEL: cir.func @do_not_promote_bitcast_copy_diamond
+  cir.func @do_not_promote_bitcast_copy_diamond(%cond: !cir.bool, %src: 
!cir.ptr<!rec_B>) -> !rec_A {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    // CHECK: %[[BC:.*]] = cir.cast bitcast %[[SLOT]] : !cir.ptr<!rec_A> -> 
!cir.ptr<!rec_B>
+    // CHECK: cir.copy %{{.+}} to %[[BC]] : !cir.ptr<!rec_B>
+    // CHECK: cir.load %[[SLOT]] : !cir.ptr<!rec_A>, !rec_A
+    %slot = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    cir.brcond %cond ^store, ^skip
+  ^store:
+    %bc = cir.cast bitcast %slot : !cir.ptr<!rec_A> -> !cir.ptr<!rec_B>
+    cir.copy %src to %bc : !cir.ptr<!rec_B>
+    cir.br ^merge
+  ^skip:
+    cir.br ^merge
+  ^merge:
+    %v = cir.load %slot : !cir.ptr<!rec_A>, !rec_A
+    cir.return %v : !rec_A
+  }
+
+  // CHECK-LABEL: cir.func @do_not_promote_self_copy
+  cir.func @do_not_promote_self_copy(%v: !rec_A) -> !rec_A {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    // CHECK: cir.copy %[[SLOT]] to %[[SLOT]] : !cir.ptr<!rec_A>
+    %slot = cir.alloca "s" align(4) init : !cir.ptr<!rec_A>
+    cir.store %v, %slot : !rec_A, !cir.ptr<!rec_A>
+    cir.copy %slot to %slot : !cir.ptr<!rec_A>
+    %r = cir.load %slot : !cir.ptr<!rec_A>, !rec_A
+    cir.return %r : !rec_A
+  }
+
+  // CHECK-LABEL: cir.func @do_not_promote_volatile_copy
+  cir.func @do_not_promote_volatile_copy(%src: !cir.ptr<!rec_Whole>) -> 
!rec_Whole {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "s" align(2) init : 
!cir.ptr<!rec_Whole>
+    // CHECK: cir.copy %{{.+}} to %[[SLOT]] volatile : !cir.ptr<!rec_Whole>
+    // CHECK: cir.load %[[SLOT]] : !cir.ptr<!rec_Whole>, !rec_Whole
+    %slot = cir.alloca "s" align(2) init : !cir.ptr<!rec_Whole>
+    cir.copy %src to %slot volatile : !cir.ptr<!rec_Whole>
+    %v = cir.load %slot : !cir.ptr<!rec_Whole>, !rec_Whole
+    cir.return %v : !rec_Whole
+  }
+
+  // CHECK-LABEL: cir.func @promote_slot_with_lifetime_markers
+  cir.func @promote_slot_with_lifetime_markers() -> !s32i {
+    // CHECK-NOT: cir.alloca
+    // CHECK-NOT: cir.lifetime
+    // CHECK: %[[CONST:.*]] = cir.const #cir.int<42> : !s32i
+    // CHECK-NOT: cir.lifetime
+    // CHECK: cir.return %[[CONST]] : !s32i
+    %slot = cir.alloca "s" align(4) : !cir.ptr<!s32i>
+    cir.lifetime.start %slot : !cir.ptr<!s32i>
+    %c = cir.const #cir.int<42> : !s32i
+    cir.store %c, %slot : !s32i, !cir.ptr<!s32i>
+    %v = cir.load %slot : !cir.ptr<!s32i>, !s32i
+    cir.lifetime.end %slot : !cir.ptr<!s32i>
+    cir.return %v : !s32i
+  }
+
+  // Lifetime markers stay when something else keeps the slot in memory.
+  // CHECK-LABEL: cir.func @keep_lifetime_markers_of_unpromoted_slot
+  cir.func @keep_lifetime_markers_of_unpromoted_slot() -> !s32i {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "s" align(4) : !cir.ptr<!s32i>
+    // CHECK: cir.lifetime.start %[[SLOT]] : !cir.ptr<!s32i>
+    // CHECK: cir.load volatile %[[SLOT]] : !cir.ptr<!s32i>, !s32i
+    // CHECK: cir.lifetime.end %[[SLOT]] : !cir.ptr<!s32i>
+    %slot = cir.alloca "s" align(4) : !cir.ptr<!s32i>
+    cir.lifetime.start %slot : !cir.ptr<!s32i>
+    %c = cir.const #cir.int<42> : !s32i
+    cir.store %c, %slot : !s32i, !cir.ptr<!s32i>
+    %v = cir.load volatile %slot : !cir.ptr<!s32i>, !s32i
+    cir.lifetime.end %slot : !cir.ptr<!s32i>
+    cir.return %v : !s32i
+  }
+
+  // CHECK-LABEL: cir.func @do_not_promote_union_slot
+  cir.func @do_not_promote_union_slot(%src: !cir.ptr<!rec_U>) -> !rec_U {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "u" align(4) init : !cir.ptr<!rec_U>
+    // CHECK: cir.copy %{{.+}} to %[[SLOT]] : !cir.ptr<!rec_U>
+    // CHECK: cir.load %[[SLOT]] : !cir.ptr<!rec_U>, !rec_U
+    %slot = cir.alloca "u" align(4) init : !cir.ptr<!rec_U>
+    cir.copy %src to %slot : !cir.ptr<!rec_U>
+    %v = cir.load %slot : !cir.ptr<!rec_U>, !rec_U
+    cir.return %v : !rec_U
+  }
+
+  // CHECK-LABEL: cir.func @do_not_promote_struct_with_union_member
+  cir.func @do_not_promote_struct_with_union_member(%src: !cir.ptr<!rec_HasU>) 
-> !rec_HasU {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "s" align(4) init : !cir.ptr<!rec_HasU>
+    // CHECK: cir.copy %{{.+}} to %[[SLOT]] : !cir.ptr<!rec_HasU>
+    // CHECK: cir.load %[[SLOT]] : !cir.ptr<!rec_HasU>, !rec_HasU
+    %slot = cir.alloca "s" align(4) init : !cir.ptr<!rec_HasU>
+    cir.copy %src to %slot : !cir.ptr<!rec_HasU>
+    %v = cir.load %slot : !cir.ptr<!rec_HasU>, !rec_HasU
+    cir.return %v : !rec_HasU
+  }
+
+  // CHECK-LABEL: cir.func @do_not_promote_union_array
+  cir.func @do_not_promote_union_array(%src: !cir.ptr<!cir.array<!rec_U x 2>>) 
-> !cir.array<!rec_U x 2> {
+    // CHECK: %[[SLOT:.*]] = cir.alloca "a" align(4) init : 
!cir.ptr<!cir.array<!rec_U x 2>>
+    // CHECK: cir.copy %{{.+}} to %[[SLOT]] : !cir.ptr<!cir.array<!rec_U x 2>>
+    // CHECK: cir.load %[[SLOT]] : !cir.ptr<!cir.array<!rec_U x 2>>, 
!cir.array<!rec_U x 2>
+    %slot = cir.alloca "a" align(4) init : !cir.ptr<!cir.array<!rec_U x 2>>
+    cir.copy %src to %slot : !cir.ptr<!cir.array<!rec_U x 2>>
+    %v = cir.load %slot : !cir.ptr<!cir.array<!rec_U x 2>>, !cir.array<!rec_U 
x 2>
+    cir.return %v : !cir.array<!rec_U x 2>
+  }
+
+  // Typed loads and stores of a union are not made lossier by promotion.
+  // CHECK-LABEL: cir.func @promote_union_slot_without_copies
+  // CHECK-SAME: (%[[V:[^:]+]]: !rec_U)
+  cir.func @promote_union_slot_without_copies(%v: !rec_U) -> !rec_U {
+    // CHECK-NEXT: cir.return %[[V]] : !rec_U
+    %slot = cir.alloca "u" align(4) init : !cir.ptr<!rec_U>
+    cir.store %v, %slot : !rec_U, !cir.ptr<!rec_U>
+    %r = cir.load %slot : !cir.ptr<!rec_U>, !rec_U
+    cir.return %r : !rec_U
+  }
+
+  // A pointer to a union holds no union bytes, so the slot still promotes.
+  // CHECK-LABEL: cir.func @promote_struct_with_union_pointer
+  cir.func @promote_struct_with_union_pointer(%src: !cir.ptr<!rec_HasUPtr>) -> 
!rec_HasUPtr {
+    // CHECK-NOT: cir.alloca
+    // CHECK-NOT: cir.copy
+    // CHECK: %[[V:.*]] = cir.load %{{.+}} : !cir.ptr<!rec_HasUPtr>, 
!rec_HasUPtr
+    // CHECK: cir.return %[[V]] : !rec_HasUPtr
+    %slot = cir.alloca "s" align(8) init : !cir.ptr<!rec_HasUPtr>
+    cir.copy %src to %slot : !cir.ptr<!rec_HasUPtr>
+    %v = cir.load %slot : !cir.ptr<!rec_HasUPtr>, !rec_HasUPtr
+    cir.return %v : !rec_HasUPtr
+  }
+
+  // Promoted copies keep the alignment of the side that stays in memory.
+  // CHECK-LABEL: cir.func @promote_copies_keep_alignment
+  cir.func @promote_copies_keep_alignment(%src: !cir.ptr<!rec_Whole>, %dst: 
!cir.ptr<!rec_Whole>) {
+    // CHECK-NOT: cir.alloca
+    // CHECK: %[[V:.*]] = cir.load align(1) %{{.+}} : !cir.ptr<!rec_Whole>, 
!rec_Whole
+    // CHECK: cir.store align(1) %[[V]], %{{.+}} : !rec_Whole, 
!cir.ptr<!rec_Whole>
+    %slot = cir.alloca "s" align(2) init : !cir.ptr<!rec_Whole>
+    cir.copy %src align(1) to %slot align(2) : !cir.ptr<!rec_Whole>
+    cir.copy %slot align(2) to %dst align(1) : !cir.ptr<!rec_Whole>
+    cir.return
+  }
 }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to