Halide 22.0.0 Halide compiler and libraries

Loading...
Searching...
No Matches
Util.h
Go to the documentation of this file.
1// Always use assert, even if llvm-config defines NDEBUG
2#ifdef NDEBUG
3#undef NDEBUG
4#include <assert.h>
5#define NDEBUG
6#else
7#include <cassert>
8#endif
9
10#ifndef HALIDE_UTIL_H
11#define HALIDE_UTIL_H
12
13/** \file
14 * Various utility functions used internally Halide. */
15
16#include <cmath>
17#include <cstdint>
18#include <cstring>
19#include <functional>
20#include <limits>
21#include <sstream>
22#include <string>
23#include <utility>
24#include <vector>
25
26#include "runtime/HalideRuntime.h"
27
28#ifdef Halide_STATIC_DEFINE
29#define HALIDE_EXPORT
30#else
31#if defined(_MSC_VER)
32// Halide_EXPORTS is quietly defined by CMake when building a shared library
33#ifdef Halide_EXPORTS
34#define HALIDE_EXPORT __declspec(dllexport)
35#else
36#define HALIDE_EXPORT __declspec(dllimport)
37#endif
38#else
39#define HALIDE_EXPORT __attribute__((visibility("default")))
40#endif
41#endif
42
43// If we're in user code, we don't want certain functions to be inlined.
44#if defined(COMPILING_HALIDE) || defined(BUILDING_PYTHON)
45#define HALIDE_NO_USER_CODE_INLINE
46#else
47#define HALIDE_NO_USER_CODE_INLINE HALIDE_NEVER_INLINE
48#endif
49
50// Clang uses __has_feature() for sanitizers...
51#if defined(__has_feature)
52#if __has_feature(address_sanitizer)
53#define HALIDE_INTERNAL_USING_ASAN
54#endif
55#if __has_feature(memory_sanitizer)
56#define HALIDE_INTERNAL_USING_MSAN
57#endif
58#if __has_feature(thread_sanitizer)
59#define HALIDE_INTERNAL_USING_TSAN
60#endif
61#if __has_feature(coverage_sanitizer)
62#define HALIDE_INTERNAL_USING_COVSAN
63#endif
64#if __has_feature(undefined_behavior_sanitizer)
65#define HALIDE_INTERNAL_USING_UBSAN
66#endif
67#endif
68
69// ...but GCC/MSVC don't like __has_feature, so handle them separately.
70// (Only AddressSanitizer for now, not sure if any others are well-supported
71// outside of Clang.
72#if defined(__SANITIZE_ADDRESS__) && !defined(HALIDE_INTERNAL_USING_ASAN)
73#define HALIDE_INTERNAL_USING_ASAN
74#endif
75
76 namespace Halide {
77
78/** Load a plugin in the form of a dynamic library (e.g. for custom autoschedulers).
79 * If the string doesn't contain any . characters, the proper prefix and/or suffix
80 * for the platform will be added:
81 *
82 * foo -> libfoo.so (Linux/OSX/etc -- note that .dylib is not supported)
83 * foo -> foo.dll (Windows)
84 *
85 * otherwise, it is assumed to be an appropriate pathname.
86 *
87 * Any error in loading will assert-fail. */
88 void load_plugin(const std::string &lib_name);
89
90 namespace Internal {
91
92/** Some numeric conversions are UB if the value won't fit in the result;
93 * safe_numeric_cast<>() is meant as a drop-in replacement for a C/C++ cast
94 * that adds well-defined behavior for the UB cases, attempting to mimic
95 * common implementation behavior as much as possible.
96 */
97 template<typename DST, typename SRC,
98 std::enable_if_t<std::is_floating_point_v<SRC>> * = nullptr>
99 DST safe_numeric_cast(SRC s) {
100 if (std::is_integral_v<DST>) {
101 // Treat float -> int as a saturating cast; this is handled
102 // in different ways by different compilers, so an arbitrary but safe
103 // choice like this is reasonable.
104 if (s < (SRC)std::numeric_limits<DST>::min()) {
105 return std::numeric_limits<DST>::min();
106 }
107 if (s > (SRC)std::numeric_limits<DST>::max()) {
108 return std::numeric_limits<DST>::max();
109 }
110 }
111 return (DST)s;
112}
113
114 template<typename DST, typename SRC,
115 std::enable_if_t<std::is_integral_v<SRC>> * = nullptr>
116 DST safe_numeric_cast(SRC s) {
117 if (std::is_integral_v<DST>) {
118 // any-int -> signed-int is technically UB if value won't fit;
119 // in practice, common compilers implement such conversions as done below
120 // (as verified by exhaustive testing on Clang for x86-64). We could
121 // probably continue to rely on that behavior, but making it explicit
122 // avoids possible wrather of UBSan and similar debug helpers.
123 // (Yes, using sizeof for this comparison is a little odd for the uint->int
124 // case, but the intent is to match existing common behavior, which this does.)
125 if (std::is_integral_v<SRC> && std::is_signed_v<DST> && sizeof(DST) < sizeof(SRC)) {
126 using UnsignedSrc = std::make_unsigned_t<SRC>;
127 return (DST)(s & (UnsignedSrc)(-1));
128 }
129 }
130 return (DST)s;
131}
132
133/** An aggressive form of reinterpret cast used for correct type-punning. */
134 template<typename DstType, typename SrcType>
135 DstType reinterpret_bits(const SrcType &src) {
136 static_assert(sizeof(SrcType) == sizeof(DstType), "Types must be same size");
137 DstType dst;
138 memcpy(&dst, &src, sizeof(SrcType));
139 return dst;
140}
141
142/** Get value of an environment variable. Returns its value
143 * is defined in the environment. If the var is not defined, an empty string
144 * is returned.
145 */
146std::string get_env_variable(char const *env_var_name);
147
148/** Get the name of the currently running executable. Platform-specific.
149 * If program name cannot be retrieved, function returns an empty string. */
150std::string running_program_name();
151
152/** Generate a unique name starting with the given prefix. It's unique
153 * relative to all other strings returned by unique_name in this
154 * process.
155 *
156 * The single-character version always appends a numeric suffix to the
157 * character.
158 *
159 * The string version will either return the input as-is (with high
160 * probability on the first time it is called with that input), or
161 * replace any existing '$' characters with underscores, then add a
162 * '$' sign and a numeric suffix to it.
163 *
164 * Note that unique_name('f') therefore differs from
165 * unique_name("f"). The former returns something like f123, and the
166 * latter returns either f or f$123.
167 */
168// @{
169std::string unique_name(char prefix);
170std::string unique_name(const std::string &prefix);
171// @}
172
173/** Test if the first string starts with the second string */
174 bool starts_with(const std::string &str, const std::string &prefix);
175
176/** Test if the first string ends with the second string */
177 bool ends_with(const std::string &str, const std::string &suffix);
178
179/** Replace all matches of the second string in the first string with the last string.
180 * The string to search-and-replace in is passed by value, offering the ability to
181 * std::move() a string in if you're not interested in keeping the original string.
182 * This is useful when the original string does not contain the find-string, causing
183 * this function to return the same string without any copies being made. */
184std::string replace_all(std::string str, const std::string &find, const std::string &replace);
185
186/** Split the source string using 'delim' as the divider. */
187std::vector<std::string> split_string(const std::string &source, const std::string &delim);
188
189/** Join the source vector using 'delim' as the divider. */
190 template<typename T>
191std::string join_strings(const std::vector<T> &sources, const std::string &delim) {
192 size_t sz = 0;
193 if (!sources.empty()) {
194 sz += delim.size() * (sources.size() - 1);
195 }
196 for (const auto &s : sources) {
197 sz += s.size();
198 }
199 std::string result;
200 result.reserve(sz);
201 bool need_delim = false;
202 for (const auto &s : sources) {
203 if (need_delim) {
204 result += delim;
205 }
206 result += s;
207 need_delim = true;
208 }
209 return result;
210}
211
212 template<typename... Args>
213std::string concat_strings(Args &&...args) {
214 std::stringstream ss;
215 (ss << ... << args);
216 return ss.str();
217}
218
219/** Perform a left fold of a vector. Returns a default-constructed
220 * vector element if the vector is empty. Similar to std::accumulate
221 * but with a less clunky syntax. */
222 template<typename T, typename Fn>
223T fold_left(const std::vector<T> &vec, Fn f) {
224 T result;
225 if (vec.empty()) {
226 return result;
227 }
228 result = vec[0];
229 for (size_t i = 1; i < vec.size(); i++) {
230 result = f(result, vec[i]);
231 }
232 return result;
233}
234
235/** Returns a right fold of a vector. Returns a default-constructed
236 * vector element if the vector is empty. */
237 template<typename T, typename Fn>
238T fold_right(const std::vector<T> &vec, Fn f) {
239 T result;
240 if (vec.empty()) {
241 return result;
242 }
243 result = vec.back();
244 for (size_t i = vec.size() - 1; i > 0; i--) {
245 result = f(vec[i - 1], result);
246 }
247 return result;
248}
249
250 template<typename... T>
251 struct meta_and : std::true_type {};
252
253 template<typename T1, typename... Args>
254 struct meta_and<T1, Args...> : std::integral_constant<bool, T1::value && meta_and<Args...>::value> {};
255
256 template<typename... T>
257 struct meta_or : std::false_type {};
258
259 template<typename T1, typename... Args>
260 struct meta_or<T1, Args...> : std::integral_constant<bool, T1::value || meta_or<Args...>::value> {};
261
262 template<typename To, typename... Args>
263 struct all_are_convertible : meta_and<std::is_convertible<Args, To>...> {};
264
265/** Returns base name and fills in namespaces, outermost one first in vector. */
266std::string extract_namespaces(const std::string &name, std::vector<std::string> &namespaces);
267
268/** Like extract_namespaces(), but strip and discard the namespaces, returning base name only */
269std::string strip_namespaces(const std::string &name);
270
271 struct FileStat {
272 uint64_t file_size;
273 uint32_t mod_time; // Unix epoch time
274 uint32_t uid;
275 uint32_t gid;
276 uint32_t mode;
277};
278
279/** Create a unique file with a name of the form prefixXXXXXsuffix in an arbitrary
280 * (but writable) directory; this is typically /tmp, but the specific
281 * location is not guaranteed. (Note that the exact form of the file name
282 * may vary; in particular, the suffix may be ignored on Windows.)
283 * The file is created (but not opened), thus this can be called from
284 * different threads (or processes, e.g. when building with parallel make)
285 * without risking collision. Note that if this file is used as a temporary
286 * file, the caller is responsibly for deleting it. Neither the prefix nor suffix
287 * may contain a directory separator.
288 */
289std::string file_make_temp(const std::string &prefix, const std::string &suffix);
290
291/** Create a unique directory in an arbitrary (but writable) directory; this is
292 * typically somewhere inside /tmp, but the specific location is not guaranteed.
293 * The directory will be empty (i.e., this will never return /tmp itself,
294 * but rather a new directory inside /tmp). The caller is responsible for removing the
295 * directory after use.
296 */
297std::string dir_make_temp();
298
299/** Wrapper for access(). Quietly ignores errors. */
300 bool file_exists(const std::string &name);
301
302/** assert-fail if the file doesn't exist. useful primarily for testing purposes. */
303 void assert_file_exists(const std::string &name);
304
305/** assert-fail if the file DOES exist. useful primarily for testing purposes. */
306 void assert_no_file_exists(const std::string &name);
307
308/** Wrapper for unlink(). Asserts upon error. */
309 void file_unlink(const std::string &name);
310
311/** Wrapper for unlink(). Quietly ignores errors. */
312 void file_unlink(const std::string &name);
313
314/** Ensure that no file with this path exists. If such a file
315 * exists and cannot be removed, assert-fail. */
316 void ensure_no_file_exists(const std::string &name);
317
318/** Wrapper for rmdir(). Asserts upon error. */
319 void dir_rmdir(const std::string &name);
320
321/** Wrapper for stat(). Asserts upon error. */
322 FileStat file_stat(const std::string &name);
323
324/** Read the entire contents of a file into a vector<char>. The file
325 * is read in binary mode. Errors trigger an assertion failure. */
326std::vector<char> read_entire_file(const std::string &pathname);
327
328/** Create or replace the contents of a file with a given pointer-and-length
329 * of memory. If the file doesn't exist, it is created; if it does exist, it
330 * is completely overwritten. Any error triggers an assertion failure. */
331 void write_entire_file(const std::string &pathname, const void *source, size_t source_len);
332
333 inline void write_entire_file(const std::string &pathname, const std::vector<char> &source) {
334 write_entire_file(pathname, source.data(), source.size());
335}
336
337/** A simple utility class that creates a temporary file in its ctor and
338 * deletes that file in its dtor; this is useful for temporary files that you
339 * want to ensure are deleted when exiting a certain scope. Since this is essentially
340 * just an RAII wrapper around file_make_temp() and file_unlink(), it has the same
341 * failure modes (i.e.: assertion upon error).
342 */
343 class TemporaryFile final {
344 public:
345 TemporaryFile(const std::string &prefix, const std::string &suffix)
346 : temp_path(file_make_temp(prefix, suffix)) {
347 }
348 const std::string &pathname() const {
349 return temp_path;
350 }
351 ~TemporaryFile() {
352 if (do_unlink) {
353 file_unlink(temp_path);
354 }
355 }
356 // You can call this if you want to defeat the automatic deletion;
357 // this is rarely what you want to do (since it defeats the purpose
358 // of this class), but can be quite handy for debugging purposes.
359 void detach() {
360 do_unlink = false;
361 }
362
363 private:
364 const std::string temp_path;
365 bool do_unlink = true;
366
367 public:
368 TemporaryFile(const TemporaryFile &) = delete;
369 TemporaryFile &operator=(const TemporaryFile &) = delete;
370 TemporaryFile(TemporaryFile &&) = delete;
371 TemporaryFile &operator=(TemporaryFile &&) = delete;
372};
373
374/** Routines to test if math would overflow for signed integers with
375 * the given number of bits. */
376// @{
377 bool add_would_overflow(int bits, int64_t a, int64_t b);
378 bool sub_would_overflow(int bits, int64_t a, int64_t b);
379 bool mul_would_overflow(int bits, int64_t a, int64_t b);
380// @}
381
382/** Routines to perform arithmetic on signed types without triggering signed
383 * overflow. If overflow would occur, sets result to zero, and returns
384 * false. Otherwise set result to the correct value, and returns true. */
385// @{
386 HALIDE_MUST_USE_RESULT bool add_with_overflow(int bits, int64_t a, int64_t b, int64_t *result);
387 HALIDE_MUST_USE_RESULT bool sub_with_overflow(int bits, int64_t a, int64_t b, int64_t *result);
388 HALIDE_MUST_USE_RESULT bool mul_with_overflow(int bits, int64_t a, int64_t b, int64_t *result);
389// @}
390
391/** Helper class for saving/restoring variable values on the stack, to allow
392 * for early-exit that preserves correctness */
393 template<typename T>
394 struct ScopedValue {
395 T &var;
396 T old_value;
397 /** Preserve the old value, restored at dtor time */
398 ScopedValue(T &var)
399 : var(var), old_value(var) {
400 }
401 /** Preserve the old value, then set the var to a new value. */
402 ScopedValue(T &var, const T &new_value)
403 : var(var), old_value(var) {
404 var = new_value;
405 }
406 ~ScopedValue() {
407 var = old_value;
408 }
409 operator T() const {
410 return old_value;
411 }
412 // allow move but not copy
413 ScopedValue(const ScopedValue &that) = delete;
414 ScopedValue(ScopedValue &&that) noexcept = default;
415};
416
417// Helpers for timing blocks of code. Put 'TIC;' at the start and
418// 'TOC;' at the end. Timing is reported at the toc via
419// debug(0). The calls can be nested and will pretty-print
420// appropriately. Took this idea from matlab via Jon Barron.
421//
422// Note that this uses global state internally, and is not thread-safe
423// at all. Only use it for single-threaded debugging sessions.
424
425 void halide_tic_impl(const char *file, int line);
426 void halide_toc_impl(const char *file, int line);
427#define HALIDE_TIC Halide::Internal::halide_tic_impl(__FILE__, __LINE__)
428#define HALIDE_TOC Halide::Internal::halide_toc_impl(__FILE__, __LINE__)
429#ifdef COMPILING_HALIDE
430#define TIC HALIDE_TIC
431#define TOC HALIDE_TOC
432#endif
433
434// statically cast a value from one type to another: this is really just
435// some syntactic sugar around static_cast<>() to avoid compiler warnings
436// regarding 'bool' in some compliation configurations.
437 template<typename TO>
438 struct StaticCast {
439 template<typename FROM>
440 constexpr static TO value(const FROM &from) {
441 if constexpr (std::is_same_v<TO, bool>) {
442 return from != 0;
443 } else {
444 return static_cast<TO>(from);
445 }
446 }
447};
448
449// Like std::is_convertible, but with additional tests for arithmetic types:
450// ensure that the value will roundtrip losslessly (e.g., no integer truncation
451// or dropping of fractional parts).
452 template<typename TO>
453 struct IsRoundtrippable {
454 template<typename FROM>
455 constexpr static bool value(const FROM &from) {
456 if constexpr (std::is_convertible_v<FROM, TO>) {
457 if constexpr (std::is_arithmetic_v<TO> &&
458 std::is_arithmetic_v<FROM> &&
459 !std::is_same_v<TO, FROM>) {
460 const TO to = static_cast<TO>(from);
461 const FROM roundtripped = static_cast<FROM>(to);
462 return roundtripped == from;
463 } else {
464 return true;
465 }
466 } else {
467 return false;
468 }
469 }
470};
471
472 template<typename T>
473 struct reverse_adaptor {
474 T &range;
475};
476
477 template<typename T>
478 auto begin(reverse_adaptor<T> i) {
479 return std::rbegin(i.range);
480}
481
482 template<typename T>
483 auto end(reverse_adaptor<T> i) {
484 return std::rend(i.range);
485}
486
487/**
488 * Reverse-order adaptor for range-based for-loops.
489 * TODO: Replace with std::ranges::reverse_view when upgrading to C++20.
490 */
491 template<typename T>
492 reverse_adaptor<T> reverse_view(T &&range) {
493 return {range};
494}
495
496/** Emit a version of a string that is a valid identifier in C (. is replaced with _)
497 * If prefix_underscore is true (the default), an underscore will be prepended if the
498 * input starts with an alphabetic character to avoid reserved word clashes.
499 */
500std::string c_print_name(const std::string &name, bool prefix_underscore = true);
501
502/** Return the LLVM_VERSION against which this libHalide is compiled. This is provided
503 * only for internal tests which need to verify behavior; please don't use this outside
504 * of Halide tests. */
505 int get_llvm_version();
506
507} // namespace Internal
508
509/** Set how much stack the compiler should use for compilation in
510 * bytes. This can also be set through the environment variable
511 * HL_COMPILER_STACK_SIZE, though this function takes precedence. A
512 * value of zero causes the compiler to just use the calling stack for
513 * all compilation tasks.
514 *
515 * Calling this or setting the environment variable should not be
516 * necessary. It is provided for three kinds of testing:
517 *
518 * First, Halide uses it in our internal tests to make sure
519 * we're not using a silly amount of stack size on some
520 * canary programs to avoid stack usage regressions.
521 *
522 * Second, if you have a mysterious crash inside a generator, you can
523 * set a larger stack size as a way to test if it's a stack
524 * overflow. Perhaps our default stack size is not large enough for
525 * your program and schedule. Use this call or the environment var as
526 * a workaround, and then open a bug with a reproducer at
527 * github.com/halide/Halide/issues so that we can determine what's
528 * going wrong that is causing your code to use so much stack.
529 *
530 * Third, perhaps using a side-stack is causing problems with
531 * sanitizing, debugging, or profiling tools. If this is a problem,
532 * you can set HL_COMPILER_STACK_SIZE to zero to make Halide stay on
533 * the main thread's stack.
534 */
535 void set_compiler_stack_size(size_t);
536
537/** The default amount of stack used for lowering and codegen. 32 MB
538 * ought to be enough for anyone. */
539 constexpr size_t default_compiler_stack_size = 32 * 1024 * 1024;
540
541/** Return how much stack size the compiler should use for calls that
542 * go through run_with_large_stack below. Currently that's lowering
543 * and codegen. If no call to set_compiler_stack_size has been made,
544 * this checks the value of the environment variable
545 * HL_COMPILER_STACK_SIZE. If that's unset, it returns
546 * default_compiler_stack_size, defined above. */
547 size_t get_compiler_stack_size();
548
549 namespace Internal {
550
551/** Call the given action in a platform-specific context that
552 * provides at least the stack space returned by
553 * get_compiler_stack_size. If that value is zero, just calls the
554 * function on the calling thread. Otherwise on Windows this
555 * uses a Fiber, and on other platforms it uses swapcontext. */
556 void run_with_large_stack(const std::function<void()> &action);
557
558/** Portable versions of popcount, count-leading-zeros, and
559 count-trailing-zeros. */
560// @{
561 int popcount64(uint64_t x);
562 int clz64(uint64_t x);
563 int ctz64(uint64_t x);
564// @}
565
566/** Return an integer 2^n, for some n, which is >= x. Argument x must be > 0. */
567 inline int64_t next_power_of_two(int64_t x) {
568 return static_cast<int64_t>(1) << static_cast<int64_t>(std::ceil(std::log2(x)));
569}
570
571 template<typename T>
572 inline T align_up(T x, int n) {
573 return (x + n - 1) / n * n;
574}
575
576} // namespace Internal
577} // namespace Halide
578
579#endif
HalideRuntime.h
This file declares the routines used by Halide internally in its runtime.
HALIDE_MUST_USE_RESULT
#define HALIDE_MUST_USE_RESULT
Definition HalideRuntime.h:65
Halide::Internal::TemporaryFile
A simple utility class that creates a temporary file in its ctor and deletes that file in its dtor; t...
Definition Util.h:343
Halide::Internal::TemporaryFile::operator=
TemporaryFile & operator=(TemporaryFile &&)=delete
Halide::Internal::TemporaryFile::TemporaryFile
TemporaryFile(TemporaryFile &&)=delete
Halide::Internal::TemporaryFile::operator=
TemporaryFile & operator=(const TemporaryFile &)=delete
Halide::Internal::TemporaryFile::detach
void detach()
Definition Util.h:359
Halide::Internal::TemporaryFile::pathname
const std::string & pathname() const
Definition Util.h:348
Halide::Internal::TemporaryFile::~TemporaryFile
~TemporaryFile()
Definition Util.h:351
Halide::Internal::TemporaryFile::TemporaryFile
TemporaryFile(const TemporaryFile &)=delete
Halide::Internal::TemporaryFile::TemporaryFile
TemporaryFile(const std::string &prefix, const std::string &suffix)
Definition Util.h:345
Halide::Internal::assert_file_exists
void assert_file_exists(const std::string &name)
assert-fail if the file doesn't exist.
Halide::Internal::align_up
T align_up(T x, int n)
Definition Util.h:572
Halide::Internal::ctz64
int ctz64(uint64_t x)
Halide::Internal::file_unlink
void file_unlink(const std::string &name)
Wrapper for unlink().
Halide::Internal::ends_with
bool ends_with(const std::string &str, const std::string &suffix)
Test if the first string ends with the second string.
Halide::Internal::begin
auto begin(reverse_adaptor< T > i)
Definition Util.h:478
Halide::Internal::concat_strings
std::string concat_strings(Args &&...args)
Definition Util.h:213
Halide::Internal::run_with_large_stack
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 ...
Halide::Internal::write_entire_file
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.
Halide::Internal::read_entire_file
std::vector< char > read_entire_file(const std::string &pathname)
Read the entire contents of a file into a vector<char>.
Halide::Internal::join_strings
std::string join_strings(const std::vector< T > &sources, const std::string &delim)
Join the source vector using 'delim' as the divider.
Definition Util.h:191
Halide::Internal::file_make_temp
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...
Halide::Internal::replace_all
std::string replace_all(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.
Halide::Internal::get_llvm_version
int get_llvm_version()
Return the LLVM_VERSION against which this libHalide is compiled.
Halide::Internal::dir_rmdir
void dir_rmdir(const std::string &name)
Wrapper for rmdir().
Halide::Internal::c_print_name
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 (.
Halide::Internal::clz64
int clz64(uint64_t x)
Halide::Internal::halide_toc_impl
void halide_toc_impl(const char *file, int line)
Halide::Internal::add_with_overflow
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.
Halide::Internal::sub_would_overflow
bool sub_would_overflow(int bits, int64_t a, int64_t b)
Halide::Internal::get_env_variable
std::string get_env_variable(char const *env_var_name)
Get value of an environment variable.
Halide::Internal::add_would_overflow
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.
Halide::Internal::extract_namespaces
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::Internal::mul_with_overflow
HALIDE_MUST_USE_RESULT bool mul_with_overflow(int bits, int64_t a, int64_t b, int64_t *result)
Halide::Internal::ensure_no_file_exists
void ensure_no_file_exists(const std::string &name)
Ensure that no file with this path exists.
Halide::Internal::reinterpret_bits
DstType reinterpret_bits(const SrcType &src)
An aggressive form of reinterpret cast used for correct type-punning.
Definition Util.h:135
Halide::Internal::mul_would_overflow
bool mul_would_overflow(int bits, int64_t a, int64_t b)
Halide::Internal::file_stat
FileStat file_stat(const std::string &name)
Wrapper for stat().
Halide::Internal::split_string
std::vector< std::string > split_string(const std::string &source, const std::string &delim)
Split the source string using 'delim' as the divider.
Halide::Internal::fold_left
T fold_left(const std::vector< T > &vec, Fn f)
Perform a left fold of a vector.
Definition Util.h:223
Halide::Internal::unique_name
std::string unique_name(char prefix)
Generate a unique name starting with the given prefix.
Halide::Internal::reverse_view
reverse_adaptor< T > reverse_view(T &&range)
Reverse-order adaptor for range-based for-loops.
Definition Util.h:492
Halide::Internal::running_program_name
std::string running_program_name()
Get the name of the currently running executable.
Halide::Internal::safe_numeric_cast
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 ...
Definition Util.h:99
Halide::Internal::assert_no_file_exists
void assert_no_file_exists(const std::string &name)
assert-fail if the file DOES exist.
Halide::Internal::dir_make_temp
std::string dir_make_temp()
Create a unique directory in an arbitrary (but writable) directory; this is typically somewhere insid...
Halide::Internal::popcount64
int popcount64(uint64_t x)
Portable versions of popcount, count-leading-zeros, and count-trailing-zeros.
Halide::Internal::next_power_of_two
int64_t next_power_of_two(int64_t x)
Return an integer 2^n, for some n, which is >= x.
Definition Util.h:567
Halide::Internal::sub_with_overflow
HALIDE_MUST_USE_RESULT bool sub_with_overflow(int bits, int64_t a, int64_t b, int64_t *result)
Halide::Internal::end
auto end(reverse_adaptor< T > i)
Definition Util.h:483
Halide::Internal::halide_tic_impl
void halide_tic_impl(const char *file, int line)
Halide::Internal::starts_with
bool starts_with(const std::string &str, const std::string &prefix)
Test if the first string starts with the second string.
Halide::Internal::strip_namespaces
std::string strip_namespaces(const std::string &name)
Like extract_namespaces(), but strip and discard the namespaces, returning base name only.
Halide::Internal::fold_right
T fold_right(const std::vector< T > &vec, Fn f)
Returns a right fold of a vector.
Definition Util.h:238
Halide::Internal::file_exists
bool file_exists(const std::string &name)
Wrapper for access().
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::LinkageType::Internal
@ Internal
Not visible externally, similar to 'static' linkage in C.
Halide::default_compiler_stack_size
constexpr size_t default_compiler_stack_size
The default amount of stack used for lowering and codegen.
Definition Util.h:539
Halide::get_compiler_stack_size
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...
Halide::load_plugin
void load_plugin(const std::string &lib_name)
Load a plugin in the form of a dynamic library (e.g.
Halide::cast
Internal::ConstantInterval cast(Type t, const Internal::ConstantInterval &a)
Cast operators for ConstantIntervals.
Halide::set_compiler_stack_size
void set_compiler_stack_size(size_t)
Set how much stack the compiler should use for compilation in bytes.
uint64_t
unsigned __INT64_TYPE__ uint64_t
Definition runtime_internal.h:23
int64_t
signed __INT64_TYPE__ int64_t
Definition runtime_internal.h:22
memcpy
void * memcpy(void *s1, const void *s2, size_t n)
uint32_t
unsigned __INT32_TYPE__ uint32_t
Definition runtime_internal.h:25
Halide::Internal::FileStat
Definition Util.h:271
Halide::Internal::FileStat::gid
uint32_t gid
Definition Util.h:275
Halide::Internal::FileStat::mod_time
uint32_t mod_time
Definition Util.h:273
Halide::Internal::FileStat::mode
uint32_t mode
Definition Util.h:276
Halide::Internal::FileStat::file_size
uint64_t file_size
Definition Util.h:272
Halide::Internal::FileStat::uid
uint32_t uid
Definition Util.h:274
Halide::Internal::IsRoundtrippable
Definition Util.h:453
Halide::Internal::IsRoundtrippable::value
static constexpr bool value(const FROM &from)
Definition Util.h:455
Halide::Internal::ScopedValue
Helper class for saving/restoring variable values on the stack, to allow for early-exit that preserve...
Definition Util.h:394
Halide::Internal::ScopedValue::ScopedValue
ScopedValue(T &var, const T &new_value)
Preserve the old value, then set the var to a new value.
Definition Util.h:402
Halide::Internal::ScopedValue::ScopedValue
ScopedValue(ScopedValue &&that) noexcept=default
Halide::Internal::ScopedValue::~ScopedValue
~ScopedValue()
Definition Util.h:406
Halide::Internal::ScopedValue::ScopedValue
ScopedValue(T &var)
Preserve the old value, restored at dtor time.
Definition Util.h:398
Halide::Internal::ScopedValue::var
T & var
Definition Util.h:395
Halide::Internal::ScopedValue::ScopedValue
ScopedValue(const ScopedValue &that)=delete
Halide::Internal::ScopedValue::old_value
T old_value
Definition Util.h:396
Halide::Internal::StaticCast
Definition Util.h:438
Halide::Internal::StaticCast::value
static constexpr TO value(const FROM &from)
Definition Util.h:440
Halide::Internal::all_are_convertible
Definition Util.h:263
Halide::Internal::meta_and
Definition Util.h:251
Halide::Internal::meta_or
Definition Util.h:257
Halide::Internal::reverse_adaptor
Definition Util.h:473
Halide::Internal::reverse_adaptor::range
T & range
Definition Util.h:474