[clang] [CIR] Add transform test for cir-flatten-cfg (PR #130861)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 07:45:04 PDT 2025


================
@@ -0,0 +1,58 @@
+// RUN: cir-opt %s -cir-flatten-cfg -o - | FileCheck %s
+
+module {
+  cir.func @foo() {
+    cir.scope {
+      %0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["a", init] {alignment = 4 : i64}
+      %1 = cir.const #cir.int<4> : !cir.int<u, 32>
+      cir.store %1, %0 : !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>
+    }
+    cir.return
+  }
+// CHECK:  cir.func @foo() {
+// CHECK:    cir.br ^bb1
+// CHECK:  ^bb1:  // pred: ^bb0
+// CHECK:    %0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["a", init] {alignment = 4 : i64}
+// CHECK:    %1 = cir.const #cir.int<4> : !cir.int<u, 32>
+// CHECK:    cir.store %1, %0 : !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>
+// CHECK:    cir.br ^bb2
+// CHECK:  ^bb2:  // pred: ^bb1
+// CHECK:    cir.return
+// CHECK:  }
+
+  // Should drop empty scopes.
+  cir.func @empty_scope() {
+    cir.scope {
+    }
+    cir.return
+  }
+// CHECK:  cir.func @empty_scope() {
+// CHECK:    cir.return
+// CHECK:  }
+
+  cir.func @scope_with_return() -> !cir.int<u, 32> {
+    %0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["__retval"] {alignment = 4 : i64}
+    cir.scope {
+      %2 = cir.const #cir.int<0> : !cir.int<u, 32>
+      cir.store %2, %0 : !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>
+      %3 = cir.load %0 : !cir.ptr<!cir.int<u, 32>>, !cir.int<u, 32>
+      cir.return %3 : !cir.int<u, 32>
+    }
+    %1 = cir.load %0 : !cir.ptr<!cir.int<u, 32>>, !cir.int<u, 32>
+    cir.return %1 : !cir.int<u, 32>
+  }
+
+// CHECK:  cir.func @scope_with_return() -> !cir.int<u, 32> {
+// CHECK:    %0 = cir.alloca !cir.int<u, 32>, !cir.ptr<!cir.int<u, 32>>, ["__retval"] {alignment = 4 : i64}
+// CHECK:    cir.br ^bb1
----------------
erichkeane wrote:

This is another block that it seems to me could be removed/merged with `bb1`.  

I guess there are later passes that do these sorts of merges?  Also, removing `bb2` (which is an obviously `dead` block).

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


More information about the cfe-commits mailing list