Skip to content

Commit

Permalink
Fixes #529: rtc::program_t::add_registered_globals() can now also t…
Browse files Browse the repository at this point in the history
…ake an lvalue-ref container of std::string's, plus comment and documentation tweaks.
  • Loading branch information
eyalroz committed Jun 11, 2023
1 parent d655848 commit 2be698c
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/cuda/rtc/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,15 @@ class program_t<cuda_cpp> : public program_base_t<cuda_cpp> {
}

/**
* @brief Register a pre-mangled name of a kernel, to make available for use
* @brief Register a pre-mangled name of a global, to make available for use
* after compilation
*
* @param name The text of an expression, e.g. "my_global_func()", "f1", "N1::N2::n2",
* @param unmangled_name The text of an expression, e.g. "my_global_func()", "f1", "N1::N2::n2",
*
* @note The name must continue to exist past the compilation of the program - as it is not copied,
* only referenced
*/
///@{
program_t& add_registered_global(const char* unmangled_name)
{
globals_to_register_.push_back(unmangled_name);
Expand All @@ -455,15 +458,41 @@ class program_t<cuda_cpp> : public program_base_t<cuda_cpp> {
globals_to_register_.push_back(unmangled_name.c_str());
return *this;
}
// TODO: Accept string_view's with C++17
///@}

/**
* @brief Register multiple pre-mangled names of global, to make available for use
* after compilation
*
* @param globals_to_register a container of elements constituting the text of an expression
* identifying a global, e.g. "my_global_func()", "f1", "N1::N2::n2",
*
* @note All names in the container must continue to exist past the compilation of the
* program - as they are not copied, only referenced. Thus, as a safety precaution, we
* also assume the container continues to exist
*/
///@{
template <typename Container>
program_t& add_registered_globals(Container&& globals_to_register)
program_t& add_registered_globals(const Container& globals_to_register)
{
globals_to_register_.reserve(globals_to_register_.size() + globals_to_register.size());
::std::copy(globals_to_register.cbegin(), globals_to_register.cend(), ::std::back_inserter(globals_to_register_));
for(const auto& global_name : globals_to_register) {
add_registered_global(global_name);
}
return *this;
}

template <typename Container>
program_t& add_registered_globals(Container&& globals_to_register)
{
static_assert(::std::is_same<typename Container::value_type, const char*>::value,
"For an rvalue container, we only accept raw C strings as the value type, to prevent"
"the possible passing of string-like objects at the end of their lifetime");
return add_registered_globals(static_cast<const Container&>(globals_to_register));
}
///@}

public: // constructors and destructor
program_t(::std::string name) : program_base_t(::std::move(name)) {}
program_t(const program_t&) = default;
Expand Down

0 comments on commit 2be698c

Please sign in to comment.