Add function/parameter attributes and IR metadata to LLVM 22 IR to unlock optimizer opportunities. Covers NoUnwind, ReadNone, NoCapture, Noundef, loop vectorization hints, branch weights, TBAA, !range, and !nonnull.
Use this skill to annotate your generated IR so the optimizer can make better decisions — inlining, alias analysis, vectorization, branch layout, and more.
Correct attributes are one of the highest-ROI improvements you can make to a frontend:
nounwind eliminates EH cleanup code on every call.nocapture on parameters enables escape analysis and alias analysis.noundef enables constant folding and value-range propagation.Set once per function, before or after emitting its body:
// Never throws — most important; enables call-site EH cleanup elision
F->addFnAttr(llvm::Attribute::NoUnwind);
// Reads no memory (pure function)
F->addFnAttr(llvm::Attribute::ReadNone);
// Reads memory but never writes
F->addFnAttr(llvm::Attribute::ReadOnly);
// Only accesses memory via its own arguments (no globals)
F->addFnAttr(llvm::Attribute::ArgMemOnly);
// Does not recurse (direct or indirect)
F->addFnAttr(llvm::Attribute::NoRecurse);
// Always returns (no infinite loops, no exit() calls)
F->addFnAttr(llvm::Attribute::WillReturn);
// Never returns (e.g., panic(), abort()) — always emit unreachable after call
F->addFnAttr(llvm::Attribute::NoReturn);
// Rarely called — deprioritize in layout
F->addFnAttr(llvm::Attribute::Cold);
// On a call site (not function definition):
CI->addFnAttr(llvm::Attribute::NoUnwind);