summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorArtur Pilipenko <apilipenko@azulsystems.com>2016-06-24 15:10:29 +0000
committerArtur Pilipenko <apilipenko@azulsystems.com>2016-06-24 15:10:29 +0000
commit6c7a8abf5c78005bd329f389db8d36820bbee819 (patch)
tree7d5b2da07eb00fc9255b344c6fb41a95c8c60441 /llvm/lib
parentb68b82117ab6777074ab344574ccdc93e7a1b36c (diff)
downloadbcm5719-llvm-6c7a8abf5c78005bd329f389db8d36820bbee819.tar.gz
bcm5719-llvm-6c7a8abf5c78005bd329f389db8d36820bbee819.zip
Remangle intrinsics names when types are renamed
This is a resubmittion of previously reverted rL273568. 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: 273686
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/AsmParser/LLParser.cpp13
-rw-r--r--llvm/lib/Bitcode/Reader/BitcodeReader.cpp25
-rw-r--r--llvm/lib/IR/Function.cpp34
-rw-r--r--llvm/lib/Linker/IRMover.cpp9
4 files changed, 79 insertions, 2 deletions
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 691625209e4..bf4934c4f73 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -18,12 +18,14 @@
#include "llvm/AsmParser/SlotMapping.h"
#include "llvm/IR/AutoUpgrade.h"
#include "llvm/IR/CallingConv.h"
+#include "llvm/IR/CallSite.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
@@ -210,6 +212,17 @@ bool LLParser::ValidateEndOfModule() {
for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
UpgradeCallsToIntrinsic(&*FI++); // must be post-increment, as we remove
+ // 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.
+ for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) {
+ Function *F = &*FI++;
+ if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
+ F->replaceAllUsesWith(Remangled.getValue());
+ F->eraseFromParent();
+ }
+ }
+
UpgradeDebugInfo(*M);
UpgradeModuleFlags(*M);
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index a9c509809cb..cdb6a1f30bf 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -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);
diff --git a/llvm/lib/IR/Function.cpp b/llvm/lib/IR/Function.cpp
index 29ed68a2c5e..0da9b98258c 100644
--- a/llvm/lib/IR/Function.cpp
+++ b/llvm/lib/IR/Function.cpp
@@ -1080,6 +1080,40 @@ Intrinsic::matchIntrinsicVarArg(bool isVarArg,
return true;
}
+Optional<Function*> Intrinsic::remangleIntrinsicFunction(Function *F) {
+ Intrinsic::ID ID = F->getIntrinsicID();
+ if (!ID)
+ return None;
+
+ FunctionType *FTy = F->getFunctionType();
+ // Accumulate an array of overloaded types for the given intrinsic
+ SmallVector<Type *, 4> ArgTys;
+ {
+ SmallVector<Intrinsic::IITDescriptor, 8> Table;
+ getIntrinsicInfoTableEntries(ID, Table);
+ ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
+
+ // If we encounter any problems matching the signature with the descriptor
+ // just give up remangling. It's up to verifier to report the discrepancy.
+ if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
+ return None;
+ for (auto Ty : FTy->params())
+ if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
+ return None;
+ if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
+ return None;
+ }
+
+ StringRef Name = F->getName();
+ if (Name == Intrinsic::getName(ID, ArgTys))
+ return None;
+
+ auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
+ NewDecl->setCallingConv(F->getCallingConv());
+ assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
+ return NewDecl;
+}
+
/// hasAddressTaken - returns true if there are any uses of this function
/// other than direct calls or invokes to it.
bool Function::hasAddressTaken(const User* *PutOffender) const {
diff --git a/llvm/lib/Linker/IRMover.cpp b/llvm/lib/Linker/IRMover.cpp
index 7d09b7be868..c82fc8b3dff 100644
--- a/llvm/lib/Linker/IRMover.cpp
+++ b/llvm/lib/Linker/IRMover.cpp
@@ -16,6 +16,7 @@
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/GVMaterializer.h"
+#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/Support/Error.h"
#include "llvm/Transforms/Utils/Cloning.h"
@@ -901,6 +902,14 @@ Expected<Constant *> IRLinker::linkGlobalValueProto(GlobalValue *SGV,
if (ShouldLink || !ForAlias)
forceRenaming(NewGV, SGV->getName());
}
+
+ // Overloaded intrinsics have overloaded types names as part of their
+ // names. If we renamed overloaded types we should rename the intrinsic
+ // as well.
+ if (Function *F = dyn_cast<Function>(NewGV))
+ if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F))
+ NewGV = Remangled.getValue();
+
if (ShouldLink || ForAlias) {
if (const Comdat *SC = SGV->getComdat()) {
if (auto *GO = dyn_cast<GlobalObject>(NewGV)) {
OpenPOWER on IntegriCloud