-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Store GUIDs in metadata #133682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Store GUIDs in metadata #133682
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===-- AssignGUID.h - Unique identifier assignment pass --------*- 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 provides a pass which assigns a a GUID (globally unique identifier) | ||
// to every GlobalValue in the module, according to its current name, linkage, | ||
// and originating file. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_TRANSFORMS_UTILS_ASSIGNGUID_H | ||
#define LLVM_TRANSFORMS_UTILS_ASSIGNGUID_H | ||
|
||
#include "llvm/IR/Module.h" | ||
#include "llvm/IR/PassManager.h" | ||
|
||
namespace llvm { | ||
|
||
class AssignGUIDPass : public PassInfoMixin<AssignGUIDPass> { | ||
public: | ||
AssignGUIDPass() = default; | ||
|
||
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); | ||
|
||
static bool isRequired() { return true; } | ||
}; | ||
|
||
} // end namespace llvm | ||
|
||
#endif // LLVM_TRANSFORMS_UTILS_ASSIGNGUID_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,27 @@ GlobalValue::getGUIDAssumingExternalLinkage(StringRef GlobalIdentifier) { | |
return MD5Hash(GlobalIdentifier); | ||
} | ||
|
||
void GlobalValue::assignGUID() { | ||
orodley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (getMetadata(LLVMContext::MD_unique_id) != nullptr) | ||
return; | ||
|
||
const GUID G = | ||
GlobalValue::getGUIDAssumingExternalLinkage(getGlobalIdentifier()); | ||
setMetadata( | ||
LLVMContext::MD_unique_id, | ||
MDNode::get(getContext(), {ConstantAsMetadata::get(ConstantInt::get( | ||
Type::getInt64Ty(getContext()), G))})); | ||
} | ||
|
||
GlobalValue::GUID GlobalValue::getGUID() const { | ||
auto *MD = getMetadata(LLVMContext::MD_unique_id); | ||
assert(MD != nullptr && "GUID was not assigned before calling GetGUID()"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like something that would be reachable through Though generally, requiring that a certain pass runs before another pass and no new globals are introduced in between sounds fragile to me.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH I don't really get how this works as-implemented. You are assigning GUIDs at the very start of the pipeline, but there's lots of passes that can insert new globals after that... |
||
return cast<ConstantInt>(cast<ConstantAsMetadata>(MD->getOperand(0)) | ||
->getValue() | ||
->stripPointerCasts()) | ||
->getZExtValue(); | ||
} | ||
|
||
void GlobalValue::removeFromParent() { | ||
switch (getValueID()) { | ||
#define HANDLE_GLOBAL_VALUE(NAME) \ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===-- AssignGUID.cpp - Unique identifier assignment pass ------*- 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 provides a pass which assigns a a GUID (globally unique identifier) | ||
// to every GlobalValue in the module, according to its current name, linkage, | ||
// and originating file. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Transforms/Utils/AssignGUID.h" | ||
#include "llvm/Support/Debug.h" | ||
|
||
using namespace llvm; | ||
|
||
PreservedAnalyses AssignGUIDPass::run(Module &M, ModuleAnalysisManager &AM) { | ||
for (auto &GV : M.globals()) { | ||
if (GV.isDeclaration()) | ||
continue; | ||
GV.assignGUID(); | ||
dbgs() << "[Added GUID to GV:] " << GV.getName() << "\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be inside LLVM_DEBUG. |
||
} | ||
for (auto &F : M.functions()) { | ||
if (F.isDeclaration()) | ||
continue; | ||
F.assignGUID(); | ||
dbgs() << "[Added GUID to F:] " << F.getName() << "\n"; | ||
} | ||
return PreservedAnalyses::none(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Description needs to be updated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done -- does the new description look good?