Skip to content

Commit ae31fac

Browse files
authored
Handle compiler.used (rust-lang#970)
1 parent 35d9f98 commit ae31fac

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

enzyme/Enzyme/PreserveNVVM.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -356,37 +356,39 @@ bool preserveNVVM(bool Begin, Function &F) {
356356
}
357357

358358
for (auto G : toErase) {
359-
if (auto V = F.getParent()->getGlobalVariable("llvm.used")) {
360-
auto C = cast<ConstantArray>(V->getInitializer());
361-
SmallVector<Constant *, 1> toKeep;
362-
bool found = false;
363-
for (unsigned i = 0; i < C->getNumOperands(); i++) {
364-
Value *Op = C->getOperand(i)->stripPointerCasts();
365-
if (Op == G)
366-
found = true;
367-
else
368-
toKeep.push_back(C->getOperand(i));
369-
}
370-
if (found) {
371-
if (toKeep.size()) {
372-
auto CA = ConstantArray::get(
373-
ArrayType::get(C->getType()->getElementType(), toKeep.size()),
374-
toKeep);
375-
GlobalVariable *NGV = new GlobalVariable(
376-
CA->getType(), V->isConstant(), V->getLinkage(), CA, "",
377-
V->getThreadLocalMode());
378-
V->getParent()->getGlobalList().insert(V->getIterator(), NGV);
379-
NGV->takeName(V);
359+
for (auto name : {"llvm.used", "llvm.compiler.used"}) {
360+
if (auto V = F.getParent()->getGlobalVariable(name)) {
361+
auto C = cast<ConstantArray>(V->getInitializer());
362+
SmallVector<Constant *, 1> toKeep;
363+
bool found = false;
364+
for (unsigned i = 0; i < C->getNumOperands(); i++) {
365+
Value *Op = C->getOperand(i)->stripPointerCasts();
366+
if (Op == G)
367+
found = true;
368+
else
369+
toKeep.push_back(C->getOperand(i));
370+
}
371+
if (found) {
372+
if (toKeep.size()) {
373+
auto CA = ConstantArray::get(
374+
ArrayType::get(C->getType()->getElementType(), toKeep.size()),
375+
toKeep);
376+
GlobalVariable *NGV = new GlobalVariable(
377+
CA->getType(), V->isConstant(), V->getLinkage(), CA, "",
378+
V->getThreadLocalMode());
379+
V->getParent()->getGlobalList().insert(V->getIterator(), NGV);
380+
NGV->takeName(V);
380381

381-
// Nuke the old list, replacing any uses with the new one.
382-
if (!V->use_empty()) {
383-
Constant *VV = NGV;
384-
if (VV->getType() != V->getType())
385-
VV = ConstantExpr::getBitCast(VV, V->getType());
386-
V->replaceAllUsesWith(VV);
382+
// Nuke the old list, replacing any uses with the new one.
383+
if (!V->use_empty()) {
384+
Constant *VV = NGV;
385+
if (VV->getType() != V->getType())
386+
VV = ConstantExpr::getBitCast(VV, V->getType());
387+
V->replaceAllUsesWith(VV);
388+
}
387389
}
390+
V->eraseFromParent();
388391
}
389-
V->eraseFromParent();
390392
}
391393
}
392394
changed = true;

0 commit comments

Comments
 (0)