Skip to content

Commit 0cec59e

Browse files
authored
[SYCL] Change emitKernelInstrumentationData to avoid temporary std::string object creation (#17895)
Change the `emitKernelInstrumentationData` to accept `std::string_view` instead of `std::string` to avoid temporary `std::string` object creation on a hot path.
1 parent 99277f6 commit 0cec59e

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

sycl/source/detail/scheduler/commands.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ struct DemangleHandle {
136136

137137
~DemangleHandle() { std::free(p); }
138138
};
139-
static std::string demangleKernelName(std::string Name) {
139+
static std::string demangleKernelName(const std::string_view Name) {
140140
int Status = -1; // some arbitrary value to eliminate the compiler warning
141-
DemangleHandle result(abi::__cxa_demangle(Name.c_str(), NULL, NULL, &Status));
142-
return (Status == 0) ? result.p : Name;
141+
DemangleHandle result(abi::__cxa_demangle(Name.data(), NULL, NULL, &Status));
142+
return (Status == 0) ? result.p : std::string(Name);
143143
}
144144
#else
145-
static std::string demangleKernelName(std::string Name) { return Name; }
145+
static std::string demangleKernelName(const std::string_view Name) {
146+
return std::string(Name);
147+
}
146148
#endif
147149

148150
static std::string accessModeToString(access::mode Mode) {
@@ -1961,7 +1963,7 @@ ExecCGCommand::ExecCGCommand(
19611963
#ifdef XPTI_ENABLE_INSTRUMENTATION
19621964
std::string instrumentationGetKernelName(
19631965
const std::shared_ptr<detail::kernel_impl> &SyclKernel,
1964-
const std::string &FunctionName, const std::string &SyclKernelName,
1966+
const std::string_view FunctionName, const std::string_view SyclKernelName,
19651967
void *&Address, std::optional<bool> &FromSource) {
19661968
std::string KernelName;
19671969
if (SyclKernel && SyclKernel->isCreatedFromSource()) {
@@ -2049,17 +2051,17 @@ void instrumentationFillCommonData(const std::string &KernelName,
20492051
xpti::payload_t Payload;
20502052
if (!FileName.empty()) {
20512053
// File name has a valid string
2052-
Payload = xpti::payload_t(FuncName.empty() ? KernelName.c_str()
2053-
: FuncName.c_str(),
2054-
FileName.c_str(), Line, Column, Address);
2054+
Payload =
2055+
xpti::payload_t(FuncName.empty() ? KernelName.data() : FuncName.data(),
2056+
FileName.data(), Line, Column, Address);
20552057
HasSourceInfo = true;
20562058
} else if (Address) {
20572059
// We have a valid function name and an address
2058-
Payload = xpti::payload_t(KernelName.c_str(), Address);
2060+
Payload = xpti::payload_t(KernelName.data(), Address);
20592061
} else {
20602062
// In any case, we will have a valid function name and we'll use that to
20612063
// create the hash
2062-
Payload = xpti::payload_t(KernelName.c_str());
2064+
Payload = xpti::payload_t(KernelName.data());
20632065
}
20642066

20652067
uint64_t CGKernelInstanceNo;
@@ -2096,7 +2098,7 @@ void instrumentationFillCommonData(const std::string &KernelName,
20962098
std::pair<xpti_td *, uint64_t> emitKernelInstrumentationData(
20972099
int32_t StreamID, const std::shared_ptr<detail::kernel_impl> &SyclKernel,
20982100
const detail::code_location &CodeLoc, bool IsTopCodeLoc,
2099-
const std::string &SyclKernelName, const QueueImplPtr &Queue,
2101+
const std::string_view SyclKernelName, const QueueImplPtr &Queue,
21002102
const NDRDescT &NDRDesc,
21012103
const std::shared_ptr<detail::kernel_bundle_impl> &KernelBundleImplPtr,
21022104
std::vector<ArgDesc> &CGArgs) {
@@ -2109,8 +2111,7 @@ std::pair<xpti_td *, uint64_t> emitKernelInstrumentationData(
21092111
void *Address = nullptr;
21102112
std::optional<bool> FromSource;
21112113
std::string KernelName = instrumentationGetKernelName(
2112-
SyclKernel, std::string(CodeLoc.functionName()), SyclKernelName, Address,
2113-
FromSource);
2114+
SyclKernel, CodeLoc.functionName(), SyclKernelName, Address, FromSource);
21142115

21152116
auto &[CmdTraceEvent, InstanceID] = XptiObjects;
21162117

@@ -2135,9 +2136,9 @@ std::pair<xpti_td *, uint64_t> emitKernelInstrumentationData(
21352136
if (Queue.get())
21362137
xpti::framework::stash_tuple(XPTI_QUEUE_INSTANCE_ID_KEY,
21372138
getQueueID(Queue));
2138-
instrumentationAddExtraKernelMetadata(CmdTraceEvent, NDRDesc,
2139-
KernelBundleImplPtr, SyclKernelName,
2140-
SyclKernel, Queue, CGArgs);
2139+
instrumentationAddExtraKernelMetadata(
2140+
CmdTraceEvent, NDRDesc, KernelBundleImplPtr,
2141+
std::string(SyclKernelName), SyclKernel, Queue, CGArgs);
21412142

21422143
xptiNotifySubscribers(
21432144
StreamID, NotificationTraceType, detail::GSYCLGraphEvent, CmdTraceEvent,

sycl/source/detail/scheduler/commands.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ class ExecCGCommand : public Command {
686686
std::pair<xpti_td *, uint64_t> emitKernelInstrumentationData(
687687
int32_t StreamID, const std::shared_ptr<detail::kernel_impl> &SyclKernel,
688688
const detail::code_location &CodeLoc, bool IsTopCodeLoc,
689-
const std::string &SyclKernelName, const QueueImplPtr &Queue,
689+
std::string_view SyclKernelName, const QueueImplPtr &Queue,
690690
const NDRDescT &NDRDesc,
691691
const std::shared_ptr<detail::kernel_bundle_impl> &KernelBundleImplPtr,
692692
std::vector<ArgDesc> &CGArgs);

0 commit comments

Comments
 (0)