[clang] [CIR] Upstream initial support for union type (PR #137501)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 28 10:53:13 PDT 2025
================
@@ -5,25 +5,146 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
// RUN: FileCheck --check-prefix=OGCG --input-file=%t.ll %s
+union U1 {
+ int n;
+ char c;
+};
+
+// CIR: !rec_U1 = !cir.record<union "U1" {!s32i, !s8i}>
+// LLVM: %union.U1 = type { i32 }
+// OGCG: %union.U1 = type { i32 }
+
+union U2 {
+ char b;
+ short s;
+ int i;
+ float f;
+ double d;
+};
+
+// CIR: !rec_U2 = !cir.record<union "U2" {!s8i, !s16i, !s32i, !cir.float, !cir.double}>
+// LLVM: %union.U2 = type { double }
+// OGCG: %union.U2 = type { double }
+
union IncompleteU *p;
-// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!rec_IncompleteU>
+// CIR: cir.global external @p = #cir.ptr<null> : !cir.ptr<!rec_IncompleteU>
// LLVM: @p = dso_local global ptr null
// OGCG: @p = global ptr null, align 8
-void f(void) {
+void f1(void) {
union IncompleteU *p;
}
-// CIR: cir.func @f()
-// CIR-NEXT: cir.alloca !cir.ptr<!rec_IncompleteU>, !cir.ptr<!cir.ptr<!rec_IncompleteU>>, ["p"]
-// CIR-NEXT: cir.return
+// CIR: cir.func @f1()
+// CIR-NEXT: cir.alloca !cir.ptr<!rec_IncompleteU>, !cir.ptr<!cir.ptr<!rec_IncompleteU>>, ["p"]
+// CIR-NEXT: cir.return
-// LLVM: define void @f()
+// LLVM: define void @f1()
// LLVM-NEXT: %[[P:.*]] = alloca ptr, i64 1, align 8
// LLVM-NEXT: ret void
-// OGCG: define{{.*}} void @f()
+// OGCG: define{{.*}} void @f1()
// OGCG-NEXT: entry:
// OGCG-NEXT: %[[P:.*]] = alloca ptr, align 8
// OGCG-NEXT: ret void
+
+int f2(void) {
+ union U1 u;
+ u.n = 42;
+ return u.n;
+}
+
+// CIR: cir.func @f2() -> !s32i
+// CIR-NEXT: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64}
----------------
andykaylor wrote:
Use pattern matching to avoid hard-coding the %0, %1, etc. names throughout these tests, or just drop them from the check if they aren't referenced by a later check.
https://github.com/llvm/llvm-project/pull/137501
More information about the cfe-commits
mailing list