https://github.com/Men-cotton created
https://github.com/llvm/llvm-project/pull/172666
Handles empty unions (`cir.record<union {}>`) by generating empty LLVM structs.
This ensures successful lowering without triggering crashes in
`getLargestMember` due to missing members.
Added regression test in `clang/test/CIR/Lowering/union.cir`.
>From b19f14a588d3e5e7b47713c9081bd1b6ee2c9ba1 Mon Sep 17 00:00:00 2001
From: mencotton <[email protected]>
Date: Wed, 17 Dec 2025 23:03:09 +0900
Subject: [PATCH] [CIR] Handle empty unions in record lowering
---
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 2 ++
clang/test/CIR/Lowering/union.cir | 10 ++++++++++
2 files changed, 12 insertions(+)
create mode 100644 clang/test/CIR/Lowering/union.cir
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 7d854997848aa..06cce00a9aca4 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -2969,6 +2969,8 @@ static void prepareTypeConverter(mlir::LLVMTypeConverter
&converter,
break;
// Unions are lowered as only the largest member.
case cir::RecordType::Union:
+ if (type.getMembers().empty())
+ break;
if (auto largestMember = type.getLargestMember(dataLayout))
llvmMembers.push_back(
convertTypeForMemory(converter, dataLayout, largestMember));
diff --git a/clang/test/CIR/Lowering/union.cir
b/clang/test/CIR/Lowering/union.cir
new file mode 100644
index 0000000000000..c7bb8efccadcd
--- /dev/null
+++ b/clang/test/CIR/Lowering/union.cir
@@ -0,0 +1,10 @@
+// RUN: cir-opt %s --cir-to-llvm | FileCheck %s
+
+!u_empty = !cir.record<union {}>
+
+module {
+ cir.func @empty_union(%u: !u_empty) {
+ // CHECK-LABEL: llvm.func @empty_union
+ cir.return
+ }
+}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits