https://github.com/koachan created 
https://github.com/llvm/llvm-project/pull/90338

Make sure that empty structs are treated as if it has a size of one bit in 
function parameters and return types so that it occupies a full argument and/or 
return register slot.

This fixes crashes and miscompilations when passing and/or returning empty 
structs.

>From 5935e661941fd681b2bf6b3d915e97fe0d73fcd8 Mon Sep 17 00:00:00 2001
From: Koakuma <koac...@protonmail.com>
Date: Thu, 25 Apr 2024 22:37:03 +0700
Subject: [PATCH] [clang][SPARC] Treat empty structs as if it's a one-bit type
 in the CC

Make sure that empty structs are treated as if it has a size of
one bit in function parameters and return types so that it occupies
a full argument and/or return register slot.

This fixes crashes and miscompilations when passing and/or returning
empty structs.
---
 clang/lib/CodeGen/Targets/Sparc.cpp | 5 ++++-
 clang/test/CodeGen/sparcv9-abi.c    | 6 ++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/Targets/Sparc.cpp 
b/clang/lib/CodeGen/Targets/Sparc.cpp
index 9025a633f328e2..b82e9a69e19671 100644
--- a/clang/lib/CodeGen/Targets/Sparc.cpp
+++ b/clang/lib/CodeGen/Targets/Sparc.cpp
@@ -263,7 +263,10 @@ SparcV9ABIInfo::classifyType(QualType Ty, unsigned 
SizeLimit) const {
 
   CoerceBuilder CB(getVMContext(), getDataLayout());
   CB.addStruct(0, StrTy);
-  CB.pad(llvm::alignTo(CB.DL.getTypeSizeInBits(StrTy), 64));
+  // All structs, even empty ones, should take up a register argument slot,
+  // so pin the minimum struct size to one bit.
+  CB.pad(llvm::alignTo(
+      std::max(CB.DL.getTypeSizeInBits(StrTy).getKnownMinValue(), 1UL), 64));
 
   // Try to use the original type for coercion.
   llvm::Type *CoerceTy = CB.isUsableType(StrTy) ? StrTy : CB.getType();
diff --git a/clang/test/CodeGen/sparcv9-abi.c b/clang/test/CodeGen/sparcv9-abi.c
index 5e74a9a883cefa..360bd9d9019e56 100644
--- a/clang/test/CodeGen/sparcv9-abi.c
+++ b/clang/test/CodeGen/sparcv9-abi.c
@@ -21,6 +21,12 @@ char f_int_4(char x) { return x; }
 // CHECK-LABEL: define{{.*}} fp128 @f_ld(fp128 noundef %x)
 long double f_ld(long double x) { return x; }
 
+// Empty struct is lowered as a placeholder word parameter.
+struct empty {};
+
+// CHECK-LABEL: define{{.*}} i64 @f_empty(i64 %x.coerce)
+struct empty f_empty(struct empty x) { return x; }
+
 // Small structs are passed in registers.
 struct small {
   int *a, *b;

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to