Skip to content

Add LLVM IR codegen for StructDefStmt (struct type definitions) #143

@omsherikar

Description

@omsherikar

Description

Implement the translation of astx.StructDefStmt to LLVM IR identified struct types. This is the foundation for struct support in IRx.

Background

  • astx already provides StructDefStmt for defining structs
  • IRx needs to translate this to LLVM's IdentifiedStructType
  • Inspired by LLVM's CodeGenTypes.cpp

Tasks

  • Add struct_types: dict[str, ir.IdentifiedStructType] in LLVMLiteIRVisitor to track struct name → LLVM type mapping
  • Add struct_defs: dict[str, astx.StructDefStmt] to store struct definitions for field lookup
  • Implement visit(node: astx.StructDefStmt) visitor method:
    • Create LLVM IdentifiedStructType using module.context.get_identified_type(name)
    • Map each field's astx type to LLVM type using get_data_type()
    • Set struct body with struct_type.set_body(*field_types)

Example Input (Python)

struct_def = astx.StructDefStmt(
    name="Point",
    attributes=[
        astx.VariableDeclaration(name="x", type_=astx.Int32()),
        astx.VariableDeclaration(name="y", type_=astx.Int32()),
    ],
)

Expected LLVM IR Output

%"Point" = type {i32, i32}

Files to Modify

  • src/irx/builders/llvmliteir.py

Acceptance Criteria

  • StructDefStmt translates to LLVM IdentifiedStructType
  • Struct types are registered and can be looked up by name
  • Unit test test_struct_definition passes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions