28 #ifdef Halide_STATIC_DEFINE
34 #define HALIDE_EXPORT __declspec(dllexport)
36 #define HALIDE_EXPORT __declspec(dllimport)
39 #define HALIDE_EXPORT __attribute__((visibility("default")))
44 #if defined(COMPILING_HALIDE) || defined(BUILDING_PYTHON)
45 #define HALIDE_NO_USER_CODE_INLINE
47 #define HALIDE_NO_USER_CODE_INLINE HALIDE_NEVER_INLINE
51 #if defined(__has_feature)
52 #if __has_feature(address_sanitizer)
53 #define HALIDE_INTERNAL_USING_ASAN
55 #if __has_feature(memory_sanitizer)
56 #define HALIDE_INTERNAL_USING_MSAN
58 #if __has_feature(thread_sanitizer)
59 #define HALIDE_INTERNAL_USING_TSAN
61 #if __has_feature(coverage_sanitizer)
62 #define HALIDE_INTERNAL_USING_COVSAN
64 #if __has_feature(undefined_behavior_sanitizer)
65 #define HALIDE_INTERNAL_USING_UBSAN
72 #if defined(__SANITIZE_ADDRESS__) && !defined(HALIDE_INTERNAL_USING_ASAN)
73 #define HALIDE_INTERNAL_USING_ASAN
97 template<
typename DST,
typename SRC,
98 typename std::enable_if<std::is_floating_point<SRC>::value>::type * =
nullptr>
100 if (std::is_integral<DST>::value) {
114 template<
typename DST,
typename SRC,
115 typename std::enable_if<std::is_integral<SRC>::value>::type * =
nullptr>
117 if (std::is_integral<DST>::value) {
125 if (std::is_integral<SRC>::value && std::is_signed<DST>::value &&
sizeof(DST) <
sizeof(SRC)) {
126 using UnsignedSrc =
typename std::make_unsigned<SRC>::type;
127 return (DST)(s & (UnsignedSrc)(-1));
134 template<
typename DstType,
typename SrcType>
136 static_assert(
sizeof(SrcType) ==
sizeof(DstType),
"Types must be same size");
138 memcpy(&dst, &src,
sizeof(SrcType));
174 bool starts_with(
const std::string &str,
const std::string &prefix);
177 bool ends_with(
const std::string &str,
const std::string &suffix);
180 std::string
replace_all(
const std::string &str,
const std::string &find,
const std::string &replace);
183 std::vector<std::string>
split_string(
const std::string &source,
const std::string &delim);
187 std::string
join_strings(
const std::vector<T> &sources,
const std::string &delim) {
189 if (!sources.empty()) {
190 sz += delim.size() * (sources.size() - 1);
192 for (
const auto &s : sources) {
197 bool need_delim =
false;
198 for (
const auto &s : sources) {
211 template<
typename T,
typename Fn>
218 for (
size_t i = 1; i < vec.size(); i++) {
219 result = f(result, vec[i]);
226 template<
typename T,
typename Fn>
233 for (
size_t i = vec.size() - 1; i > 0; i--) {
234 result = f(vec[i - 1], result);
239 template<
typename... T>
242 template<
typename T1,
typename... Args>
243 struct meta_and<T1, Args...> : std::integral_constant<bool, T1::value && meta_and<Args...>::value> {};
245 template<
typename... T>
248 template<
typename T1,
typename... Args>
249 struct meta_or<T1, Args...> : std::integral_constant<bool, T1::value || meta_or<Args...>::value> {};
251 template<
typename To,
typename... Args>
278 std::string
file_make_temp(
const std::string &prefix,
const std::string &suffix);
353 const std::string temp_path;
354 bool do_unlink =
true;
416 #define HALIDE_TIC Halide::Internal::halide_tic_impl(__FILE__, __LINE__)
417 #define HALIDE_TOC Halide::Internal::halide_toc_impl(__FILE__, __LINE__)
418 #ifdef COMPILING_HALIDE
419 #define TIC HALIDE_TIC
420 #define TOC HALIDE_TOC
426 template<
typename TO>
428 template<
typename FROM>
429 constexpr
static TO
value(
const FROM &from) {
430 if constexpr (std::is_same<TO, bool>::value) {
433 return static_cast<TO
>(from);
441 template<
typename TO>
443 template<
typename FROM>
444 constexpr
static bool value(
const FROM &from) {
445 if constexpr (std::is_convertible<FROM, TO>::value) {
446 if constexpr (std::is_arithmetic<TO>::value &&
447 std::is_arithmetic<FROM>::value &&
448 !std::is_same<TO, FROM>::value) {
449 const TO to =
static_cast<TO
>(from);
450 const FROM roundtripped =
static_cast<FROM
>(to);
451 return roundtripped == from;
468 return std::rbegin(i.
range);
473 return std::rend(i.
range);
489 std::string
c_print_name(
const std::string &name,
bool prefix_underscore =
true);
562 return (x + n - 1) / n * n;
This file declares the routines used by Halide internally in its runtime.
#define HALIDE_MUST_USE_RESULT
A simple utility class that creates a temporary file in its ctor and deletes that file in its dtor; t...
TemporaryFile(TemporaryFile &&)=delete
TemporaryFile & operator=(TemporaryFile &&)=delete
TemporaryFile(const TemporaryFile &)=delete
const std::string & pathname() const
TemporaryFile(const std::string &prefix, const std::string &suffix)
TemporaryFile & operator=(const TemporaryFile &)=delete
void assert_file_exists(const std::string &name)
assert-fail if the file doesn't exist.
void file_unlink(const std::string &name)
Wrapper for unlink().
bool ends_with(const std::string &str, const std::string &suffix)
Test if the first string ends with the second string.
auto begin(reverse_adaptor< T > i)
std::vector< std::string > split_string(const std::string &source, const std::string &delim)
Split the source string using 'delim' as the divider.
ConstantInterval min(const ConstantInterval &a, const ConstantInterval &b)
void run_with_large_stack(const std::function< void()> &action)
Call the given action in a platform-specific context that provides at least the stack space returned ...
void write_entire_file(const std::string &pathname, const void *source, size_t source_len)
Create or replace the contents of a file with a given pointer-and-length of memory.
std::string join_strings(const std::vector< T > &sources, const std::string &delim)
Join the source vector using 'delim' as the divider.
std::string file_make_temp(const std::string &prefix, const std::string &suffix)
Create a unique file with a name of the form prefixXXXXXsuffix in an arbitrary (but writable) directo...
int get_llvm_version()
Return the LLVM_VERSION against which this libHalide is compiled.
ConstantInterval max(const ConstantInterval &a, const ConstantInterval &b)
void dir_rmdir(const std::string &name)
Wrapper for rmdir().
std::string c_print_name(const std::string &name, bool prefix_underscore=true)
Emit a version of a string that is a valid identifier in C (.
reverse_adaptor< T > reverse_view(T &&range)
Reverse-order adaptor for range-based for-loops.
void halide_toc_impl(const char *file, int line)
HALIDE_MUST_USE_RESULT bool add_with_overflow(int bits, int64_t a, int64_t b, int64_t *result)
Routines to perform arithmetic on signed types without triggering signed overflow.
bool sub_would_overflow(int bits, int64_t a, int64_t b)
std::string get_env_variable(char const *env_var_name)
Get value of an environment variable.
bool add_would_overflow(int bits, int64_t a, int64_t b)
Routines to test if math would overflow for signed integers with the given number of bits.
std::string extract_namespaces(const std::string &name, std::vector< std::string > &namespaces)
Returns base name and fills in namespaces, outermost one first in vector.
HALIDE_MUST_USE_RESULT bool mul_with_overflow(int bits, int64_t a, int64_t b, int64_t *result)
void ensure_no_file_exists(const std::string &name)
Ensure that no file with this path exists.
DstType reinterpret_bits(const SrcType &src)
An aggressive form of reinterpret cast used for correct type-punning.
bool mul_would_overflow(int bits, int64_t a, int64_t b)
std::string replace_all(const std::string &str, const std::string &find, const std::string &replace)
Replace all matches of the second string in the first string with the last string.
FileStat file_stat(const std::string &name)
Wrapper for stat().
T fold_left(const std::vector< T > &vec, Fn f)
Perform a left fold of a vector.
std::string unique_name(char prefix)
Generate a unique name starting with the given prefix.
std::string running_program_name()
Get the name of the currently running executable.
DST safe_numeric_cast(SRC s)
Some numeric conversions are UB if the value won't fit in the result; safe_numeric_cast<>() is meant ...
void assert_no_file_exists(const std::string &name)
assert-fail if the file DOES exist.
std::string dir_make_temp()
Create a unique directory in an arbitrary (but writable) directory; this is typically somewhere insid...
int popcount64(uint64_t x)
Portable versions of popcount, count-leading-zeros, and count-trailing-zeros.
int64_t next_power_of_two(int64_t x)
Return an integer 2^n, for some n, which is >= x.
std::vector< char > read_entire_file(const std::string &pathname)
Read the entire contents of a file into a vector<char>.
HALIDE_MUST_USE_RESULT bool sub_with_overflow(int bits, int64_t a, int64_t b, int64_t *result)
auto end(reverse_adaptor< T > i)
void halide_tic_impl(const char *file, int line)
bool starts_with(const std::string &str, const std::string &prefix)
Test if the first string starts with the second string.
std::string strip_namespaces(const std::string &name)
Like extract_namespaces(), but strip and discard the namespaces, returning base name only.
T fold_right(const std::vector< T > &vec, Fn f)
Returns a right fold of a vector.
bool file_exists(const std::string &name)
Wrapper for access().
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Expr ceil(Expr x)
Return the least whole number greater than or equal to a floating-point expression.
@ Internal
Not visible externally, similar to 'static' linkage in C.
constexpr size_t default_compiler_stack_size
The default amount of stack used for lowering and codegen.
size_t get_compiler_stack_size()
Return how much stack size the compiler should use for calls that go through run_with_large_stack bel...
void load_plugin(const std::string &lib_name)
Load a plugin in the form of a dynamic library (e.g.
void set_compiler_stack_size(size_t)
Set how much stack the compiler should use for compilation in bytes.
range_reader< T > range(const T &low, const T &high)
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
unsigned __INT32_TYPE__ uint32_t
void * memcpy(void *s1, const void *s2, size_t n)
constexpr static bool value(const FROM &from)
Helper class for saving/restoring variable values on the stack, to allow for early-exit that preserve...
ScopedValue(ScopedValue &&that) noexcept=default
ScopedValue(T &var, T new_value)
Preserve the old value, then set the var to a new value.
ScopedValue(T &var)
Preserve the old value, restored at dtor time.
ScopedValue(const ScopedValue &that)=delete
constexpr static TO value(const FROM &from)