Remangle intrinsics names when types are renamed

This is a fix for the problem mentioned in "LTO and intrinsics mangling" llvm-dev mail thread:
http://lists.llvm.org/pipermail/llvm-dev/2016-April/098387.html

Reviewers: mehdi_amini, reames

Differential Revision: http://reviews.llvm.org/D19373

llvm-svn: 273568
This commit is contained in:
Artur Pilipenko
2016-06-23 15:25:09 +00:00
parent b1a2b5a708
commit f0c9f81379
8 changed files with 163 additions and 2 deletions

View File

@@ -15,6 +15,7 @@
#include "llvm/Bitcode/LLVMBitCodes.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/IR/AutoUpgrade.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
@@ -230,8 +231,10 @@ class BitcodeReader : public GVMaterializer {
// When intrinsic functions are encountered which require upgrading they are
// stored here with their replacement function.
typedef DenseMap<Function*, Function*> UpgradedIntrinsicMap;
UpgradedIntrinsicMap UpgradedIntrinsics;
typedef DenseMap<Function*, Function*> UpdatedIntrinsicMap;
UpdatedIntrinsicMap UpgradedIntrinsics;
// Intrinsics which were remangled because of types rename
UpdatedIntrinsicMap RemangledIntrinsics;
// Map the bitcode's custom MDKind ID to the Module's MDKind ID.
DenseMap<unsigned, unsigned> MDKindMap;
@@ -3425,6 +3428,11 @@ std::error_code BitcodeReader::globalCleanup() {
Function *NewFn;
if (UpgradeIntrinsicFunction(&F, NewFn))
UpgradedIntrinsics[&F] = NewFn;
else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F))
// Some types could be renamed during loading if several modules are
// loaded in the same LLVMContext (LTO scenario). In this case we should
// remangle intrinsics names as well.
RemangledIntrinsics[&F] = Remangled.getValue();
}
// Look for global variables which need to be renamed.
@@ -5601,6 +5609,13 @@ std::error_code BitcodeReader::materialize(GlobalValue *GV) {
}
}
// Update calls to the remangled intrinsics
for (auto &I : RemangledIntrinsics)
for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
UI != UE;)
// Don't expect any other users than call sites
CallSite(*UI++).setCalledFunction(I.second);
// Finish fn->subprogram upgrade for materialized functions.
if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
F->setSubprogram(SP);
@@ -5654,6 +5669,12 @@ std::error_code BitcodeReader::materializeModule() {
I.first->eraseFromParent();
}
UpgradedIntrinsics.clear();
// Do the same for remangled intrinsics
for (auto &I : RemangledIntrinsics) {
I.first->replaceAllUsesWith(I.second);
I.first->eraseFromParent();
}
RemangledIntrinsics.clear();
UpgradeDebugInfo(*TheModule);