/******************************************************************************* * * MIT License * * Copyright (c) 2025 Advanced Micro Devices, Inc. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * *******************************************************************************/ #pragma once #include #include #include #include #include namespace lib_env { struct LibEnvVar { constexpr LibEnvVar(std::string_view name_in) : name(name_in) {} explicit operator bool() const { return miopen::debug::env::GetEnvVariable(name).has_value(); } std::string_view name; }; template constexpr bool is_type_bool = std::is_same_v; template constexpr bool is_type_int = (std::is_integral_v && !is_type_bool); template constexpr bool is_type_str_r = (std::is_same_v); template constexpr bool is_type_str_w = (std::is_same_v || std::is_same_v); template constexpr bool is_type_char_ptr = (std::is_pointer_v && std::is_same_v>, char>); template || is_type_int || is_type_str_r, bool> = true> inline T value(const LibEnvVar& env) { const auto value = miopen::debug::env::GetEnvVariable(env.name); if(!value) { MIOPEN_THROW(miopenStatusInternalError); } if constexpr(is_type_bool) { bool bvalue = (value != "0"); return bvalue; } else if constexpr(is_type_int) { T ivalue; const auto res = std::from_chars( value.value().data(), value.value().data() + value.value().size(), ivalue); if(res.ec == std::errc::invalid_argument || res.ec == std::errc::result_out_of_range) { MIOPEN_THROW(miopenStatusInvalidValue, "Invalid value for env variable: " + value.value()); } return ivalue; } else if constexpr(is_type_str_r) { return value.value(); } } template < class T, std::enable_if_t || is_type_int || is_type_str_w || is_type_char_ptr, bool> = true> inline void update(const LibEnvVar& env, T value) { if constexpr(is_type_bool) { miopen::debug::env::UpdateEnvVariable(env.name, value ? "1" : "0"); } else if constexpr(is_type_int) { miopen::debug::env::UpdateEnvVariable(env.name, std::to_string(value)); } else if constexpr(is_type_str_w) { miopen::debug::env::UpdateEnvVariable(env.name, value); } else if constexpr(is_type_char_ptr) { miopen::debug::env::UpdateEnvVariable(env.name, std::string_view{value}); } } inline void clear(const LibEnvVar& env) { miopen::debug::env::ClearEnvVariable(env.name); } } // namespace lib_env #define MIOPEN_LIB_ENV_VAR(name) [[maybe_unused]] inline constexpr lib_env::LibEnvVar name(#name);