Skip to content
Merged
Show file tree
Hide file tree
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
13 changes: 8 additions & 5 deletions unified-runtime/source/common/logger/ur_sinks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ class Sink {
<< "[" << level_to_str(level) << "]: ";
}

format(buffer, filename, lineno, fmt, std::forward<Args &&>(args)...);
format(buffer, filename, lineno, fmt, std::forward<Args>(args)...);
if (add_fileline) {
buffer << " <" << filename << ":" << lineno << ">";
}
if (!skip_linebreak) {
buffer << "\n";
}

std::string message = buffer.str();

// This is a temporary workaround on windows, where UR adapter is teardowned
// before the UR loader, which will result in access violation when we use print
// function as the overrided print function was already released with the UR
Expand All @@ -50,12 +53,12 @@ class Sink {
// using thier own sink class that inherit from logger::Sink.
#if defined(_WIN32)
if (isTearDowned) {
std::cerr << buffer.str() << "\n";
std::cerr << message << "\n";
} else {
print(level, buffer.str());
print(level, message);
}
#else
print(level, buffer.str());
print(level, message);
#endif
}

Expand Down Expand Up @@ -155,7 +158,7 @@ class Sink {
}
}

format(buffer, filename, lineno, ++fmt, std::forward<Args &&>(args)...);
format(buffer, filename, lineno, ++fmt, std::forward<Args>(args)...);
}
};

Expand Down
57 changes: 34 additions & 23 deletions unified-runtime/source/common/ur_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ getenv_to_vec(const char *env_var_name) {
return std::nullopt;
}

auto is_quoted = [](std::string &str) {
return (str.front() == '\'' && str.back() == '\'') ||
(str.front() == '"' && str.back() == '"');
auto is_quoted = [](const std::string &str) {
return str.size() >= 2 && ((str.front() == '\'' && str.back() == '\'') ||
(str.front() == '"' && str.back() == '"'));
};
auto has_colon = [](std::string &str) {
auto has_colon = [](const std::string &str) {
return str.find(':') != std::string::npos;
};
auto has_semicolon = [](std::string &str) {
auto has_semicolon = [](const std::string &str) {
return str.find(';') != std::string::npos;
};

Expand All @@ -188,8 +188,7 @@ getenv_to_vec(const char *env_var_name) {
}

if (is_quoted(value)) {
value.erase(value.cbegin());
value.erase(value.cend() - 1);
value = value.substr(1, value.length() - 2);
}

values_vec.push_back(value);
Expand Down Expand Up @@ -238,27 +237,29 @@ inline std::optional<EnvVarMap> getenv_to_map(const char *env_var_name,
return std::nullopt;
}

auto is_quoted = [](std::string &str) {
return (str.front() == '\'' && str.back() == '\'') ||
(str.front() == '"' && str.back() == '"');
auto is_quoted = [](const std::string &str) {
return str.size() >= 2 && ((str.front() == '\'' && str.back() == '\'') ||
(str.front() == '"' && str.back() == '"'));
};
auto has_colon = [](std::string &str) {
auto has_colon = [](const std::string &str) {
return str.find(':') != std::string::npos;
};

std::stringstream ss(*env_var);
std::string key_value;
while (std::getline(ss, key_value, main_delim)) {
std::string key;
std::string values;
std::stringstream kv_ss(key_value);

if (reject_empty && !has_colon(key_value)) {
throw_wrong_format_map(env_var_name, *env_var);
}

std::getline(kv_ss, key, key_value_delim);
std::getline(kv_ss, values);
size_t colon_pos = key_value.find(key_value_delim);
if (colon_pos == std::string::npos) {
throw_wrong_format_map(env_var_name, *env_var);
}

std::string key = key_value.substr(0, colon_pos);
std::string values = key_value.substr(colon_pos + 1);

if (key.empty() || (reject_empty && values.empty()) ||
(map.find(key) != map.end() && !allow_duplicate)) {
throw_wrong_format_map(env_var_name, *env_var);
Expand All @@ -269,18 +270,28 @@ inline std::optional<EnvVarMap> getenv_to_map(const char *env_var_name,
}

std::vector<std::string> values_vec;
std::stringstream values_ss(values);
std::string value;
while (std::getline(values_ss, value, values_delim)) {

size_t start = 0;
size_t pos = 0;
while ((pos = values.find(values_delim, start)) != std::string::npos ||
start < values.length()) {
// No delimiter found, process the last value
if (pos == std::string::npos) {
pos = values.length();
}

std::string value = values.substr(start, pos - start);
if (value.empty() || (has_colon(value) && !is_quoted(value))) {
throw_wrong_format_map(env_var_name, *env_var);
}
if (is_quoted(value)) {
value.erase(value.cbegin());
value.pop_back();
value = value.substr(1, value.length() - 2);
}
values_vec.push_back(value);
values_vec.push_back(std::move(value));

start = pos + 1;
}

if (map.find(key) != map.end()) {
map[key].insert(map[key].end(), values_vec.begin(), values_vec.end());
} else {
Expand Down
10 changes: 5 additions & 5 deletions unified-runtime/source/loader/ur_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ context_t::context_t() { parseEnvEnabledLayers(); }
context_t::~context_t() {}

void context_t::parseEnvEnabledLayers() {
auto maybeEnableEnvVarMap = getenv_to_map("UR_ENABLE_LAYERS", false);
if (!maybeEnableEnvVarMap.has_value()) {
auto maybeEnableEnvVarVec = getenv_to_vec("UR_ENABLE_LAYERS");
if (!maybeEnableEnvVarVec.has_value()) {
return;
}
auto enableEnvVarMap = maybeEnableEnvVarMap.value();
auto enableEnvVarVec = maybeEnableEnvVarVec.value();

for (auto &key : enableEnvVarMap) {
enabledLayerNames.insert(key.first);
for (auto &layer : enableEnvVarVec) {
enabledLayerNames.insert(layer);
}
}

Expand Down