Skip to content

Commit bc50455

Browse files
committed
move GetOrCreateFunctionComdat to Instrumentation.cpp/Instrumentation.h
Summary: GetOrCreateFunctionComdat is currently used in SanitizerCoverage, where it's defined. I'm planing to use it in HWASAN as well, so moving it into a common location. NFC Reviewers: morehouse Reviewed By: morehouse Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D53218 llvm-svn: 344433
1 parent 748d080 commit bc50455

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

llvm/include/llvm/Transforms/Instrumentation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace llvm {
2727
class FunctionPass;
2828
class ModulePass;
2929
class OptimizationRemarkEmitter;
30+
class Comdat;
3031

3132
/// Instrumentation passes often insert conditional checks into entry blocks.
3233
/// Call this function before splitting the entry block to move instructions
@@ -41,6 +42,11 @@ GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
4142
bool AllowMerging,
4243
const char *NamePrefix = "");
4344

45+
// Returns F.getComdat() if it exists.
46+
// Otherwise creates a new comdat, sets F's comdat, and returns it.
47+
// Returns nullptr on failure.
48+
Comdat *GetOrCreateFunctionComdat(Function &F, const std::string &ModuleId);
49+
4450
// Insert GCOV profiling instrumentation
4551
struct GCOVOptions {
4652
static GCOVOptions getDefault();

llvm/lib/Transforms/Instrumentation/Instrumentation.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
7070
return GV;
7171
}
7272

73+
Comdat *llvm::GetOrCreateFunctionComdat(Function &F,
74+
const std::string &ModuleId) {
75+
if (auto Comdat = F.getComdat()) return Comdat;
76+
assert(F.hasName());
77+
Module *M = F.getParent();
78+
std::string Name = F.getName();
79+
if (F.hasLocalLinkage()) {
80+
if (ModuleId.empty())
81+
return nullptr;
82+
Name += ModuleId;
83+
}
84+
F.setComdat(M->getOrInsertComdat(Name));
85+
return F.getComdat();
86+
}
87+
7388
/// initializeInstrumentation - Initialize all passes in the TransformUtils
7489
/// library.
7590
void llvm::initializeInstrumentation(PassRegistry &Registry) {

llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@ class SanitizerCoverageModule : public ModulePass {
220220
MDNode::get(*C, None));
221221
}
222222

223-
Comdat *GetOrCreateFunctionComdat(Function &F);
224-
225223
std::string getSectionName(const std::string &Section) const;
226224
std::string getSectionStart(const std::string &Section) const;
227225
std::string getSectionEnd(const std::string &Section) const;
@@ -590,28 +588,16 @@ bool SanitizerCoverageModule::runOnFunction(Function &F) {
590588
return true;
591589
}
592590

593-
Comdat *SanitizerCoverageModule::GetOrCreateFunctionComdat(Function &F) {
594-
if (auto Comdat = F.getComdat()) return Comdat;
595-
if (!TargetTriple.isOSBinFormatELF()) return nullptr;
596-
assert(F.hasName());
597-
std::string Name = F.getName();
598-
if (F.hasLocalLinkage()) {
599-
if (CurModuleUniqueId.empty()) return nullptr;
600-
Name += CurModuleUniqueId;
601-
}
602-
auto Comdat = CurModule->getOrInsertComdat(Name);
603-
F.setComdat(Comdat);
604-
return Comdat;
605-
}
606-
607591
GlobalVariable *SanitizerCoverageModule::CreateFunctionLocalArrayInSection(
608592
size_t NumElements, Function &F, Type *Ty, const char *Section) {
609593
ArrayType *ArrayTy = ArrayType::get(Ty, NumElements);
610594
auto Array = new GlobalVariable(
611595
*CurModule, ArrayTy, false, GlobalVariable::PrivateLinkage,
612596
Constant::getNullValue(ArrayTy), "__sancov_gen_");
613-
if (auto Comdat = GetOrCreateFunctionComdat(F))
614-
Array->setComdat(Comdat);
597+
598+
if (TargetTriple.isOSBinFormatELF())
599+
if (auto Comdat = GetOrCreateFunctionComdat(F, CurModuleUniqueId))
600+
Array->setComdat(Comdat);
615601
Array->setSection(getSectionName(Section));
616602
Array->setAlignment(Ty->isPointerTy() ? DL->getPointerSize()
617603
: Ty->getPrimitiveSizeInBits() / 8);

0 commit comments

Comments
 (0)