12 KiB
Python API Reference
This project's comprehensive API reference is the generated type stub:
.venv/Lib/site-packages/llvm/__init__.pyi
That file is generated from the nanobind bindings and contains the full public surface area (functions, classes, methods, properties, argument names, and types) for the currently built version.
How To Regenerate
uv sync
The stub is regenerated during rebuild.
How To Browse Quickly
# List public classes
rg "^class " .venv/Lib/site-packages/llvm/__init__.pyi
# List top-level functions
rg "^def " .venv/Lib/site-packages/llvm/__init__.pyi
# Jump to one API quickly
rg "replace_all_uses_with|erase_from_parent|split_basic_block|const_string" .venv/Lib/site-packages/llvm/__init__.pyi
Notes
- This is the canonical reference for exact signatures.
- Narrative usage guidance and caveats remain in:
README.mddevdocs/porting-guide.mddevdocs/lit-tests.mddevdocs/DEBUGGING.md
High-Level UX Helpers
These APIs wrap common workflows that previously required LLVM-C-style boilerplate:
# Intrinsics by name.
builder.intrinsic("llvm.sqrt", [x], overloaded_types=[x.type])
# Explicit PassBuilder pipeline optimization.
mod.optimize("default<O2>", target_machine=tm)
# Direct object/assembly emission. Optimize explicitly first when desired.
obj = mod.emit_object(target_machine=tm)
asm = mod.emit_assembly(target_machine=tm)
# Host target machine convenience.
tm = llvm.TargetMachine.host()
# LLVM-C ORC LLJIT.
with llvm.JIT.host() as jit:
jit.add_module(mod) # invalidates mod on success
addr = jit.lookup("compiled_function")
# Metadata by name, without public kind IDs.
text = ctx.md_string("frontend")
assert text.is_string
assert text.string == "frontend"
node = ctx.md_node([text])
for operand in node.operands:
assert operand.string == "frontend"
inst.metadata["llvm.loop"] = loop_md
md = inst.metadata.get("llvm.loop")
src_inst.metadata.copy_to(dst_inst)
del inst.metadata["llvm.loop"]
# Named metadata and module flags.
mod.named_metadata["llvm.dbg.cu"].append(compile_unit)
mod.module_flags.add("Debug Info Version", llvm.ModuleFlagBehavior.Warning, md)
# Debug-location scopes.
with builder.debug_location(line=12, column=4, scope=subprogram):
inst = builder.add(a, b, "sum")
builder.intrinsic(..., overloaded_types=[...]) uses LLVM's intrinsic
overload-disambiguation type list. This list selects the intrinsic declaration;
it is not the same as the call operand list.
Value API Validity Matrix
This section documents when llvm.Value accessors are valid to call. Invalid
calls should raise llvm.LLVMAssertionError (not crash).
Always Valid (for any live Value)
- Generic identity/introspection:
type,name,value_kind,is_constant,is_undef,is_poison
- Use/operand graph:
uses,users,has_uses,num_operands,operands,get_operand,set_operand,get_operand_use- Prefer semantic accessors over raw operand indexing whenever available.
Raw operand layout is instruction-specific and can differ from printed IR;
see
devdocs/operands.md.
- Type predicates:
is_*predicate properties (function/global/constant/etc.)
Global Families
- Global variable only:
next_global,prev_globalinitializer(get/set)set_constant,is_global_constantset_thread_local,is_thread_localset_externally_initialized,is_externally_initializeddelete_global,delete
- Global value:
global_value_type,unnamed_addresslinkage,visibility,dll_storage_classmetadatamapping view; usevalue.metadata.copy_to(other)for bulk copies
- Global object:
comdat,set_comdatsectionalignment(global path)
Function Families
- Function only:
has_personality_fn,personality_fn,set_personality_fnhas_prefix_data,prefix_data,set_prefix_datahas_prologue_data,prologue_data,set_prologue_datafunction_type- Accessors
personality_fn/prefix_data/prologue_dataare exception-first:- use
has_personality_fn/has_prefix_data/has_prologue_databefore accessing when absence is possible.
- use
Instruction Families
- Any instruction:
opcode,opcode_namemetadatamapping view; useinst.metadata.copy_to(other)for bulk copiesnext_instruction,prev_instructionremove_from_parent,erase_from_parent,delete_instruction,instruction_clone
- Branch:
is_conditional,conditionrequirebr
- Terminator:
num_successors,get_successor,successors,unwind_dest- BranchInst gotcha (conditional
br):- successor order is
[true, false] - raw operand order is
[cond, false, true] - mapping:
get_successor(0) == get_operand(2).value_as_basic_block()andget_successor(1) == get_operand(1).value_as_basic_block()
- successor order is
- PHI:
num_incoming,get_incoming_value,get_incoming_block,incoming
- LandingPad:
num_clauses,get_clause,is_cleanup,set_cleanup,add_clause
- CatchPad/CatchSwitch:
parent_catch_switch,num_handlers,handlers,add_handler
- ShuffleVector:
num_mask_elements,get_mask_value
Opcode-Specific Instruction Accessors
icmp_predicate:icmpfcmp_predicate:fcmpnsw/set_nsw:add,sub,mul,shlnuw/set_nuw:add,sub,mul,shlexact/set_exact:udiv,sdiv,lshr,ashrnneg/set_nneg:zextis_disjoint/set_is_disjoint:oricmp_same_sign/set_icmp_same_sign:icmpallocated_type:alloca
Call/Invoke/CallBr Families
- Call-like (
call,invoke,callbr):num_operand_bundles,get_operand_bundle_at_indexcalled_function_type,called_value,set_called_operand
- Arg operand instructions (
call,invoke,callbr,catchpad,cleanuppad):num_arg_operands,get_arg_operand
- Tail call kind (
call,invoke):tail_call_kind,set_tail_call_kind
- Callsite attributes use slot accessors:
callsite_attributesfor callsite-level attributes.callsite_return_attributesfor return-value attributes.callsite_param_attributes(i)for argument attributes, using a 0-based Python index.- Requires a call-like instruction.
Atomic/Memory Families
- Volatile accessors:
is_volatile,set_volatilerequireload/store
- Ordering accessors:
ordering,set_orderingrequire atomic-capable memory instruction (load,store,fence,atomicrmw,cmpxchg)
- Atomic metadata:
is_atomic: instruction onlyatomic_sync_scope_id,set_atomic_sync_scope_id: atomic-capable memory instructionatomic_rmw_bin_op:atomicrmwcmpxchg_success_ordering,cmpxchg_failure_ordering,weak,set_weak:cmpxchg
Indexed/GEP Families
num_indices:getelementptr,extractvalue,insertvalue(instruction or const-expr)
indices:extractvalue/insertvalueinstruction, or const-exprgetelementptr/extractvalue/insertvalue
gep_source_element_type,gep_no_wrap_flags:- GEP value (instruction or const-expr)
Constant-Only / Type-Safe Mutators
- Constant-only:
const_bitcastrequires a constant value
- Replacement:
replace_all_uses_withrequires same context and identical type
- Fast-math:
fast_math_flags,set_fast_math_flagsrequirecan_use_fast_math_flags == True
Non-Value API Validity Matrix
This section captures guard preconditions for wrapper classes other than
llvm.Value.
Type (llvm.Type)
- Struct-only:
is_packed_struct,is_opaque_struct,is_literal_structstruct_name,struct_element_count,get_struct_element_type
- Function-only:
is_vararg,return_type,param_count,param_types
- Pointer-only:
is_opaque_pointer,pointer_address_space
- Array-only:
array_length
- Vector-only:
vector_size
- Target-extension-only:
target_ext_type_name,target_ext_type_num_type_params,target_ext_type_num_int_params,get_target_ext_type_type_param,get_target_ext_type_int_param- target-ext parameter accessors require in-range indices.
- Element-type family:
element_typerequires pointer/vector/array type.
- Constant constructors:
constantrequires an integer type.real_constantrequires a floating type.constant(str, radix)requires2 <= radix <= 36.
- Named struct creation:
ctx.types.struct("Name", [field_types...])defaults toreuse_existing=True.- Reusing the same name with the same body returns the existing identified
struct; reusing an opaque declaration completes it; conflicting bodies or
packing raise
LLVMAssertionError. - Pass
reuse_existing=Falsefor raw LLVM insertion behavior, which may append a suffix to duplicate names.
- Struct body mutation:
set_bodyrequires identified opaque struct type (not literal, not already non-opaque).
BasicBlock (llvm.BasicBlock)
terminatorrequires the block to have a terminator.first_non_phiis semantic-optional:- returns
Nonewhen the block has no non-PHI instruction.
- returns
- For insertion before first non-PHI without optional handling:
- use
create_builder(first_non_phi=True), which falls back to block-end when no non-PHI instruction exists.
- use
- Parent navigation:
functionrequires block attached to a function.module/contextrequire function parent and module parent.
- Split helpers:
split_basic_block/split_basic_block_beforerequire:- instruction operand is an instruction in this block,
- not a PHI split point,
- source block already has a terminator.
Function (llvm.Function)
append_basic_blockrequires function parent module and module context.append_existing_basic_blockrequires an unattached block.- Basic block accessors are exception-first:
entry_blockrequiresis_declaration == False.first_basic_block/last_basic_blockrequirebasic_block_count > 0.
- Attribute APIs use slot accessors, not raw LLVM indices:
attributesfor function-level attributes.return_attributesfor return-value attributes.param_attributes(i)for parameter attributes, using a 0-based Python index.- Use
llvm.Attribute.enum/type/string(...)orslot.add("noreturn").
block_addressrequires block ownership by that function. Preferbb.block_address()when the function can be inferred.- Parent navigation:
module/contextrequire function has a parent module.
Builder (llvm.Builder)
- Builder creation from instruction:
Context.create_builder(inst)andValue.create_builder()require instruction values attached to a basic block.
- Positioning:
position_before(inst)requires an instruction attached to a block.position_at(bb, inst)requiresinstto be an instruction inbb.
- Instruction-only insertion helpers:
insert_into_builder_with_name(instr)requires instruction value.add_metadata_to_inst(instr)requires instruction value.
BasicBlock block_address()
- Requires the block to be attached to a function.
Function.block_address(bb)is also available when explicitly checking a block against a specific function.
OperandBundle (llvm.OperandBundle)
get_arg_at_index(index):- requires
0 <= index < num_args
- requires
Binary/Object Iterators (llvm.Binary)
sectionsandsymbols:- require object-file binary types (
COFF,ELF*,MachO*,Wasm). - non-object binaries (e.g.,
IR, archive, import file) raiseLLVMAssertionErrorwith the concrete binary type in the message.
- require object-file binary types (