[clang] [CIR][NFC] Upstream mem2reg.cir from ClangIR (PR #194517)

Akimasa Watanuki via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 27 19:58:11 PDT 2026


https://github.com/Men-cotton created https://github.com/llvm/llvm-project/pull/194517

Upstream `mem2reg.cir` from ClangIR.

Check that stack slots are promoted away after CFG flattening.

Partially addresses #156747.

>From 573902dd416175ed7b89478ab581913f1d90479b Mon Sep 17 00:00:00 2001
From: mencotton <mencotton0410 at gmail.com>
Date: Sun, 26 Apr 2026 01:47:43 +0900
Subject: [PATCH] [CIR][NFC] Upstream mem2reg.cir from ClangIR

Upstream `mem2reg.cir` from ClangIR.

Check that simple stack slots are promoted away after CFG flattening.

Partially addresses #156747.
---
 clang/test/CIR/Transforms/mem2reg.cir | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
 create mode 100644 clang/test/CIR/Transforms/mem2reg.cir

diff --git a/clang/test/CIR/Transforms/mem2reg.cir b/clang/test/CIR/Transforms/mem2reg.cir
new file mode 100644
index 0000000000000..85688d77e57ca
--- /dev/null
+++ b/clang/test/CIR/Transforms/mem2reg.cir
@@ -0,0 +1,24 @@
+// RUN: cir-opt %s -cir-flatten-cfg -mem2reg -o - | FileCheck %s \
+// RUN:   --implicit-check-not=cir.alloca \
+// RUN:   --implicit-check-not=cir.load \
+// RUN:   --implicit-check-not=cir.store
+
+!s32i = !cir.int<s, 32>
+
+module {
+  // Promote CIR stack slots through the generic mem2reg pass after CFG flattening.
+  cir.func @promote_simple_stack_slots() -> !s32i {
+    %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64}
+    %1 = cir.alloca !s32i, !cir.ptr<!s32i>, ["y", init] {alignment = 4 : i64}
+    %2 = cir.const #cir.int<42> : !s32i
+    cir.store %2, %1 : !s32i, !cir.ptr<!s32i>
+    %3 = cir.load %1 : !cir.ptr<!s32i>, !s32i
+    cir.store %3, %0 : !s32i, !cir.ptr<!s32i>
+    %4 = cir.load %0 : !cir.ptr<!s32i>, !s32i
+    cir.return %4 : !s32i
+  }
+
+  // CHECK-LABEL: cir.func @promote_simple_stack_slots
+  // CHECK: %[[CONST:.*]] = cir.const #cir.int<42>
+  // CHECK: cir.return %[[CONST]]
+}



More information about the cfe-commits mailing list