Skip to content

Commit

Permalink
mark dllimport/dllexport more accurately during codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Dec 2, 2015
1 parent bd2d0fe commit 240374c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
25 changes: 14 additions & 11 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

// utility procedures used in code generation

#if defined(USE_MCJIT) && defined(_OS_WINDOWS_)
template<class T> // for GlobalObject's
static T* addComdat(T *G)
{
if (imaging_mode && (!G->isDeclarationForLinker())) {
#if defined(_OS_WINDOWS_)
if (imaging_mode && !G->isDeclaration()) {
#ifdef LLVM35
// Add comdat information to make MSVC link.exe happy
Comdat *jl_Comdat = G->getParent()->getOrInsertComdat(G->getName());
jl_Comdat->setSelectionKind(Comdat::NoDuplicates);
G->setComdat(jl_Comdat);
// add __declspec(dllexport) to everything marked for export
if (G->getLinkage() == GlobalValue::ExternalLinkage)
G->setDLLStorageClass(GlobalValue::DLLExportStorageClass);
#endif
}
#endif
return G;
}
#else
template<class T>
static T* addComdat(T *G) { return G; }
#endif

static Instruction *tbaa_decorate(MDNode* md, Instruction* load_or_store)
{
Expand Down Expand Up @@ -67,9 +70,9 @@ static llvm::Value *prepare_call(llvm::Value* Callee)
}

#ifdef LLVM35
static inline void add_named_global(GlobalObject *gv, void *addr)
static inline void add_named_global(GlobalObject *gv, void *addr, bool dllimport = true)
#else
static inline void add_named_global(GlobalValue *gv, void *addr)
static inline void add_named_global(GlobalValue *gv, void *addr, bool dllimport = true)
#endif
{
#ifdef LLVM34
Expand All @@ -81,16 +84,16 @@ static inline void add_named_global(GlobalValue *gv, void *addr)

#ifdef _OS_WINDOWS_
// setting DLLEXPORT correctly only matters when building a binary
if (jl_generating_output()) {
if (dllimport && imaging_mode) {
assert(gv->getLinkage() == GlobalValue::ExternalLinkage);
#ifdef LLVM35
// add the __declspec(dllimport) attribute
gv->setDLLStorageClass(GlobalValue::DLLImportStorageClass);
// this will cause llvm to rename it, so we do the same
imp_name = Twine("__imp_", name).str();
name = StringRef(imp_name);
#else
if (gv->getLinkage() == GlobalValue::ExternalLinkage)
gv->setLinkage(GlobalValue::DLLImportLinkage);
gv->setLinkage(GlobalValue::DLLImportLinkage);
#endif
#if defined(_P64) || defined(LLVM35)
// __imp_ variables are indirection pointers, so use malloc to simulate that
Expand Down
17 changes: 9 additions & 8 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5955,29 +5955,29 @@ static void init_julia_llvm_env(Module *m)
add_named_global(jlenter_func, (void*)&jl_enter_handler);

#ifdef _OS_WINDOWS_
resetstkoflw_func = Function::Create(FunctionType::get(T_void, false),
resetstkoflw_func = Function::Create(FunctionType::get(T_int32, false),
Function::ExternalLinkage, "_resetstkoflw", m);
add_named_global(resetstkoflw_func, (void*)&_resetstkoflw);
#ifndef FORCE_ELF
#if defined(_CPU_X86_64_)
#if defined(_COMPILER_MINGW_)
Function *chkstk_func = Function::Create(FunctionType::get(T_void, false),
Function::ExternalLinkage, "___chkstk_ms", m);
add_named_global(chkstk_func, (void*)&___chkstk_ms);
add_named_global(chkstk_func, (void*)&___chkstk_ms, /*dllimport*/false);
#else
Function *chkstk_func = Function::Create(FunctionType::get(T_void, false),
Function::ExternalLinkage, "__chkstk", m);
add_named_global(chkstk_func, (void*)&__chkstk);
add_named_global(chkstk_func, (void*)&__chkstk, /*dllimport*/false);
#endif
#else
#if defined(_COMPILER_MINGW_)
Function *chkstk_func = Function::Create(FunctionType::get(T_void, false),
Function::ExternalLinkage, "_alloca", m);
add_named_global(chkstk_func, (void*)&_alloca);
add_named_global(chkstk_func, (void*)&_alloca, /*dllimport*/false);
#else
Function *chkstk_func = Function::Create(FunctionType::get(T_void, false),
Function::ExternalLinkage, "_chkstk", m);
add_named_global(chkstk_func, (void*)&_chkstk);
add_named_global(chkstk_func, (void*)&_chkstk, /*dllimport*/false);
#endif
#endif
#endif
Expand Down Expand Up @@ -6084,18 +6084,19 @@ static void init_julia_llvm_env(Module *m)
jlpowf_func = Function::Create(FunctionType::get(T_float32, powf_type, false),
Function::ExternalLinkage,
"powf", m);
add_named_global(jlpowf_func, (void*)&powf);
add_named_global(jlpowf_func, (void*)&powf, false);

Type *pow_type[2] = { T_float64, T_float64 };
jlpow_func = Function::Create(FunctionType::get(T_float64, pow_type, false),
Function::ExternalLinkage,
"pow", m);
add_named_global(jlpow_func,
#ifdef _COMPILER_MICROSOFT_
static_cast<double (*)(double, double)>(&pow));
static_cast<double (*)(double, double)>(&pow),
#else
(void*)&pow);
(void*)&pow,
#endif
false);
#endif

// set up optimization passes
Expand Down

0 comments on commit 240374c

Please sign in to comment.