llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Akimasa Watanuki (Men-cotton)
<details>
<summary>Changes</summary>
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`.
---
Full diff: https://github.com/llvm/llvm-project/pull/172666.diff
2 Files Affected:
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+2)
- (added) clang/test/CIR/Lowering/union.cir (+10)
``````````diff
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
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/172666
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits