LLVM-IR2VEC(1)

๐Ÿ“ฆ version 23.1.3, in centos-stream 9

๐ŸŒธ NAME ๐ŸŒธ

okay bestie, this is LLVM-IR2VEC(1) โ€” the llvm-ir2vec page from the LLVM manual. It's literally the IR2Vec and MIR2Vec Embedding Generation Tool, and it's here to make embeddings happen. ๐Ÿ’–

llvm-ir2vec is a standalone command-line tool for IR2Vec and MIR2Vec. It generates embeddings for both LLVM IR and Machine IR (MIR) and supports triplet generation for vocabulary training. no cap. โœจ

๐ŸŽ€ SYNOPSIS ๐ŸŽ€

llvm-ir2vec [subcommand] [options]

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

okay so the tool gives you three main subcommands, and they're all kinda iconic:

  1. triplets โ€” Generates numeric triplets in train2id format for vocabulary training from LLVM IR.
  2. entities โ€” Generates entity mapping files (entity2id.txt) for vocabulary training.
  3. embeddings โ€” Generates IR2Vec or MIR2Vec embeddings using a trained vocabulary at different granularity levels (instruction, basic block, or function).

The tool supports two operation modes:

  • LLVM IR mode (--mode=llvm) โ€” Process LLVM IR bitcode files and generate IR2Vec embeddings
  • Machine IR mode (--mode=mir) โ€” Process Machine IR (.mir) files and generate MIR2Vec embeddings

The tool is designed to facilitate machine learning applications that work with LLVM IR or Machine IR by converting them into numerical representations that can be used by ML models. The triplets subcommand generates numeric IDs directly instead of string triplets, streamlining the training data preparation workflow. periodt. ๐Ÿ’…

NOTE: For information about using IR2Vec and MIR2Vec programmatically within LLVM passes and the C++ API, see the IR2Vec Embeddings section in the MLGO documentation.

๐ŸŒˆ OPERATION MODES ๐ŸŒˆ

The tool operates in two modes: LLVM IR mode and Machine IR mode. The mode is selected using the --mode option (default: llvm).

Triplet Generation and Entity Mapping Modes are used for preparing vocabulary and training data for knowledge graph embeddings. The Embedding Mode is used for generating embeddings from LLVM IR using a pre-trained vocabulary.

