-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[ctxprof] ProfileWriter abstraction #129590
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
[ctxprof] ProfileWriter abstraction #129590
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
91ab7e6
to
21d73ce
Compare
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.
LGTM.
21d73ce
to
5b223e7
Compare
@llvm/pr-subscribers-pgo Author: Mircea Trofin (mtrofin) ChangesIntroduce a Full diff: https://github.com/llvm/llvm-project/pull/129590.diff 6 Files Affected:
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
index 36a996632b71e..6b020733e1f37 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
@@ -112,6 +112,14 @@ class ContextNode final {
uint64_t entrycount() const { return counters()[0]; }
};
+
+class ProfileWriter {
+public:
+ virtual void startContextSection() = 0;
+ virtual void writeContextual(const ctx_profile::ContextNode &RootNode) = 0;
+ virtual void endContextSection() = 0;
+ virtual ~ProfileWriter() = default;
+};
} // namespace ctx_profile
} // namespace llvm
#endif
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index df30986cdfc69..992aa94a6631d 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -294,12 +294,11 @@ void __llvm_ctx_profile_start_collection() {
__sanitizer::Printf("[ctxprof] Initial NumMemUnits: %zu \n", NumMemUnits);
}
-bool __llvm_ctx_profile_fetch(void *Data,
- bool (*Writer)(void *W, const ContextNode &)) {
- assert(Writer);
+bool __llvm_ctx_profile_fetch(ProfileWriter &Writer) {
__sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
&AllContextsMutex);
+ Writer.startContextSection();
for (int I = 0, E = AllContextRoots.Size(); I < E; ++I) {
auto *Root = AllContextRoots[I];
__sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> TakenLock(
@@ -308,9 +307,9 @@ bool __llvm_ctx_profile_fetch(void *Data,
__sanitizer::Printf("[ctxprof] Contextual Profile is %s\n", "invalid");
return false;
}
- if (!Writer(Data, *Root->FirstNode))
- return false;
+ Writer.writeContextual(*Root->FirstNode);
}
+ Writer.endContextSection();
return true;
}
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
index 74d346d6e0a07..8a6949d4ec288 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.h
@@ -169,7 +169,6 @@ void __llvm_ctx_profile_free();
/// The Writer's first parameter plays the role of closure for Writer, and is
/// what the caller of __llvm_ctx_profile_fetch passes as the Data parameter.
/// The second parameter is the root of a context tree.
-bool __llvm_ctx_profile_fetch(void *Data,
- bool (*Writer)(void *, const ContextNode &));
+bool __llvm_ctx_profile_fetch(ProfileWriter &);
}
#endif // CTX_PROFILE_CTXINSTRPROFILING_H_
diff --git a/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp b/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
index d9f08b1e7efe8..d9183a8a0ff76 100644
--- a/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
+++ b/compiler-rt/lib/ctx_profile/tests/CtxInstrProfilingTest.cpp
@@ -179,13 +179,15 @@ TEST_F(ContextTest, Dump) {
(void)Subctx;
__llvm_ctx_profile_release_context(&Root);
- struct Writer {
+ struct Writer : public ProfileWriter {
ContextRoot *const Root;
const size_t Entries;
bool State = false;
Writer(ContextRoot *Root, size_t Entries) : Root(Root), Entries(Entries) {}
- bool write(const ContextNode &Node) {
+ void startContextSection() override {}
+ void endContextSection() override {}
+ void writeContextual(const ContextNode &Node) override {
EXPECT_FALSE(Root->Taken.TryLock());
EXPECT_EQ(Node.guid(), 1U);
EXPECT_EQ(Node.counters()[0], Entries);
@@ -202,22 +204,17 @@ TEST_F(ContextTest, Dump) {
EXPECT_EQ(SN.callsites_size(), 1U);
EXPECT_EQ(SN.subContexts()[0], nullptr);
State = true;
- return true;
}
};
Writer W(&Root, 1);
EXPECT_FALSE(W.State);
- __llvm_ctx_profile_fetch(&W, [](void *W, const ContextNode &Node) -> bool {
- return reinterpret_cast<Writer *>(W)->write(Node);
- });
+ __llvm_ctx_profile_fetch(W);
EXPECT_TRUE(W.State);
// this resets all counters but not the internal structure.
__llvm_ctx_profile_start_collection();
Writer W2(&Root, 0);
EXPECT_FALSE(W2.State);
- __llvm_ctx_profile_fetch(&W2, [](void *W, const ContextNode &Node) -> bool {
- return reinterpret_cast<Writer *>(W)->write(Node);
- });
+ __llvm_ctx_profile_fetch(W2);
EXPECT_TRUE(W2.State);
}
diff --git a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
index 797b871860655..c26f4edfe783e 100644
--- a/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
+++ b/compiler-rt/test/ctx_profile/TestCases/generate-context.cpp
@@ -15,9 +15,7 @@
#include <iostream>
using namespace llvm::ctx_profile;
-extern "C" bool __llvm_ctx_profile_fetch(void *Data,
- bool (*Writer)(void *,
- const ContextNode &));
+extern "C" bool __llvm_ctx_profile_fetch(ProfileWriter &);
// avoid name mangling
extern "C" {
@@ -46,22 +44,31 @@ __attribute__((noinline)) void theRoot() {
// CHECK-NEXT: check even
// CHECK-NEXT: check odd
-void printProfile(const ContextNode &Node, const std::string &Indent,
- const std::string &Increment) {
- std::cout << Indent << "Guid: " << Node.guid() << std::endl;
- std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
- std::cout << Indent << Node.counters_size() << " counters and "
- << Node.callsites_size() << " callsites" << std::endl;
- std::cout << Indent << "Counter values: ";
- for (uint32_t I = 0U; I < Node.counters_size(); ++I)
- std::cout << Node.counters()[I] << " ";
- std::cout << std::endl;
- for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
- for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
- std::cout << Indent << "At Index " << I << ":" << std::endl;
- printProfile(*N, Indent + Increment, Increment);
- }
-}
+class TestProfileWriter : public ProfileWriter {
+ void printProfile(const ContextNode &Node, const std::string &Indent,
+ const std::string &Increment) {
+ std::cout << Indent << "Guid: " << Node.guid() << std::endl;
+ std::cout << Indent << "Entries: " << Node.entrycount() << std::endl;
+ std::cout << Indent << Node.counters_size() << " counters and "
+ << Node.callsites_size() << " callsites" << std::endl;
+ std::cout << Indent << "Counter values: ";
+ for (uint32_t I = 0U; I < Node.counters_size(); ++I)
+ std::cout << Node.counters()[I] << " ";
+ std::cout << std::endl;
+ for (uint32_t I = 0U; I < Node.callsites_size(); ++I)
+ for (const auto *N = Node.subContexts()[I]; N; N = N->next()) {
+ std::cout << Indent << "At Index " << I << ":" << std::endl;
+ printProfile(*N, Indent + Increment, Increment);
+ }
+ }
+
+public:
+ void startContextSection() override {}
+ void writeContextual(const ContextNode &RootNode) override {
+ printProfile(RootNode, "", "");
+ }
+ void endContextSection() override {}
+};
// 8657661246551306189 is theRoot. We expect 2 callsites and 2 counters - one
// for the entry basic block and one for the loop.
@@ -88,11 +95,8 @@ void printProfile(const ContextNode &Node, const std::string &Indent,
// CHECK-NEXT: Counter values: 2 1
bool profileWriter() {
- return __llvm_ctx_profile_fetch(
- nullptr, +[](void *, const ContextNode &Node) {
- printProfile(Node, "", " ");
- return true;
- });
+ TestProfileWriter W;
+ return __llvm_ctx_profile_fetch(W);
}
int main(int argc, char **argv) {
diff --git a/llvm/include/llvm/ProfileData/CtxInstrContextNode.h b/llvm/include/llvm/ProfileData/CtxInstrContextNode.h
index 36a996632b71e..6b020733e1f37 100644
--- a/llvm/include/llvm/ProfileData/CtxInstrContextNode.h
+++ b/llvm/include/llvm/ProfileData/CtxInstrContextNode.h
@@ -112,6 +112,14 @@ class ContextNode final {
uint64_t entrycount() const { return counters()[0]; }
};
+
+class ProfileWriter {
+public:
+ virtual void startContextSection() = 0;
+ virtual void writeContextual(const ctx_profile::ContextNode &RootNode) = 0;
+ virtual void endContextSection() = 0;
+ virtual ~ProfileWriter() = default;
+};
} // namespace ctx_profile
} // namespace llvm
#endif
|
5b223e7
to
ce10a34
Compare
} | ||
} | ||
class TestProfileWriter : public ProfileWriter { | ||
void printProfile(const ContextNode &Node, const std::string &Indent, |
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.
I think it might be usefult to pass in a llvm::raw_ostream&
and then writing to it. I think this will give you more flexibility based when used in tests (e.g using a string buffer).
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.
This is a .cpp file that gets compiled (and run) as part of the test, so we basically have access to std::
things.
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.
To make things clear, here's an example of the usage I'm thinking of: https://github.com/llvm/llvm-project/blob/main/llvm/unittests/IR/MetadataTest.cpp#L164-L172
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.
Just to make sure we're talking about the same thing: this here is a lit test. Note the RUN lines at the top of the file.
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.
Discussed offline and since this in compiler-rt its not useful to use ostream.
@@ -179,13 +179,15 @@ TEST_F(ContextTest, Dump) { | |||
(void)Subctx; | |||
__llvm_ctx_profile_release_context(&Root); | |||
|
|||
struct Writer { | |||
struct Writer : public ProfileWriter { |
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.
Its a bit strange to have Writer inherit from ProfileWriter. To me it makes more sense to call the interface Writer
and the derived class / struct profile writer here. Wdyt?
Also a struct derived from a class seems strange to me.
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.
Writer
is too generic. I only use it here because this is a test. I can rename it to TestProfileWriter
(and make it a class, that is weird, indeed)
@@ -112,6 +112,12 @@ class ContextNode final { | |||
|
|||
uint64_t entrycount() const { return counters()[0]; } | |||
}; | |||
|
|||
class ProfileWriter { |
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.
Add a comment to describe the interface and expected usage?
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 - briefly. Updated further up the change stack, where more members appear.
} | ||
|
||
public: | ||
void startContextSection() override {} |
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.
startContextSection
and endContextSection
are empty funcs in both derived classes. Maybe introduce them when they are used? Also they aren't in the base class so override seems unneccessary?
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.
Removed them.
ce10a34
to
8fed7ea
Compare
✅ With the latest revision this PR passed the C/C++ code formatter. |
8773741
to
7c64ba2
Compare
7c64ba2
to
cb3945b
Compare
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.
lgtm
Introduce a `ProfileWriter` abstraction to replace the callback passed to `__llvm_ctx_profile_fetch`. Subsequent changes will add support for flat profile collection (as in, collection of non-contextual profile for those functions not under a contextual root), which require also a change in the profile format. The abstraction makes it easy to add "write flat" - related capabilities without constantly complicating the signature of `__llvm_ctx_profile_fetch`.
Introduce a
ProfileWriter
abstraction to replace the callback passed to__llvm_ctx_profile_fetch
. Subsequent changes will add support for flat profile collection (as in, collection of non-contextual profile for those functions not under a contextual root), which require also a change in the profile format. The abstraction makes it easy to add "write flat" - related capabilities without constantly complicating the signature of__llvm_ctx_profile_fetch
.