[PATCH] D61670: [RFC] [MinGW] Allow opting out from .refptr stubs

Martin Storsjö via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 8 02:54:34 PDT 2019


mstorsjo created this revision.
mstorsjo added a reviewer: rnk.
Herald added a project: clang.

In GCC, the .refptr stubs are only generated for x86_64, and only for code models medium and larger (and medium is the default for x86_64 since this was introduced). They can be omitted (for projects that are conscious about performance and size, and don't need automatically importing dll data members), by passing -mcmodel=small.

Within LLVM, the default code model is small, but within clang we can distinguish between nothing being specified at all (where CodeModel is equal to "default") and when the small (and tiny) models have been explicitly requested.

I'm not very pleased with the form of it (in splitting hairs between default which implies small, and explicit small), but it has the upside of achieving the same effect when the same option is passed as to GCC. Is there some other option that would be more suitable for controlling it (with the downside of needing different options wrt to GCC)? Or perhaps it's not worth the mess at all? The actual savings from omitting it are miniscule.

For reference, the whole refptr business was introduced in GCC in this commit, https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=5496fac88af2009a2c4d5cfec0e722d5f962d80e, to fix this issue: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52790


Repository:
  rC Clang

https://reviews.llvm.org/D61670

Files:
  lib/CodeGen/CodeGenModule.cpp
  test/CodeGen/dso-local-executable.c


Index: test/CodeGen/dso-local-executable.c
===================================================================
--- test/CodeGen/dso-local-executable.c
+++ test/CodeGen/dso-local-executable.c
@@ -9,8 +9,10 @@
 // COFF-DAG: @import_var = external dllimport global i32
 // COFF-DAG: declare dllimport void @import_func()
 
-// RUN: %clang_cc1 -triple x86_64-w64-mingw32 -emit-llvm %s -o - | FileCheck -allow-deprecated-dag-overlap --check-prefix=MINGW %s
-// MINGW-DAG: @bar = external global i32
+// RUN: %clang_cc1 -triple x86_64-w64-mingw32 -emit-llvm %s -o - | FileCheck -allow-deprecated-dag-overlap --check-prefixes=MINGW,MINGW-DEFAULT %s
+// RUN: %clang_cc1 -triple x86_64-w64-mingw32 -mcode-model small -emit-llvm %s -o - | FileCheck -allow-deprecated-dag-overlap --check-prefixes=MINGW,MINGW-SMALL %s
+// MINGW-DEFAULT-DAG: @bar = external global i32
+// MINGW-SMALL-DAG: @bar = external dso_local global i32
 // MINGW-DAG: @weak_bar = extern_weak global i32
 // MINGW-DAG: declare dso_local void @foo()
 // MINGW-DAG: @baz = dso_local global i32 42
Index: lib/CodeGen/CodeGenModule.cpp
===================================================================
--- lib/CodeGen/CodeGenModule.cpp
+++ lib/CodeGen/CodeGenModule.cpp
@@ -755,12 +755,16 @@
     return false;
 
   const llvm::Triple &TT = CGM.getTriple();
+  const auto &CGOpts = CGM.getCodeGenOpts();
   if (TT.isWindowsGNUEnvironment()) {
     // In MinGW, variables without DLLImport can still be automatically
     // imported from a DLL by the linker; don't mark variables that
     // potentially could come from another DLL as DSO local.
+    // If the small or tiny code model has been explicitly requested,
+    // mark everything as DSO local.
     if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
-        !GV->isThreadLocal())
+        !GV->isThreadLocal() && CGOpts.CodeModel != "small" &&
+        CGOpts.CodeModel != "tiny")
       return false;
   }
 
@@ -783,7 +787,6 @@
     return false;
 
   // If this is not an executable, don't assume anything is local.
-  const auto &CGOpts = CGM.getCodeGenOpts();
   llvm::Reloc::Model RM = CGOpts.RelocationModel;
   const auto &LOpts = CGM.getLangOpts();
   if (RM != llvm::Reloc::Static && !LOpts.PIE)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61670.198602.patch
Type: text/x-patch
Size: 2245 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190508/cd7639e5/attachment.bin>


More information about the cfe-commits mailing list