Loading...
Searching...
No Matches
Module.h
Go to the documentation of this file.
#ifndef HALIDE_MODULE_H
#define HALIDE_MODULE_H
/** \file
*
* Defines Module, an IR container that fully describes a Halide program.
*/
#include <functional>
#include <map>
#include <memory>
#include <string>
#include "Argument.h"
#include "Expr.h"
#include "Function.h" // for NameMangling
#include "ModulusRemainder.h"
namespace Halide {
template<typename T, int Dims>
class Buffer;
struct Target;
/** Enums specifying various kinds of outputs that can be produced from a Halide Pipeline. */
enum class OutputFileType {
assembly,
bitcode,
c_header,
c_source,
compiler_log,
cpp_stub,
featurization,
function_info_header,
hlpipe,
llvm_assembly,
object,
python_extension,
pytorch_wrapper,
registration,
schedule,
static_library,
stmt,
conceptual_stmt,
stmt_html,
conceptual_stmt_html,
device_code,
};
/** Type of linkage a function in a lowered Halide module can have.
Also controls whether auxiliary functions and metadata are generated. */
enum class LinkageType {
External, ///< Visible externally.
ExternalPlusMetadata, ///< Visible externally. Argument metadata and an argv wrapper are also generated.
ExternalPlusArgv, ///< Visible externally. Argv wrapper is generated but *not* argument metadata.
Internal, ///< Not visible externally, similar to 'static' linkage in C.
};
namespace Internal {
struct OutputInfo {
std::string name, extension;
// `is_multi` indicates how these outputs are generated
// when using the compile_to_multitarget_xxx() APIs (or via the
// Generator command-line mode):
//
// - If `is_multi` is true, then a separate file of this Output type is
// generated for each target in the multitarget (e.g. object files,
// assembly files, etc). Each of the files will have a suffix appended
// that is based on the specific subtarget.
//
// - If `is_multi` is false, then only one file of this Output type
// regardless of how many targets are in the multitarget. No additional
// suffix will be appended to the filename.
//
bool is_multi{false};
};
std::map<OutputFileType, const OutputInfo> get_output_info(const Target &target);
/** Definition of an argument to a LoweredFunc. This is similar to
* Argument, except it enables passing extra information useful to
* some targets to LoweredFunc. */
struct LoweredArgument : public Argument {
/** For scalar arguments, the modulus and remainder of this
* argument. */
ModulusRemainder alignment;
LoweredArgument() = default;
explicit LoweredArgument(const Argument &arg)
: Argument(arg) {
}
LoweredArgument(const std::string &_name, Kind _kind, const Type &_type, uint8_t _dimensions, const ArgumentEstimates &argument_estimates)
: Argument(_name, _kind, _type, _dimensions, argument_estimates) {
}
};
/** Definition of a lowered function. This object provides a concrete
* mapping between parameters used in the function body and their
* declarations in the argument list. */
struct LoweredFunc {
std::string name;
/** Arguments referred to in the body of this function. */
std::vector<LoweredArgument> args;
/** Body of this function. */
Stmt body;
/** The linkage of this function. */
LinkageType linkage;
/** The name-mangling choice for the function. Defaults to using
* the Target. */
NameMangling name_mangling;
LoweredFunc(const std::string &name,
const std::vector<LoweredArgument> &args,
Stmt body,
LinkageType linkage,
NameMangling mangling = NameMangling::Default);
LoweredFunc(const std::string &name,
const std::vector<Argument> &args,
Stmt body,
LinkageType linkage,
NameMangling mangling = NameMangling::Default);
};
} // namespace Internal
namespace Internal {
struct ModuleContents;
class CompilerLogger;
} // namespace Internal
struct AutoSchedulerResults;
using MetadataNameMap = std::map<std::string, std::string>;
/** A halide module. This represents IR containing lowered function
* definitions and buffers. */
class Module {
Internal::IntrusivePtr<Internal::ModuleContents> contents;
public:
Module(const std::string &name, const Target &target, const MetadataNameMap &metadata_name_map = {});
/** Get the target this module has been lowered for. */
const Target &target() const;
/** The name of this module. This is used as the default filename
* for output operations. */
const std::string &name() const;
/** If this Module had an auto-generated schedule, return a read-only pointer
* to the AutoSchedulerResults. If not, return nullptr. */
const AutoSchedulerResults *get_auto_scheduler_results() const;
/** Return whether this module uses strict floating-point anywhere. */
bool any_strict_float() const;
/** The declarations contained in this module. */
// @{
const std::vector<Buffer<void>> &buffers() const;
const std::vector<Internal::LoweredFunc> &functions() const;
std::vector<Internal::LoweredFunc> &functions();
const std::vector<Module> &submodules() const;
// @}
/** Tries to locate the offloaded CUDA PTX assembly contained in this Module.
* Might return a nullptr in case such buffer is not present in this Module.
*/
Buffer<> get_cuda_ptx_assembly_buffer() const;
/**
* Tries to locate the offloaded (GPU) Device assembly contained in this Module.
* This can be any of the GPU kernel sources, etc...
*/
Buffer<> get_device_code_buffer() const;
/** Return the function with the given name. If no such function
* exists in this module, assert. */
Internal::LoweredFunc get_function_by_name(const std::string &name) const;
/** Add a declaration to this module. */
// @{
void append(const Buffer<void> &buffer);
void append(const Internal::LoweredFunc &function);
void append(const Module &module);
// @}
/** Compile a halide Module to variety of outputs, depending on
* the fields set in output_files. */
void compile(const std::map<OutputFileType, std::string> &output_files) const;
/** Compile a halide Module to in-memory object code. Currently
* only supports LLVM based compilation, but should be extended to
* handle source code backends. */
Buffer<uint8_t> compile_to_buffer() const;
/** Return a new module with all submodules compiled to buffers on
* on the result Module. */
Module resolve_submodules() const;
/** When generating metadata from this module, remap any occurrences
* of 'from' into 'to'. */
void remap_metadata_name(const std::string &from, const std::string &to) const;
/** Retrieve the metadata name map. */
MetadataNameMap get_metadata_name_map() const;
/** Set the AutoSchedulerResults for the Module. It is an error to call this
* multiple times for a given Module. */
void set_auto_scheduler_results(const AutoSchedulerResults &results);
/** Set whether this module uses strict floating-point directives anywhere. */
void set_any_strict_float(bool any_strict_float);
/** Remember the Stmt during lowing before device-specific offloading. */
void set_conceptual_code_stmt(const Internal::Stmt &stmt);
/** Get the remembered conceptual Stmt, remembered before device-specific offloading. */
const Internal::Stmt &get_conceptual_stmt() const;
};
/** Link a set of modules together into one module. */
Module link_modules(const std::string &name, const std::vector<Module> &modules);
/** Create an object file containing the Halide runtime for a given target. For
* use with Target::NoRuntime. Standalone runtimes are only compatible with
* pipelines compiled by the same build of Halide used to call this function. */
void compile_standalone_runtime(const std::string &object_filename, const Target &t);
/** Create an object and/or static library file containing the Halide runtime
* for a given target. For use with Target::NoRuntime. Standalone runtimes are
* only compatible with pipelines compiled by the same build of Halide used to
* call this function. Return a map with just the actual outputs filled in
* (typically, OutputFileType::object and/or OutputFileType::static_library).
*/
std::map<OutputFileType, std::string> compile_standalone_runtime(const std::map<OutputFileType, std::string> &output_files, const Target &t);
using ModuleFactory = std::function<Module(const std::string &fn_name, const Target &target)>;
using CompilerLoggerFactory = std::function<std::unique_ptr<Internal::CompilerLogger>(const std::string &fn_name, const Target &target)>;
void compile_multitarget(const std::string &fn_name,
const std::map<OutputFileType, std::string> &output_files,
const std::vector<Target> &targets,
const std::vector<std::string> &suffixes,
const ModuleFactory &module_factory,
const CompilerLoggerFactory &compiler_logger_factory = nullptr);
} // namespace Halide
#endif
Argument.h
Defines a type used for expressing the type signature of a generated halide pipeline.
Expr.h
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt).
Function.h
Defines the internal representation of a halide function and related classes.
ModulusRemainder.h
Routines for statically determining what expressions are divisible by.
Halide::Buffer
A Halide::Buffer is a named shared reference to a Halide::Runtime::Buffer.
Definition Buffer.h:122
Halide::Module
A halide module.
Definition Module.h:142
Halide::Module::append
void append(const Buffer< void > &buffer)
Add a declaration to this module.
Halide::Module::get_metadata_name_map
MetadataNameMap get_metadata_name_map() const
Retrieve the metadata name map.
Halide::Module::get_cuda_ptx_assembly_buffer
Buffer get_cuda_ptx_assembly_buffer() const
Tries to locate the offloaded CUDA PTX assembly contained in this Module.
Halide::Module::get_device_code_buffer
Buffer get_device_code_buffer() const
Tries to locate the offloaded (GPU) Device assembly contained in this Module.
Halide::Module::set_conceptual_code_stmt
void set_conceptual_code_stmt(const Internal::Stmt &stmt)
Remember the Stmt during lowing before device-specific offloading.
Halide::Module::name
const std::string & name() const
The name of this module.
Halide::Module::compile_to_buffer
Buffer< uint8_t > compile_to_buffer() const
Compile a halide Module to in-memory object code.
Halide::Module::set_auto_scheduler_results
void set_auto_scheduler_results(const AutoSchedulerResults &results)
Set the AutoSchedulerResults for the Module.
Halide::Module::resolve_submodules
Module resolve_submodules() const
Return a new module with all submodules compiled to buffers on on the result Module.
Halide::Module::remap_metadata_name
void remap_metadata_name(const std::string &from, const std::string &to) const
When generating metadata from this module, remap any occurrences of 'from' into 'to'.
Halide::Module::get_auto_scheduler_results
const AutoSchedulerResults * get_auto_scheduler_results() const
If this Module had an auto-generated schedule, return a read-only pointer to the AutoSchedulerResults...
Halide::Module::buffers
const std::vector< Buffer< void > > & buffers() const
The declarations contained in this module.
Halide::Module::functions
const std::vector< Internal::LoweredFunc > & functions() const
Halide::Module::compile
void compile(const std::map< OutputFileType, std::string > &output_files) const
Compile a halide Module to variety of outputs, depending on the fields set in output_files.
Halide::Module::Module
Module(const std::string &name, const Target &target, const MetadataNameMap &metadata_name_map={})
Halide::Module::target
const Target & target() const
Get the target this module has been lowered for.
Halide::Module::get_function_by_name
Internal::LoweredFunc get_function_by_name(const std::string &name) const
Return the function with the given name.
Halide::Module::submodules
const std::vector< Module > & submodules() const
Halide::Module::append
void append(const Module &module)
Halide::Module::set_any_strict_float
void set_any_strict_float(bool any_strict_float)
Set whether this module uses strict floating-point directives anywhere.
Halide::Module::any_strict_float
bool any_strict_float() const
Return whether this module uses strict floating-point anywhere.
Halide::Module::append
void append(const Internal::LoweredFunc &function)
Halide::Module::get_conceptual_stmt
const Internal::Stmt & get_conceptual_stmt() const
Get the remembered conceptual Stmt, remembered before device-specific offloading.
Halide::Module::functions
std::vector< Internal::LoweredFunc > & functions()
Halide::Internal
Definition AbstractGenerator.h:24
Halide::Internal::get_output_info
std::map< OutputFileType, const OutputInfo > get_output_info(const Target &target)
Halide
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Definition AbstractGenerator.h:19
Halide::CompilerLoggerFactory
std::function< std::unique_ptr< Internal::CompilerLogger >(const std::string &fn_name, const Target &target)> CompilerLoggerFactory
Definition Module.h:243
Halide::LinkageType
LinkageType
Type of linkage a function in a lowered Halide module can have.
Definition Module.h:52
Halide::LinkageType::ExternalPlusMetadata
@ ExternalPlusMetadata
Visible externally. Argument metadata and an argv wrapper are also generated.
Definition Module.h:54
Halide::LinkageType::ExternalPlusArgv
@ ExternalPlusArgv
Visible externally. Argv wrapper is generated but not argument metadata.
Definition Module.h:55
Halide::LinkageType::External
@ External
Visible externally.
Definition Module.h:53
Halide::ModuleFactory
std::function< Module(const std::string &fn_name, const Target &target)> ModuleFactory
Definition Module.h:242
Halide::OutputFileType
OutputFileType
Enums specifying various kinds of outputs that can be produced from a Halide Pipeline.
Definition Module.h:26
Halide::OutputFileType::c_source
@ c_source
Definition Module.h:30
Halide::OutputFileType::bitcode
@ bitcode
Definition Module.h:28
Halide::OutputFileType::assembly
@ assembly
Definition Module.h:27
Halide::OutputFileType::compiler_log
@ compiler_log
Definition Module.h:31
Halide::OutputFileType::static_library
@ static_library
Definition Module.h:42
Halide::OutputFileType::stmt
@ stmt
Definition Module.h:43
Halide::OutputFileType::c_header
@ c_header
Definition Module.h:29
Halide::OutputFileType::conceptual_stmt
@ conceptual_stmt
Definition Module.h:44
Halide::OutputFileType::registration
@ registration
Definition Module.h:40
Halide::OutputFileType::llvm_assembly
@ llvm_assembly
Definition Module.h:36
Halide::OutputFileType::hlpipe
@ hlpipe
Definition Module.h:35
Halide::OutputFileType::cpp_stub
@ cpp_stub
Definition Module.h:32
Halide::OutputFileType::device_code
@ device_code
Definition Module.h:47
Halide::OutputFileType::featurization
@ featurization
Definition Module.h:33
Halide::OutputFileType::schedule
@ schedule
Definition Module.h:41
Halide::OutputFileType::stmt_html
@ stmt_html
Definition Module.h:45
Halide::OutputFileType::function_info_header
@ function_info_header
Definition Module.h:34
Halide::OutputFileType::object
@ object
Definition Module.h:37
Halide::OutputFileType::python_extension
@ python_extension
Definition Module.h:38
Halide::OutputFileType::conceptual_stmt_html
@ conceptual_stmt_html
Definition Module.h:46
Halide::OutputFileType::pytorch_wrapper
@ pytorch_wrapper
Definition Module.h:39
Halide::NameMangling
NameMangling
An enum to specify calling convention for extern stages.
Definition Function.h:26
Halide::NameMangling::Default
@ Default
Match whatever is specified in the Target.
Definition Function.h:27
Halide::compile_standalone_runtime
void compile_standalone_runtime(const std::string &object_filename, const Target &t)
Create an object file containing the Halide runtime for a given target.
Halide::compile_multitarget
void compile_multitarget(const std::string &fn_name, const std::map< OutputFileType, std::string > &output_files, const std::vector< Target > &targets, const std::vector< std::string > &suffixes, const ModuleFactory &module_factory, const CompilerLoggerFactory &compiler_logger_factory=nullptr)
Halide::link_modules
Module link_modules(const std::string &name, const std::vector< Module > &modules)
Link a set of modules together into one module.
Halide::MetadataNameMap
std::map< std::string, std::string > MetadataNameMap
Definition Module.h:138
uint8_t
unsigned __INT8_TYPE__ uint8_t
Definition runtime_internal.h:29
Halide::ArgumentEstimates
Definition Argument.h:18
Halide::Argument::Argument
Argument()=default
Halide::Argument::Kind
Kind
An argument is either a primitive type (for parameters), or a buffer pointer.
Definition Argument.h:52
Halide::Argument::argument_estimates
ArgumentEstimates argument_estimates
Definition Argument.h:72
Halide::AutoSchedulerResults
Definition Pipeline.h:94
Halide::Internal::IntrusivePtr
Intrusive shared pointers have a reference count (a RefCount object) stored in the class itself.
Definition IntrusivePtr.h:71
Halide::Internal::LoweredArgument::alignment
ModulusRemainder alignment
For scalar arguments, the modulus and remainder of this argument.
Definition Module.h:87
Halide::Internal::LoweredArgument::LoweredArgument
LoweredArgument(const Argument &arg)
Definition Module.h:90
Halide::Internal::LoweredArgument::LoweredArgument
LoweredArgument(const std::string &_name, Kind _kind, const Type &_type, uint8_t _dimensions, const ArgumentEstimates &argument_estimates)
Definition Module.h:93
Halide::Internal::LoweredArgument::LoweredArgument
LoweredArgument()=default
Halide::Internal::LoweredFunc
Definition of a lowered function.
Definition Module.h:101
Halide::Internal::LoweredFunc::LoweredFunc
LoweredFunc(const std::string &name, const std::vector< LoweredArgument > &args, Stmt body, LinkageType linkage, NameMangling mangling=NameMangling::Default)
Halide::Internal::LoweredFunc::args
std::vector< LoweredArgument > args
Arguments referred to in the body of this function.
Definition Module.h:105
Halide::Internal::LoweredFunc::LoweredFunc
LoweredFunc(const std::string &name, const std::vector< Argument > &args, Stmt body, LinkageType linkage, NameMangling mangling=NameMangling::Default)
Halide::Internal::LoweredFunc::name_mangling
NameMangling name_mangling
The name-mangling choice for the function.
Definition Module.h:115
Halide::Internal::LoweredFunc::linkage
LinkageType linkage
The linkage of this function.
Definition Module.h:111
Halide::Internal::LoweredFunc::name
std::string name
Definition Module.h:102
Halide::Internal::LoweredFunc::body
Stmt body
Body of this function.
Definition Module.h:108
Halide::Internal::ModulusRemainder
The result of modulus_remainder analysis.
Definition ModulusRemainder.h:33
Halide::Internal::OutputInfo
Definition Module.h:61
Halide::Internal::OutputInfo::name
std::string name
Definition Module.h:62
Halide::Internal::OutputInfo::is_multi
bool is_multi
Definition Module.h:77
Halide::Internal::OutputInfo::extension
std::string extension
Definition Module.h:62
Halide::Internal::Stmt
A reference-counted handle to a statement node.
Definition Expr.h:427
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