[PATCH] D51637: [CODEGEN] GlobalAlias with non-zero size and public linkage ends up with wrong size, PR#38794

Daphne Pfister via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 4 08:25:30 PDT 2018


daphnediane created this revision.
daphnediane added reviewers: john.brawn, DmitryPolukhin.
Herald added subscribers: llvm-commits, hiraditya.

Fix PR#38794

The size of global aliases were not showing up correctly in object files. The updated logic checks if the alias is at an offset from the global object or has a size and that size is smaller than the global object. This expands the logic in r244752.


Repository:
  rL LLVM

https://reviews.llvm.org/D51637

Files:
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
  llvm/test/CodeGen/X86/global-alias.ll


Index: llvm/test/CodeGen/X86/global-alias.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/global-alias.ll
@@ -0,0 +1,16 @@
+; RUN: llc < %s -mtriple=i386-unknown-linux-gnu | FileCheck %s
+
+%global.type = type <{ [4 x i32] }>
+ at global = linkonce_odr global %global.type zeroinitializer, align 4
+ at my_alias = alias [2 x i32], bitcast (i32* getelementptr inbounds (i32, i32* bitcast (%global.type* @global to i32*), i32 2) to [2 x i32]*)
+ at my_alias2 = alias [4 x i32], bitcast (i32* getelementptr inbounds (i32, i32* bitcast (%global.type* @global to i32*), i32 2) to [4 x i32]*)
+ at my_alias3 = alias %global.type, %global.type* @global
+
+;CHECK: .weak	global
+;CHECK: .size	global, 16
+;CHECK: .set my_alias, global+8
+;CHECK: .size	my_alias, 8
+;CHECK: .set my_alias2, global+8
+;CHECK: .size	my_alias2, 16
+;CHECK: .set my_alias3, global
+;CHECK-NOT: .size	my_alias3
Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1327,16 +1327,22 @@
 
   if (auto *GA = dyn_cast<GlobalAlias>(&GIS)) {
     // If the aliasee does not correspond to a symbol in the output, i.e. the
-    // alias is not of an object or the aliased object is private, then set the
+    // alias is not of an object, the aliased object is private, the aliasee is
+    // offset from the object or is smaller then the base object, then set the
     // size of the alias symbol from the type of the alias. We don't do this in
     // other situations as the alias and aliasee having differing types but same
     // size may be intentional.
-    const GlobalObject *BaseObject = GA->getBaseObject();
-    if (MAI->hasDotTypeDotSizeDirective() && GA->getValueType()->isSized() &&
-        (!BaseObject || BaseObject->hasPrivateLinkage())) {
+    const GlobalObject *BaseObject =
+        dyn_cast<GlobalObject>(GA->stripPointerCasts());
+    if (MAI->hasDotTypeDotSizeDirective() && GA->getValueType()->isSized()) {
       const DataLayout &DL = M.getDataLayout();
       uint64_t Size = DL.getTypeAllocSize(GA->getValueType());
-      OutStreamer->emitELFSize(Name, MCConstantExpr::create(Size, OutContext));
+      if (!BaseObject || BaseObject->hasPrivateLinkage() ||
+          !BaseObject->getValueType()->isSized() ||
+          Size < DL.getTypeAllocSize(BaseObject->getValueType())) {
+        OutStreamer->emitELFSize(Name,
+                                 MCConstantExpr::create(Size, OutContext));
+      }
     }
   }
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D51637.163825.patch
Type: text/x-patch
Size: 2627 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180904/a545671a/attachment.bin>


More information about the llvm-commits mailing list