Skip to content

gcc backend (initial pass) via libgccjit - #239

Draft
SamuelMarks wants to merge 2 commits into
edgcpp:mainfrom
SamuelMarks:gcc_backend
Draft

SamuelMarks wants to merge 2 commits into
edgcpp:mainfrom
SamuelMarks:gcc_backend

Conversation

@SamuelMarks

@SamuelMarks SamuelMarks commented Oct 7, 2026 •

Copy link
Copy Markdown

NOTE: this is not intended to be merged; just trying to start a conversation…


PR: Native GCC (libgccjit) Backend (gcc_gen_be) with Modular Subsystems

Overview

This pull request introduces a native, direct code-generation backend in the EDG C++ front end (cpfe) powered by libgccjit (gcc_gen_be).

Historically, EDG lowers its Intermediate Language (IL) primarily to C or C++ source code (c_gen_be, cp_gen_be) or passes IL to proprietary code generators. This PR establishes a direct lowering pipeline from EDG's lowered Intermediate Language directly to libgccjit IR. This enables cpfe to directly emit machine code artifacts (object files .o, assembly .s, and shared libraries .so/.dylib/.dll) without requiring a secondary textual compiler invocation.

Across the commits comprising this branch, the backend is architected into 10 decoupled, testable subsystems with comprehensive CLI options, full CMake/CTest/CPack integration, dynamic loader support across POSIX and Windows, and robust coverage verification tooling.


Commit Summary

  1. cc0d088356 GCC [libgccjit] backend (initial pass):
    • Established the baseline gcc_gen_be architecture, hooking into src/cfe.c, src/cmd_line.[ch], and src/host_envir.h.
    • Introduced CMake detection via cmake/FindLibGCCJIT.cmake and cross-platform CPack packaging.
    • Implemented initial end-to-end IL lowering across scalar/aggregate types, declarations, control flow (conditionals, loops, switch tables, inline assembly), and expression trees.
  2. 533a5b95ac Modularize GCC backend into dedicated subsystems and expand CLI options:
    • Refactored the monolithic implementation into 10 dedicated compilation units (gcc_gen_be_*.c and matching headers).
    • Introduced centralized error handling (gcc_gen_be_error.h) with typed error codes (gcc_gen_be_error_t), error stringification, and GCC_GEN_BE_NODISCARD / GCC_GEN_BE_CHECK error propagation macros.
    • Expanded driver CLI options in src/cmd_line.[ch] for backend optimization levels (-O0 through -O3, -Os, -Ofast), debug generation (-g), position-independent code (-fPIC, -fPIE), and JIT intermediate representation dumping (--dump-initial-tree, --dump-gimple).
    • Expanded the test suite under tests/gcc_be/ with 11 targeted test suites and modernized check_coverage.py to evaluate line and branch coverage across all backend compilation units.

Architectural Highlights

1. Modular Subsystem Architecture

The backend is decomposed into specialized subsystems located under src/:

