Skip to content
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

[llvm][ctx_profile] Add the llvm.instrprof.callsite intrinsic #89939

Merged
merged 6 commits into from
Apr 25, 2024

Conversation

mtrofin
Copy link
Member

@mtrofin mtrofin commented Apr 24, 2024

Add the callsite intrinsic.

Structurally, it is very similar to the counter intrinsics, hence the inheritance relationship. We can probably rename InstrProfCntrInstBase to InstrProfIndexedBase later - because the "counting" aspect is really left to derived types of InstrProfCntrInstBase, and it only concerns itself with the index aspect (which is what we care about for callsite, too)

(Tracking Issue: #89287, RFC referenced there)

@llvmbot
Copy link
Collaborator

llvmbot commented Apr 24, 2024

@llvm/pr-subscribers-llvm-ir

Author: Mircea Trofin (mtrofin)

Changes

Add the callsite intrinsic.

Structurally, it is very similar to the counter intrinsics, hence the inheritance relationship. We can probably rename later InstrProfCntrInstBase to InstrProfIndexedBase later - because the "counting" aspect is really left to derived types of InstrProfCntrInstBase, and it only concerns itself with the index aspect (which is what we care about for callsite, too)

(Tracking Issue: #89287, RFC referenced there)


Full diff: https://github.com/llvm/llvm-project/pull/89939.diff

3 Files Affected:

  • (modified) llvm/include/llvm/IR/IntrinsicInst.h (+16)
  • (modified) llvm/include/llvm/IR/Intrinsics.td (+5)
  • (modified) llvm/unittests/IR/IntrinsicsTest.cpp (+2)
diff --git a/llvm/include/llvm/IR/IntrinsicInst.h b/llvm/include/llvm/IR/IntrinsicInst.h
index 4f22720f1c558d..8a137ffe28b9f8 100644
--- a/llvm/include/llvm/IR/IntrinsicInst.h
+++ b/llvm/include/llvm/IR/IntrinsicInst.h
@@ -1435,6 +1435,7 @@ class InstrProfInstBase : public IntrinsicInst {
     case Intrinsic::instrprof_cover:
     case Intrinsic::instrprof_increment:
     case Intrinsic::instrprof_increment_step:
+    case Intrinsic::instrprof_callsite:
     case Intrinsic::instrprof_timestamp:
     case Intrinsic::instrprof_value_profile:
       return true;
@@ -1519,6 +1520,21 @@ class InstrProfIncrementInstStep : public InstrProfIncrementInst {
   }
 };
 
+/// This represents the llvm.instrprof.increment intrinsic.
+/// It is structurally like the increment or step counters, hence the
+/// inheritance relationship, albeit somewhat tenuous (it's not 'counting' per
+/// se)
+class InstrProfCallsite : public InstrProfCntrInstBase {
+public:
+  static bool classof(const IntrinsicInst *I) {
+    return I->getIntrinsicID() == Intrinsic::instrprof_callsite;
+  }
+  static bool classof(const Value *V) {
+    return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
+  }
+  Value *getCallee() const;
+};
+
 /// This represents the llvm.instrprof.timestamp intrinsic.
 class InstrProfTimestampInst : public InstrProfCntrInstBase {
 public:
diff --git a/llvm/include/llvm/IR/Intrinsics.td b/llvm/include/llvm/IR/Intrinsics.td
index 1d20f7e1b19854..a14e9dedef8c9e 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -914,6 +914,11 @@ def int_instrprof_increment_step : Intrinsic<[],
                                         [llvm_ptr_ty, llvm_i64_ty,
                                          llvm_i32_ty, llvm_i32_ty, llvm_i64_ty]>;
 
+// Callsite instrumentation for contextual profiling
+def int_instrprof_callsite : Intrinsic<[],
+                                        [llvm_ptr_ty, llvm_i64_ty,
+                                         llvm_i32_ty, llvm_i32_ty, llvm_ptr_ty]>;
+
 // A timestamp for instrumentation based profiling.
 def int_instrprof_timestamp : Intrinsic<[], [llvm_ptr_ty, llvm_i64_ty,
                                              llvm_i32_ty, llvm_i32_ty]>;
diff --git a/llvm/unittests/IR/IntrinsicsTest.cpp b/llvm/unittests/IR/IntrinsicsTest.cpp
index 3fa4b2cf73b6be..dddd2f73d4446b 100644
--- a/llvm/unittests/IR/IntrinsicsTest.cpp
+++ b/llvm/unittests/IR/IntrinsicsTest.cpp
@@ -81,6 +81,7 @@ TEST_F(IntrinsicsTest, InstrProfInheritance) {
   __ISA(InstrProfCoverInst, InstrProfCntrInstBase);
   __ISA(InstrProfIncrementInst, InstrProfCntrInstBase);
   __ISA(InstrProfIncrementInstStep, InstrProfIncrementInst);
+  __ISA(InstrProfCallsite, InstrProfCntrInstBase);
   __ISA(InstrProfTimestampInst, InstrProfCntrInstBase);
   __ISA(InstrProfValueProfileInst, InstrProfCntrInstBase);
   __ISA(InstrProfMCDCBitmapInstBase, InstrProfInstBase);
@@ -94,6 +95,7 @@ TEST_F(IntrinsicsTest, InstrProfInheritance) {
           {Intrinsic::instrprof_cover, isInstrProfCoverInst},
           {Intrinsic::instrprof_increment, isInstrProfIncrementInst},
           {Intrinsic::instrprof_increment_step, isInstrProfIncrementInstStep},
+          {Intrinsic::instrprof_callsite, isInstrProfCallsite},
           {Intrinsic::instrprof_mcdc_condbitmap_update,
            isInstrProfMCDCCondBitmapUpdate},
           {Intrinsic::instrprof_mcdc_parameters,

Copy link
Contributor

@ellishg ellishg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also update the docs?

'``llvm.instrprof.timestamp``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

@@ -1519,6 +1520,21 @@ class InstrProfIncrementInstStep : public InstrProfIncrementInst {
}
};

/// This represents the llvm.instrprof.increment intrinsic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// This represents the llvm.instrprof.increment intrinsic.
/// This represents the llvm.instrprof.callsite intrinsic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done - thanks for the catch!

@mtrofin
Copy link
Member Author

mtrofin commented Apr 24, 2024

Can you also update the docs?

'``llvm.instrprof.timestamp``' Intrinsic
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

done. I'll also update the docs again once the ctx lowering pass is in, because the other intrinsics' docs are too specific. The doc for this one can become more precise at that point, too.

Copy link

github-actions bot commented Apr 24, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.


Arguments:
""""""""""
The first 3 arguments are similar to ``llvm.instrprof.increment``. The indexing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to have this documentation stand alone and describe the parameters here instead of relying on the reader to go look up the documentation of the llvm.instrprof.increment intrinsic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the style of the rest of the documentation, see the intrinsics above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take the opportunity to improve instead of following what exists. I think it's low overhead to include it in here inline.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I meant it as the typical pattern for this type of thing, didn't want to type that: either we fix the rest, too (separate change); or we think this is actually a good think for some reason (and then that separate change can be rejected). Either way, seems like a separate change.

Overview:
"""""""""

The '``llvm.instrprof.callsite``' intrinsic should be emitted before a callsite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"should be emitted" -> can you specify under what modes this intrinsic is emitted (or is it always as this statement implies)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update this after I introduce the pass.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a FIXME? Same for the other one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


Semantics:
""""""""""
This is lowered by contextual profiling.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand this a bit to spell out how it is used (apart from just mentioning contextual profiling) like how it should be lowered, does it modify program behaviour, should it be treated as immovable by optimizations, when it should be ignored by backends and so on?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, I will need to update after I land the lowering pass.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -291,6 +291,8 @@ Value *InstrProfIncrementInst::getStep() const {
return ConstantInt::get(Type::getInt64Ty(Context), 1);
}

Value *InstrProfCallsite::getCallee() const { return getArgOperand(4); }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need a check like L286 in getStep above to get the callee? If not can you add a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, because nothing stops someone from reinterpret_cast-ing. Fixed.

Copy link
Contributor

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, with some minor nits.

Overview:
"""""""""

The '``llvm.instrprof.callsite``' intrinsic should be emitted before a callsite
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a FIXME? Same for the other one.


Arguments:
""""""""""
The first 3 arguments are similar to ``llvm.instrprof.increment``. The indexing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should take the opportunity to improve instead of following what exists. I think it's low overhead to include it in here inline.

@mtrofin mtrofin merged commit ddb67e6 into llvm:main Apr 25, 2024
4 of 5 checks passed
@mtrofin mtrofin deleted the callsite branch April 25, 2024 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants