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
97template<
typename DST,
typename SRC,
98 typename std::enable_if<std::is_floating_point<SRC>::value>::type * =
nullptr>
100 if (std::is_integral<DST>::value) {
104 if (s < (SRC)std::numeric_limits<DST>::min()) {
105 return std::numeric_limits<DST>::min();
107 if (s > (SRC)std::numeric_limits<DST>::max()) {
108 return std::numeric_limits<DST>::max();
114template<
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));
134template<
typename DstType,
typename SrcType>
136 static_assert(
sizeof(SrcType) ==
sizeof(DstType),
"Types must be same size");
138 memcpy(&dst, &src,
sizeof(SrcType));
174bool starts_with(
const std::string &str,
const std::string &prefix);
177bool ends_with(
const std::string &str,
const std::string &suffix);
180std::string
replace_all(
const std::string &str,
const std::string &find,
const std::string &replace);
183std::vector<std::string>
split_string(
const std::string &source,
const std::string &delim);
187std::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) {
211template<
typename T,
typename Fn>
218 for (
size_t i = 1; i < vec.size(); i++) {
219 result = f(result, vec[i]);
226template<
typename T,
typename Fn>
233 for (
size_t i = vec.size() - 1; i > 0; i--) {
234 result = f(vec[i - 1], result);
239template<
typename... T>
242template<
typename T1,
typename... Args>
243struct meta_and<T1, Args...> : std::integral_constant<bool, T1::value && meta_and<Args...>::value> {};
245template<
typename... T>
248template<
typename T1,
typename... Args>
249struct meta_or<T1, Args...> : std::integral_constant<bool, T1::value || meta_or<Args...>::value> {};
251template<
typename To,
typename... Args>
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
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);
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;
465std::string
c_print_name(
const std::string &name,
bool prefix_underscore =
true);
533 return static_cast<int64_t>(1) <<
static_cast<int64_t>(std::ceil(std::log2(x)));
538 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 & operator=(TemporaryFile &&)=delete
TemporaryFile(TemporaryFile &&)=delete
TemporaryFile & operator=(const TemporaryFile &)=delete
const std::string & pathname() const
TemporaryFile(const TemporaryFile &)=delete
TemporaryFile(const std::string &prefix, const std::string &suffix)
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.
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::vector< char > read_entire_file(const std::string &pathname)
Read the entire contents of a file into a vector<char>.
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.
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 (.
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().
std::vector< std::string > split_string(const std::string &source, const std::string &delim)
Split the source string using 'delim' as the divider.
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.
HALIDE_MUST_USE_RESULT bool sub_with_overflow(int bits, int64_t a, int64_t b, int64_t *result)
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,...
@ 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.
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
void * memcpy(void *s1, const void *s2, size_t n)
unsigned __INT32_TYPE__ uint32_t
static constexpr 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
static constexpr TO value(const FROM &from)