Loading...
Searching...
No Matches
CodeGen_LLVM.h
Go to the documentation of this file.
#ifndef HALIDE_CODEGEN_LLVM_H
#define HALIDE_CODEGEN_LLVM_H
/** \file
*
* Defines the base-class for all architecture-specific code
* generators that use llvm.
*/
namespace llvm {
class Value;
class Module;
class Function;
class FunctionType;
class IRBuilderDefaultInserter;
class ConstantFolder;
template<typename, typename>
class IRBuilder;
class LLVMContext;
class Type;
class PointerType;
class StructType;
class Instruction;
class CallInst;
class ExecutionEngine;
class AllocaInst;
class Constant;
class Triple;
class MDNode;
class NamedMDNode;
class DataLayout;
class BasicBlock;
class GlobalVariable;
} // namespace llvm
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>
#include "IRVisitor.h"
#include "Module.h"
#include "Scope.h"
#include "Target.h"
namespace Halide {
struct ExternSignature;
namespace Internal {
/** A code generator abstract base class. Actual code generators
* (e.g. CodeGen_X86) inherit from this. This class is responsible
* for taking a Halide Stmt and producing llvm bitcode, machine
* code in an object file, or machine code accessible through a
* function pointer.
*/
class CodeGen_LLVM : public IRVisitor {
public:
/** Create an instance of CodeGen_LLVM suitable for the target. */
static std::unique_ptr<CodeGen_LLVM> new_for_target(const Target &target, llvm::LLVMContext &context);
/** Takes a halide Module and compiles it to an llvm Module. */
virtual std::unique_ptr<llvm::Module> compile(const Module &module);
/** The target we're generating code for */
const Target &get_target() const {
return target;
}
/** Tell the code generator which LLVM context to use. */
void set_context(llvm::LLVMContext &context);
/** Initialize internal llvm state for the enabled targets. */
static void initialize_llvm();
static std::unique_ptr<llvm::Module> compile_trampolines(
const Target &target,
llvm::LLVMContext &context,
const std::string &suffix,
const std::vector<std::pair<std::string, ExternSignature>> &externs);
size_t get_requested_alloca_total() const {
return requested_alloca_total;
}
protected:
CodeGen_LLVM(const Target &t);
/** Compile a specific halide declaration into the llvm Module. */
// @{
virtual void compile_func(const LoweredFunc &func, const std::string &simple_name, const std::string &extern_name);
virtual void compile_buffer(const Buffer<> &buffer);
// @}
/** Helper functions for compiling Halide functions to llvm
* functions. begin_func performs all the work necessary to begin
* generating code for a function with a given argument list with
* the IRBuilder. A call to begin_func should be a followed by a
* call to end_func with the same arguments, to generate the
* appropriate cleanup code. */
// @{
virtual void begin_func(LinkageType linkage, const std::string &simple_name,
const std::string &extern_name, const std::vector<LoweredArgument> &args);
virtual void end_func(const std::vector<LoweredArgument> &args);
// @}
/** What should be passed as -mcpu (warning: implies attrs!), -mattrs,
* and related for compilation. The architecture-specific code generator
* should define these.
*
* `mcpu_target()` - target this specific CPU, in the sense of the allowed
* ISA sets *and* the CPU-specific tuning/assembly instruction scheduling.
*
* `mcpu_tune()` - expect that we will be running on this specific CPU,
* so perform CPU-specific tuning/assembly instruction scheduling, *but*
* DON'T sacrifice the portability, support running on other CPUs, only
* make use of the ISAs that are enabled by `mcpu_target()`+`mattrs()`.
*/
// @{
virtual std::string mcpu_target() const = 0;
virtual std::string mcpu_tune() const = 0;
virtual std::string mattrs() const = 0;
virtual std::string mabi() const;
virtual bool use_soft_float_abi() const = 0;
virtual bool use_pic() const;
// @}
/** Should indexing math be promoted to 64-bit on platforms with
* 64-bit pointers? */
virtual bool promote_indices() const {
return true;
}
/** What's the natural vector bit-width to use for loads, stores, etc. */
virtual int native_vector_bits() const = 0;
/** Used to decide whether to break a vector up into multiple smaller
* operations. This is the largest size the architecture supports. */
virtual int maximum_vector_bits() const {
return native_vector_bits();
}
/** For architectures that have vscale vectors, return the constant vscale to use.
* Default of 0 means do not use vscale vectors. Generally will depend on
* the target flags and vector_bits settings.
*/
virtual int target_vscale() const {
return 0;
}
/** Return the type in which arithmetic should be done for the
* given storage type. */
virtual Type upgrade_type_for_arithmetic(const Type &) const;
/** Return the type that a given Halide type should be
* stored/loaded from memory as. */
virtual Type upgrade_type_for_storage(const Type &) const;
/** Return the type that a Halide type should be passed in and out
* of functions as. */
virtual Type upgrade_type_for_argument_passing(const Type &) const;
std::unique_ptr<llvm::Module> module;
llvm::Function *function = nullptr;
llvm::LLVMContext *context = nullptr;
std::unique_ptr<llvm::IRBuilder<llvm::ConstantFolder, llvm::IRBuilderDefaultInserter>> builder;
llvm::Value *value = nullptr;
llvm::MDNode *very_likely_branch = nullptr;
llvm::MDNode *fast_fp_math_md = nullptr;
llvm::MDNode *strict_fp_math_md = nullptr;
std::vector<LoweredArgument> current_function_args;
bool in_strict_float = false;
bool any_strict_float = false;
/** Change floating-point math op emission to use fast flags. */
void set_fast_fp_math();
/** Change floating-point math op emission to use strict flags. */
void set_strict_fp_math();
/** If any_strict_float is true, sets fast math flags for the lifetime of
* this object, then sets them to strict on destruction. If any_strict_float
* is false, does nothing. Any call to an IRBuilder method that starts with
* "CreateF" should probably be wrapped in one of these, but it's safe to
* miss one - we just miss out on some optimizations. In this way codegen is
* designed to fail safe. */
struct ScopedFastMath {
CodeGen_LLVM *codegen;
ScopedFastMath(CodeGen_LLVM *);
~ScopedFastMath();
};
/** The target we're generating code for */
Halide::Target target;
/** Grab all the context specific internal state. */
virtual void init_context();
/** Initialize the CodeGen_LLVM internal state to compile a fresh
* module. This allows reuse of one CodeGen_LLVM object to compiled
* multiple related modules (e.g. multiple device kernels). */
virtual void init_module();
/** Run all of llvm's optimization passes on the module. */
void optimize_module();
/** Add an entry to the symbol table, hiding previous entries with
* the same name. Call this when new values come into scope. */
void sym_push(const std::string &name, llvm::Value *value);
/** Remove an entry for the symbol table, revealing any previous
* entries with the same name. Call this when values go out of
* scope. */
void sym_pop(const std::string &name);
/** Fetch an entry from the symbol table. If the symbol is not
* found, it either errors out (if the second arg is true), or
* returns nullptr. */
llvm::Value *sym_get(const std::string &name,
bool must_succeed = true) const;
/** Test if an item exists in the symbol table. */
bool sym_exists(const std::string &name) const;
/** Given a Halide ExternSignature, return the equivalent llvm::FunctionType. */
llvm::FunctionType *signature_to_type(const ExternSignature &signature);
/** Some useful llvm types */
// @{
llvm::Type *void_t = nullptr, *i1_t = nullptr, *i8_t = nullptr, *i16_t = nullptr, *i32_t = nullptr, *i64_t = nullptr, *f16_t = nullptr, *f32_t = nullptr, *f64_t = nullptr;
llvm::PointerType *ptr_t = nullptr;
llvm::StructType *halide_buffer_t_type = nullptr,
*type_t_type,
*dimension_t_type,
*metadata_t_type = nullptr,
*argument_t_type = nullptr,
*scalar_value_t_type = nullptr,
*device_interface_t_type = nullptr,
*pseudostack_slot_t_type = nullptr,
*semaphore_t_type;
// @}
/** Some wildcard variables used for peephole optimizations in
* subclasses */
// @{
Expr wild_u1x_, wild_i8x_, wild_u8x_, wild_i16x_, wild_u16x_;
Expr wild_i32x_, wild_u32x_, wild_i64x_, wild_u64x_;
Expr wild_f32x_, wild_f64x_;
// Wildcards for scalars.
Expr wild_u1_, wild_i8_, wild_u8_, wild_i16_, wild_u16_;
Expr wild_i32_, wild_u32_, wild_i64_, wild_u64_;
Expr wild_f32_, wild_f64_;
// @}
/** Emit code that evaluates an expression, and return the llvm
* representation of the result of the expression. */
llvm::Value *codegen(const Expr &);
/** Emit code that runs a statement. */
void codegen(const Stmt &);
/** Codegen a vector Expr by codegenning each lane and combining. */
void scalarize(const Expr &);
/** Some destructors should always be called. Others should only
* be called if the pipeline is exiting with an error code. */
enum DestructorType { Always,
OnError,
OnSuccess };
/* Call this at the location of object creation to register how an
* object should be destroyed. This does three things:
* 1) Emits code here that puts the object in a unique
* null-initialized stack slot
* 2) Adds an instruction to the destructor block that calls the
* destructor on that stack slot if it's not null.
* 3) Returns that stack slot, so you can neuter the destructor
* (by storing null to the stack slot) or destroy the object early
* (by calling trigger_destructor).
*/
llvm::Value *register_destructor(llvm::Function *destructor_fn, llvm::Value *obj, DestructorType when);
/** Call a destructor early. Pass in the value returned by register destructor. */
void trigger_destructor(llvm::Function *destructor_fn, llvm::Value *stack_slot);
/** Retrieves the block containing the error handling
* code. Creates it if it doesn't already exist for this
* function. */
llvm::BasicBlock *get_destructor_block();
/** Codegen an assertion. If false, returns the error code (if not
* null), or evaluates and returns the message, which must be an
* Int(32) expression. */
// @{
void create_assertion(llvm::Value *condition, const Expr &message, llvm::Value *error_code = nullptr);
// @}
/** Codegen a block of asserts with pure conditions */
void codegen_asserts(const std::vector<const AssertStmt *> &asserts);
/** Return the the pipeline with the given error code. Will run
* the destructor block. */
void return_with_error_code(llvm::Value *error_code);
/** Put a string constant in the module as a global variable and return a pointer to it. */
llvm::Constant *create_string_constant(const std::string &str);
/** Put a binary blob in the module as a global variable and return a pointer to it. */
llvm::Constant *create_binary_blob(const std::vector<char> &data, const std::string &name, bool constant = true);
/** Widen an llvm scalar into an llvm vector with the given number of lanes. */
llvm::Value *create_broadcast(llvm::Value *, int lanes);
/** Generate a pointer into a named buffer at a given index, of a
* given type. The index counts according to the scalar type of
* the type passed in. */
// @{
llvm::Value *codegen_buffer_pointer(const std::string &buffer, Type type, llvm::Value *index);
llvm::Value *codegen_buffer_pointer(const std::string &buffer, Type type, Expr index);
llvm::Value *codegen_buffer_pointer(llvm::Value *base_address, Type type, Expr index);
llvm::Value *codegen_buffer_pointer(llvm::Value *base_address, Type type, llvm::Value *index);
// @}
/** Return type string for LLVM type using LLVM IR intrinsic type mangling.
* E.g. ".i32 or ".f32" for scalars, ".p0" for pointers,
* ".nxv4i32" for a scalable vector of four 32-bit integers,
* or ".v4f32" for a fixed vector of four 32-bit floats.
* The dot is included in the result.
*/
std::string mangle_llvm_type(llvm::Type *type);
/** Turn a Halide Type into an llvm::Value representing a constant halide_type_t */
llvm::Value *make_halide_type_t(const Type &);
/** Mark a load or store with type-based-alias-analysis metadata
* so that llvm knows it can reorder loads and stores across
* different buffers */
void add_tbaa_metadata(llvm::Instruction *inst, std::string buffer, const Expr &index);
/** Get a unique name for the actual block of memory that an
* allocate node uses. Used so that alias analysis understands
* when multiple Allocate nodes shared the same memory. */
virtual std::string get_allocation_name(const std::string &n) {
return n;
}
/** Add the appropriate function attribute to tell LLVM that the function
* doesn't access memory. */
void function_does_not_access_memory(llvm::Function *fn);
using IRVisitor::visit;
/** Generate code for various IR nodes. These can be overridden by
* architecture-specific code to perform peephole
* optimizations. The result of each is stored in \ref value */
// @{
void visit(const IntImm *) override;
void visit(const UIntImm *) override;
void visit(const FloatImm *) override;
void visit(const StringImm *) override;
void visit(const Cast *) override;
void visit(const Reinterpret *) override;
void visit(const Variable *) override;
void visit(const Add *) override;
void visit(const Sub *) override;
void visit(const Mul *) override;
void visit(const Div *) override;
void visit(const Mod *) override;
void visit(const Min *) override;
void visit(const Max *) override;
void visit(const EQ *) override;
void visit(const NE *) override;
void visit(const LT *) override;
void visit(const LE *) override;
void visit(const GT *) override;
void visit(const GE *) override;
void visit(const And *) override;
void visit(const Or *) override;
void visit(const Not *) override;
void visit(const Select *) override;
void visit(const Load *) override;
void visit(const Ramp *) override;
void visit(const Broadcast *) override;
void visit(const Call *) override;
void visit(const Let *) override;
void visit(const LetStmt *) override;
void visit(const AssertStmt *) override;
void visit(const ProducerConsumer *) override;
void visit(const For *) override;
void visit(const Store *) override;
void visit(const Block *) override;
void visit(const IfThenElse *) override;
void visit(const Evaluate *) override;
void visit(const Shuffle *) override;
void visit(const VectorReduce *) override;
void visit(const Prefetch *) override;
void visit(const Atomic *) override;
// @}
/** Generate code for an allocate node. It has no default
* implementation - it must be handled in an architecture-specific
* way. */
void visit(const Allocate *) override = 0;
/** Generate code for a free node. It has no default
* implementation and must be handled in an architecture-specific
* way. */
void visit(const Free *) override = 0;
/** These IR nodes should have been removed during
* lowering. CodeGen_LLVM will error out if they are present */
// @{
void visit(const Provide *) override;
void visit(const Realize *) override;
// @}
/** Get the llvm type equivalent to the given halide type in the
* current context. */
virtual llvm::Type *llvm_type_of(const Type &) const;
/** Get the llvm type equivalent to a given halide type. If
* effective_vscale is nonzero and the type is a vector type with lanes
* a multiple of effective_vscale, a scalable vector type is generated
* with total lanes divided by effective_vscale. That is a scalable
* vector intended to be used with a fixed vscale of effective_vscale.
*/
llvm::Type *llvm_type_of(llvm::LLVMContext *context, Halide::Type t,
int effective_vscale) const;
/** Perform an alloca at the function entrypoint. Will be cleaned
* on function exit. */
llvm::Value *create_alloca_at_entry(llvm::Type *type, int n,
bool zero_initialize = false,
const std::string &name = "");
/** A (very) conservative guess at the size of all alloca() storage requested
* (including alignment padding). It's currently meant only to be used as
* a very coarse way to ensure there is enough stack space when testing
* on the WebAssembly backend.
*
* It is *not* meant to be a useful proxy for "stack space needed", for a
* number of reasons:
* - allocas with non-overlapping lifetimes will share space
* - on some backends, LLVM may promote register-sized allocas into registers
* - while this accounts for alloca() calls we know about, it doesn't attempt
* to account for stack spills, function call overhead, etc.
*/
size_t requested_alloca_total = 0;
/** The user_context argument. May be a constant null if the
* function is being compiled without a user context. */
llvm::Value *get_user_context() const;
/** Implementation of the intrinsic call to
* interleave_vectors. This implementation allows for interleaving
* an arbitrary number of vectors.*/
virtual llvm::Value *interleave_vectors(const std::vector<llvm::Value *> &);
/** Description of an intrinsic function overload. Overloads are resolved
* using both argument and return types. The scalar types of the arguments
* and return type must match exactly for an overload resolution to succeed. */
struct Intrinsic {
Type result_type;
std::vector<Type> arg_types;
llvm::Function *impl;
Intrinsic(Type result_type, std::vector<Type> arg_types, llvm::Function *impl)
: result_type(result_type), arg_types(std::move(arg_types)), impl(impl) {
}
};
/** Mapping of intrinsic functions to the various overloads implementing it. */
std::map<std::string, std::vector<Intrinsic>> intrinsics;
/** Get an LLVM intrinsic declaration. If it doesn't exist, it will be created. */
llvm::Function *get_llvm_intrin(const Type &ret_type, const std::string &name, const std::vector<Type> &arg_types, bool scalars_are_vectors = false);
llvm::Function *get_llvm_intrin(llvm::Type *ret_type, const std::string &name, const std::vector<llvm::Type *> &arg_types);
/** Declare an intrinsic function that participates in overload resolution. */
llvm::Function *declare_intrin_overload(const std::string &name, const Type &ret_type, const std::string &impl_name, std::vector<Type> arg_types, bool scalars_are_vectors = false);
void declare_intrin_overload(const std::string &name, const Type &ret_type, llvm::Function *impl, std::vector<Type> arg_types);
/** Call an overloaded intrinsic function. Returns nullptr if no suitable overload is found. */
llvm::Value *call_overloaded_intrin(const Type &result_type, const std::string &name, const std::vector<Expr> &args);
/** Generate a call to a vector intrinsic or runtime inlined
* function. The arguments are sliced up into vectors of the width
* given by 'intrin_lanes', the intrinsic is called on each
* piece, then the results (if any) are concatenated back together
* into the original type 't'. For the version that takes an
* llvm::Type *, the type may be void, so the vector width of the
* arguments must be specified explicitly as
* 'called_lanes'. */
// @{
llvm::Value *call_intrin(const Type &t, int intrin_lanes,
const std::string &name, std::vector<Expr>);
llvm::Value *call_intrin(const Type &t, int intrin_lanes,
llvm::Function *intrin, std::vector<Expr>);
llvm::Value *call_intrin(const llvm::Type *t, int intrin_lanes,
const std::string &name, std::vector<llvm::Value *>,
bool scalable_vector_result = false, bool is_reduction = false);
llvm::Value *call_intrin(const llvm::Type *t, int intrin_lanes,
llvm::Function *intrin, std::vector<llvm::Value *>,
bool is_reduction = false);
// @}
/** Take a slice of lanes out of an llvm vector. Pads with undefs
* if you ask for more lanes than the vector has. */
virtual llvm::Value *slice_vector(llvm::Value *vec, int start, int extent);
/** Concatenate a bunch of llvm vectors. Must be of the same type. */
virtual llvm::Value *concat_vectors(const std::vector<llvm::Value *> &);
/** Create an LLVM shuffle vectors instruction. Takes a combination of
* fixed or scalable vectors as input, so long as the effective lengths match,
* but always returns a fixed vector. */
virtual llvm::Value *shuffle_vectors(llvm::Value *a, llvm::Value *b,
const std::vector<int> &indices);
/** Shorthand for shuffling a single vector. */
llvm::Value *shuffle_vectors(llvm::Value *v, const std::vector<int> &indices);
/** Go looking for a vector version of a runtime function. Will
* return the best match. Matches in the following order:
*
* 1) The requested vector width.
*
* 2) The width which is the smallest power of two
* greater than or equal to the vector width.
*
* 3) All the factors of 2) greater than one, in decreasing order.
*
* 4) The smallest power of two not yet tried.
*
* So for a 5-wide vector, it tries: 5, 8, 4, 2, 16.
*
* If there's no match, returns (nullptr, 0).
*/
std::pair<llvm::Function *, int> find_vector_runtime_function(const std::string &name, int lanes);
virtual bool supports_atomic_add(const Type &t) const;
/** Compile a horizontal reduction that starts with an explicit
* initial value. There are lots of complex ways to peephole
* optimize this pattern, especially with the proliferation of
* dot-product instructions, and they can usefully share logic
* across backends. */
virtual void codegen_vector_reduce(const VectorReduce *op, const Expr &init);
/** Are we inside an atomic node that uses mutex locks?
This is used for detecting deadlocks from nested atomics & illegal vectorization. */
bool inside_atomic_mutex_node = false;
/** Emit atomic store instructions? */
bool emit_atomic_stores = false;
/** Can we call this operation with float16 type?
This is used to avoid "emulated" equivalent code-gen in case target has FP16 feature **/
virtual bool supports_call_as_float16(const Call *op) const;
/** call_intrin does far too much to be useful and generally breaks things
* when one has carefully set things up for a specific architecture. This
* just does the bare minimum. call_intrin should be refactored and could
* call this, possibly with renaming of the methods. */
llvm::Value *simple_call_intrin(const std::string &intrin,
const std::vector<llvm::Value *> &args,
llvm::Type *result_type);
/** Ensure that a vector value is either fixed or vscale depending to match desired_type.
*/
llvm::Value *normalize_fixed_scalable_vector_type(llvm::Type *desired_type, llvm::Value *result);
/** Convert between two LLVM vectors of potentially different scalable/fixed and size.
* Used to handle converting to/from fixed vectors that are smaller than the minimum
* size scalable vector. */
llvm::Value *convert_fixed_or_scalable_vector_type(llvm::Value *arg,
llvm::Type *desired_type);
/** Convert an LLVM fixed vector value to the corresponding vscale vector value. */
llvm::Value *fixed_to_scalable_vector_type(llvm::Value *fixed);
/** Convert an LLVM vscale vector value to the corresponding fixed vector value. */
llvm::Value *scalable_to_fixed_vector_type(llvm::Value *scalable);
/** Get number of vector elements, taking into account scalable vectors. Returns 1 for scalars. */
int get_vector_num_elements(const llvm::Type *t);
/** Interface to abstract vector code generation as LLVM is now
* providing multiple options to express even simple vector
* operations. Specifically traditional fixed length vectors, vscale
* based variable length vectors, and the vector predicate based approach
* where an explict length is passed with each instruction.
*/
// @{
enum class VectorTypeConstraint {
None, /// Use default for current target.
Fixed, /// Force use of fixed size vectors.
VScale, /// For use of scalable vectors.
};
llvm::Type *get_vector_type(llvm::Type *, int n,
VectorTypeConstraint type_constraint = VectorTypeConstraint::None) const;
// @}
llvm::Constant *get_splat(int lanes, llvm::Constant *value,
VectorTypeConstraint type_constraint = VectorTypeConstraint::None) const;
/** Make sure a value type has the same scalable/fixed vector type as a guide. */
// @{
llvm::Value *match_vector_type_scalable(llvm::Value *value, VectorTypeConstraint constraint);
llvm::Value *match_vector_type_scalable(llvm::Value *value, llvm::Type *guide);
llvm::Value *match_vector_type_scalable(llvm::Value *value, llvm::Value *guide);
// @}
/** Support for generating LLVM vector predication intrinsics
* ("@llvm.vp.*" and "@llvm.experimental.vp.*")
*/
// @{
/** Struct to hold descriptor for an argument to a vector
* predicated intrinsic. This includes the value, whether the
* type of the argument should be mangled into the intrisic name
* and if so, where, and the alignment for pointer arguments. */
struct VPArg {
llvm::Value *value;
// If provided, put argument's type into the intrinsic name via LLVM IR type mangling.
std::optional<size_t> mangle_index;
int alignment;
VPArg(llvm::Value *value, std::optional<size_t> mangle_index = std::nullopt, int32_t alignment = 0)
: value(value), mangle_index(mangle_index), alignment(alignment) {
}
};
/** Type indicating an intrinsic does not take a mask. */
struct NoMask {
};
/** Type indicating mask to use is all true -- all lanes enabled. */
struct AllEnabledMask {
};
/** Predication mask using the above two types for special cases
* and an llvm::Value for the general one. */
using MaskVariant = std::variant<NoMask, AllEnabledMask, llvm::Value *>;
/** Generate a vector predicated comparison intrinsic call if
* use_llvm_vp_intrinsics is true and result_type is a vector
* type. If generated, assigns result of vp intrinsic to value and
* returns true if it an instuction is generated, otherwise
* returns false. */
bool try_vector_predication_comparison(const std::string &name, const Type &result_type,
MaskVariant mask, llvm::Value *a, llvm::Value *b,
const char *cmp_op);
struct VPResultType {
llvm::Type *type;
std::optional<size_t> mangle_index;
VPResultType(llvm::Type *type, std::optional<size_t> mangle_index = std::nullopt)
: type(type), mangle_index(mangle_index) {
}
};
/** Generate an intrisic call if use_llvm_vp_intrinsics is true
* and length is greater than 1. If generated, assigns result
* of vp intrinsic to value and returns true if it an instuction
* is generated, otherwise returns false. */
bool try_vector_predication_intrinsic(const std::string &name, VPResultType result_type,
int32_t length, MaskVariant mask, std::vector<VPArg> args);
/** Controls use of vector predicated intrinsics for vector operations.
* Will be set by certain backends (e.g. RISC V) to control codegen. */
bool use_llvm_vp_intrinsics = false;
// @}
/** Generate a basic dense vector load, with an optional predicate and
* control over whether or not we should slice the load into native
* vectors. Used by CodeGen_ARM to help with vld2/3/4 emission. */
llvm::Value *codegen_dense_vector_load(const Load *load, llvm::Value *vpred = nullptr, bool slice_to_native = true);
/** Warning messages which we want to avoid displaying number of times */
enum class WarningKind {
EmulatedFloat16,
};
std::map<WarningKind, std::string> onetime_warnings;
private:
/** All the values in scope at the current code location during
* codegen. Use sym_push and sym_pop to access. */
Scope<llvm::Value *> symbol_table;
/** String constants already emitted to the module. Tracked to
* prevent emitting the same string many times. */
std::map<std::string, llvm::Constant *> string_constants;
/** A basic block to branch to on error that triggers all
* destructors. As destructors are registered, code gets added
* to this block. */
llvm::BasicBlock *destructor_block = nullptr;
/** Turn off all unsafe math flags in scopes while this is set. */
bool strict_float;
/** Use the LLVM large code model when this is set. */
bool llvm_large_code_model;
/** Cache the result of target_vscale from architecture specific implementation
* as this is used on every Halide to LLVM type conversion.
*/
int effective_vscale = 0;
/** Assign a unique ID to each producer-consumer and for-loop node. The IDs
* are printed as comments in assembly and used to link visualizations with
* the generated assembly code within `StmtToViz`
*/
int producer_consumer_id = 0;
int for_loop_id = 0;
/** Embed an instance of halide_filter_metadata_t in the code, using
* the given name (by convention, this should be ${FUNCTIONNAME}_metadata)
* as extern "C" linkage. Note that the return value is a function-returning-
* pointer-to-constant-data.
*/
llvm::Function *embed_metadata_getter(const std::string &metadata_getter_name,
const std::string &function_name, const std::vector<LoweredArgument> &args,
const MetadataNameMap &metadata_name_map);
/** Embed a constant expression as a global variable. */
llvm::Constant *embed_constant_expr(Expr e, llvm::Type *t);
llvm::Constant *embed_constant_scalar_value_t(const Expr &e);
llvm::Function *add_argv_wrapper(llvm::Function *fn, const std::string &name,
bool result_in_argv, std::vector<bool> &arg_is_buffer);
llvm::Value *codegen_vector_load(const Type &type, const std::string &name, const Expr &base,
const Buffer<> &image, const Parameter &param, const ModulusRemainder &alignment,
llvm::Value *vpred = nullptr, bool slice_to_native = true, llvm::Value *stride = nullptr);
virtual void codegen_predicated_load(const Load *op);
virtual void codegen_predicated_store(const Store *op);
void codegen_atomic_rmw(const Store *op);
void init_codegen(const std::string &name);
std::unique_ptr<llvm::Module> finish_codegen();
/** A helper routine for generating folded vector reductions. */
template<typename Op>
bool try_to_fold_vector_reduce(const Expr &a, Expr b);
/** Records the StructType for pointer values returned from
* make_struct intrinsic. Required for opaque pointer support.
* This map should never grow without bound as each entry
* represents a unique struct type created by a closure or similar.
*/
std::map<llvm::Value *, llvm::Type *> struct_type_recovery;
};
} // namespace Internal
/** Given a Halide module, generate an llvm::Module. */
std::unique_ptr<llvm::Module> codegen_llvm(const Module &module,
llvm::LLVMContext &context);
} // namespace Halide
#endif
IRVisitor.h
Defines the base class for things that recursively walk over the IR.
Module.h
Defines Module, an IR container that fully describes a Halide program.
Scope.h
Defines the Scope class, which is used for keeping track of names in a scope while traversing IR.
Target.h
Defines the structure that describes a Halide target.
Halide::Buffer<>
Halide::Internal::CodeGen_LLVM::get_splat
llvm::Constant * get_splat(int lanes, llvm::Constant *value, VectorTypeConstraint type_constraint=VectorTypeConstraint::None) const
Halide::Internal::CodeGen_LLVM::value
llvm::Value * value
Definition CodeGen_LLVM.h:169
Halide::Internal::CodeGen_LLVM::visit
void visit(const Let *) override
Halide::Internal::CodeGen_LLVM::find_vector_runtime_function
std::pair< llvm::Function *, int > find_vector_runtime_function(const std::string &name, int lanes)
Go looking for a vector version of a runtime function.
Halide::Internal::CodeGen_LLVM::metadata_t_type
llvm::StructType * metadata_t_type
Definition CodeGen_LLVM.h:237
Halide::Internal::CodeGen_LLVM::end_func
virtual void end_func(const std::vector< LoweredArgument > &args)
Halide::Internal::CodeGen_LLVM::argument_t_type
llvm::StructType * argument_t_type
Definition CodeGen_LLVM.h:238
Halide::Internal::CodeGen_LLVM::create_binary_blob
llvm::Constant * create_binary_blob(const std::vector< char > &data, const std::string &name, bool constant=true)
Put a binary blob in the module as a global variable and return a pointer to it.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Select *) override
Halide::Internal::CodeGen_LLVM::maximum_vector_bits
virtual int maximum_vector_bits() const
Used to decide whether to break a vector up into multiple smaller operations.
Definition CodeGen_LLVM.h:142
Halide::Internal::CodeGen_LLVM::visit
void visit(const Or *) override
Halide::Internal::CodeGen_LLVM::f64_t
llvm::Type * f64_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::visit
void visit(const AssertStmt *) override
Halide::Internal::CodeGen_LLVM::emit_atomic_stores
bool emit_atomic_stores
Emit atomic store instructions?
Definition CodeGen_LLVM.h:555
Halide::Internal::CodeGen_LLVM::sym_pop
void sym_pop(const std::string &name)
Remove an entry for the symbol table, revealing any previous entries with the same name.
Halide::Internal::CodeGen_LLVM::init_context
virtual void init_context()
Grab all the context specific internal state.
Halide::Internal::CodeGen_LLVM::target_vscale
virtual int target_vscale() const
For architectures that have vscale vectors, return the constant vscale to use.
Definition CodeGen_LLVM.h:149
Halide::Internal::CodeGen_LLVM::device_interface_t_type
llvm::StructType * device_interface_t_type
Definition CodeGen_LLVM.h:240
Halide::Internal::CodeGen_LLVM::visit
void visit(const Mul *) override
Halide::Internal::CodeGen_LLVM::semaphore_t_type
llvm::StructType * semaphore_t_type
Definition CodeGen_LLVM.h:242
Halide::Internal::CodeGen_LLVM::supports_atomic_add
virtual bool supports_atomic_add(const Type &t) const
Halide::Internal::CodeGen_LLVM::llvm_type_of
virtual llvm::Type * llvm_type_of(const Type &) const
Get the llvm type equivalent to the given halide type in the current context.
Halide::Internal::CodeGen_LLVM::i64_t
llvm::Type * i64_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::current_function_args
std::vector< LoweredArgument > current_function_args
Definition CodeGen_LLVM.h:173
Halide::Internal::CodeGen_LLVM::get_target
const Target & get_target() const
The target we're generating code for.
Definition CodeGen_LLVM.h:69
Halide::Internal::CodeGen_LLVM::return_with_error_code
void return_with_error_code(llvm::Value *error_code)
Return the the pipeline with the given error code.
Halide::Internal::CodeGen_LLVM::wild_f64_
Expr wild_f64_
Definition CodeGen_LLVM.h:256
Halide::Internal::CodeGen_LLVM::call_intrin
llvm::Value * call_intrin(const llvm::Type *t, int intrin_lanes, const std::string &name, std::vector< llvm::Value * >, bool scalable_vector_result=false, bool is_reduction=false)
Halide::Internal::CodeGen_LLVM::compile
virtual std::unique_ptr< llvm::Module > compile(const Module &module)
Takes a halide Module and compiles it to an llvm Module.
Halide::Internal::CodeGen_LLVM::codegen_buffer_pointer
llvm::Value * codegen_buffer_pointer(const std::string &buffer, Type type, Expr index)
Halide::Internal::CodeGen_LLVM::get_llvm_intrin
llvm::Function * get_llvm_intrin(llvm::Type *ret_type, const std::string &name, const std::vector< llvm::Type * > &arg_types)
Halide::Internal::CodeGen_LLVM::mcpu_tune
virtual std::string mcpu_tune() const =0
Halide::Internal::CodeGen_LLVM::ptr_t
llvm::PointerType * ptr_t
Definition CodeGen_LLVM.h:233
Halide::Internal::CodeGen_LLVM::try_vector_predication_intrinsic
bool try_vector_predication_intrinsic(const std::string &name, VPResultType result_type, int32_t length, MaskVariant mask, std::vector< VPArg > args)
Generate an intrisic call if use_llvm_vp_intrinsics is true and length is greater than 1.
Halide::Internal::CodeGen_LLVM::trigger_destructor
void trigger_destructor(llvm::Function *destructor_fn, llvm::Value *stack_slot)
Call a destructor early.
Halide::Internal::CodeGen_LLVM::wild_i8x_
Expr wild_i8x_
Definition CodeGen_LLVM.h:249
Halide::Internal::CodeGen_LLVM::visit
void visit(const Reinterpret *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const Min *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const For *) override
Halide::Internal::CodeGen_LLVM::codegen_buffer_pointer
llvm::Value * codegen_buffer_pointer(llvm::Value *base_address, Type type, llvm::Value *index)
Halide::Internal::CodeGen_LLVM::visit
void visit(const Not *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const Sub *) override
Halide::Internal::CodeGen_LLVM::sym_push
void sym_push(const std::string &name, llvm::Value *value)
Add an entry to the symbol table, hiding previous entries with the same name.
Halide::Internal::CodeGen_LLVM::type_t_type
llvm::StructType * type_t_type
Definition CodeGen_LLVM.h:235
Halide::Internal::CodeGen_LLVM::VectorTypeConstraint
VectorTypeConstraint
Interface to abstract vector code generation as LLVM is now providing multiple options to express eve...
Definition CodeGen_LLVM.h:595
Halide::Internal::CodeGen_LLVM::VectorTypeConstraint::Fixed
@ Fixed
Use default for current target.
Definition CodeGen_LLVM.h:597
Halide::Internal::CodeGen_LLVM::VectorTypeConstraint::None
@ None
Definition CodeGen_LLVM.h:596
Halide::Internal::CodeGen_LLVM::VectorTypeConstraint::VScale
@ VScale
Force use of fixed size vectors.
Definition CodeGen_LLVM.h:598
Halide::Internal::CodeGen_LLVM::DestructorType
DestructorType
Some destructors should always be called.
Definition CodeGen_LLVM.h:271
Halide::Internal::CodeGen_LLVM::Always
@ Always
Definition CodeGen_LLVM.h:271
Halide::Internal::CodeGen_LLVM::OnError
@ OnError
Definition CodeGen_LLVM.h:272
Halide::Internal::CodeGen_LLVM::OnSuccess
@ OnSuccess
Definition CodeGen_LLVM.h:273
Halide::Internal::CodeGen_LLVM::target
Halide::Target target
The target we're generating code for.
Definition CodeGen_LLVM.h:197
Halide::Internal::CodeGen_LLVM::begin_func
virtual void begin_func(LinkageType linkage, const std::string &simple_name, const std::string &extern_name, const std::vector< LoweredArgument > &args)
Helper functions for compiling Halide functions to llvm functions.
Halide::Internal::CodeGen_LLVM::try_vector_predication_comparison
bool try_vector_predication_comparison(const std::string &name, const Type &result_type, MaskVariant mask, llvm::Value *a, llvm::Value *b, const char *cmp_op)
Generate a vector predicated comparison intrinsic call if use_llvm_vp_intrinsics is true and result_t...
Halide::Internal::CodeGen_LLVM::fast_fp_math_md
llvm::MDNode * fast_fp_math_md
Definition CodeGen_LLVM.h:171
Halide::Internal::CodeGen_LLVM::scalar_value_t_type
llvm::StructType * scalar_value_t_type
Definition CodeGen_LLVM.h:239
Halide::Internal::CodeGen_LLVM::builder
std::unique_ptr< llvm::IRBuilder< llvm::ConstantFolder, llvm::IRBuilderDefaultInserter > > builder
Definition CodeGen_LLVM.h:168
Halide::Internal::CodeGen_LLVM::i16_t
llvm::Type * i16_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::use_llvm_vp_intrinsics
bool use_llvm_vp_intrinsics
Controls use of vector predicated intrinsics for vector operations.
Definition CodeGen_LLVM.h:670
Halide::Internal::CodeGen_LLVM::create_broadcast
llvm::Value * create_broadcast(llvm::Value *, int lanes)
Widen an llvm scalar into an llvm vector with the given number of lanes.
Halide::Internal::CodeGen_LLVM::module
std::unique_ptr< llvm::Module > module
Definition CodeGen_LLVM.h:165
Halide::Internal::CodeGen_LLVM::wild_i32_
Expr wild_i32_
Definition CodeGen_LLVM.h:255
Halide::Internal::CodeGen_LLVM::mcpu_target
virtual std::string mcpu_target() const =0
What should be passed as -mcpu (warning: implies attrs!), -mattrs, and related for compilation.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Max *) override
Halide::Internal::CodeGen_LLVM::codegen
void codegen(const Stmt &)
Emit code that runs a statement.
Halide::Internal::CodeGen_LLVM::compile_buffer
virtual void compile_buffer(const Buffer<> &buffer)
Halide::Internal::CodeGen_LLVM::promote_indices
virtual bool promote_indices() const
Should indexing math be promoted to 64-bit on platforms with 64-bit pointers?
Definition CodeGen_LLVM.h:133
Halide::Internal::CodeGen_LLVM::function
llvm::Function * function
Definition CodeGen_LLVM.h:166
Halide::Internal::CodeGen_LLVM::visit
void visit(const GE *) override
Halide::Internal::CodeGen_LLVM::wild_u1_
Expr wild_u1_
Definition CodeGen_LLVM.h:254
Halide::Internal::CodeGen_LLVM::wild_i64_
Expr wild_i64_
Definition CodeGen_LLVM.h:255
Halide::Internal::CodeGen_LLVM::visit
void visit(const Variable *) override
Halide::Internal::CodeGen_LLVM::declare_intrin_overload
void declare_intrin_overload(const std::string &name, const Type &ret_type, llvm::Function *impl, std::vector< Type > arg_types)
Halide::Internal::CodeGen_LLVM::codegen_dense_vector_load
llvm::Value * codegen_dense_vector_load(const Load *load, llvm::Value *vpred=nullptr, bool slice_to_native=true)
Generate a basic dense vector load, with an optional predicate and control over whether or not we sho...
Halide::Internal::CodeGen_LLVM::wild_f64x_
Expr wild_f64x_
Definition CodeGen_LLVM.h:251
Halide::Internal::CodeGen_LLVM::wild_f32x_
Expr wild_f32x_
Definition CodeGen_LLVM.h:251
Halide::Internal::CodeGen_LLVM::visit
void visit(const Atomic *) override
Halide::Internal::CodeGen_LLVM::wild_u16_
Expr wild_u16_
Definition CodeGen_LLVM.h:254
Halide::Internal::CodeGen_LLVM::create_assertion
void create_assertion(llvm::Value *condition, const Expr &message, llvm::Value *error_code=nullptr)
Codegen an assertion.
Halide::Internal::CodeGen_LLVM::upgrade_type_for_storage
virtual Type upgrade_type_for_storage(const Type &) const
Return the type that a given Halide type should be stored/loaded from memory as.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Shuffle *) override
Halide::Internal::CodeGen_LLVM::very_likely_branch
llvm::MDNode * very_likely_branch
Definition CodeGen_LLVM.h:170
Halide::Internal::CodeGen_LLVM::visit
void visit(const Allocate *) override=0
Generate code for an allocate node.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Prefetch *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const Provide *) override
These IR nodes should have been removed during lowering.
Halide::Internal::CodeGen_LLVM::upgrade_type_for_arithmetic
virtual Type upgrade_type_for_arithmetic(const Type &) const
Return the type in which arithmetic should be done for the given storage type.
Halide::Internal::CodeGen_LLVM::any_strict_float
bool any_strict_float
Definition CodeGen_LLVM.h:176
Halide::Internal::CodeGen_LLVM::normalize_fixed_scalable_vector_type
llvm::Value * normalize_fixed_scalable_vector_type(llvm::Type *desired_type, llvm::Value *result)
Ensure that a vector value is either fixed or vscale depending to match desired_type.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Div *) override
Halide::Internal::CodeGen_LLVM::halide_buffer_t_type
llvm::StructType * halide_buffer_t_type
Definition CodeGen_LLVM.h:234
Halide::Internal::CodeGen_LLVM::WarningKind
WarningKind
Warning messages which we want to avoid displaying number of times.
Definition CodeGen_LLVM.h:679
Halide::Internal::CodeGen_LLVM::WarningKind::EmulatedFloat16
@ EmulatedFloat16
Definition CodeGen_LLVM.h:680
Halide::Internal::CodeGen_LLVM::visit
void visit(const EQ *) override
Halide::Internal::CodeGen_LLVM::native_vector_bits
virtual int native_vector_bits() const =0
What's the natural vector bit-width to use for loads, stores, etc.
Halide::Internal::CodeGen_LLVM::wild_u8x_
Expr wild_u8x_
Definition CodeGen_LLVM.h:249
Halide::Internal::CodeGen_LLVM::match_vector_type_scalable
llvm::Value * match_vector_type_scalable(llvm::Value *value, llvm::Type *guide)
Halide::Internal::CodeGen_LLVM::dimension_t_type
llvm::StructType * dimension_t_type
Definition CodeGen_LLVM.h:236
Halide::Internal::CodeGen_LLVM::onetime_warnings
std::map< WarningKind, std::string > onetime_warnings
Definition CodeGen_LLVM.h:682
Halide::Internal::CodeGen_LLVM::i8_t
llvm::Type * i8_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::compile_trampolines
static std::unique_ptr< llvm::Module > compile_trampolines(const Target &target, llvm::LLVMContext &context, const std::string &suffix, const std::vector< std::pair< std::string, ExternSignature > > &externs)
Halide::Internal::CodeGen_LLVM::compile_func
virtual void compile_func(const LoweredFunc &func, const std::string &simple_name, const std::string &extern_name)
Compile a specific halide declaration into the llvm Module.
Halide::Internal::CodeGen_LLVM::signature_to_type
llvm::FunctionType * signature_to_type(const ExternSignature &signature)
Given a Halide ExternSignature, return the equivalent llvm::FunctionType.
Halide::Internal::CodeGen_LLVM::create_string_constant
llvm::Constant * create_string_constant(const std::string &str)
Put a string constant in the module as a global variable and return a pointer to it.
Halide::Internal::CodeGen_LLVM::i1_t
llvm::Type * i1_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::concat_vectors
virtual llvm::Value * concat_vectors(const std::vector< llvm::Value * > &)
Concatenate a bunch of llvm vectors.
Halide::Internal::CodeGen_LLVM::context
llvm::LLVMContext * context
Definition CodeGen_LLVM.h:167
Halide::Internal::CodeGen_LLVM::set_strict_fp_math
void set_strict_fp_math()
Change floating-point math op emission to use strict flags.
Halide::Internal::CodeGen_LLVM::wild_i16x_
Expr wild_i16x_
Definition CodeGen_LLVM.h:249
Halide::Internal::CodeGen_LLVM::wild_u64x_
Expr wild_u64x_
Definition CodeGen_LLVM.h:250
Halide::Internal::CodeGen_LLVM::create_alloca_at_entry
llvm::Value * create_alloca_at_entry(llvm::Type *type, int n, bool zero_initialize=false, const std::string &name="")
Perform an alloca at the function entrypoint.
Halide::Internal::CodeGen_LLVM::f32_t
llvm::Type * f32_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::match_vector_type_scalable
llvm::Value * match_vector_type_scalable(llvm::Value *value, llvm::Value *guide)
Halide::Internal::CodeGen_LLVM::wild_u32_
Expr wild_u32_
Definition CodeGen_LLVM.h:255
Halide::Internal::CodeGen_LLVM::get_allocation_name
virtual std::string get_allocation_name(const std::string &n)
Get a unique name for the actual block of memory that an allocate node uses.
Definition CodeGen_LLVM.h:347
Halide::Internal::CodeGen_LLVM::use_soft_float_abi
virtual bool use_soft_float_abi() const =0
Halide::Internal::CodeGen_LLVM::mabi
virtual std::string mabi() const
Halide::Internal::CodeGen_LLVM::visit
void visit(const Evaluate *) override
Halide::Internal::CodeGen_LLVM::intrinsics
std::map< std::string, std::vector< Intrinsic > > intrinsics
Mapping of intrinsic functions to the various overloads implementing it.
Definition CodeGen_LLVM.h:476
Halide::Internal::CodeGen_LLVM::mangle_llvm_type
std::string mangle_llvm_type(llvm::Type *type)
Return type string for LLVM type using LLVM IR intrinsic type mangling.
Halide::Internal::CodeGen_LLVM::visit
void visit(const LE *) override
Halide::Internal::CodeGen_LLVM::codegen_buffer_pointer
llvm::Value * codegen_buffer_pointer(const std::string &buffer, Type type, llvm::Value *index)
Generate a pointer into a named buffer at a given index, of a given type.
Halide::Internal::CodeGen_LLVM::visit
void visit(const NE *) override
Halide::Internal::CodeGen_LLVM::add_tbaa_metadata
void add_tbaa_metadata(llvm::Instruction *inst, std::string buffer, const Expr &index)
Mark a load or store with type-based-alias-analysis metadata so that llvm knows it can reorder loads ...
Halide::Internal::CodeGen_LLVM::visit
void visit(const And *) override
Halide::Internal::CodeGen_LLVM::use_pic
virtual bool use_pic() const
Halide::Internal::CodeGen_LLVM::scalarize
void scalarize(const Expr &)
Codegen a vector Expr by codegenning each lane and combining.
Halide::Internal::CodeGen_LLVM::visit
void visit(const StringImm *) override
Halide::Internal::CodeGen_LLVM::in_strict_float
bool in_strict_float
Definition CodeGen_LLVM.h:175
Halide::Internal::CodeGen_LLVM::codegen_vector_reduce
virtual void codegen_vector_reduce(const VectorReduce *op, const Expr &init)
Compile a horizontal reduction that starts with an explicit initial value.
Halide::Internal::CodeGen_LLVM::codegen_asserts
void codegen_asserts(const std::vector< const AssertStmt * > &asserts)
Codegen a block of asserts with pure conditions.
Halide::Internal::CodeGen_LLVM::get_llvm_intrin
llvm::Function * get_llvm_intrin(const Type &ret_type, const std::string &name, const std::vector< Type > &arg_types, bool scalars_are_vectors=false)
Get an LLVM intrinsic declaration.
Halide::Internal::CodeGen_LLVM::get_requested_alloca_total
size_t get_requested_alloca_total() const
Definition CodeGen_LLVM.h:85
Halide::Internal::CodeGen_LLVM::new_for_target
static std::unique_ptr< CodeGen_LLVM > new_for_target(const Target &target, llvm::LLVMContext &context)
Create an instance of CodeGen_LLVM suitable for the target.
Halide::Internal::CodeGen_LLVM::upgrade_type_for_argument_passing
virtual Type upgrade_type_for_argument_passing(const Type &) const
Return the type that a Halide type should be passed in and out of functions as.
Halide::Internal::CodeGen_LLVM::visit
void visit(const GT *) override
Halide::Internal::CodeGen_LLVM::fixed_to_scalable_vector_type
llvm::Value * fixed_to_scalable_vector_type(llvm::Value *fixed)
Convert an LLVM fixed vector value to the corresponding vscale vector value.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Cast *) override
Halide::Internal::CodeGen_LLVM::CodeGen_LLVM
CodeGen_LLVM(const Target &t)
Halide::Internal::CodeGen_LLVM::visit
void visit(const Ramp *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const Broadcast *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const Mod *) override
Halide::Internal::CodeGen_LLVM::make_halide_type_t
llvm::Value * make_halide_type_t(const Type &)
Turn a Halide Type into an llvm::Value representing a constant halide_type_t.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Call *) override
Halide::Internal::CodeGen_LLVM::initialize_llvm
static void initialize_llvm()
Initialize internal llvm state for the enabled targets.
Halide::Internal::CodeGen_LLVM::simple_call_intrin
llvm::Value * simple_call_intrin(const std::string &intrin, const std::vector< llvm::Value * > &args, llvm::Type *result_type)
call_intrin does far too much to be useful and generally breaks things when one has carefully set thi...
Halide::Internal::CodeGen_LLVM::set_context
void set_context(llvm::LLVMContext &context)
Tell the code generator which LLVM context to use.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Store *) override
Halide::Internal::CodeGen_LLVM::get_vector_num_elements
int get_vector_num_elements(const llvm::Type *t)
Get number of vector elements, taking into account scalable vectors.
Halide::Internal::CodeGen_LLVM::call_intrin
llvm::Value * call_intrin(const Type &t, int intrin_lanes, llvm::Function *intrin, std::vector< Expr >)
Halide::Internal::CodeGen_LLVM::register_destructor
llvm::Value * register_destructor(llvm::Function *destructor_fn, llvm::Value *obj, DestructorType when)
Halide::Internal::CodeGen_LLVM::wild_u8_
Expr wild_u8_
Definition CodeGen_LLVM.h:254
Halide::Internal::CodeGen_LLVM::wild_i8_
Expr wild_i8_
Definition CodeGen_LLVM.h:254
Halide::Internal::CodeGen_LLVM::codegen
llvm::Value * codegen(const Expr &)
Emit code that evaluates an expression, and return the llvm representation of the result of the expre...
Halide::Internal::CodeGen_LLVM::wild_i16_
Expr wild_i16_
Definition CodeGen_LLVM.h:254
Halide::Internal::CodeGen_LLVM::optimize_module
void optimize_module()
Run all of llvm's optimization passes on the module.
Halide::Internal::CodeGen_LLVM::codegen_buffer_pointer
llvm::Value * codegen_buffer_pointer(llvm::Value *base_address, Type type, Expr index)
Halide::Internal::CodeGen_LLVM::set_fast_fp_math
void set_fast_fp_math()
Change floating-point math op emission to use fast flags.
Halide::Internal::CodeGen_LLVM::sym_get
llvm::Value * sym_get(const std::string &name, bool must_succeed=true) const
Fetch an entry from the symbol table.
Halide::Internal::CodeGen_LLVM::visit
void visit(const ProducerConsumer *) override
Halide::Internal::CodeGen_LLVM::shuffle_vectors
virtual llvm::Value * shuffle_vectors(llvm::Value *a, llvm::Value *b, const std::vector< int > &indices)
Create an LLVM shuffle vectors instruction.
Halide::Internal::CodeGen_LLVM::visit
void visit(const LT *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const Load *) override
Halide::Internal::CodeGen_LLVM::MaskVariant
std::variant< NoMask, AllEnabledMask, llvm::Value * > MaskVariant
Predication mask using the above two types for special cases and an llvm::Value for the general one.
Definition CodeGen_LLVM.h:642
Halide::Internal::CodeGen_LLVM::convert_fixed_or_scalable_vector_type
llvm::Value * convert_fixed_or_scalable_vector_type(llvm::Value *arg, llvm::Type *desired_type)
Convert between two LLVM vectors of potentially different scalable/fixed and size.
Halide::Internal::CodeGen_LLVM::slice_vector
virtual llvm::Value * slice_vector(llvm::Value *vec, int start, int extent)
Take a slice of lanes out of an llvm vector.
Halide::Internal::CodeGen_LLVM::declare_intrin_overload
llvm::Function * declare_intrin_overload(const std::string &name, const Type &ret_type, const std::string &impl_name, std::vector< Type > arg_types, bool scalars_are_vectors=false)
Declare an intrinsic function that participates in overload resolution.
Halide::Internal::CodeGen_LLVM::inside_atomic_mutex_node
bool inside_atomic_mutex_node
Are we inside an atomic node that uses mutex locks?
Definition CodeGen_LLVM.h:552
Halide::Internal::CodeGen_LLVM::visit
void visit(const FloatImm *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const IntImm *) override
Generate code for various IR nodes.
Halide::Internal::CodeGen_LLVM::function_does_not_access_memory
void function_does_not_access_memory(llvm::Function *fn)
Add the appropriate function attribute to tell LLVM that the function doesn't access memory.
Halide::Internal::CodeGen_LLVM::strict_fp_math_md
llvm::MDNode * strict_fp_math_md
Definition CodeGen_LLVM.h:172
Halide::Internal::CodeGen_LLVM::call_intrin
llvm::Value * call_intrin(const Type &t, int intrin_lanes, const std::string &name, std::vector< Expr >)
Generate a call to a vector intrinsic or runtime inlined function.
Halide::Internal::CodeGen_LLVM::get_destructor_block
llvm::BasicBlock * get_destructor_block()
Retrieves the block containing the error handling code.
Halide::Internal::CodeGen_LLVM::wild_f32_
Expr wild_f32_
Definition CodeGen_LLVM.h:256
Halide::Internal::CodeGen_LLVM::init_module
virtual void init_module()
Initialize the CodeGen_LLVM internal state to compile a fresh module.
Halide::Internal::CodeGen_LLVM::get_user_context
llvm::Value * get_user_context() const
The user_context argument.
Halide::Internal::CodeGen_LLVM::wild_u64_
Expr wild_u64_
Definition CodeGen_LLVM.h:255
Halide::Internal::CodeGen_LLVM::mattrs
virtual std::string mattrs() const =0
Halide::Internal::CodeGen_LLVM::visit
void visit(const Realize *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const VectorReduce *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const IfThenElse *) override
Halide::Internal::CodeGen_LLVM::i32_t
llvm::Type * i32_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::requested_alloca_total
size_t requested_alloca_total
A (very) conservative guess at the size of all alloca() storage requested (including alignment paddin...
Definition CodeGen_LLVM.h:452
Halide::Internal::CodeGen_LLVM::wild_u1x_
Expr wild_u1x_
Some wildcard variables used for peephole optimizations in subclasses.
Definition CodeGen_LLVM.h:249
Halide::Internal::CodeGen_LLVM::wild_u16x_
Expr wild_u16x_
Definition CodeGen_LLVM.h:249
Halide::Internal::CodeGen_LLVM::pseudostack_slot_t_type
llvm::StructType * pseudostack_slot_t_type
Definition CodeGen_LLVM.h:241
Halide::Internal::CodeGen_LLVM::wild_u32x_
Expr wild_u32x_
Definition CodeGen_LLVM.h:250
Halide::Internal::CodeGen_LLVM::llvm_type_of
llvm::Type * llvm_type_of(llvm::LLVMContext *context, Halide::Type t, int effective_vscale) const
Get the llvm type equivalent to a given halide type.
Halide::Internal::CodeGen_LLVM::scalable_to_fixed_vector_type
llvm::Value * scalable_to_fixed_vector_type(llvm::Value *scalable)
Convert an LLVM vscale vector value to the corresponding fixed vector value.
Halide::Internal::CodeGen_LLVM::wild_i64x_
Expr wild_i64x_
Definition CodeGen_LLVM.h:250
Halide::Internal::CodeGen_LLVM::call_intrin
llvm::Value * call_intrin(const llvm::Type *t, int intrin_lanes, llvm::Function *intrin, std::vector< llvm::Value * >, bool is_reduction=false)
Halide::Internal::CodeGen_LLVM::sym_exists
bool sym_exists(const std::string &name) const
Test if an item exists in the symbol table.
Halide::Internal::CodeGen_LLVM::match_vector_type_scalable
llvm::Value * match_vector_type_scalable(llvm::Value *value, VectorTypeConstraint constraint)
Make sure a value type has the same scalable/fixed vector type as a guide.
Halide::Internal::CodeGen_LLVM::visit
void visit(const Free *) override=0
Generate code for a free node.
Halide::Internal::CodeGen_LLVM::shuffle_vectors
llvm::Value * shuffle_vectors(llvm::Value *v, const std::vector< int > &indices)
Shorthand for shuffling a single vector.
Halide::Internal::CodeGen_LLVM::wild_i32x_
Expr wild_i32x_
Definition CodeGen_LLVM.h:250
Halide::Internal::CodeGen_LLVM::visit
void visit(const Add *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const Block *) override
Halide::Internal::CodeGen_LLVM::supports_call_as_float16
virtual bool supports_call_as_float16(const Call *op) const
Can we call this operation with float16 type?
Halide::Internal::CodeGen_LLVM::visit
void visit(const UIntImm *) override
Halide::Internal::CodeGen_LLVM::visit
void visit(const LetStmt *) override
Halide::Internal::CodeGen_LLVM::void_t
llvm::Type * void_t
Some useful llvm types.
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::f16_t
llvm::Type * f16_t
Definition CodeGen_LLVM.h:232
Halide::Internal::CodeGen_LLVM::call_overloaded_intrin
llvm::Value * call_overloaded_intrin(const Type &result_type, const std::string &name, const std::vector< Expr > &args)
Call an overloaded intrinsic function.
Halide::Internal::CodeGen_LLVM::interleave_vectors
virtual llvm::Value * interleave_vectors(const std::vector< llvm::Value * > &)
Implementation of the intrinsic call to interleave_vectors.
Halide::Internal::CodeGen_LLVM::get_vector_type
llvm::Type * get_vector_type(llvm::Type *, int n, VectorTypeConstraint type_constraint=VectorTypeConstraint::None) const
Halide::Internal::IRVisitor::visit
virtual void visit(const IntImm *)
Halide::Internal::IRVisitor::IRVisitor
IRVisitor()=default
Halide::Internal::Scope
A common pattern when traversing Halide IR is that you need to keep track of stuff when you find a Le...
Definition Scope.h:94
Halide::Module
A halide module.
Definition Module.h:142
Halide::Parameter
A reference-counted handle to a parameter to a halide pipeline.
Definition Parameter.h:40
llvm::IRBuilder
Definition CodeGen_Internal.h:31
Halide::Internal
Definition AbstractGenerator.h:24
Halide::Internal::ArgInfoKind::Function
@ Function
Definition AbstractGenerator.h:27
Halide::Internal::SyntheticParamType::Type
@ Type
Definition Generator.h:2897
Halide::Internal::Monotonic::Constant
@ Constant
Definition Monotonic.h:26
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::LinkageType
LinkageType
Type of linkage a function in a lowered Halide module can have.
Definition Module.h:52
Halide::codegen_llvm
std::unique_ptr< llvm::Module > codegen_llvm(const Module &module, llvm::LLVMContext &context)
Given a Halide module, generate an llvm::Module.
Halide::MetadataNameMap
std::map< std::string, std::string > MetadataNameMap
Definition Module.h:138
llvm
Definition CodeGen_Internal.h:18
int32_t
signed __INT32_TYPE__ int32_t
Definition runtime_internal.h:24
Halide::Expr
A fragment of Halide syntax.
Definition Expr.h:258
Halide::ExternSignature
Definition Pipeline.h:501
Halide::Internal::Add
The sum of two expressions.
Definition IR.h:56
Halide::Internal::Allocate
Allocate a scratch area called with the given name, type, and size.
Definition IR.h:371
Halide::Internal::And
Logical and - are both expressions true.
Definition IR.h:175
Halide::Internal::AssertStmt
If the 'condition' is false, then evaluate and return the message, which should be a call to an error...
Definition IR.h:294
Halide::Internal::Atomic
Lock all the Store nodes in the body statement.
Definition IR.h:994
Halide::Internal::Block
A sequence of statements to be executed in-order.
Definition IR.h:442
Halide::Internal::Broadcast
A vector with 'lanes' elements, in which every element is 'value'.
Definition IR.h:259
Halide::Internal::Call
A function call.
Definition IR.h:490
Halide::Internal::Cast
The actual IR nodes begin here.
Definition IR.h:30
Halide::Internal::CodeGen_LLVM::AllEnabledMask
Type indicating mask to use is all true – all lanes enabled.
Definition CodeGen_LLVM.h:637
Halide::Internal::CodeGen_LLVM::Intrinsic::Intrinsic
Intrinsic(Type result_type, std::vector< Type > arg_types, llvm::Function *impl)
Definition CodeGen_LLVM.h:471
Halide::Internal::CodeGen_LLVM::Intrinsic::impl
llvm::Function * impl
Definition CodeGen_LLVM.h:469
Halide::Internal::CodeGen_LLVM::Intrinsic::result_type
Type result_type
Definition CodeGen_LLVM.h:467
Halide::Internal::CodeGen_LLVM::Intrinsic::arg_types
std::vector< Type > arg_types
Definition CodeGen_LLVM.h:468
Halide::Internal::CodeGen_LLVM::NoMask
Type indicating an intrinsic does not take a mask.
Definition CodeGen_LLVM.h:633
Halide::Internal::CodeGen_LLVM::ScopedFastMath::codegen
CodeGen_LLVM * codegen
Definition CodeGen_LLVM.h:191
Halide::Internal::CodeGen_LLVM::ScopedFastMath::~ScopedFastMath
~ScopedFastMath()
Halide::Internal::CodeGen_LLVM::ScopedFastMath::ScopedFastMath
ScopedFastMath(CodeGen_LLVM *)
Halide::Internal::CodeGen_LLVM::VPArg::value
llvm::Value * value
Definition CodeGen_LLVM.h:623
Halide::Internal::CodeGen_LLVM::VPArg::mangle_index
std::optional< size_t > mangle_index
Definition CodeGen_LLVM.h:625
Halide::Internal::CodeGen_LLVM::VPArg::alignment
int alignment
Definition CodeGen_LLVM.h:626
Halide::Internal::CodeGen_LLVM::VPArg::VPArg
VPArg(llvm::Value *value, std::optional< size_t > mangle_index=std::nullopt, int32_t alignment=0)
Definition CodeGen_LLVM.h:627
Halide::Internal::CodeGen_LLVM::VPResultType
Definition CodeGen_LLVM.h:653
Halide::Internal::CodeGen_LLVM::VPResultType::type
llvm::Type * type
Definition CodeGen_LLVM.h:654
Halide::Internal::CodeGen_LLVM::VPResultType::VPResultType
VPResultType(llvm::Type *type, std::optional< size_t > mangle_index=std::nullopt)
Definition CodeGen_LLVM.h:656
Halide::Internal::CodeGen_LLVM::VPResultType::mangle_index
std::optional< size_t > mangle_index
Definition CodeGen_LLVM.h:655
Halide::Internal::Div
The ratio of two expressions.
Definition IR.h:83
Halide::Internal::EQ
Is the first expression equal to the second.
Definition IR.h:121
Halide::Internal::Evaluate
Evaluate and discard an expression, presumably because it has some side-effect.
Definition IR.h:476
Halide::Internal::FloatImm
Floating point constants.
Definition Expr.h:236
Halide::Internal::For
A for loop.
Definition IR.h:848
Halide::Internal::Free
Free the resources associated with the given buffer.
Definition IR.h:413
Halide::Internal::GE
Is the first expression greater than or equal to the second.
Definition IR.h:166
Halide::Internal::GT
Is the first expression greater than the second.
Definition IR.h:157
Halide::Internal::IfThenElse
An if-then-else block.
Definition IR.h:466
Halide::Internal::IntImm
Integer constants.
Definition Expr.h:218
Halide::Internal::LE
Is the first expression less than or equal to the second.
Definition IR.h:148
Halide::Internal::LT
Is the first expression less than the second.
Definition IR.h:139
Halide::Internal::Let
A let expression, like you might find in a functional language.
Definition IR.h:271
Halide::Internal::LetStmt
The statement form of a let node.
Definition IR.h:282
Halide::Internal::Load
Load a value from a named symbol if predicate is true.
Definition IR.h:217
Halide::Internal::LoweredFunc
Definition of a lowered function.
Definition Module.h:101
Halide::Internal::Max
The greater of two values.
Definition IR.h:112
Halide::Internal::Min
The lesser of two values.
Definition IR.h:103
Halide::Internal::Mod
The remainder of a / b.
Definition IR.h:94
Halide::Internal::ModulusRemainder
The result of modulus_remainder analysis.
Definition ModulusRemainder.h:33
Halide::Internal::Mul
The product of two expressions.
Definition IR.h:74
Halide::Internal::NE
Is the first expression not equal to the second.
Definition IR.h:130
Halide::Internal::Not
Logical not - true if the expression false.
Definition IR.h:193
Halide::Internal::Or
Logical or - is at least one of the expression true.
Definition IR.h:184
Halide::Internal::Prefetch
Represent a multi-dimensional region of a Func or an ImageParam that needs to be prefetched.
Definition IR.h:956
Halide::Internal::ProducerConsumer
This node is a helpful annotation to do with permissions.
Definition IR.h:315
Halide::Internal::Provide
This defines the value of a function at a multi-dimensional location.
Definition IR.h:354
Halide::Internal::Ramp
A linear ramp vector node.
Definition IR.h:247
Halide::Internal::Realize
Allocate a multi-dimensional buffer of the given type and size.
Definition IR.h:427
Halide::Internal::Reinterpret
Reinterpret value as another type, without affecting any of the bits (on little-endian systems).
Definition IR.h:47
Halide::Internal::Select
A ternary operator.
Definition IR.h:204
Halide::Internal::Shuffle
Construct a new vector by taking elements from another sequence of vectors.
Definition IR.h:884
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition Expr.h:427
Halide::Internal::Store
Store a 'value' to the buffer called 'name' at a given 'index' if 'predicate' is true.
Definition IR.h:333
Halide::Internal::StringImm
String constants.
Definition Expr.h:245
Halide::Internal::Sub
The difference of two expressions.
Definition IR.h:65
Halide::Internal::UIntImm
Unsigned integer constants.
Definition Expr.h:227
Halide::Internal::Variable
A named variable.
Definition IR.h:801
Halide::Internal::VectorReduce
Horizontally reduce a vector to a scalar or narrower vector using the given commutative and associati...
Definition IR.h:1012
Halide::Target
A struct representing a target machine and os to generate code for.
Definition Target.h:19
Halide::Type
Types in the halide type system.
Definition Type.h:283