Subsystem Header / Source Description
Error Handling gcc_gen_be_error.[ch] Defines gcc_gen_be_error_t status codes, GCC_GEN_BE_NODISCARD attributes, GCC_GEN_BE_CHECK macro, and gcc_gen_be_error_string().
Context & State gcc_gen_be_context.[ch] Manages JIT context lifecycle (gcc_gen_be_init, gcc_gen_be_cleanup), active blocks, break/continue/switch exit stacks, cleanup stacks, and driver options.
Memoization & Cache gcc_gen_be_cache.[ch] Fast hash-map memoization structures mapping EDG AST pointers (a_type_ptr, a_variable_ptr, a_routine_ptr, a_label_ptr, a_field_ptr) to JIT equivalents.
Dynamic Library Loader gcc_gen_be_lib_loader.[ch] Cross-platform runtime loader handling dynamic symbol resolution (dlopen on POSIX, LoadLibraryA on Windows with function wrappers) to avoid hard build-time link dependencies where appropriate.
Location Tracking gcc_gen_be_location.[ch] Translates EDG sequence numbers (conv_seq_to_file_and_line) from expressions and statements into gcc_jit_location* records for debug info and diagnostics.
Type Lowering gcc_gen_be_type.[ch] Translates scalar types, pointers, arrays, aggregate structs/classes/unions (with cycle detection via opaque forward declarations), bitfields, and routine signatures.
Declaration Lowering gcc_gen_be_decl.[ch] Lowers global variables, local variables, parameters, linkage classification (INTERNAL, EXPORTED, IMPORTED), and dynamic initializers.
Expression Lowering gcc_gen_be_expr.[ch] Lowers expressions to gcc_jit_rvalue* and gcc_jit_lvalue*: constants, arithmetic/bitwise/logical operations, comparisons, casts, assignments, calls, and builtins.
Statement & CFG Lowering gcc_gen_be_stmt.[ch] Builds basic blocks and control-flow graphs for blocks, expressions, conditionals (if, constexpr if, if consteval), multi-way switch tables, loops (while, do-while, for), gotos, and extended inline asm.
Main Driver gcc_gen_be_main.[ch] Orchestrates the top-level backend entry point (gcc_gen_be(), back_end()), visits the primary scope, compiles to target files, and handles backend error reporting.

2. Frontend Integration & CLI Switches

  • Minimal Core Intrusion: EDG core files (src/cfe.c, src/cmd_line.c, src/cmd_line.h, src/host_envir.h) guard backend hooks behind #if BACK_END_IS_GCC_GEN_BE.
  • Output Target Redirection: Command-line flag -o sets gcc_be_output_file_name to determine the emitted artifact kind based on extension (.o for object files, .s for assembly, .so/.dylib/.dll for shared libraries).
  • Optimization & Debug Flags: Added CLI option descriptors in src/cmd_line.c and flags in src/cmd_line.h:
    • Optimization levels: --O0, --O1, --O2, --O3, --Os, --Ofast
    • Debugging: -g
    • Code generation flags: -fPIC, -fPIE
    • Tree/GIMPLE dumps: --dump-initial-tree, --dump-gimple

3. Build, Packaging & CTest Integration

  • cmake/FindLibGCCJIT.cmake: Probes standard system directories (Linux multiarch, Homebrew, MacPorts, MinGW/MSYS2), creates LibGCCJIT::LibGCCJIT, and inspects header feature availability macros (LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option, LIBGCCJIT_HAVE_gcc_jit_version, etc.).
  • Cross-Platform RPATH & Linking: Automatically sets $ORIGIN/../lib on Linux/FreeBSD and @executable_path/../lib;@rpath on macOS. Configures /DELAYLOAD:libgccjit.dll for MSVC.
  • Coverage Instrumentation: Automatic --coverage and -fprofile-arcs -ftest-coverage configuration for all gcc_gen_be_*.c compilation units when building with GCC or Clang.
  • CPack Packaging: Preconfigured Debian (.deb), Red Hat (.rpm), macOS (.dmg, .tar.gz), Windows (.zip, .msi), and FreeBSD (.txz) packagers with runtime library dependencies.

File-by-File Breakdown

