]> granicus.if.org Git - clang/commitdiff
[Linker] Provide callback for internalization
authorJonas Devlieghere <jonas@devlieghere.com>
Mon, 13 Mar 2017 18:08:11 +0000 (18:08 +0000)
committerJonas Devlieghere <jonas@devlieghere.com>
Mon, 13 Mar 2017 18:08:11 +0000 (18:08 +0000)
Differential Revision: https://reviews.llvm.org/D30738

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@297649 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/CodeGen/CodeGenAction.h
include/clang/Frontend/CodeGenOptions.h
lib/CodeGen/CodeGenAction.cpp
lib/Frontend/CompilerInvocation.cpp

index 65eda5bb98e258abd0e5e0a8f3672c69f60bd0a9..5a18a9de030b5c1c9d4d0d83aa056570029d500e 100644 (file)
@@ -36,6 +36,9 @@ private:
     /// function ourselves.
     bool PropagateAttrs;
 
+    /// If true, we use LLVM module internalizer.
+    bool Internalize;
+
     /// Bitwise combination of llvm::LinkerFlags used when we link the module.
     unsigned LinkFlags;
   };
index a21abac24b9f5be493d06da9602dba854d38bba5..f8f32666c5387cd0d178d3357f24f92dd6f74a32 100644 (file)
@@ -137,6 +137,8 @@ public:
     /// our CodeGenOptions, much as we set attrs on functions that we generate
     /// ourselves.
     bool PropagateAttrs = false;
+    /// If true, we use LLVM module internalizer.
+    bool Internalize = false;
     /// Bitwise combination of llvm::Linker::Flags, passed to the LLVM linker.
     unsigned LinkFlags = 0;
   };
index c76041456190b53683dbb2f7950f8a63f69c13d3..20a61f33f9445343a4c99ac85066b6704d5d35a9 100644 (file)
@@ -28,6 +28,7 @@
 #include "llvm/IR/DebugInfo.h"
 #include "llvm/IR/DiagnosticInfo.h"
 #include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IR/GlobalValue.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IRReader/IRReader.h"
@@ -38,6 +39,8 @@
 #include "llvm/Support/Timer.h"
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/YAMLTraits.h"
+#include "llvm/Transforms/IPO/Internalize.h"
+
 #include <memory>
 using namespace clang;
 using namespace llvm;
@@ -168,8 +171,22 @@ namespace clang {
             Gen->CGM().AddDefaultFnAttrs(F);
 
         CurLinkModule = LM.Module.get();
-        if (Linker::linkModules(*getModule(), std::move(LM.Module),
-                                LM.LinkFlags))
+
+        bool Err;
+        if (LM.Internalize) {
+          Err = Linker::linkModules(
+              *getModule(), std::move(LM.Module), LM.LinkFlags,
+              [](llvm::Module &M, const llvm::StringSet<> &GVS) {
+                internalizeModule(M, [&M, &GVS](const llvm::GlobalValue &GV) {
+                  return !GV.hasName() || (GVS.count(GV.getName()) == 0);
+                });
+              });
+        } else {
+          Err = Linker::linkModules(*getModule(), std::move(LM.Module),
+                                    LM.LinkFlags);
+        }
+
+        if (Err)
           return true;
       }
       return false; // success
@@ -319,7 +336,7 @@ namespace clang {
     void OptimizationFailureHandler(
         const llvm::DiagnosticInfoOptimizationFailure &D);
   };
-  
+
   void BackendConsumer::anchor() {}
 }
 
@@ -388,7 +405,7 @@ void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
   // code.
   if (LocCookie.isValid()) {
     Diags.Report(LocCookie, DiagID).AddString(Message);
-    
+
     if (D.getLoc().isValid()) {
       DiagnosticBuilder B = Diags.Report(Loc, diag::note_fe_inline_asm_here);
       // Convert the SMDiagnostic ranges into SourceRange and attach them
@@ -401,7 +418,7 @@ void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D,
     }
     return;
   }
-  
+
   // Otherwise, report the backend issue as occurring in the generated .s file.
   // If Loc is invalid, we still need to report the issue, it just gets no
   // location info.
@@ -815,8 +832,8 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
         LinkModules.clear();
         return nullptr;
       }
-      LinkModules.push_back(
-          {std::move(ModuleOrErr.get()), F.PropagateAttrs, F.LinkFlags});
+      LinkModules.push_back({std::move(ModuleOrErr.get()), F.PropagateAttrs,
+                             F.Internalize, F.LinkFlags});
     }
 
   CoverageSourceInfo *CoverageInfo = nullptr;
index 598af20864b9968bf98e343ad7a19e4ece662b71..23529749099d1fdb8c34ca62dcab94637970d401 100644 (file)
@@ -727,11 +727,11 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
     CodeGenOptions::BitcodeFileToLink F;
     F.Filename = A->getValue();
     if (A->getOption().matches(OPT_mlink_cuda_bitcode)) {
-      F.LinkFlags = llvm::Linker::Flags::LinkOnlyNeeded |
-                    llvm::Linker::Flags::InternalizeLinkedSymbols;
+      F.LinkFlags = llvm::Linker::Flags::LinkOnlyNeeded;
       // When linking CUDA bitcode, propagate function attributes so that
       // e.g. libdevice gets fast-math attrs if we're building with fast-math.
       F.PropagateAttrs = true;
+      F.Internalize = true;
     }
     Opts.LinkBitcodeFiles.push_back(F);
   }