[llvm] e28435c - [ThinLTO] Copy UnnamedAddr when spliting module.

Zequan Wu via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 19 14:05:14 PDT 2021


Author: Zequan Wu
Date: 2021-04-19T14:04:58-07:00
New Revision: e28435caf63b1d2437538c2b581bf005d3c57fb2

URL: https://github.com/llvm/llvm-project/commit/e28435caf63b1d2437538c2b581bf005d3c57fb2
DIFF: https://github.com/llvm/llvm-project/commit/e28435caf63b1d2437538c2b581bf005d3c57fb2.diff

LOG: [ThinLTO] Copy UnnamedAddr when spliting module.

The unnamedaddr property of a function is lost when using
`-fwhole-program-vtables` and thinlto which causes size increase under linker's
safe icf mode.

The size increase of chrome on Linux when switching from all icf to safe icf
drops from 5 MB to 3 MB after this change, and from 6 MB to 4 MB on Windows.

There is a repro:
```
# a.h
struct A {
  virtual int f();
  virtual int g();
};

# a.cpp
#include "a.h"
int A::f() { return 10; }
int A::g() { return 10; }

# main.cpp
#include "a.h"

int g(A* a) {
  return a->f();
}

int main(int argv, char** args) {
  A a;
  return g(&a);
}

$ clang++ -O2 -ffunction-sections -flto=thin -fwhole-program-vtables -fsplit-lto-unit -c main.cpp -o main.o  && clang++ -Wl,--icf=safe -fuse-ld=lld  -flto=thin main.o -o a.out && llvm-readobj -t a.out | grep -A 1 -e _ZN1A1fEv -e _ZN1A1gEv
    Name: _ZN1A1fEv (480)
    Value: 0x201830
--
    Name: _ZN1A1gEv (490)
    Value: 0x201840
```

Differential Revision: https://reviews.llvm.org/D100498

Added: 
    

Modified: 
    llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
    llvm/test/Transforms/ThinLTOBitcodeWriter/split-vfunc.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
index 797416f5e13aa..37329b489555e 100644
--- a/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ b/llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -158,7 +158,11 @@ void simplifyExternals(Module &M) {
     Function *NewF =
         Function::Create(EmptyFT, GlobalValue::ExternalLinkage,
                          F.getAddressSpace(), "", &M);
-    NewF->setVisibility(F.getVisibility());
+    NewF->copyAttributesFrom(&F);
+    // Only copy function attribtues.
+    NewF->setAttributes(
+        AttributeList::get(M.getContext(), AttributeList::FunctionIndex,
+                           F.getAttributes().getFnAttributes()));
     NewF->takeName(&F);
     F.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, F.getType()));
     F.eraseFromParent();

diff  --git a/llvm/test/Transforms/ThinLTOBitcodeWriter/split-vfunc.ll b/llvm/test/Transforms/ThinLTOBitcodeWriter/split-vfunc.ll
index fcf575188f791..f772ee235e661 100644
--- a/llvm/test/Transforms/ThinLTOBitcodeWriter/split-vfunc.ll
+++ b/llvm/test/Transforms/ThinLTOBitcodeWriter/split-vfunc.ll
@@ -2,9 +2,9 @@
 ; RUN: llvm-modextract -b -n 0 -o - %t | llvm-dis | FileCheck --check-prefix=M0 %s
 ; RUN: llvm-modextract -b -n 1 -o - %t | llvm-dis | FileCheck --check-prefix=M1 %s
 
-; M0: @g = external constant [9 x i8*]{{$}}
-; M1: @g = constant [9 x i8*]
- at g = constant [9 x i8*] [
+; M0: @g = external constant [10 x i8*]{{$}}
+; M1: @g = constant [10 x i8*]
+ at g = constant [10 x i8*] [
   i8* bitcast (i64 (i8*)* @ok1 to i8*),
   i8* bitcast (i64 (i8*, i64)* @ok2 to i8*),
   i8* bitcast (void (i8*)* @wrongtype1 to i8*),
@@ -13,7 +13,8 @@
   i8* bitcast (i64 (i8*, i8*)* @wrongtype4 to i8*),
   i8* bitcast (i64 (i8*, i128)* @wrongtype5 to i8*),
   i8* bitcast (i64 (i8*)* @usesthis to i8*),
-  i8* bitcast (i8 (i8*)* @reads to i8*)
+  i8* bitcast (i8 (i8*)* @reads to i8*),
+  i8* bitcast (i8* (i8*, i8)* @attributedFunc to i8*)
 ], !type !0
 
 ; M0: define i64 @ok1
@@ -76,4 +77,11 @@ define i8 @reads(i8* %this) {
   ret i8 %l
 }
 
+; Check function attributes are copied over splitted module
+; M0: declare dso_local noundef i8* @attributedFunc(i8* noalias, i8 zeroext) unnamed_addr #[[ATTR0:[0-9]+]]
+; M1: declare dso_local void @attributedFunc() unnamed_addr #[[ATTR1:[0-9]+]]
+declare dso_local noundef i8* @attributedFunc(i8* noalias, i8 zeroext) unnamed_addr alwaysinline willreturn
+; M0: attributes #[[ATTR0]] = { alwaysinline willreturn }
+; M1: attributes #[[ATTR1]] = { alwaysinline willreturn }
+
 !0 = !{i32 0, !"typeid"}


        


More information about the llvm-commits mailing list