File Change Summary
CMakeLists.txt Adds CMake module path; declares EDG_ENABLE_GCC_BACKEND option; enables testing (CTest); adds tests/gcc_be subdirectory; defines multi-platform CPack packager metadata.
cmake/FindLibGCCJIT.cmake Finds libgccjit.h and libraries; creates imported target LibGCCJIT::LibGCCJIT; checks feature availability macros.
src/CMakeLists.txt Conditionally compiles all gcc_gen_be_*.c sources; links LibGCCJIT::LibGCCJIT; sets platform RPATHs and delay-load settings; adds coverage flags for GCC backend sources.
src/cfe.c Adds conditional includes for gcc_gen_be_main.h and gcc_gen_be_context.h; registers backend invocation.
src/cmd_line.c Registers backend options (-O0..-O3, -Os, -Ofast, -g, -fPIC, -fPIE, dumps, -o); parses and assigns corresponding variables.
src/cmd_line.h Declares option kind enums and thread-local configuration variables (gcc_be_output_file_name, gcc_be_opt_level, gcc_be_debug_info, gcc_be_fPIC, etc.).
src/host_envir.h Configures BACK_END_IS_GCC_GEN_BE; deactivates BACK_END_IS_C_GEN_BE; asserts DO_IL_LOWERING requirement.
src/gcc_gen_be_error.[ch] New module: centralized status codes (gcc_gen_be_error_t), error strings, and propagation macros.
src/gcc_gen_be_context.[ch] New module: global context state, option application, control stacks, and lifecycle management.
src/gcc_gen_be_cache.[ch] New module: hash-map caches for AST-to-JIT memoization across types, vars, functions, labels, and fields.
src/gcc_gen_be_lib_loader.[ch] New module: runtime dynamic loader and function wrappers for Windows and POSIX.
src/gcc_gen_be_location.[ch] New module: source position and AST node location translation to JIT locations.
src/gcc_gen_be_type.[ch] New module: lowering scalar types, pointers, arrays, structs/unions/classes, and routine prototypes.
src/gcc_gen_be_decl.[ch] New module: lowering variable declarations, parameters, routines, and initializers.
src/gcc_gen_be_expr.[ch] New module: lowering lvalues and rvalues (operators, casts, calls, intrinsics).
src/gcc_gen_be_stmt.[ch] New module: lowering basic blocks, loops, switch statements, jumps, and inline asm.
src/gcc_gen_be_main.[ch] New module: driver entry points gcc_gen_be() and back_end(), and compilation output.
tests/gcc_be/CMakeLists.txt Defines CTest test cases for C and C++ test files and registers coverage targets (coverage_gcc_be).
tests/gcc_be/check_coverage.py Automated gcov test runner calculating line and branch coverage across all gcc_gen_be_*.c files.
tests/gcc_be/test_*.c[pp] Test source fixtures for scalars, types, expressions, short-circuiting, control flow, functions, aggregates, C++ classes, virtual methods, exception handling, inline asm, and edge cases.

Verification & Testing

  • Configuration & Build: Verified CMake configuration and compilation under EDG_ENABLE_GCC_BACKEND=ON.
  • Target Platforms: Validated build configuration paths on Linux, macOS (Apple Silicon & x86_64), FreeBSD, and Windows (MSVC delay-load & MinGW).
  • Test Suite: Verified test discovery and fixture setups via ctest and target run_gcc_be_tests.
  • Coverage Runner: Modernized check_coverage.py to evaluate execution and branch metrics across all modular backend compilation units.

Decompose monolithic gcc_gen_be.c and gcc_gen_be.h into modular,
decoupled backend subsystems to improve maintainability, testability,
and separation of concerns:
- gcc_gen_be_cache: hash map memoization for types, variables, functions,
  and fields.
- gcc_gen_be_context: context lifecycle, option configuration, and active
  basic block tracking.
- gcc_gen_be_decl: declaration lowering for globals, local variables, and
  parameters.
- gcc_gen_be_error: centralized backend error handling and diagnostics.
- gcc_gen_be_expr: expression lowering for operators, calls, and accessors.
- gcc_gen_be_lib_loader: cross-platform dynamic loader for libgccjit.
- gcc_gen_be_location: mapping frontend source locations to JIT locations.
- gcc_gen_be_main: driver entry points and compilation invocation.
- gcc_gen_be_stmt: statement and CFG control-flow lowering.
- gcc_gen_be_type: type system lowering across scalar, aggregate, pointer,
  and function types.

Additionally:
- Support GCC backend CLI optimization and debug flags in cmd_line.[ch]
  (-O0..-O3, -Os, -Ofast, -g, -fPIC, -fPIE, dump flags).
- Update src/CMakeLists.txt for the new modular sources and coverage flags.
- Expand tests/gcc_be/ suite and modernize coverage verification script
  to inspect all gcc_gen_be_*.c compilation units.
@wchilders-nvidia

Copy link
Copy Markdown
Collaborator

Thanks, I've opened #245 for a central discussion of the topic of some kind of "native IR" back end for the EDG compiler project.

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.

2 participants