|
| 1 | +//==---------------- config.hpp - SYCL context ------------------*- C++-*---==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <cstdlib> |
| 12 | + |
| 13 | +namespace cl { |
| 14 | +namespace sycl { |
| 15 | +namespace detail { |
| 16 | + |
| 17 | +#ifdef DISABLE_CONFIG_FROM_ENV |
| 18 | +constexpr bool ConfigFromEnvEnabled = false; |
| 19 | +#else |
| 20 | +constexpr bool ConfigFromEnvEnabled = true; |
| 21 | +#endif // DISABLE_CONFIG_FROM_ENV |
| 22 | + |
| 23 | +#ifdef DISABLE_CONFIG_FROM_CONFIG_FILE |
| 24 | +constexpr bool ConfigFromFileEnabled = false; |
| 25 | +#else |
| 26 | +constexpr bool ConfigFromFileEnabled = true; |
| 27 | +#endif // DISABLE_CONFIG_FROM_CONFIG_FILE |
| 28 | + |
| 29 | +#ifdef DISABLE_CONFIG_FROM_COMPILE_TIME |
| 30 | +constexpr bool ConfigFromCompileDefEnabled = false; |
| 31 | +#else |
| 32 | +constexpr bool ConfigFromCompileDefEnabled = true; |
| 33 | +#endif // DISABLE_CONFIG_FROM_COMPILE_TIME |
| 34 | + |
| 35 | +// Enum of config IDs for accessing other arrays |
| 36 | +enum ConfigID { |
| 37 | + START = 0, |
| 38 | +#define CONFIG(name, ...) name, |
| 39 | +#include "config.def" |
| 40 | +#undef CONFIG |
| 41 | + END |
| 42 | +}; |
| 43 | + |
| 44 | +// Consider strings starting with __ as unset |
| 45 | +constexpr const char *getStrOrNullptr(const char *Str) { |
| 46 | + return (Str[0] == '_' && Str[1] == '_') ? nullptr : Str; |
| 47 | +} |
| 48 | + |
| 49 | +template <ConfigID Config> class SYCLConfigBase; |
| 50 | + |
| 51 | +#define CONFIG(Name, MaxSize, CompileTimeDef) \ |
| 52 | + template <> class SYCLConfigBase<Name> { \ |
| 53 | + public: \ |
| 54 | + /*Preallocated storage for config value which is extracted from a config \ |
| 55 | + * file*/ \ |
| 56 | + static char MStorage[MaxSize]; \ |
| 57 | + /*Points to the storage if config is set in the file, nullptr otherwise*/ \ |
| 58 | + static const char *MValueFromFile; \ |
| 59 | + /*The name of the config*/ \ |
| 60 | + static const char *const MConfigName; \ |
| 61 | + /*Points to the value which is set during compilation, nullptr otherwise. \ |
| 62 | + * Detection of whether a value is set or not is based on checking the \ |
| 63 | + * beginning of the string, if it starts with double underscore(__) the \ |
| 64 | + * value is not set.*/ \ |
| 65 | + static const char *const MCompileTimeDef; \ |
| 66 | + }; |
| 67 | +#include "config.def" |
| 68 | +#undef CONFIG |
| 69 | + |
| 70 | +// Intializes configs from the configuration file |
| 71 | +void readConfig(); |
| 72 | + |
| 73 | +template <ConfigID Config> class SYCLConfig { |
| 74 | + using BaseT = SYCLConfigBase<Config>; |
| 75 | + |
| 76 | +public: |
| 77 | + static const char *get() { |
| 78 | + const char *ValStr = getRawValue(); |
| 79 | + return ValStr; |
| 80 | + } |
| 81 | + |
| 82 | +private: |
| 83 | + static const char *getRawValue() { |
| 84 | + if (ConfigFromEnvEnabled) |
| 85 | + if (const char *ValStr = getenv(BaseT::MConfigName)) |
| 86 | + return ValStr; |
| 87 | + |
| 88 | + if (ConfigFromFileEnabled) { |
| 89 | + readConfig(); |
| 90 | + if (BaseT::MValueFromFile) |
| 91 | + return BaseT::MValueFromFile; |
| 92 | + } |
| 93 | + |
| 94 | + if (ConfigFromCompileDefEnabled && BaseT::MCompileTimeDef) |
| 95 | + return BaseT::MCompileTimeDef; |
| 96 | + |
| 97 | + return nullptr; |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +} // namespace cl |
| 102 | +} // namespace sycl |
| 103 | +} // namespace detail |
0 commit comments