[PATCH] [mips] Handle transparent unions correctly.

Daniel Sanders daniel.sanders at imgtec.com
Wed Jan 14 02:27:49 PST 2015


Hi atanasyan,

This fixes MultiSource/Applications/lemon on big-endian N32 by correcting the
handling of the argument to wait(). glibc defines it as a transparent union of
void* and int*. Such unions are passed according to the rules of the first
member so the argument must be passed as if it were a void* (sign extended from
i32 to i64) and not as a union (shifted to the upper bits of an i64).

wait() already behaves correctly on big-endian O32 and N64 since the union is
already the same size as an argument slot.

http://reviews.llvm.org/D6963

Files:
  lib/CodeGen/TargetInfo.cpp
  test/CodeGen/mips-transparent-union.c

Index: lib/CodeGen/TargetInfo.cpp
===================================================================
--- lib/CodeGen/TargetInfo.cpp
+++ lib/CodeGen/TargetInfo.cpp
@@ -5703,6 +5703,8 @@
   unsigned CurrOffset = llvm::RoundUpToAlignment(Offset, Align);
   Offset = CurrOffset + llvm::RoundUpToAlignment(TySize, Align * 8) / 8;
 
+  Ty = useFirstFieldIfTransparentUnion(Ty);
+
   if (isAggregateTypeForABI(Ty) || Ty->isVectorType()) {
     // Ignore empty aggregates.
     if (TySize == 0)
Index: test/CodeGen/mips-transparent-union.c
===================================================================
--- /dev/null
+++ test/CodeGen/mips-transparent-union.c
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple mips64-linux-gnu -S -o - -emit-llvm %s | FileCheck %s
+//
+// Transparent unions are passed according to the calling convention rules of
+// the first member. In this case, it is as if it were a void pointer so we
+// do not have the inreg attribute we would normally have for unions.
+//
+// This comes up in glibc's wait() function and matters for the big-endian N32
+// case where pointers are promoted to i64 and a non-transparent union would be
+// passed in the upper 32-bits of an i64.
+
+union either_pointer {
+  void *void_ptr;
+  int *int_ptr;
+} __attribute__((transparent_union));
+
+extern void foo(union either_pointer p);
+
+int data;
+
+void bar(void) {
+  return foo(&data);
+}
+
+// CHECK-LABEL: define void @bar()
+// CHECK:         call void @foo(i8* %{{[0-9]+}})
+
+// CHECK: declare void @foo(i8*)

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6963.18143.patch
Type: text/x-patch
Size: 1523 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150114/46888da1/attachment.bin>


More information about the cfe-commits mailing list