Repository navigation
gcc backend (initial pass) via libgccjit - #239
Draft
SamuelMarks wants to merge 2 commits into
Draft
SamuelMarks wants to merge 2 commits into
SamuelMarks wants to merge 2 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. |
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 SubsystemsOverview
This pull request introduces a native, direct code-generation backend in the EDG C++ front end (
cpfe) powered bylibgccjit(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 tolibgccjitIR. This enablescpfeto 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
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 (--dump-initial-tree,--dump-gimple).tests/gcc_be/with 11 targeted test suites and modernizedcheck_coverage.pyto 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/:gcc_gen_be_error.[ch]gcc_gen_be_error_tstatus codes,GCC_GEN_BE_NODISCARDattributes,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,LoadLibraryAon Windows with function wrappers) to avoid hard build-time 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), and dynamic initializers.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. Frontend Integration & CLI Switches
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.-osetsgcc_be_output_file_nameto determine the emitted artifact kind based on extension (.ofor object files,.sfor assembly,.so/.dylib/.dllfor shared libraries).src/cmd_line.cand flags insrc/cmd_line.h:--O0,--O1,--O2,--O3,--Os,--Ofast-g-fPIC,-fPIE--dump-initial-tree,--dump-gimple3. Build, Packaging & CTest Integration
cmake/FindLibGCCJIT.cmake: Probes standard system directories (Linux multiarch, Homebrew, MacPorts, MinGW/MSYS2), createsLibGCCJIT::LibGCCJIT, and inspects header feature availability macros (LIBGCCJIT_HAVE_gcc_jit_context_add_command_line_option,LIBGCCJIT_HAVE_gcc_jit_version, etc.).$ORIGIN/../libon Linux/FreeBSD and@executable_path/../lib;@rpathon macOS. Configures/DELAYLOAD:libgccjit.dllfor MSVC.--coverageand-fprofile-arcs -ftest-coverageconfiguration for allgcc_gen_be_*.ccompilation units when building with GCC or Clang..deb), Red Hat (.rpm), macOS (.dmg,.tar.gz), Windows (.zip,.msi), and FreeBSD (.txz) packagers with runtime library dependencies.File-by-File Breakdown
CMakeLists.txtEDG_ENABLE_GCC_BACKENDoption; enables testing (CTest); addstests/gcc_besubdirectory; defines multi-platform CPack packager metadata.cmake/FindLibGCCJIT.cmakelibgccjit.hand libraries; creates imported targetLibGCCJIT::LibGCCJIT; checks feature availability macros.src/CMakeLists.txtgcc_gen_be_*.csources; linksLibGCCJIT::LibGCCJIT; sets platform RPATHs and delay-load settings; adds coverage flags for GCC backend sources.src/cfe.cgcc_gen_be_main.handgcc_gen_be_context.h; registers backend invocation.src/cmd_line.c-O0..-O3,-Os,-Ofast,-g,-fPIC,-fPIE, dumps,-o); parses and assigns corresponding variables.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; deactivatesBACK_END_IS_C_GEN_BE; assertsDO_IL_LOWERINGrequirement.src/gcc_gen_be_error.[ch]gcc_gen_be_error_t), error strings, and propagation macros.src/gcc_gen_be_context.[ch]src/gcc_gen_be_cache.[ch]src/gcc_gen_be_lib_loader.[ch]src/gcc_gen_be_location.[ch]src/gcc_gen_be_type.[ch]src/gcc_gen_be_decl.[ch]src/gcc_gen_be_expr.[ch]src/gcc_gen_be_stmt.[ch]src/gcc_gen_be_main.[ch]gcc_gen_be()andback_end(), and compilation output.tests/gcc_be/CMakeLists.txtcoverage_gcc_be).tests/gcc_be/check_coverage.pygcc_gen_be_*.cfiles.tests/gcc_be/test_*.c[pp]Verification & Testing
EDG_ENABLE_GCC_BACKEND=ON.ctestand targetrun_gcc_be_tests.check_coverage.pyto evaluate execution and branch metrics across all modular backend compilation units.