Skip to content

fix: Ensure submodules compile before programs for LFortran direct mode (#1252)#1253

Open
ijatinydv wants to merge 1 commit intofortran-lang:mainfrom
ijatinydv:fix/submodule-ordering
Open

fix: Ensure submodules compile before programs for LFortran direct mode (#1252)#1253
ijatinydv wants to merge 1 commit intofortran-lang:mainfrom
ijatinydv:fix/submodule-ordering

Conversation

@ijatinydv
Copy link

Resolves #1252

Currently, FPM's topological sort allows programs and submodules sharing a parent module to be scheduled in the same OpenMP region. For compilers using direct compilation (like LFortran), this creates a race condition where the main program attempts to compile before all .smod files are generated.

The Fix
Added a post-resolution pass in resolve_module_dependencies (fpm_targets.f90) that establishes a blanket dependency edge from all FPM_UNIT_PROGRAM and FPM_UNIT_TEST targets to all FPM_UNIT_SUBMODULE targets.

Why a blanket rule instead of parent-matching?
Parent-matching fails on transitive dependencies (e.g., main -> mod_A -> mod_B -> submod_B misses the indirect submodule because modules_used only contains mod_A). Since programs and tests are terminal nodes in the build graph, adding blanket edges has zero performance cost and guarantees correct compilation order.

Tests Added
Added 5 new tests to test_module_dependencies.f90 to verify:

  1. Basic program/submodule ordering.
  2. Transitive dependency resolution.
  3. App-scope submodules.
  4. Multi-program scenarios.
  5. Tree-shaking: Verified that the new blanket rule does not prevent unused submodules from being pruned.

cc @certik

@ijatinydv ijatinydv force-pushed the fix/submodule-ordering branch from 778b021 to 27272f3 Compare February 27, 2026 07:17
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.

Compile submodules before the main program

1 participant