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(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) 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