New pass to make internal linkage symbol names unique.

With clang option -funique-internal-linkage-symbols, symbols with
internal linkage get names with the module hash appended.

Differential Revision: https://reviews.llvm.org/D78243
This commit is contained in:
Sriraman Tallam 2020-04-20 14:41:30 -07:00
parent 7e1e427f28
commit a427ed091b
8 changed files with 151 additions and 0 deletions

View File

@ -304,6 +304,7 @@ void initializeModuloScheduleTestPass(PassRegistry&);
void initializeMustExecutePrinterPass(PassRegistry&);
void initializeMustBeExecutedContextPrinterPass(PassRegistry&);
void initializeNameAnonGlobalLegacyPassPass(PassRegistry&);
void initializeUniqueInternalLinkageNamesLegacyPassPass(PassRegistry &);
void initializeNaryReassociateLegacyPassPass(PassRegistry&);
void initializeNewGVNLegacyPassPass(PassRegistry&);
void initializeObjCARCAAWrapperPassPass(PassRegistry&);

View File

@ -25,6 +25,12 @@ class Pass;
//
ModulePass *createMetaRenamerPass();
//===----------------------------------------------------------------------===//
// createUniqueInternalLinkageNamesPass - Make internal linkage symbol names
// unique.
//
ModulePass *createUniqueInternalLinkageNamesPass();
//===----------------------------------------------------------------------===//
//
// LowerInvoke - This pass removes invoke instructions, converting them to call

View File

@ -0,0 +1,31 @@
//===-- UniqueInternalLinkageNames.h - Uniq. Int. Linkage Names -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements unique naming of internal linkage symbols with option
// -funique-internal-linkage-symbols.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TRANSFORMS_UTILS_UNIQUEINTERNALLINKAGENAMES_H
#define LLVM_TRANSFORMS_UTILS_UNIQUEINTERNALLINKAGENAMES_H
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
namespace llvm {
/// Simple pass that provides a name to every anonymous globals.
class UniqueInternalLinkageNamesPass
: public PassInfoMixin<UniqueInternalLinkageNamesPass> {
public:
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
};
} // end namespace llvm
#endif // LLVM_TRANSFORMS_UTILS_UNIQUEINTERNALLINKAGENAMES_H

View File

@ -65,6 +65,7 @@ add_llvm_component_library(LLVMTransformUtils
SymbolRewriter.cpp
UnifyFunctionExitNodes.cpp
UnifyLoopExits.cpp
UniqueInternalLinkageNames.cpp
Utils.cpp
ValueMapper.cpp
VNCoercion.cpp

View File

@ -0,0 +1,97 @@
//===- UniqueInternalLinkageNames.cpp - Unique Internal Linkage Sym Names -===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements unique naming of internal linkage symbols with option
// -funique-internal-linkage-symbols.
//
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/UniqueInternalLinkageNames.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/IR/Module.h"
#include "llvm/InitializePasses.h"
#include "llvm/Support/MD5.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
using namespace llvm;
static bool uniqueifyInternalLinkageNames(Module &M) {
llvm::MD5 Md5;
Md5.update(M.getSourceFileName());
llvm::MD5::MD5Result R;
Md5.final(R);
SmallString<32> Str;
llvm::MD5::stringifyResult(R, Str);
std::string ModuleNameHash = (Twine(".") + Twine(Str)).str();
bool Changed = false;
// Append the module hash to all internal linkage functions.
for (auto &F : M) {
if (F.hasInternalLinkage()) {
F.setName(F.getName() + ModuleNameHash);
Changed = true;
}
}
// Append the module hash to all internal linkage globals.
for (auto &GV : M.globals()) {
if (GV.hasInternalLinkage()) {
GV.setName(GV.getName() + ModuleNameHash);
Changed = true;
}
}
return Changed;
}
namespace {
// Legacy pass that provides a name to every anon globals.
class UniqueInternalLinkageNamesLegacyPass : public ModulePass {
public:
/// Pass identification, replacement for typeid
static char ID;
/// Specify pass name for debug output
StringRef getPassName() const override {
return "Unique Internal Linkage Names";
}
explicit UniqueInternalLinkageNamesLegacyPass() : ModulePass(ID) {
initializeUniqueInternalLinkageNamesLegacyPassPass(
*PassRegistry::getPassRegistry());
}
bool runOnModule(Module &M) override {
return uniqueifyInternalLinkageNames(M);
}
};
char UniqueInternalLinkageNamesLegacyPass::ID = 0;
} // anonymous namespace
PreservedAnalyses
UniqueInternalLinkageNamesPass::run(Module &M, ModuleAnalysisManager &AM) {
if (!uniqueifyInternalLinkageNames(M))
return PreservedAnalyses::all();
return PreservedAnalyses::none();
}
INITIALIZE_PASS_BEGIN(UniqueInternalLinkageNamesLegacyPass,
"unique-internal-linkage-names",
"Uniqueify internal linkage names", false, false)
INITIALIZE_PASS_END(UniqueInternalLinkageNamesLegacyPass,
"unique-internal-linkage-names",
"Uniqueify Internal linkage names", false, false)
namespace llvm {
ModulePass *createUniqueInternalLinkageNamesPass() {
return new UniqueInternalLinkageNamesLegacyPass();
}
} // namespace llvm

View File

@ -42,6 +42,7 @@ void llvm::initializeTransformUtils(PassRegistry &Registry) {
initializeInjectTLIMappingsLegacyPass(Registry);
initializeFixIrreduciblePass(Registry);
initializeUnifyLoopExitsPass(Registry);
initializeUniqueInternalLinkageNamesLegacyPassPass(Registry);
}
/// LLVMInitializeTransformUtils - C binding for initializeTransformUtilsPasses.

View File

@ -0,0 +1,13 @@
; RUN: opt -S -unique-internal-linkage-names < %s | FileCheck %s
source_filename = "foo.c"
@glob = internal global i32 0
define internal i32 @foo() {
entry:
ret i32 0
}
; CHECK: @glob.6ae72bb15a7d1834b42ae042a58f7a4d = internal global
; CHECK: define internal i32 @foo.6ae72bb15a7d1834b42ae042a58f7a4d()

View File

@ -72,6 +72,7 @@ static_library("Utils") {
"SymbolRewriter.cpp",
"UnifyFunctionExitNodes.cpp",
"UnifyLoopExits.cpp",
"UniqueInternalLinkageNames.cpp",
"Utils.cpp",
"VNCoercion.cpp",
"ValueMapper.cpp",