|
| 1 | +//===------- Utilities.h - Target independent OpenMP target RTL -- 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 | +// Routines and classes used to provide useful functionalities like string |
| 10 | +// parsing and environment variables. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#ifndef OPENMP_LIBOMPTARGET_INCLUDE_UTILITIES_H |
| 15 | +#define OPENMP_LIBOMPTARGET_INCLUDE_UTILITIES_H |
| 16 | + |
| 17 | +#include "llvm/ADT/STLFunctionalExtras.h" |
| 18 | + |
| 19 | +#include "Debug.h" |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <cassert> |
| 23 | +#include <cstdint> |
| 24 | +#include <cstdlib> |
| 25 | +#include <functional> |
| 26 | +#include <sstream> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +namespace llvm { |
| 30 | +namespace omp { |
| 31 | +namespace target { |
| 32 | + |
| 33 | +/// Utility class for parsing strings to other types. |
| 34 | +struct StringParser { |
| 35 | + /// Parse a string to another type. |
| 36 | + template <typename Ty> static bool parse(const char *Value, Ty &Result); |
| 37 | +}; |
| 38 | + |
| 39 | +/// Class for reading and checking environment variables. Currently working with |
| 40 | +/// integer, floats, std::string and bool types. |
| 41 | +template <typename Ty> class Envar { |
| 42 | + Ty Data; |
| 43 | + bool IsPresent; |
| 44 | + bool Initialized; |
| 45 | + |
| 46 | +public: |
| 47 | + /// Auxiliary function to safely create envars. This static function safely |
| 48 | + /// creates envars using fallible constructors. See the constructors to know |
| 49 | + /// more details about the creation parameters. |
| 50 | + template <typename... ArgsTy> |
| 51 | + static Expected<Envar> create(ArgsTy &&...Args) { |
| 52 | + Error Err = Error::success(); |
| 53 | + Envar Envar(std::forward<ArgsTy>(Args)..., Err); |
| 54 | + if (Err) |
| 55 | + return std::move(Err); |
| 56 | + return std::move(Envar); |
| 57 | + } |
| 58 | + |
| 59 | + /// Create an empty envar. Cannot be consulted. This constructor is merely |
| 60 | + /// for convenience. This constructor is not fallible. |
| 61 | + Envar() : Data(Ty()), IsPresent(false), Initialized(false) {} |
| 62 | + |
| 63 | + /// Create an envar with a name and an optional default. The Envar object will |
| 64 | + /// take the value read from the environment variable, or the default if it |
| 65 | + /// was not set or not correct. This constructor is not fallible. |
| 66 | + Envar(StringRef Name, Ty Default = Ty()) |
| 67 | + : Data(Default), IsPresent(false), Initialized(true) { |
| 68 | + |
| 69 | + if (const char *EnvStr = getenv(Name.data())) { |
| 70 | + // Check whether the envar is defined and valid. |
| 71 | + IsPresent = StringParser::parse<Ty>(EnvStr, Data); |
| 72 | + |
| 73 | + if (!IsPresent) { |
| 74 | + DP("Ignoring invalid value %s for envar %s\n", EnvStr, Name.data()); |
| 75 | + Data = Default; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// Get the definitive value. |
| 81 | + const Ty &get() const { |
| 82 | + // Throw a runtime error in case this envar is not initialized. |
| 83 | + if (!Initialized) |
| 84 | + FATAL_MESSAGE0(1, "Consulting envar before initialization"); |
| 85 | + |
| 86 | + return Data; |
| 87 | + } |
| 88 | + |
| 89 | + /// Get the definitive value. |
| 90 | + operator Ty() const { return get(); } |
| 91 | + |
| 92 | + /// Indicate whether the environment variable was defined and valid. |
| 93 | + bool isPresent() const { return IsPresent; } |
| 94 | + |
| 95 | +private: |
| 96 | + /// This constructor should never fail but we provide it for convenience. This |
| 97 | + /// way, the constructor can be used by the Envar::create() static function |
| 98 | + /// to safely create this kind of envars. |
| 99 | + Envar(StringRef Name, Ty Default, Error &Err) : Envar(Name, Default) { |
| 100 | + ErrorAsOutParameter EAO(&Err); |
| 101 | + Err = Error::success(); |
| 102 | + } |
| 103 | + |
| 104 | + /// Create an envar with a name, getter function and a setter function. The |
| 105 | + /// Envar object will take the value read from the environment variable if |
| 106 | + /// this value is accepted by the setter function. Otherwise, the getter |
| 107 | + /// function will be executed to get the default value. The getter should be |
| 108 | + /// of the form Error GetterFunctionTy(Ty &Value) and the setter should |
| 109 | + /// be of the form Error SetterFunctionTy(Ty Value). This constructor has a |
| 110 | + /// private visibility because is a fallible constructor. Please use the |
| 111 | + /// Envar::create() static function to safely create this object instead. |
| 112 | + template <typename GetterFunctor, typename SetterFunctor> |
| 113 | + Envar(StringRef Name, GetterFunctor Getter, SetterFunctor Setter, Error &Err) |
| 114 | + : Data(Ty()), IsPresent(false), Initialized(true) { |
| 115 | + ErrorAsOutParameter EAO(&Err); |
| 116 | + Err = init(Name, Getter, Setter); |
| 117 | + } |
| 118 | + |
| 119 | + template <typename GetterFunctor, typename SetterFunctor> |
| 120 | + Error init(StringRef Name, GetterFunctor Getter, SetterFunctor Setter); |
| 121 | +}; |
| 122 | + |
| 123 | +/// Define some common envar types. |
| 124 | +using IntEnvar = Envar<int>; |
| 125 | +using Int32Envar = Envar<int32_t>; |
| 126 | +using Int64Envar = Envar<int64_t>; |
| 127 | +using UInt32Envar = Envar<uint32_t>; |
| 128 | +using UInt64Envar = Envar<uint64_t>; |
| 129 | +using StringEnvar = Envar<std::string>; |
| 130 | +using BoolEnvar = Envar<bool>; |
| 131 | + |
| 132 | +template <> |
| 133 | +inline bool StringParser::parse(const char *ValueStr, bool &Result) { |
| 134 | + std::string Value(ValueStr); |
| 135 | + |
| 136 | + // Convert the string to lowercase. |
| 137 | + std::transform(Value.begin(), Value.end(), Value.begin(), |
| 138 | + [](unsigned char c) { return std::tolower(c); }); |
| 139 | + |
| 140 | + // May be implemented with fancier C++ features, but let's keep it simple. |
| 141 | + if (Value == "true" || Value == "yes" || Value == "on" || Value == "1") |
| 142 | + Result = true; |
| 143 | + else if (Value == "false" || Value == "no" || Value == "off" || Value == "0") |
| 144 | + Result = false; |
| 145 | + else |
| 146 | + return false; |
| 147 | + |
| 148 | + // Parsed correctly. |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
| 152 | +template <typename Ty> |
| 153 | +inline bool StringParser::parse(const char *Value, Ty &Result) { |
| 154 | + assert(Value && "Parsed value cannot be null"); |
| 155 | + |
| 156 | + std::istringstream Stream(Value); |
| 157 | + Stream >> Result; |
| 158 | + |
| 159 | + return !Stream.fail(); |
| 160 | +} |
| 161 | + |
| 162 | +template <typename Ty> |
| 163 | +template <typename GetterFunctor, typename SetterFunctor> |
| 164 | +inline Error Envar<Ty>::init(StringRef Name, GetterFunctor Getter, |
| 165 | + SetterFunctor Setter) { |
| 166 | + // Get the default value. |
| 167 | + Ty Default; |
| 168 | + if (Error Err = Getter(Default)) |
| 169 | + return Err; |
| 170 | + |
| 171 | + if (const char *EnvStr = getenv(Name.data())) { |
| 172 | + IsPresent = StringParser::parse<Ty>(EnvStr, Data); |
| 173 | + if (IsPresent) { |
| 174 | + // Check whether the envar value is actually valid. |
| 175 | + Error Err = Setter(Data); |
| 176 | + if (Err) { |
| 177 | + // The setter reported an invalid value. Mark the user-defined value as |
| 178 | + // not present and reset to the getter value (default). |
| 179 | + IsPresent = false; |
| 180 | + Data = Default; |
| 181 | + DP("Setter of envar %s failed, resetting to %s\n", Name.data(), |
| 182 | + std::to_string(Data).data()); |
| 183 | + consumeError(std::move(Err)); |
| 184 | + } |
| 185 | + } else { |
| 186 | + DP("Ignoring invalid value %s for envar %s\n", EnvStr, Name.data()); |
| 187 | + Data = Default; |
| 188 | + } |
| 189 | + } else { |
| 190 | + Data = Default; |
| 191 | + } |
| 192 | + |
| 193 | + return Error::success(); |
| 194 | +} |
| 195 | + |
| 196 | +} // namespace target |
| 197 | +} // namespace omp |
| 198 | +} // namespace llvm |
| 199 | + |
| 200 | +#endif // OPENMP_LIBOMPTARGET_INCLUDE_UTILITIES_H |
0 commit comments