Expand description
§A crate for lowering HUGR
s into LLVM.
§Five minute Introduction to LLVM and inkwell
References:
- The full specification for LLVM IR as on the main branch: https://llvm.org/docs/LangRef.html.
- The full specification for LLVM IR version for version 14.0: https://releases.llvm.org/14.0.0/docs/LangRef.html
- The documentation for the inkwell crate https://thedan64.github.io/inkwell/inkwell/index.html
LLVM offers a stable C interface to most of it’s functionality. These
bindings are exposed to rust through the llvm-sys
crate; there is quite a
lot of feature-ing and build-time logic done there to support various
linking configurations and llvm versions.
The inkwell crate offers safe wrappers around these bindings and is what we use throughout.
§Definition of LLVM terms:
-
Context: A context owns all of the many LLVM objects. Most all
inkwell
types take a lifetime parameter which ensures they do not outlive their owningContext
. AContext
is not thread safe. A Context is used to construct modules, types, builders, and basic blocks. -
Module: A module is owned by a
Context
. It is a container for globals and functions. Afoo.ll
file containing LLVM IR would be loaded into aModule
. -
Function: A function has a name(symbol), parameters, a return type, linkage(symbol visibility) and various other attributes. It may contain basic blocks, in which case compiling the owning module will produce object code for the function. If it does not contain basic blocks then it represents an external symbol that can be called, and object code for that symbol must be linked with the object code from this module.
-
Instruction: A basic block is an ordered list of instructions. Examples:
load
,store
,iadd
,ret
. They are all defined in theLangRef
. -
Intrinsic: A “special function” provided by LLVM. These are called like functions but are treated like an instruction(i.e. special cased) by various passes. The differences between
Instruction
andIntrinsic
are not well motivated and are largely an accident of history. They are all defined in theLangRef
. -
Values: The things
Instruction
s take and return. They can be the parameters of functions, the results of instructions, constants, symbol references to globals, and other more esoteric things. Mostly we useBasicValueEnum
orBasicValue
. -
Types: Every
Value
has a type. For examplei32
,f64
,ptr
. In particular types are used to construct constant values. -
Builder: This is the mechanism by which one inserts instructions into a basic block into a basic block. A builder has a “current position” where the next instruction will be inserted. It has many functions such as
inkwell::builder::Builder::build_call
which are used to create and insert instructions.
Re-exports§
pub use custom::CodegenExtension;
pub use custom::CodegenExtsBuilder;
pub use inkwell;
pub use inkwell::llvm_sys;
Modules§
- custom
- Provides an interface for extending
hugr-llvm
to emitCustomType
s,CustomConst
s, andExtensionOp
s. - emit
- extension
- sum
- types
- utils
- Module for utilities that do not depend on LLVM. These are candidates for upstreaming.