Skip to content

Commit

Permalink
Make sure global constants are recognized as such in our own pass
Browse files Browse the repository at this point in the history
The metadata on the load may be striped by LLVM when moving code around
  • Loading branch information
yuyichao committed Aug 8, 2020
1 parent dffc889 commit e0bbce5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ static Value *julia_pgv(jl_codectx_t &ctx, const char *cname, void *addr)
gv = new GlobalVariable(*M, T_pjlvalue,
false, GlobalVariable::PrivateLinkage,
NULL, localname);
// LLVM passes sometimes strip metadata when moving load around
// since the load at the new location satisfy the same condition as the origional one.
// Mark the global as constant to LLVM code using our own metadata
// which is much less likely to be striped.
gv->setMetadata("julia.constgv", MDNode::get(gv->getContext(), None));
assert(localname == gv->getName());
assert(!gv->hasInitializer());
return gv;
Expand Down
11 changes: 6 additions & 5 deletions src/llvm-late-gc-lowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,11 +1124,12 @@ static bool isLoadFromConstGV(LoadInst *LI)
{
// We only emit single slot GV in codegen
// but LLVM global merging can change the pointer operands to GEPs/bitcasts
if (!isa<GlobalVariable>(LI->getPointerOperand()->stripInBoundsOffsets()))
return false;
MDNode *TBAA = LI->getMetadata(LLVMContext::MD_tbaa);
if (isTBAA(TBAA, {"jtbaa_const"}))
return true;
if (auto gv = dyn_cast<GlobalVariable>(LI->getPointerOperand()->stripInBoundsOffsets())) {
MDNode *TBAA = LI->getMetadata(LLVMContext::MD_tbaa);
if (isTBAA(TBAA, {"jtbaa_const"}) || gv->getMetadata("julia.constgv")) {
return true;
}
}
return false;
}

Expand Down

0 comments on commit e0bbce5

Please sign in to comment.