<div dir="ltr">Assuming this is for propeller it seems reasonable to document that (unless we have some other uses in mind as well).<div><br></div><div>-eric</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Apr 20, 2020 at 3:05 PM Sriraman Tallam via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
Author: Sriraman Tallam<br>
Date: 2020-04-20T15:05:22-07:00<br>
New Revision: 365b60fc9371209f855d89f5d08c3f5ea9400711<br>
<br>
URL: <a href="https://github.com/llvm/llvm-project/commit/365b60fc9371209f855d89f5d08c3f5ea9400711" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/365b60fc9371209f855d89f5d08c3f5ea9400711</a><br>
DIFF: <a href="https://github.com/llvm/llvm-project/commit/365b60fc9371209f855d89f5d08c3f5ea9400711.diff" rel="noreferrer" target="_blank">https://github.com/llvm/llvm-project/commit/365b60fc9371209f855d89f5d08c3f5ea9400711.diff</a><br>
<br>
LOG: New pass to make internal linkage symbol names unique.<br>
<br>
With clang option -funique-internal-linkage-symbols, symbols with<br>
internal linkage get names with the module hash appended.<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D78243" rel="noreferrer" target="_blank">https://reviews.llvm.org/D78243</a><br>
<br>
Added: <br>
llvm/include/llvm/Transforms/Utils/UniqueInternalLinkageNames.h<br>
llvm/lib/Transforms/Utils/UniqueInternalLinkageNames.cpp<br>
llvm/test/Transforms/UniqueInternalLinkageNames/unique_symbol_names.ll<br>
<br>
Modified: <br>
llvm/include/llvm/InitializePasses.h<br>
llvm/include/llvm/Transforms/Utils.h<br>
llvm/lib/Transforms/Utils/CMakeLists.txt<br>
llvm/lib/Transforms/Utils/Utils.cpp<br>
llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn<br>
<br>
Removed: <br>
<br>
<br>
<br>
################################################################################<br>
diff --git a/llvm/include/llvm/InitializePasses.h b/llvm/include/llvm/InitializePasses.h<br>
index f35815a898d4..5eed68599419 100644<br>
--- a/llvm/include/llvm/InitializePasses.h<br>
+++ b/llvm/include/llvm/InitializePasses.h<br>
@@ -304,6 +304,7 @@ void initializeModuloScheduleTestPass(PassRegistry&);<br>
void initializeMustExecutePrinterPass(PassRegistry&);<br>
void initializeMustBeExecutedContextPrinterPass(PassRegistry&);<br>
void initializeNameAnonGlobalLegacyPassPass(PassRegistry&);<br>
+void initializeUniqueInternalLinkageNamesLegacyPassPass(PassRegistry &);<br>
void initializeNaryReassociateLegacyPassPass(PassRegistry&);<br>
void initializeNewGVNLegacyPassPass(PassRegistry&);<br>
void initializeObjCARCAAWrapperPassPass(PassRegistry&);<br>
<br>
diff --git a/llvm/include/llvm/Transforms/Utils.h b/llvm/include/llvm/Transforms/Utils.h<br>
index 59beb0caab1d..29090e2b2ac0 100644<br>
--- a/llvm/include/llvm/Transforms/Utils.h<br>
+++ b/llvm/include/llvm/Transforms/Utils.h<br>
@@ -25,6 +25,12 @@ class Pass;<br>
//<br>
ModulePass *createMetaRenamerPass();<br>
<br>
+//===----------------------------------------------------------------------===//<br>
+// createUniqueInternalLinkageNamesPass - Make internal linkage symbol names<br>
+// unique.<br>
+//<br>
+ModulePass *createUniqueInternalLinkageNamesPass();<br>
+<br>
//===----------------------------------------------------------------------===//<br>
//<br>
// LowerInvoke - This pass removes invoke instructions, converting them to call<br>
<br>
diff --git a/llvm/include/llvm/Transforms/Utils/UniqueInternalLinkageNames.h b/llvm/include/llvm/Transforms/Utils/UniqueInternalLinkageNames.h<br>
new file mode 100644<br>
index 000000000000..637b5d8e8e51<br>
--- /dev/null<br>
+++ b/llvm/include/llvm/Transforms/Utils/UniqueInternalLinkageNames.h<br>
@@ -0,0 +1,31 @@<br>
+//===-- UniqueInternalLinkageNames.h - Uniq. Int. Linkage Names -*- C++ -*-===//<br>
+//<br>
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.<br>
+// See <a href="https://llvm.org/LICENSE.txt" rel="noreferrer" target="_blank">https://llvm.org/LICENSE.txt</a> for license information.<br>
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements unique naming of internal linkage symbols with option<br>
+// -funique-internal-linkage-symbols.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#ifndef LLVM_TRANSFORMS_UTILS_UNIQUEINTERNALLINKAGENAMES_H<br>
+#define LLVM_TRANSFORMS_UTILS_UNIQUEINTERNALLINKAGENAMES_H<br>
+<br>
+#include "llvm/IR/Module.h"<br>
+#include "llvm/IR/PassManager.h"<br>
+<br>
+namespace llvm {<br>
+<br>
+/// Simple pass that provides a name to every anonymous globals.<br>
+class UniqueInternalLinkageNamesPass<br>
+ : public PassInfoMixin<UniqueInternalLinkageNamesPass> {<br>
+public:<br>
+ PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);<br>
+};<br>
+<br>
+} // end namespace llvm<br>
+<br>
+#endif // LLVM_TRANSFORMS_UTILS_UNIQUEINTERNALLINKAGENAMES_H<br>
<br>
diff --git a/llvm/lib/Transforms/Utils/CMakeLists.txt b/llvm/lib/Transforms/Utils/CMakeLists.txt<br>
index 9a316ad1b36f..0ca23669e97d 100644<br>
--- a/llvm/lib/Transforms/Utils/CMakeLists.txt<br>
+++ b/llvm/lib/Transforms/Utils/CMakeLists.txt<br>
@@ -65,6 +65,7 @@ add_llvm_component_library(LLVMTransformUtils<br>
SymbolRewriter.cpp<br>
UnifyFunctionExitNodes.cpp<br>
UnifyLoopExits.cpp<br>
+ UniqueInternalLinkageNames.cpp<br>
Utils.cpp<br>
ValueMapper.cpp<br>
VNCoercion.cpp<br>
<br>
diff --git a/llvm/lib/Transforms/Utils/UniqueInternalLinkageNames.cpp b/llvm/lib/Transforms/Utils/UniqueInternalLinkageNames.cpp<br>
new file mode 100644<br>
index 000000000000..5b58548e54dc<br>
--- /dev/null<br>
+++ b/llvm/lib/Transforms/Utils/UniqueInternalLinkageNames.cpp<br>
@@ -0,0 +1,97 @@<br>
+//===- UniqueInternalLinkageNames.cpp - Unique Internal Linkage Sym Names -===//<br>
+//<br>
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.<br>
+// See <a href="https://llvm.org/LICENSE.txt" rel="noreferrer" target="_blank">https://llvm.org/LICENSE.txt</a> for license information.<br>
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+//<br>
+// This file implements unique naming of internal linkage symbols with option<br>
+// -funique-internal-linkage-symbols.<br>
+//<br>
+//===----------------------------------------------------------------------===//<br>
+<br>
+#include "llvm/Transforms/Utils/UniqueInternalLinkageNames.h"<br>
+#include "llvm/ADT/SmallString.h"<br>
+#include "llvm/IR/Module.h"<br>
+#include "llvm/InitializePasses.h"<br>
+#include "llvm/Support/MD5.h"<br>
+#include "llvm/Transforms/Utils/ModuleUtils.h"<br>
+<br>
+using namespace llvm;<br>
+<br>
+static bool uniqueifyInternalLinkageNames(Module &M) {<br>
+ llvm::MD5 Md5;<br>
+ Md5.update(M.getSourceFileName());<br>
+ llvm::MD5::MD5Result R;<br>
+ Md5.final(R);<br>
+ SmallString<32> Str;<br>
+ llvm::MD5::stringifyResult(R, Str);<br>
+ std::string ModuleNameHash = (Twine(".") + Twine(Str)).str();<br>
+ bool Changed = false;<br>
+<br>
+ // Append the module hash to all internal linkage functions.<br>
+ for (auto &F : M) {<br>
+ if (F.hasInternalLinkage()) {<br>
+ F.setName(F.getName() + ModuleNameHash);<br>
+ Changed = true;<br>
+ }<br>
+ }<br>
+<br>
+ // Append the module hash to all internal linkage globals.<br>
+ for (auto &GV : M.globals()) {<br>
+ if (GV.hasInternalLinkage()) {<br>
+ GV.setName(GV.getName() + ModuleNameHash);<br>
+ Changed = true;<br>
+ }<br>
+ }<br>
+ return Changed;<br>
+}<br>
+<br>
+namespace {<br>
+<br>
+// Legacy pass that provides a name to every anon globals.<br>
+class UniqueInternalLinkageNamesLegacyPass : public ModulePass {<br>
+<br>
+public:<br>
+ /// Pass identification, replacement for typeid<br>
+ static char ID;<br>
+<br>
+ /// Specify pass name for debug output<br>
+ StringRef getPassName() const override {<br>
+ return "Unique Internal Linkage Names";<br>
+ }<br>
+<br>
+ explicit UniqueInternalLinkageNamesLegacyPass() : ModulePass(ID) {<br>
+ initializeUniqueInternalLinkageNamesLegacyPassPass(<br>
+ *PassRegistry::getPassRegistry());<br>
+ }<br>
+<br>
+ bool runOnModule(Module &M) override {<br>
+ return uniqueifyInternalLinkageNames(M);<br>
+ }<br>
+};<br>
+<br>
+char UniqueInternalLinkageNamesLegacyPass::ID = 0;<br>
+} // anonymous namespace<br>
+<br>
+PreservedAnalyses<br>
+UniqueInternalLinkageNamesPass::run(Module &M, ModuleAnalysisManager &AM) {<br>
+ if (!uniqueifyInternalLinkageNames(M))<br>
+ return PreservedAnalyses::all();<br>
+<br>
+ return PreservedAnalyses::none();<br>
+}<br>
+<br>
+INITIALIZE_PASS_BEGIN(UniqueInternalLinkageNamesLegacyPass,<br>
+ "unique-internal-linkage-names",<br>
+ "Uniqueify internal linkage names", false, false)<br>
+INITIALIZE_PASS_END(UniqueInternalLinkageNamesLegacyPass,<br>
+ "unique-internal-linkage-names",<br>
+ "Uniqueify Internal linkage names", false, false)<br>
+<br>
+namespace llvm {<br>
+ModulePass *createUniqueInternalLinkageNamesPass() {<br>
+ return new UniqueInternalLinkageNamesLegacyPass();<br>
+}<br>
+} // namespace llvm<br>
<br>
diff --git a/llvm/lib/Transforms/Utils/Utils.cpp b/llvm/lib/Transforms/Utils/Utils.cpp<br>
index 7469e388eb21..d67ff91d2208 100644<br>
--- a/llvm/lib/Transforms/Utils/Utils.cpp<br>
+++ b/llvm/lib/Transforms/Utils/Utils.cpp<br>
@@ -42,6 +42,7 @@ void llvm::initializeTransformUtils(PassRegistry &Registry) {<br>
initializeInjectTLIMappingsLegacyPass(Registry);<br>
initializeFixIrreduciblePass(Registry);<br>
initializeUnifyLoopExitsPass(Registry);<br>
+ initializeUniqueInternalLinkageNamesLegacyPassPass(Registry);<br>
}<br>
<br>
/// LLVMInitializeTransformUtils - C binding for initializeTransformUtilsPasses.<br>
<br>
diff --git a/llvm/test/Transforms/UniqueInternalLinkageNames/unique_symbol_names.ll b/llvm/test/Transforms/UniqueInternalLinkageNames/unique_symbol_names.ll<br>
new file mode 100644<br>
index 000000000000..e2ff4aed6e29<br>
--- /dev/null<br>
+++ b/llvm/test/Transforms/UniqueInternalLinkageNames/unique_symbol_names.ll<br>
@@ -0,0 +1,13 @@<br>
+; RUN: opt -S -unique-internal-linkage-names < %s | FileCheck %s<br>
+<br>
+source_filename = "foo.c"<br>
+<br>
+@glob = internal global i32 0<br>
+<br>
+define internal i32 @foo() {<br>
+entry:<br>
+ ret i32 0<br>
+}<br>
+<br>
+; CHECK: @glob.6ae72bb15a7d1834b42ae042a58f7a4d = internal global<br>
+; CHECK: define internal i32 @foo.6ae72bb15a7d1834b42ae042a58f7a4d()<br>
<br>
diff --git a/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn b/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn<br>
index c1acc7ddb76b..978db52c3eed 100644<br>
--- a/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn<br>
+++ b/llvm/utils/gn/secondary/llvm/lib/Transforms/Utils/BUILD.gn<br>
@@ -72,6 +72,7 @@ static_library("Utils") {<br>
"SymbolRewriter.cpp",<br>
"UnifyFunctionExitNodes.cpp",<br>
"UnifyLoopExits.cpp",<br>
+ "UniqueInternalLinkageNames.cpp",<br>
"Utils.cpp",<br>
"VNCoercion.cpp",<br>
"ValueMapper.cpp",<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>