<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Apr 12, 2016 at 2:35 PM, Mehdi Amini via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Author: mehdi_amini<br>
Date: Tue Apr 12 16:35:28 2016<br>
New Revision: 266132<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=266132&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=266132&view=rev</a><br>
Log:<br>
Add a pass to name anonymous/nameless function<br>
<br>
Summary:<br>
For correct handling of alias to nameless<br>
function, we need to be able to refer them through a GUID in the summary.<br>
Here we name them using a hash of the non-private global names in the module.<br>
<br>
Reviewers: tejohnson<br>
<br>
Subscribers: joker.eph, llvm-commits<br>
<br>
Differential Revision: <a href="http://reviews.llvm.org/D18883" rel="noreferrer" target="_blank">http://reviews.llvm.org/D18883</a><br>
<br>
From: Mehdi Amini <<a href="mailto:mehdi.amini@apple.com">mehdi.amini@apple.com</a>><br>
<br>
Added:<br>
llvm/trunk/lib/Transforms/Utils/NameAnonFunctions.cpp<br>
llvm/trunk/test/Transforms/NameAnonFunctions/<br>
llvm/trunk/test/Transforms/NameAnonFunctions/rename.ll<br>
Modified:<br>
llvm/trunk/include/llvm/InitializePasses.h<br>
llvm/trunk/include/llvm/Transforms/Scalar.h<br>
llvm/trunk/include/llvm/Transforms/Utils/ModuleUtils.h<br>
llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
llvm/trunk/lib/Transforms/Utils/CMakeLists.txt<br>
llvm/trunk/lib/Transforms/Utils/Utils.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/InitializePasses.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=266132&r1=266131&r2=266132&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/InitializePasses.h?rev=266132&r1=266131&r2=266132&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/InitializePasses.h (original)<br>
+++ llvm/trunk/include/llvm/InitializePasses.h Tue Apr 12 16:35:28 2016<br>
@@ -221,6 +221,7 @@ void initializeMergedLoadStoreMotionPass<br>
void initializeMetaRenamerPass(PassRegistry&);<br>
void initializeMergeFunctionsPass(PassRegistry&);<br>
void initializeModuleDebugInfoPrinterPass(PassRegistry&);<br>
+void initializeNameAnonFunctionPass(PassRegistry &);<br>
void initializeNaryReassociatePass(PassRegistry&);<br>
void initializeNoAAPass(PassRegistry&);<br>
void initializeObjCARCAAWrapperPassPass(PassRegistry&);<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Scalar.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=266132&r1=266131&r2=266132&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Scalar.h?rev=266132&r1=266131&r2=266132&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Scalar.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Scalar.h Tue Apr 12 16:35:28 2016<br>
@@ -503,6 +503,9 @@ FunctionPass *createLoopVersioningPass()<br>
//<br>
FunctionPass *createLoopDataPrefetchPass();<br>
<br>
+///===---------------------------------------------------------------------===//<br>
+ModulePass *createNameAnonFunctionPass();<br>
+<br>
} // End llvm namespace<br>
<br>
#endif<br>
<br>
Modified: llvm/trunk/include/llvm/Transforms/Utils/ModuleUtils.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/ModuleUtils.h?rev=266132&r1=266131&r2=266132&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/ModuleUtils.h?rev=266132&r1=266131&r2=266132&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/include/llvm/Transforms/Utils/ModuleUtils.h (original)<br>
+++ llvm/trunk/include/llvm/Transforms/Utils/ModuleUtils.h Tue Apr 12 16:35:28 2016<br>
@@ -61,6 +61,11 @@ std::pair<Function *, Function *> create<br>
Module &M, StringRef CtorName, StringRef InitName,<br>
ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,<br>
StringRef VersionCheckName = StringRef());<br>
+<br>
+/// Rename all the anon functions in the module using a hash computed from<br>
+/// the list of public globals in the module.<br>
+bool nameUnamedFunctions(Module &M);<br>
+<br>
} // End llvm namespace<br>
<br>
#endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H<br>
<br>
Modified: llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=266132&r1=266131&r2=266132&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp?rev=266132&r1=266131&r2=266132&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/IPO/PassManagerBuilder.cpp Tue Apr 12 16:35:28 2016<br>
@@ -247,6 +247,8 @@ void PassManagerBuilder::addFunctionSimp<br>
if (PrepareForThinLTO) {<br>
MPM.add(createAggressiveDCEPass()); // Delete dead instructions<br>
addInstructionCombiningPass(MPM); // Combine silly seq's<br>
+ // Rename anon function to export them<br>
+ MPM.add(createNameAnonFunctionPass());<br>
return;<br>
}<br>
// Rotate Loop - disable header duplication at -Oz<br>
<br>
Modified: llvm/trunk/lib/Transforms/Utils/CMakeLists.txt<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CMakeLists.txt?rev=266132&r1=266131&r2=266132&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/CMakeLists.txt?rev=266132&r1=266131&r2=266132&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Utils/CMakeLists.txt (original)<br>
+++ llvm/trunk/lib/Transforms/Utils/CMakeLists.txt Tue Apr 12 16:35:28 2016<br>
@@ -31,6 +31,7 @@ add_llvm_library(LLVMTransformUtils<br>
MemorySSA.cpp<br>
MetaRenamer.cpp<br>
ModuleUtils.cpp<br>
+ NameAnonFunctions.cpp<br>
PromoteMemoryToRegister.cpp<br>
SSAUpdater.cpp<br>
SanitizerStats.cpp<br>
<br>
Added: llvm/trunk/lib/Transforms/Utils/NameAnonFunctions.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/NameAnonFunctions.cpp?rev=266132&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/NameAnonFunctions.cpp?rev=266132&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Utils/NameAnonFunctions.cpp (added)<br>
+++ llvm/trunk/lib/Transforms/Utils/NameAnonFunctions.cpp Tue Apr 12 16:35:28 2016<br>
@@ -0,0 +1,102 @@<br>
+//===- NameAnonFunctions.cpp - ThinLTO Summary-based Function Import ------===//<br>
+//<br>
+// The LLVM Compiler Infrastructure<br>
+//<br>
+// This file is distributed under the University of Illinois Open Source<br>
+// License. See LICENSE.TXT for details.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements naming anonymous function to make sure they can be<br>
+// refered to by ThinLTO.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/ADT/SmallString.h"<br>
+#include "llvm/IR/Module.h"<br>
+#include "llvm/Support/MD5.h"<br>
+#include "llvm/Transforms/Utils/ModuleUtils.h"<br>
+<br>
+using namespace llvm;<br>
+<br>
+// Compute a "unique" hash for the module based on the name of the public<br>
+// functions.<br>
+class ModuleHasher {<br>
+ Module &TheModule;<br>
+ std::string TheHash;<br>
+<br>
+public:<br>
+ ModuleHasher(Module &M) : TheModule(M) {}<br>
+<br>
+ /// Return the lazily computed hash.<br>
+ std::string &get() {<br>
+ if (!TheHash.empty())<br>
+ // Cache hit :)<br>
+ return TheHash;<br>
+<br>
+ MD5 Hasher;<br>
+ for (auto &F : TheModule) {<br>
+ if (F.isDeclaration() || F.hasLocalLinkage() || !F.hasName())<br>
+ continue;<br>
+ auto Name = F.getName();<br>
+ Hasher.update(Name);<br>
+ }<br>
+ for (auto &GV : TheModule.globals()) {<br>
+ if (GV.isDeclaration() || GV.hasLocalLinkage() || !GV.hasName())<br>
+ continue;<br>
+ auto Name = GV.getName();<br>
+ Hasher.update(Name);<br>
+ }<br>
+<br>
+ // Now return the result.<br>
+ MD5::MD5Result Hash;<br>
+ Hasher.final(Hash);<br>
+ SmallString<32> Result;<br>
+ MD5::stringifyResult(Hash, Result);<br>
+ TheHash = Result.str();<br>
+ return TheHash;<br>
+ }<br>
+};<br>
+<br>
+// Rename all the anon functions in the module<br>
+bool llvm::nameUnamedFunctions(Module &M) {<br>
+ bool Changed = false;<br>
+ ModuleHasher ModuleHash(M);<br>
+ int count = 0;<br></blockquote><div><br></div><div>s/count/Count/</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
+ for (auto &F : M) {<br>
+ if (F.hasName())<br>
+ continue;<br>
+ F.setName(Twine("anon.") + ModuleHash.get() + "." + Twine(count++));<br>
+ Changed = true;<br>
+ }<br>
+ return Changed;<br>
+}<br>
+<br>
+namespace {<br>
+<br>
+// Simple pass that provides a name to every anon function.<br>
+class NameAnonFunction : public ModulePass {<br>
+<br>
+public:<br>
+ /// Pass identification, replacement for typeid<br>
+ static char ID;<br>
+<br>
+ /// Specify pass name for debug output<br>
+ const char *getPassName() const override { return "Name Anon Functions"; }<br>
+<br>
+ explicit NameAnonFunction() : ModulePass(ID) {}<br>
+<br>
+ bool runOnModule(Module &M) override { return nameUnamedFunctions(M); }<br>
+};<br>
+char NameAnonFunction::ID = 0;<br>
+<br>
+} // anonymous namespace<br>
+<br>
+INITIALIZE_PASS_BEGIN(NameAnonFunction, "name-anon-functions",<br>
+ "Provide a name to nameless functions", false, false)<br>
+INITIALIZE_PASS_END(NameAnonFunction, "name-anon-functions",<br>
+ "Provide a name to nameless functions", false, false)<br>
+<br>
+namespace llvm {<br>
+Pass *createNameAnonFunctionPass() { return new NameAnonFunction(); }<br>
+}<br>
<br>
Modified: llvm/trunk/lib/Transforms/Utils/Utils.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/Utils.cpp?rev=266132&r1=266131&r2=266132&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/Utils.cpp?rev=266132&r1=266131&r2=266132&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Utils/Utils.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Utils/Utils.cpp Tue Apr 12 16:35:28 2016<br>
@@ -28,6 +28,7 @@ void llvm::initializeTransformUtils(Pass<br>
initializeLoopSimplifyPass(Registry);<br>
initializeLowerInvokePass(Registry);<br>
initializeLowerSwitchPass(Registry);<br>
+ initializeNameAnonFunctionPass(Registry);<br>
initializePromotePassPass(Registry);<br>
initializeUnifyFunctionExitNodesPass(Registry);<br>
initializeInstSimplifierPass(Registry);<br>
<br>
Added: llvm/trunk/test/Transforms/NameAnonFunctions/rename.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/NameAnonFunctions/rename.ll?rev=266132&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/NameAnonFunctions/rename.ll?rev=266132&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Transforms/NameAnonFunctions/rename.ll (added)<br>
+++ llvm/trunk/test/Transforms/NameAnonFunctions/rename.ll Tue Apr 12 16:35:28 2016<br>
@@ -0,0 +1,27 @@<br>
+; RUN: opt -S -name-anon-functions < %s | FileCheck %s<br>
+<br>
+<br>
+; foo contribute to the unique hash for the module<br>
+define void @foo() {<br>
+ ret void<br>
+}<br>
+<br>
+; bar is internal, and does not contribute to the unique hash for the module<br>
+define internal void @bar() {<br>
+ ret void<br>
+}<br>
+<br>
+; CHECK: define void @anon.acbd18db4cc2f85cedef654fccc4a4d8.0()<br>
+; CHECK: define void @anon.acbd18db4cc2f85cedef654fccc4a4d8.1()<br>
+; CHECK: define void @anon.acbd18db4cc2f85cedef654fccc4a4d8.2()<br>
+<br>
+define void @0() {<br>
+ ret void<br>
+}<br>
+define void @1() {<br>
+ ret void<br>
+}<br>
+define void @2() {<br>
+ ret void<br>
+}<br>
+<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>