diff --git a/imperative/src/impl/interpreter/commands.h b/imperative/src/impl/interpreter/commands.h index e3c632058fb1fe5b1984652379275e527f620754..ed38e3e90c1a26b448c21b0247fa4dbb00a5d261 100644 --- a/imperative/src/impl/interpreter/commands.h +++ b/imperative/src/impl/interpreter/commands.h @@ -213,7 +213,7 @@ using IdentifiedCommand = std::pair; template <> struct ToStringTrait{ std::string operator()(const interpreter::intl::Command& cmd) const { - return std::visit([](auto& cmd){ + return std::visit([](const auto& cmd){ std::string result = cmd.get_name(); result += "{"; cmd.get_props([&](const char* key, auto&& value) { diff --git a/imperative/src/impl/interpreter/interpreter_impl.cpp b/imperative/src/impl/interpreter/interpreter_impl.cpp index f6158498f296734abf0c7c43c7ae7ada5331d862..4430c2fcfdaf701030ffe68f0dafe4fa68e6d0bb 100644 --- a/imperative/src/impl/interpreter/interpreter_impl.cpp +++ b/imperative/src/impl/interpreter/interpreter_impl.cpp @@ -481,8 +481,8 @@ void ChannelImpl::process_one_task(IdentifiedCommand& icmd) { finished = true; }; //TODO: remove std::visit for support osx 10.12 - auto cmd_visitor = [&](auto& cmd) { - using T = std::remove_reference_t; + auto cmd_visitor = [&](const auto& cmd) { + using T = std::decay_t; if constexpr (std::is_same_v) { auto value = cmd.no_cache ? std::make_shared(cmd.value) : Tensor::make(cmd.value); produce_tensor(cmd.dest, std::move(value)); @@ -598,10 +598,10 @@ void ChannelImpl::process_one_task(IdentifiedCommand& icmd) { do_finish_command(); m_worker_state.profiler->record_host(cmd.scope_name); } else { - static_assert(std::is_same_v); + static_assert(!std::is_same_v); } }; - std::visit([&](auto& cmd){ + std::visit([&](const auto& cmd){ using T = std::decay_t; if (!m_worker_state.options.catch_worker_execption) { cmd_visitor(cmd); diff --git a/imperative/src/impl/interpreter/profiler.cpp b/imperative/src/impl/interpreter/profiler.cpp index 7517b4858d865c8c03b27e3f4b5287a0c33d86be..5d294aecae97c40de26f5ebe6755d0ba626ddedf 100644 --- a/imperative/src/impl/interpreter/profiler.cpp +++ b/imperative/src/impl/interpreter/profiler.cpp @@ -97,7 +97,7 @@ struct InterpreterProfilerDumpChromeTimelineContext { }; // convert Command to json object. Has to be an callable object - static auto constexpr cmd_to_args = [](auto&& cmd) { + static auto constexpr cmd_to_args = [](const auto& cmd) { auto args = json::Object::make(); cmd.get_props([&](const char* key, auto&& value){ (*args)[key] = json::String::make(to_string(value)); @@ -108,14 +108,14 @@ struct InterpreterProfilerDumpChromeTimelineContext { void process() { // enumerate and process each record - for (auto&& record: profile_data.records) { - std::visit([this](auto& record){ + for (auto& record: profile_data.records) { + std::visit([this](const auto& record){ using TEvent = std::decay_t; Session(*this, record).process(); }, record); } for (size_t tid = 0; tid < thread_list.size(); ++tid) { - auto tname = std::visit([&](auto& host_or_device) -> std::string{ + auto tname = std::visit([&](auto host_or_device) -> std::string{ using T = std::decay_t; if constexpr (std::is_same_v) { // take name from host_map @@ -142,11 +142,11 @@ struct InterpreterProfilerDumpChromeTimelineContext { template struct Session { InterpreterProfilerDumpChromeTimelineContext& ctx; - ProfilerBase::EventRecord& record; - TEvent& data; + const ProfilerBase::EventRecord& record; + const TEvent& data; Session(InterpreterProfilerDumpChromeTimelineContext& ctx, - ProfilerBase::EventRecord& record) + const ProfilerBase::EventRecord& record) : ctx{ctx}, record{record}, data{record.data} {} uint64_t get_host_tid() { diff --git a/imperative/src/include/megbrain/imperative/profiler.h b/imperative/src/include/megbrain/imperative/profiler.h index e687bea2d3b2f8800add80f39602e79171c0e58f..702befabf3763287dec10a56b5d2e52678983369 100644 --- a/imperative/src/include/megbrain/imperative/profiler.h +++ b/imperative/src/include/megbrain/imperative/profiler.h @@ -57,7 +57,7 @@ public: Host tid; double time; - void wait() {} + void wait() const {} }; struct DeviceInstant { @@ -65,7 +65,7 @@ public: std::shared_ptr event; double after; - void wait() { + void wait() const { event->host_wait(); } }; @@ -77,16 +77,16 @@ public: Instant instant; TEvent data; - HostInstant& host() { + const HostInstant& host() const { return std::get(instant); } - DeviceInstant device() { + const DeviceInstant& device() const { return std::get(instant); } - void wait() { - std::visit([&](auto& instant){ instant.wait(); }, instant); + void wait() const { + std::visit([&](const auto& instant){ instant.wait(); }, instant); } }; protected: @@ -173,7 +173,7 @@ public: mgb_assert(m_status == Profiling, "profiler not active"); m_status = Stopped; for (auto&& record: m_record_list) { - std::visit([&](auto& record){ + std::visit([&](const auto& record){ record.wait(); }, record); } @@ -312,7 +312,7 @@ public: return m_content.back(); } - std::shared_ptr to_json() { + std::shared_ptr to_json() const { auto result = json::Array::make(); for (auto&& event: m_content) { result->add(event.to_json());