Repository navigation
gcc backend (initial pass) via libgccjit - #239
Open
SamuelMarks wants to merge 4 commits into
Open
SamuelMarks wants to merge 4 commits into
SamuelMarks wants to merge 4 commits into
Conversation
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.
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. |
…ackend
Enforce strict error handling invariants, non-discardable return codes,
and noexcept specification across all GCC backend (gcc_gen_be) subsystems:
- Error infrastructure (gcc_gen_be_error.[ch]):
- Attach GCC_GEN_BE_NODISCARD directly to typedef enum gcc_gen_be_error_t
to ensure compiler-enforced return value checks across all backend APIs.
- Introduce GCC_GEN_BE_NOEXCEPT macro and add GCC_GEN_BE_ERROR_COMPILATION_FAILED.
- Refactor gcc_gen_be_error_string() to take an out-parameter and return
gcc_gen_be_error_t.
- Universal error code returns and pointer out-parameters:
- Update context getters/setters (gcc_gen_be_get_state, gcc_gen_be_get_context,
gcc_gen_be_set_context, gcc_gen_be_get_current_block, gcc_gen_be_set_current_block).
- Update helper constructors such as gcc_gen_be_get_global_ctor_block().
- Convert driver entry points gcc_gen_be() and back_end() to return
gcc_gen_be_error_t.
- Strict error propagation:
- Percolate all internal fallible operations (cache lookups/inserts/clears,
type and expression lowering, location queries, memory allocations) via
GCC_GEN_BE_CHECK().
- Eliminate ignored return values, unhandled fallback paths, and silent errors.
- Universal noexcept annotation:
- Decorate all backend function declarations and definitions across
gcc_gen_be_cache, gcc_gen_be_context, gcc_gen_be_decl, gcc_gen_be_error,
gcc_gen_be_expr, gcc_gen_be_lib_loader, gcc_gen_be_location,
gcc_gen_be_main, gcc_gen_be_stmt, and gcc_gen_be_type with GCC_GEN_BE_NOEXCEPT.
SamuelMarks
marked this pull request as ready for review
October 9, 2026 14:50
…n GCC backend
Expand the native libgccjit backend (gcc_gen_be) with dynamic memory management,
subsystem diagnostics, modern libgccjit symbol loading, and a modular unit test suite:
- Context and Control-Flow Stacks (gcc_gen_be_context.[ch]):
- Replace static stack arrays with dynamically resizing stacks for break, continue,
switch exit, nested blocks, and RAII cleanup handlers.
- Introduce gcc_gen_be_push_block() / gcc_gen_be_pop_block() for nested scope tracking.
- Add thread-ownership verification via gcc_gen_be_check_thread_safety().
- Cache Resizing and Metrics (gcc_gen_be_cache.[ch]):
- Convert fixed-size hash buckets in be_cache_map to dynamically resizing tables.
- Track cache lookup metrics (hits, misses) retrievable via cache_get_metrics().
- Add GCC_GEN_BE_CACHE_SWITCH_CASE for jump target deduplication.
- Diagnostic Integration and Extended Errors (gcc_gen_be_error.[ch]):
- Introduce gcc_gen_be_report_diagnostic() bridging backend errors with EDG's
diagnostic counters and f_error.
- Add detailed error codes for EH, unresolved symbols, vtables, RTTI, asm constraints,
and DWARF emissions.
- Declarations and Expressions (gcc_gen_be_decl.[ch], gcc_gen_be_expr.[ch]):
- Separate global vs local variable lowering (gcc_gen_be_lower_global_variable_decl
and gcc_gen_be_lower_local_variable_decl).
- Add gcc_gen_be_lower_constant_rvalue() with aggregate initializer lowering.
- Dynamic Symbol Loader (gcc_gen_be_lib_loader.[ch]):
- Dynamically load optional modern libgccjit symbols (TLS models, vector types,
extended asm goto, function/variable attributes, struct/array constructors).
- Namespaces and Header Structuring:
- Standardize C++ namespaces (BEGIN_EDG_NAMESPACE / END_EDG_NAMESPACE) across
backend headers.
- Modular Unit Test Suite (tests/gcc_be/):
- Add standalone unit test suites for all backend subsystems: test_gcc_be_cache,
test_gcc_be_context, test_gcc_be_decl, test_gcc_be_errors, test_gcc_be_expr,
test_gcc_be_lib_loader, test_gcc_be_stmt, and test_gcc_be_type.
- Update tests/gcc_be/CMakeLists.txt to build and register subsystem tests.
- Tracking and Documentation:
- Add TODO_PLAN.md documenting architecture, test coverage mandates, and roadmap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 & Universal Error PercolationOverview
This pull request introduces a native, direct code-generation backend into the Edison Design Group (EDG) C++ front end (
cpfe) powered bylibgccjit(gcc_gen_be).Historically, EDG lowers its Intermediate Language (IL) into C or C++ source code (
c_gen_be,cp_gen_be) or delegates IL structures to proprietary code generators. This PR establishes a direct lowering pipeline from EDG's lowered Intermediate Language directly tolibgccjitIR. This enablescpfeto directly emit machine code artifacts—including object files (.o), assembly listings (.s), dynamic shared libraries (.so,.dylib,.dll), and executables—without requiring secondary textual compiler invocations.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, robust coverage verification tooling, and strict error code percolation with universal
noexceptguarantees.Commit History & Branch Evolution
The branch consists of three cohesive, sequential commits:
cc0d088356GCC [libgccjit] backend (initial pass):gcc_gen_bearchitecture, hooking intosrc/cfe.c,src/cmd_line.[ch], andsrc/host_envir.h.cmake/FindLibGCCJIT.cmakeand cross-platform CPack packaging.533a5b95acModularize GCC backend into dedicated subsystems and expand CLI options:gcc_gen_be_*.cand matching headers).gcc_gen_be_error.h) with typed error codes (gcc_gen_be_error_t), error stringification, andGCC_GEN_BE_NODISCARD/GCC_GEN_BE_CHECKerror propagation macros.src/cmd_line.[ch]for backend optimization levels (-O0through-O3,-Os,-Ofast), debug generation (-g), position-independent code (-fPIC,-fPIE), and JIT intermediate representation dumping (--fdump-tree-all,--fdump-tree-gimple).tests/gcc_be/with targeted test suites and modernizedcheck_coverage.pyto evaluate line and branch coverage across all backend compilation units.516331dafaEnforce universal error codes, percolation, and noexcept across GCC backend:GCC_GEN_BE_NODISCARDdirectly into thetypedef enum GCC_GEN_BE_NODISCARD { ... } gcc_gen_be_error_t;definition to guarantee compiler-enforced return value verification across all translation units.GCC_GEN_BE_NOEXCEPTmacro (noexceptwhen compiled in C++ mode) and annotated all backend function declarations and definitions.**out_ctx,**out_block,**out_lval,**out_rval,**out_type, etc.), makinggcc_gen_be_error_tthe universal return type across the entire backend.gcc_gen_be_error_string()to populate aconst char **out_strout-parameter and returngcc_gen_be_error_t.GCC_GEN_BE_ERROR_COMPILATION_FAILEDto distinguish JIT compilation errors from internal compiler faults.gcc_gen_be()andback_end()to returngcc_gen_be_error_t.GCC_GEN_BE_CHECK(), eliminating silent fallbacks and ignored return values.throw,try,catch) within the backend implementation.Architectural Highlights
1. Decoupled Subsystem Architecture
The backend is cleanly partitioned into 10 dedicated subsystems under
src/:gcc_gen_be_error.[ch]gcc_gen_be_error_tstatus codes,GCC_GEN_BE_NODISCARDattribute,GCC_GEN_BE_CHECKmacro, andgcc_gen_be_error_string().gcc_gen_be_context.[ch]gcc_gen_be_init,gcc_gen_be_cleanup), active blocks, break/continue/switch exit stacks, cleanup stacks, and driver options.gcc_gen_be_cache.[ch]a_type_ptr,a_variable_ptr,a_routine_ptr,a_label_ptr,a_field_ptr) to JIT equivalents.gcc_gen_be_lib_loader.[ch]dlopenon POSIX,LoadLibraryA/GetProcAddresson Windows with function wrappers) to avoid hard link dependencies where appropriate.gcc_gen_be_location.[ch]conv_seq_to_file_and_line) from expressions and statements intogcc_jit_location*records for debug info and diagnostics.gcc_gen_be_type.[ch]gcc_gen_be_decl.[ch]INTERNAL,EXPORTED,IMPORTED), dynamic static initializers (__edg_global_init).gcc_gen_be_expr.[ch]gcc_jit_rvalue*andgcc_jit_lvalue*: constants, arithmetic/bitwise/logical operations, comparisons, casts, assignments, calls, and builtins.gcc_gen_be_stmt.[ch]if,constexpr if,if consteval), multi-way switch tables, loops (while,do-while,for), gotos, and extended inline asm.gcc_gen_be_main.[ch]gcc_gen_be(),back_end()), visits the primary scope, compiles to target files, and handles backend error reporting.2. Strict Error Handling & Percolation Model
To ensure industrial robustness and satisfy zero-exception runtime mandates:
GCC_GEN_BE_NODISCARDis attached directly to thegcc_gen_be_error_tdefinition:gcc_gen_be_error_ttriggers a compile error if the return value is not captured or verified.All backend functions return
gcc_gen_be_error_t. Computed handles (gcc_jit_context*,gcc_jit_block*,gcc_jit_type*,gcc_jit_lvalue*,gcc_jit_rvalue*,gcc_jit_function*,gcc_jit_location*) are returned exclusively through validated out-parameters.The
GCC_GEN_BE_CHECK(expr)macro immediately returns any non-success error code up the call stack:(void)casts are used to discard returns, and all fallback paths cleanly return descriptive error codes.noexceptAnnotation:All backend functions are decorated with
GCC_GEN_BE_NOEXCEPT(resolving tonoexceptin C++), guaranteeing zero exception overhead and compile-time exception specification conformance.3. Frontend Integration & CLI Switches
src/cfe.c,src/cmd_line.[ch],src/host_envir.h) guard backend hooks behind#if BACK_END_IS_GCC_GEN_BE, keeping modifications to existing code minimal.-ocommand-line option setsgcc_be_output_file_name. The backend inspects the file extension to determine the artifact kind:.o:GCC_JIT_OUTPUT_KIND_OBJECT_FILE.s:GCC_JIT_OUTPUT_KIND_ASSEMBLER.so/.dylib/.dll:GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARYsrc/cmd_line.cand flags insrc/cmd_line.h:--O0,--O1,--O2,--O3,--Os,--Ofast-g-fPIC,-fPIE--fdump-tree-all(GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE),--fdump-tree-gimple(GCC_JIT_BOOL_OPTION_DUMP_GIMPLE)4. Build, Packaging & Platform Support
cmake/FindLibGCCJIT.cmake:/usr/lib/x86_64-linux-gnu,/usr/lib/aarch64-linux-gnu), macOS (Homebrew/opt/homebrew, MacPorts/opt/local), FreeBSD (/usr/local), and Windows (MinGW-w64, MSYS2).LibGCCJIT::LibGCCJIT.libgccjit.hfor feature availability macros (LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option,LIBGCCJIT_HAVE_gcc_jit_context_add_driver_option,LIBGCCJIT_HAVE_gcc_jit_context_new_bitcast,LIBGCCJIT_HAVE_gcc_jit_context_new_rvalue_from_vector,LIBGCCJIT_HAVE_gcc_jit_version).LoadLibraryA("libgccjit.dll")andGetProcAddress, binding 50+ JIT function pointers with type-safe wrapper macros.CMAKE_INSTALL_RPATHto$ORIGIN/../libon Linux and FreeBSD, and@executable_path/../lib;@rpathon macOS./DELAYLOAD:libgccjit.dllfor MSVC builds..debpackage with package dependency onlibgccjit0 (>= 10.0.0) | libgccjit-10-dev | libgccjit-11-dev | libgccjit-12-dev | libgccjit-13-dev | libgccjit-14-dev..rpmpackage withlibgccjitrequirement..txzpackage configured under categorydevel..dmg(DragNDrop) and.tar.gzpackages..zipand.msi(WiX) installer packages.5. Verification, CTest & Coverage Tooling
tests/gcc_be/CMakeLists.txtexecuting the compiler with--gcc_gen_beacross 12 targeted fixtures:test_scalars.c: Fundamental scalar types (void, bool, signed/unsigned int8..128, float, double, long double).test_types.c: Pointers, multi-dimensional arrays, VLAs, function pointers, qualifiers.test_expr_ops.c: Unary, binary, relational, bitwise, shift, assignment, and comma operators.test_short_circuit.c: Short-circuit boolean&&and||control flow lowering.test_control_flow.c: Loops (while,do-while,for),if/else,switch/case/default,goto, labels.test_functions.c: Function declarations, parameters, direct calls, indirect calls, variadics.test_aggregates.c: Structs, unions, bitfields, anonymous aggregates, recursive/self-referential structs.test_cpp_classes.cpp: C++ classes, member methods, constructors, destructors,thispointer adjustments.test_virtual.cpp: Virtual method dispatch, virtual function tables (vptr/vtbl).test_eh.cpp: C++ exception handling (try,catch,throw).test_inline_asm.c: Basic and extended inline assembly with operands, constraints, and clobbers.test_edge_cases.c: Fallback paths, defensive branches, null checks, and error logging branches.-fprofile-arcs -ftest-coverage/--coverage.tests/gcc_be/check_coverage.pyparsinggcovbranch and line statistics across allgcc_gen_be_*.ccompilation units to enforce coverage metrics.Detailed File-by-File Audit
CMakeLists.txtcmake/to module path; definesEDG_ENABLE_GCC_BACKENDoption; enables testing (CTest); includestests/gcc_be; configures multi-platform CPack packaging.cmake/FindLibGCCJIT.cmakelibgccjitheaders and libraries across platforms; definesLibGCCJIT::LibGCCJITtarget; checks feature availability macros.src/CMakeLists.txtgcc_gen_be_*.cunits; definesBACK_END_IS_GCC_GEN_BE=1; linksLibGCCJIT::LibGCCJIT; configures platform RPATHs, MSVC delay load, and coverage flags.src/cfe.cgcc_gen_be_early_init()incfe_main.src/cmd_line.c-O0..-O3,-Os,-Ofast,-g,-fPIC,-fPIE, dump flags, and output file capturing.src/cmd_line.hgcc_be_output_file_name,gcc_be_opt_level,gcc_be_debug_info,gcc_be_fPIC, etc.).src/host_envir.hBACK_END_IS_GCC_GEN_BE 0; clearsBACK_END_IS_C_GEN_BEwhen enabled; validatesDO_IL_LOWERINGrequirement.src/gcc_gen_be_error.hgcc_gen_be_error_twithGCC_GEN_BE_NODISCARD,GCC_GEN_BE_NOEXCEPT,GCC_GEN_BE_CHECK(expr), andgcc_gen_be_error_string().src/gcc_gen_be_error.csrc/gcc_gen_be_context.hgcc_gen_be_context_tstructure, and lifecycle functions.src/gcc_gen_be_context.csrc/gcc_gen_be_cache.hnoexceptand error codes.src/gcc_gen_be_cache.csrc/gcc_gen_be_lib_loader.hsrc/gcc_gen_be_lib_loader.cLoadLibraryA/GetProcAddressdynamic resolution and POSIX loader hooks.src/gcc_gen_be_location.hsrc/gcc_gen_be_location.cgcc_jit_location*.src/gcc_gen_be_type.hgcc_gen_be_lower_type()withnoexceptand pointer out-parameter.src/gcc_gen_be_type.csrc/gcc_gen_be_decl.hsrc/gcc_gen_be_decl.c__edg_global_init.src/gcc_gen_be_expr.hgcc_gen_be_lower_expr_lvalue()andgcc_gen_be_lower_expr_rvalue().src/gcc_gen_be_expr.csrc/gcc_gen_be_stmt.hsrc/gcc_gen_be_stmt.csrc/gcc_gen_be_main.hgcc_gen_be()andback_end()returninggcc_gen_be_error_t.src/gcc_gen_be_main.ctests/gcc_be/CMakeLists.txttests/gcc_be/check_coverage.pytests/gcc_be/test_scalars.ctests/gcc_be/test_types.ctests/gcc_be/test_expr_ops.ctests/gcc_be/test_short_circuit.ctests/gcc_be/test_control_flow.ctests/gcc_be/test_functions.ctests/gcc_be/test_aggregates.ctests/gcc_be/test_cpp_classes.cpptests/gcc_be/test_virtual.cpptests/gcc_be/test_eh.cpptests/gcc_be/test_inline_asm.ctests/gcc_be/test_edge_cases.cTotal Impact: 41 files changed, 3,875 insertions(+), 7 deletions(-).
Verification & Validation
-Werror=unused-resultensuring 100% nodiscard enforcement across all backend APIs.EDG_ENABLE_GCC_BACKENDenabled (zero regression when backend is disabled).@executable_path/../lib;@rpathverified.run_gcc_be_tests).tests/gcc_be/check_coverage.py.