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

[Cuda] Simplify and optimise the getMaxRegistersJitOptionValue utility #2110

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions source/adapters/cuda/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,50 @@

#include <algorithm>
#include <array>
#include <charconv>
#include <cstdint>
#include <cstring>
#include <memory>
#include <new>
#include <string>
#include <system_error>
#include <tuple>
#include <utility>
#include <vector>

bool getMaxRegistersJitOptionValue(const std::string &BuildOptions,
unsigned int &Value) {
static unsigned int
parseMaxRegistersJitOptionValue(std::string_view BuildOptions) {
// Note: The default value of 'CU_JIT_MAX_REGISTERS' is 0 if no user-defined
// limit is provided. This implies the limit by the underlying architecture.
static constexpr const unsigned int DefaultValue{0u};

using namespace std::string_view_literals;
const std::size_t OptionPos = BuildOptions.find_first_of("maxrregcount"sv);
if (OptionPos == std::string::npos) {
return false;
}
if (OptionPos == std::string::npos)
return DefaultValue;

const std::size_t DelimPos = BuildOptions.find('=', OptionPos + 1u);
if (DelimPos == std::string::npos) {
return false;
}
if (DelimPos == std::string::npos)
return DefaultValue;

const std::size_t Length = BuildOptions.length();
const std::size_t StartPos = DelimPos + 1u;
if (DelimPos == std::string::npos || StartPos >= Length) {
return false;
}
if (DelimPos == std::string::npos || StartPos >= Length)
return DefaultValue;

std::size_t Pos = StartPos;
while (Pos < Length &&
std::isdigit(static_cast<unsigned char>(BuildOptions[Pos]))) {
std::isdigit(static_cast<unsigned char>(BuildOptions[Pos])))
Pos++;
}

const std::string ValueString = BuildOptions.substr(StartPos, Pos - StartPos);
if (ValueString.empty()) {
return false;
}
const auto ValueString = BuildOptions.substr(StartPos, Pos - StartPos);
if (ValueString.empty())
return DefaultValue;

Value = static_cast<unsigned int>(std::stoi(ValueString));
return true;
unsigned int Value{};
auto [ErrStr, Errc] = std::from_chars(
ValueString.data(), ValueString.data() + ValueString.size(), Value);
return Errc == std::errc() ? Value : DefaultValue;
}

ur_result_t
Expand Down Expand Up @@ -137,14 +140,11 @@ ur_result_t ur_program_handle_t_::buildProgram(const char *BuildOptions) {
OptionVals[3] = (void *)(long)MaxLogSize;

if (!this->BuildOptions.empty()) {
unsigned int MaxRegs;
const bool Valid =
getMaxRegistersJitOptionValue(this->BuildOptions, MaxRegs);
if (Valid) {
Options.push_back(CU_JIT_MAX_REGISTERS);
OptionVals.push_back(
reinterpret_cast<void *>(static_cast<std::uintptr_t>(MaxRegs)));
}
unsigned int MaxRegs = parseMaxRegistersJitOptionValue(this->BuildOptions);
std::cout << "Parsed MaxRegs: " << MaxRegs << "\n";
Options.push_back(CU_JIT_MAX_REGISTERS);
OptionVals.push_back(
reinterpret_cast<void *>(static_cast<std::uintptr_t>(MaxRegs)));
}

UR_CHECK_ERROR(cuModuleLoadDataEx(&Module, static_cast<const void *>(Binary),
Expand Down
Loading