-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
[NFC]Extract the heuristic to find vtable for an indirect call into a helper function #81024
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Contributor
minglotus-6
commented
Feb 7, 2024
•
edited
edited
- This way the helper function could be re-used by indirect-call-promotion pass to find out the vtable for an indirect call and extract the value profiles if any.
- The parent patch is [NFC][IndirectCallProm] Refactor function-based conditional devirtualization and indirect call value profile update into one helper function #80762
Created using spr 1.3.4 [skip ci]
Created using spr 1.3.4
minglotus-6
changed the title
[NFC]Refactor
[NFC]Extract the heuristic to find vtable for an indirect call into a helper function for re-use
Feb 7, 2024
minglotus-6
requested review from
modiking,
teresajohnson,
david-xl and
snehasish
February 7, 2024 23:17
minglotus-6
changed the title
[NFC]Extract the heuristic to find vtable for an indirect call into a helper function for re-use
[NFC]Extract the heuristic to find vtable for an indirect call into a helper function
Feb 7, 2024
teresajohnson
approved these changes
Feb 8, 2024
@llvm/pr-subscribers-llvm-analysis Author: Mingming Liu (minglotus-6) Changes
Full diff: https://github.com/llvm/llvm-project/pull/81024.diff 1 Files Affected:
diff --git a/llvm/include/llvm/Analysis/IndirectCallVisitor.h b/llvm/include/llvm/Analysis/IndirectCallVisitor.h
index c8429e52bee96..5969241a179ea 100644
--- a/llvm/include/llvm/Analysis/IndirectCallVisitor.h
+++ b/llvm/include/llvm/Analysis/IndirectCallVisitor.h
@@ -28,6 +28,38 @@ struct PGOIndirectCallVisitor : public InstVisitor<PGOIndirectCallVisitor> {
std::vector<Instruction *> ProfiledAddresses;
PGOIndirectCallVisitor(InstructionType Type) : Type(Type) {}
+ // Given an indirect call instruction, try to find the the following pattern
+ //
+ // %vtable = load ptr, ptr %obj
+ // %vfn = getelementptr inbounds ptr, ptr %vtable, i64 1
+ // %2 = load ptr, ptr %vfn
+ // $call = tail call i32 %2
+ //
+ // A heuristic is used to find the address feeding instructions.
+ static Instruction *tryGetVTableInstruction(CallBase *CB) {
+ assert(CB != nullptr && "Caller guaranteed");
+ LoadInst *LI = dyn_cast<LoadInst>(CB->getCalledOperand());
+
+ if (LI != nullptr) {
+ Value *FuncPtr = LI->getPointerOperand(); // GEP (or bitcast)
+ Value *VTablePtr = FuncPtr->stripInBoundsConstantOffsets();
+ // FIXME: Add support in the frontend so LLVM type intrinsics are
+ // emitted without LTO. This way, added intrinsics could filter
+ // non-vtable instructions and reduce instrumentation overhead.
+ // Since a non-vtable profiled address is not within the address
+ // range of vtable objects, it's stored as zero in indexed profiles.
+ // A pass that looks up symbol with an zero hash will (almost) always
+ // find nullptr and skip the actual transformation (e.g., comparison
+ // of symbols). So the performance overhead from non-vtable profiled
+ // address is negligible if exists at all. Comparing loaded address
+ // with symbol address guarantees correctness.
+ if (VTablePtr != nullptr && isa<Instruction>(VTablePtr)) {
+ return cast<Instruction>(VTablePtr);
+ }
+ }
+ return nullptr;
+ }
+
void visitCallBase(CallBase &Call) {
if (Call.isIndirectCall()) {
IndirectCalls.push_back(&Call);
@@ -35,33 +67,10 @@ struct PGOIndirectCallVisitor : public InstVisitor<PGOIndirectCallVisitor> {
if (Type != InstructionType::kVTableVal)
return;
- LoadInst *LI = dyn_cast<LoadInst>(Call.getCalledOperand());
- // The code pattern to look for
- //
- // %vtable = load ptr, ptr %b
- // %vfn = getelementptr inbounds ptr, ptr %vtable, i64 1
- // %2 = load ptr, ptr %vfn
- // %call = tail call i32 %2(ptr %b)
- //
- // %vtable is the vtable address value to profile, and
- // %2 is the indirect call target address to profile.
- if (LI != nullptr) {
- Value *Ptr = LI->getPointerOperand();
- Value *VTablePtr = Ptr->stripInBoundsConstantOffsets();
- // This is a heuristic to find address feeding instructions.
- // FIXME: Add support in the frontend so LLVM type intrinsics are
- // emitted without LTO. This way, added intrinsics could filter
- // non-vtable instructions and reduce instrumentation overhead.
- // Since a non-vtable profiled address is not within the address
- // range of vtable objects, it's stored as zero in indexed profiles.
- // A pass that looks up symbol with an zero hash will (almost) always
- // find nullptr and skip the actual transformation (e.g., comparison
- // of symbols). So the performance overhead from non-vtable profiled
- // address is negligible if exists at all. Comparing loaded address
- // with symbol address guarantees correctness.
- if (VTablePtr != nullptr && isa<Instruction>(VTablePtr)) {
- ProfiledAddresses.push_back(cast<Instruction>(VTablePtr));
- }
+ Instruction *VPtr =
+ PGOIndirectCallVisitor::tryGetVTableInstruction(&Call);
+ if (VPtr) {
+ ProfiledAddresses.push_back(VPtr);
}
}
}
|
minglotus-6
changed the base branch from
users/minglotus-6/spr/main.nfcrefactor
to
main
April 12, 2024 00:11
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.