Skip to content

Commit 4ec9508

Browse files
committed
Trying to fix invalid globals in codegen
1 parent ba26e82 commit 4ec9508

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/cgutils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ static Value *julia_pgv(jl_codectx_t &ctx, const char *cname, void *addr)
221221
if (gv->getParent() != M)
222222
gv = cast_or_null<GlobalVariable>(M->getNamedValue(localname));
223223
}
224+
assert(localname != "");
224225
if (gv == nullptr)
225226
gv = new GlobalVariable(*M, ctx.types().T_pjlvalue,
226227
false, GlobalVariable::PrivateLinkage,

src/codegen.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7735,9 +7735,30 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
77357735
for (const auto &F: Mod->functions())
77367736
if (!F.isDeclaration())
77377737
Exports.push_back(F.getName().str());
7738+
SmallVector<std::pair<std::string,GlobalVariable**>> Globals;
7739+
for (auto &pair : ctx.global_targets)
7740+
if (pair.second->getParent() == &*Mod)
7741+
Globals.push_back({std::string(pair.second->getName()), &pair.second});
77387742
jl_merge_module(jl_Module, std::move(Mod));
77397743
for (auto FN: Exports)
77407744
jl_Module->getFunction(FN)->setLinkage(GlobalVariable::InternalLinkage);
7745+
// Remove or refresh globals defined in `Mod`
7746+
auto &globals = ctx.global_targets;
7747+
for (auto it = globals.begin(); it != globals.end();) {
7748+
bool remove = false;
7749+
for (auto pair: Globals)
7750+
if (pair.second == &it->second) {
7751+
if (auto G = jl_Module->getNamedValue(pair.first))
7752+
it->second = cast<GlobalVariable>(G);
7753+
else
7754+
remove = true;
7755+
break;
7756+
}
7757+
if (remove)
7758+
it = globals.erase(it);
7759+
else
7760+
++it;
7761+
}
77417762
}
77427763

77437764
JL_GC_POP();

0 commit comments

Comments
 (0)