The Seed Embedding Vocabulary of IR2Vec is trained on a large corpus of LLVM IR by modeling the relationships between opcodes, types, and operands as a knowledge graph. For this purpose, Triplet Generation and Entity Mapping Modes generate triplets and entity mappings in the standard format used for knowledge graph embedding training (see https://github.com/thunlp/OpenKE/tree/OpenKE-PyTorch?tab=readme-ov-file#data-format for details).

See llvm/utils/mlgo-utils/IR2Vec/generateTriplets.py for more details on how these two modes are used to generate the triplets and entity mappings. ๐Ÿซถ

๐Ÿงš Triplet Generation ๐Ÿงš

With the triplets subcommand, llvm-ir2vec analyzes LLVM IR or Machine IR and extracts numeric triplets consisting of opcode IDs and operand IDs. These triplets are generated in the standard format used for knowledge graph embedding training. The tool outputs numeric IDs directly using the vocabulary mapping infrastructure, eliminating the need for string-to-ID preprocessing. slay. ๐Ÿ’–

Usage for LLVM IR:

llvm-ir2vec triplets --mode=llvm input.bc -o triplets_train2id.txt

Usage for Machine IR:

llvm-ir2vec triplets --mode=mir input.mir -o triplets_train2id.txt

๐Ÿ“ Entity Mapping Generation ๐Ÿ“

With the entities subcommand, llvm-ir2vec generates the entity mappings supported by IR2Vec or MIR2Vec in the standard format used for knowledge graph embedding training. This subcommand outputs all supported entities with their corresponding numeric IDs. ๐Ÿ’œ

For LLVM IR, entities include opcodes, types, and operands. For Machine IR, entities include machine opcodes, common operands, and register classes (both physical and virtual).

Usage for LLVM IR:

llvm-ir2vec entities --mode=llvm -o entity2id.txt

Usage for Machine IR:

llvm-ir2vec entities --mode=mir input.mir -o entity2id.txt
NOTE: For LLVM IR mode, the entity mapping is target-independent and does not require an input file. For Machine IR mode, an input .mir file is required to determine the target architecture, as entity mappings vary by target (different architectures have different instruction sets and register classes).

โญ Embedding Generation โญ

With the embeddings subcommand, llvm-ir2vec uses a pre-trained vocabulary to generate numerical embeddings for LLVM IR or Machine IR at different levels of granularity. main character energy fr fr. ๐Ÿ‘‘

Example Usage for LLVM IR:

llvm-ir2vec embeddings --mode=llvm --ir2vec-vocab-path=vocab.json --ir2vec-kind=symbolic --level=func input.bc -o embeddings.txt

Example Usage for Machine IR:

llvm-ir2vec embeddings --mode=mir --mir2vec-vocab-path=vocab.json --level=func input.mir -o embeddings.txt

๐Ÿ’… OPTIONS ๐Ÿ’…

๐Ÿงฐ Common options (applicable to both LLVM IR and Machine IR modes)

  • --mode=<mode> โ€” Specify the operation mode. Valid values are:
    • llvm โ€” Process LLVM IR bitcode files (default)
    • mir โ€” Process Machine IR (.mir) files
  • -o <filename> โ€” Specify the output filename. Use - to write to standard output (default).
  • --help โ€” Print a summary of command line options.

๐Ÿงฉ Subcommand-specific options

embeddings subcommand

  • <input-file> โ€” The input LLVM IR/bitcode file (.ll/.bc) or Machine IR file (.mir) to process. This positional argument is required for the embeddings subcommand.
  • --level=<level> โ€” Specify the embedding generation level. Valid values are:
    • inst โ€” Generate instruction-level embeddings
    • bb โ€” Generate basic block-level embeddings
    • func โ€” Generate function-level embeddings (default)
  • --function=<name> โ€” Process only the specified function instead of all functions in the module.

IR2Vec-specific options (for --mode=llvm)

  • --ir2vec-kind=<kind> โ€” Specify the kind of IR2Vec embeddings to generate. Valid values are:
    • symbolic โ€” Generate symbolic embeddings (default)
    • flow-aware โ€” Generate flow-aware embeddings

Flow-aware embeddings consider control flow relationships between instructions, while symbolic embeddings focus on the symbolic representation of instructions.

  • --ir2vec-vocab-path=<path> โ€” Specify the path to the IR2Vec vocabulary file (required for LLVM IR embedding generation). The vocabulary file should be in JSON format and contain the trained vocabulary for embedding generation. See llvm/lib/Analysis/models for pre-trained vocabulary files.
  • --ir2vec-opc-weight=<weight> โ€” Specify the weight for opcode embeddings (default: 1.0). This controls the relative importance of instruction opcodes in the final embedding.
  • --ir2vec-type-weight=<weight> โ€” Specify the weight for type embeddings (default: 0.5). This controls the relative importance of type information in the final embedding.
  • --ir2vec-arg-weight=<weight> โ€” Specify the weight for argument embeddings (default: 0.2). This controls the relative importance of operand information in the final embedding.

MIR2Vec-specific options (for --mode=mir)

  • --mir2vec-vocab-path=<path> โ€” Specify the path to the MIR2Vec vocabulary file (required for Machine IR embedding generation). The vocabulary file should be in JSON format and contain the trained vocabulary for embedding generation.
  • --mir2vec-kind=<kind> โ€” Specify the kind of MIR2Vec embeddings to generate. Valid values are:
    • symbolic โ€” Generate symbolic embeddings (default)
  • --mir2vec-opc-weight=<weight> โ€” Specify the weight for machine opcode embeddings (default: 1.0). This controls the relative importance of machine instruction opcodes in the final embedding.
  • --mir2vec-common-operand-weight=<weight> โ€” Specify the weight for common operand embeddings (default: 1.0). This controls the relative importance of common operand types in the final embedding.
  • --mir2vec-reg-operand-weight=<weight> โ€” Specify the weight for register operand embeddings (default: 1.0). This controls the relative importance of register operands in the final embedding.

triplets subcommand

  • <input-file> โ€” The input LLVM IR/bitcode file (.ll/.bc) or Machine IR file (.mir) to process. This positional argument is required for the triplets subcommand.

entities subcommand

  • <input-file> โ€” The input Machine IR file (.mir) to process. This positional argument is required for the entities subcommand when using --mode=mir, as the entity mappings are target-specific. For --mode=llvm, no input file is required as IR2Vec entity mappings are target-independent.

โœจ OUTPUT FORMAT โœจ

Okay bestie, this is the part where you find out what actually comes out the other side ๐Ÿ’… The shape of the output depends on which mode you picked, so let's go through them one by one, no skipping, we don't do that here ๐Ÿซถ


๐ŸŽ€ Triplet Mode Output ๐ŸŽ€

In triplet mode, the output is numeric triplets in train2id format, plus some metadata headers riding along at the top. The format includes:

MAX_RELATION=<max_relation_count>
<head_entity_id> <tail_entity_id> <relation_id>
<head_entity_id> <tail_entity_id> <relation_id>
...

Every single line after that metadata header is one instruction relationship, with numeric IDs for head entity, tail entity, and relation type. The metadata header (MAX_RELATION) tells you the max relation ID that got used. It's giving receipts, honestly ๐Ÿ’–

Relation Types

For LLVM IR (IR2Vec):

  • 0 = Type relationship (instruction to its type)
  • 1 = Next relationship (sequential instructions)
  • 2+ = Argument relationships (Arg0, Arg1, Arg2, ...)

For Machine IR (MIR2Vec):

  • 0 = Next relationship (sequential instructions)
  • 1+ = Argument relationships (Arg0, Arg1, Arg2, ...)

Entity IDs

For LLVM IR: entity IDs represent opcodes, types, and operands as defined by the IR2Vec vocabulary. Iconic, defined, no cap โœจ

For Machine IR: entity IDs represent machine opcodes, common operands (immediate, frame index, etc.), physical register classes, and virtual register classes as defined by the MIR2Vec vocabulary. Heads up though โ€” the entity layout is target-specific, so don't assume it's the same everywhere ๐Ÿ™ˆ


๐Ÿฆ‹ Entity Mode Output ๐Ÿฆ‹

In entity mode, you get entity mappings, and the format is:

<total_entities>
<entity_string>      <numeric_id>
<entity_string>      <numeric_id>
...

First line = the total number of entities. After that, one entity mapping per line, with a tab-separated entity string and numeric ID. Clean, orderly, main character energy ๐Ÿ‘‘

For LLVM IR, entities include instruction opcodes (e.g. "Add", "Ret"), types (e.g. "INT", "PTR"), and operand kinds.

For Machine IR, entities include machine opcodes (e.g. "COPY", "ADD"), common operands (e.g. "Immediate", "FrameIndex"), physical register classes (e.g. "PhyReg_GR32"), and virtual register classes (e.g. "VirtReg_GR32").


๐ŸŒธ Embedding Mode Output ๐ŸŒธ

In embedding mode, the output format depends on the level you specified โ€” choose your fighter ๐ŸŽฎ

  • Function Level: One embedding vector per function
  • Basic Block Level: One embedding vector per basic block, grouped by function
  • Instruction Level: One embedding vector per instruction, grouped by basic block and function

Each embedding is represented as a floating point vector. Floating points, slay ๐Ÿ’…


๐Ÿ’œ EXIT STATUS ๐Ÿ’œ

llvm-ir2vec returns 0 on success, and a non-zero value on failure. That's the whole vibe check, honestly.

Common failure cases include:

  • Invalid or missing input file ๐Ÿ˜ญ
  • Missing or invalid vocabulary file (in embedding mode)
  • Specified function not found in the module
  • Invalid command line options
If you're getting a non-zero exit, check that list before you spiral. It's almost always one of these four, fr fr ๐Ÿซถ

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

Machine Learning - Guided Optimization (MLGO)

For more information about the IR2Vec algorithm and approach, see: IR2Vec: LLVM IR Based Scalable Program Embeddings.

For more information about the MIR2Vec algorithm and approach, see: RL4ReAl: Reinforcement Learning for Register Allocation.

Go read the papers bestie, they ate ๐Ÿ“


โญ AUTHOR โญ

Maintained by the LLVM Team (https://llvm.org/). Shoutout to the maintainers, they're carrying this whole thing on their backs ngl ๐Ÿ™Œ


2003-2026, LLVM Project


And that's the whole page, bestie โ€” you made it to the end, I'm so proud of you ๐Ÿ˜ญ๐Ÿ’– This was LLVM-IR2VEC(1), page 23, dated 2026-10-06, served fresh from the LLVM Project. Now go embed something iconic ๐Ÿงšโœจ

๐Ÿ“ฆ where this page comes from