Skip to content

LLVM IR backend (initial pass) - #221

Draft
SamuelMarks wants to merge 5 commits into
edgcpp:mainfrom
SamuelMarks:llvm_backend
Draft

SamuelMarks wants to merge 5 commits into
edgcpp:mainfrom
SamuelMarks:llvm_backend

Conversation

@SamuelMarks

Copy link
Copy Markdown

Add LLVM IR code generation back end

Introduce an LLVM IR-generating back end (BACK_END_IS_LLVM_GEN_BE) to
translate EDG intermediate language (IL) directly to LLVM IR modules.

Key changes:

  • Core back end: implement llvm_gen_be.cpp and llvm_gen_be.h for
    generating LLVM IR, covering types, globals, functions, expressions,
    and control flow.
  • Front-end integration: extend IL lowering, allocation, layout, pragmas,
    and target configuration macros in src/ to support the LLVM backend
    consistently with C generation.
  • CLI driver: introduce the --gen_llvm_file_name option to control
    the emitted LLVM IR file name.
  • Build system: add ENABLE_LLVM_BACKEND option in CMake, configure
    LLVM dependency resolution, link required LLVM libraries to cpfe
    targets, and guard against lld issues on macOS.
  • CI and testing: add a dedicated GitHub Actions coverage workflow and
    comprehensive test suite in tests/test_llvm_* validating driver flags,
    type translation, data layout, alignment, expressions, and execution.

Also fixed a bug with the newer macOS linker not behaving nicely with TAPI files (arch unfamiliarity: arm64e.x1-macos). So just use the default Apple linker (ld).


NOTE: This is not intended to be merged. Just to start a conversation…

… C++ tests

Comprehensive implementation across all planned backend phases:
- Test suite: Replaced Python test runners with C++ test executables (build config, driver flag, fuzz).
- Architecture & Scoping: Implemented scoped local symbol table for alloca tracking, LValue/RValue semantics with implicit loads, and dynamic TargetTriple/DataLayout construction from EDG target macros.
- Type System & Layout: Lowered primitives (bool, complex, float variants), empty struct padding, union lowering with bitcast access, bitfield packing/masking, and arrays/VLAs/pointer-to-member types.
- Global State & Linkage: Added linkage mappings (weak, linkonce, comdat, TLS), recursive constant evaluation for static initializers, and dynamic initializers registered in llvm.global_ctors/dtors.
- Control Flow & Statements: Implemented loops (while, do, for), switches, case/default handling, and jumps (break, continue, goto).
- Expressions & Operations: Implemented GEP memory references (arrays, struct fields), function calls (direct, indirect, vararg, sret), full arithmetic/logic/comparisons, casts, ternary operators, and restrict/noalias qualification.
- ABI & Attributes: Applied function and parameter attributes (NoReturn, inline hints, ByVal, SExt/ZExt), vtable/RTTI support.
…ted gotos, and test suite

Add support for exception handling, GNU inline assembly, computed gotos,
and extended constants in the LLVM IR generator, alongside automated
tests and CI coverage:

- Exception Handling: Added Itanium ABI personality routine setup
  (__gxx_personality_v0), landing pads, throw/rethrow runtime calls
  (__cxa_allocate_exception, __cxa_throw, __cxa_rethrow), catch blocks
  (__cxa_begin_catch, __cxa_end_catch), and invoke instructions for calls.
- Inline Assembly: Implemented stmk_asm lowering to llvm::InlineAsm with
  operand constraint string translation, register clobber handling, and
  argument forwarding.
- Computed Gotos: Implemented stmk_assigned_goto using llvm::IndirectBrInst
  dispatching to address-taken labels, with BlockAddress evaluation.
- Constants: Added fp_to_hex_constant_string evaluation for float/double/long
  double (including inf/nan), UTF-16/UTF-32 string constant arrays, and label
  address constants.
- Testing & CI: Added CTest configurations for EH, inline assembly, constants,
  and computed gotos, test coverage script, and GitHub Actions workflow.
… support, and verification

Enhance LLVM backend code generation and validation with typeinfo integration,
extended inline assembly handling, opaque pointer modernization, and verification:

- Target Triple & Lifecycles: Configured target triple derivation across macOS,
  Windows, and Linux targets; ensured thorough state cleanup in llvm_gen_be_terminate.
- Verifier Integration: Added function verification pass via llvm::verifyFunction
  with source position mapping on diagnostic failure.
- Exception Handling & RTTI: Lowered typeinfo references using EDG mangled names
  (__cxa_throw typeinfo/dtor args) and added catch clause dispatch matching via
  llvm.eh.typeid.for.
- Inline Assembly: Supported multiple outputs via struct returns, indirect memory
  operand constraints (*), and value extraction to lvalue destinations.
- Type System & Opaque Pointers: Streamlined GEP and pointer bitcasts for opaque
  pointer support; added explicit float/complex kind mappings (bfloat16, fp16,
  fp80, fp128, etc.).
- Testing & Coverage: Added multi-output inline asm test, lit test cases for for-loops
  and opaque pointers, orchestrator/verifier test targets, and coverage validation
  scripts.
…t layouts, and refine codegen

Decouple LLVM backend configuration from C-generator assumptions, adopt runtime
target configuration parameters, and resolve memory paging and terminator handling
in the LLVM IR generator:

- Target Configuration & Environment: Replaced static compile-time TARG_* macros
  with dynamic targ_* variables in DataLayout and type generation; decoupled
  BACK_END_IS_LLVM_GEN_BE from C-backend IL lowering and layout macros in front-end
  headers and lowering routines (host_envir.h, target.h, il_alloc.c, lower_c99.c, etc.).
- Back-end Isolation: Guarded back_end() definition in c_gen_be.c and cp_gen_be.c
  when BACK_END_IS_LLVM_GEN_BE is active; upgraded C++ standard to C++17 in build configs.
- Scope & Memory Management: Added get_scope_for_routine_definition with IL memory
  region paging (read_memory_region) to ensure routine definitions are available
  during code generation.
- Control Flow & Terminators: Ensured conditional branch conditions are typed as i1;
  added terminator fallbacks to unreachable/ret void to guarantee block termination
  prior to module and function verification.
- Inline Assembly & Expressions: Refactored assembly constraint string parsing, memory
  operand indirection, and register clobber lookups; corrected AST expression operator
  kinds and exception throw dynamic init handling.
- Test Suite: Extended assembly, constant, and exception handling tests covering
  primitive and pointer catches, mixed asm outputs, float variants, and complex types.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant