Merged
[Backport] fortran: Fix PRIVATE module procedure linkage [PR125430] #395
[Backport] fortran: Fix PRIVATE module procedure linkage [PR125430] #395
Merged
haibaooAdd on Aug 17
haibaoo
haibaoo
Aug 17

PR功能描述 / 为什么需要这个合入:
回移 GCC 上游 PR fortran/125430 修复,解决 Fortran 子模块无法调用父模块中 PRIVATE module procedure 的链接问题。

Fortran 子模块可以通过 host association 访问父模块中的 PRIVATE procedure。由于子模块通常作为独立翻译单元编译,这些 procedure 必须具有全局
链接属性,否则子模块生成的目标文件在最终链接时无法找到对应符号。

本补丁对 module CONTAINS 中定义的 procedure 设置 TREE_PUBLIC,使其能够被其他翻译单元中的子模块引用。同时,对 PRIVATE procedure 设置
VISIBILITY_HIDDEN,保证符号能够在同一链接单元内解析,但不会作为公开符号导出到外部动态库。

该处理方式与 GCC 中 PRIVATE module variable 的现有处理方式保持一致。

希望检视人员了解的内容:
本 PR 回移以下 GCC 上游提交:

https://gcc.gnu.org/cgit/gcc/commit/?id=1cf8c5913bfe8016b8b2436cc17a7577be1b295f

上游提交作者为 Jerry DeLisle,本次回移保留了上游作者信息,并根据 EulerGCC GCC 12 的 trans-decl.cc 上下文和现有测试框架进行了适配。

本补丁只修改 Fortran 前端及其测试,不涉及 AArch64 或 X86_64 后端实现。

新增运行时测试 submodule_private_host.f90:父模块定义 PRIVATE procedure,子模块在独立源文件中通过 host association 调用该 procedure,最
终程序验证调用结果。该测试在以下优化级别均编译并运行通过:

  • -O0
  • -O1
  • -O2
  • -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions
  • -O3 -g
  • -Os

由于 PRIVATE module procedure 现在具有 TREE_PUBLIC + VISIBILITY_HIDDEN 属性,不再能够像本地静态函数一样被直接删除,因此同步调整了以下既
有测试的预期结果:

  • module_private_2.f90
  • public_private_module_2.f90
  • public_private_module_7.f90
  • public_private_module_8.f90
  • warn_unused_function_2.f90

Checklist:
PR格式

功能验证

  • 修改涉及后端架构

  • 补丁前:

                === GCC testsuite aggregate Summary ===
    
    • of expected passes 474373
    • of unexpected failures 34
    • of unexpected successes 18
    • of expected failures 3769
    • of unresolved testcases 15
      of unsupported tests 16355

    补丁后:

                === GCC testsuite aggregate Summary ===
    
    • of expected passes 474379
    • of unexpected failures 34
    • of unexpected successes 18
    • of expected failures 3773
    • of unresolved testcases 15
    • of unsupported tests 16356

    测试结果变化来自新增的子模块运行测试和既有 Fortran 测试预期调整:

    • 新增的子模块测试在六组优化选项下编译和运行通过。
    • 4 个原有 scan-assembler-not 检查根据新的符号链接行为调整为预期失败(XFAIL)。
    • 新增的辅助源文件按 DejaGNU 约定单独运行时记为 UNSUPPORTED。
    • g++、gcc 测试汇总与基线一致。
    • 未出现新的 unexpected FAIL。
likedislike
Pull Request has been successfully merged by openeuler-ci-bot.
(Thank you for haibaoo's contribution.)
haibaoohaibaoo
Aug 17 Created pull request, commit 31928daa
atomgit-bot
atomgit-bot
Aug 17 Comments:

变更摘要

本 PR 回移 GCC 上游修复(PR fortran/125430),核心目标是解决 Fortran 子模块无法通过 host association 调用父模块中 PRIVATE module procedure 的链接问题。改动主要集中在 gcc/fortran/trans-decl.cc 的 build_function_decl:对于定义在 module CONTAINS 段中的 procedure,为其设置全局链接属性 TREE_PUBLIC;若该 procedure 为 PRIVATE,再设置 VISIBILITY_HIDDEN 及 VISIBILITY_SPECIFIED,使符号可在同一链接单元内被子模块解析,同时不会作为公开符号导出。这一处理与 GCC 对 PRIVATE module variable 的既有逻辑保持一致。测试方面新增了子模块运行时用例,并同步调整了多个既有测试的汇编/树转储预期。

主要改动

  • build_function_decl 新增 module CONTAINS procedure 识别:通过 sym->module && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE 计算 in_module_contains,使 module 内定义的过程即使为 PRIVATE 也能进入设置 TREE_PUBLIC 的分支。
  • 为 PRIVATE module procedure 设置隐藏可见性:在 TREE_PUBLIC (fndecl) = 1 后,对 in_module_contains 且 ACCESS_PRIVATE 且非 public_used 的函数设置 DECL_VISIBILITY (fndecl) = VISIBILITY_HIDDEN 和 DECL_VISIBILITY_SPECIFIED (fndecl) = true,保证符号可被子模块引用但不会导出到外部动态库。
  • 新增子模块宿主关联运行时测试:submodule_private_host.f90 定义 private 的 priv 函数及 public 的 pub 模块过程接口,submodule_private_host_aux.f90 在子模块中通过 host association 调用 priv(6) 并校验结果,覆盖独立翻译单元下的链接场景。
  • 调整既有 scan-assembler-not 预期为 XFAIL:在 public_private_module_2.f90、public_private_module_7.f90、public_private_module_8.f90 中,将原本预期 PRIVATE procedure 符号不出现的检查改为 { xfail *-*-* },以反映符号现在具有 TREE_PUBLIC + VISIBILITY_HIDDEN 属性而不再被移除。
  • 同步更新树转储与告警相关测试:module_private_2.f90 移除了 scan-tree-dump-times "priv" 0 检查(该函数不再被优化删除),warn_unused_function_2.f90 取消了对 s1 的 defined but not used 告警预期。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 17 Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
openeuler-ci-botopeneuler-ci-botCollaborators
Aug 17 Set alexanderbill,li-yancheng,cf-zhao,eastb233,wangqiang95,kuenking111_admin,SegFault,wd-gitcode,chenzheng1030 as approver
openeuler-ci-botopeneuler-ci-botCollaborators
Aug 17 Added label: sig/Compiler
openeuler-ci-bot
openeuler-ci-botCollaborators
Aug 17 Comments:

Welcome To openEuler Community

Hey @qq_56722097 , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands.

Contact Guide

If you have any questions, please contact the SIG: Compiler ,
and any of the maintainers: @SegFault, @alexanderbill, @cf-zhao, @chenzheng1030, @eastb233, @kuenking111_admin, @li-yancheng, @wangqiang95, @wd-gitcode ,
and any of the committers: @huang-xiaoquan, @huzife, @lin-4682, @xiongzhou4 .

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Aug 17 Added label: openeuler-cla/yes
openeuler-ci-bot
openeuler-ci-botCollaborators
Aug 17 Comments:

CLA Signature Pass

qq_56722097, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Aug 18 Added label: ci_processing
openeuler-ci-bot
openeuler-ci-botCollaborators
Aug 18 Comments:

门禁正在运行, 您可以通过以下链接查看实时门禁检查结果.
若您对门禁结果含义不清晰或者遇到问题不知如何解决,可参考门禁指导手册
门禁入口及编码规范检查: multiarch/openeuler/trigger/gcc/104/console

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Aug 18 Removed label: ci_processing
openeuler-ci-bot
openeuler-ci-botCollaborators
Aug 18 Comments:
Check Name Build Result 详情 Build Details
check_code ❌FAILED • CodeCheck检查发现代码缺陷,请查看报告: https://www.openlibing.com/apps/entryCheckDashCode/MR_8d4fbc75df8247bca700a9827d9e89a8/788f9c308a96f2820265fca690f0d843?projectId=300024&codeHostingPlatformFlag=gitcode #104
check_package_license ✅SUCCESS
x86_64 check_build ✅SUCCESS #112
aarch64 check_build ✅SUCCESS #104
likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Aug 18 Added label: ci_failed
openeuler-ci-bot
openeuler-ci-botCollaborators
Aug 18 Comments:
likedislike
zhuzhibiao1
Aug 20 Comments:

一般 Minor

AI 代码审阅

优点

  • 补丁目标明确:将 PRIVATE module procedure 的链接属性从“局部可被优化掉”调整为 TREE_PUBLIC + VISIBILITY_HIDDEN,与现有 gfc_finish_var_decl 对 PRIVATE module variable 的处理一致。
  • 新增运行时测试 submodule_private_host.f90 / submodule_private_host_aux.f90 能覆盖独立翻译单元中子模块通过 host association 调用父模块 PRIVATE procedure 的核心场景。
  • 既有测试预期调整合理:将多处 scan-assembler-not 改为 XFAIL,符合该补丁改变符号发射行为后的实际结果。
  • 变更范围集中在 Fortran 前端和测试,没有涉及后端或构建配置,风险相对可控。
  • PR 描述中给出了上游提交链接和多组优化级别测试结果,验证比较充分。

变更文件覆盖检查

文件 审查结果
gcc/fortran/trans-decl.cc 未发现明确必须修改的问题
gcc/testsuite/gfortran.dg/module_private_2.f90 未发现明确必须修改的问题
gcc/testsuite/gfortran.dg/public_private_module_2.f90 未发现明确必须修改的问题
gcc/testsuite/gfortran.dg/public_private_module_7.f90 未发现明确必须修改的问题
gcc/testsuite/gfortran.dg/public_private_module_8.f90 未发现明确必须修改的问题
gcc/testsuite/gfortran.dg/submodule_private_host.f90 未发现明确必须修改的问题
gcc/testsuite/gfortran.dg/submodule_private_host_aux.f90 未发现明确必须修改的问题
gcc/testsuite/gfortran.dg/warn_unused_function_2.f90 未发现明确必须修改的问题

本次变更不涉及 pom.xml、build.gradle、settings.gradle、YAML、properties、SQL、Dockerfile 等构建或部署配置文件。改动不会影响 Maven/Gradle 依赖解析、打包方式或运行时配置兼容性。

需要修改的问题

未发现明确必须修改的问题。

可优化建议

  • Suggestion: 为 VISIBILITY_HIDDEN 行为增加更直接的回归测试。

    • Reason: 新运行时测试只验证了“子模块最终能链接并运行”,但没有验证 PRIVATE module procedure 确实具有 hidden visibility,而不是仅具有普通全局可见性。补丁的核心安全属性之一是“不会导出到外部 DSO”,这一部分目前依赖 XFAIL 和人工推理。
    • Example:
      ! 在 submodule_private_host.f90 的 dg-final 中增加类似检查,
      ! 但需要根据目标平台和现有可见性测试的写法适配:
      ! { dg-final { scan-assembler "__m_MOD_priv" } }
      ! { dg-final { scan-assembler "\\.hidden[ \t]+__m_MOD_priv" { target *-*-linux* } } }
      
      具体目标选择器和汇编片段应参考 GCC 测试套件中已有的 dg-require-visibility 相关用例,避免在非 ELF 平台产生噪声。
  • Suggestion: 在 warn_unused_function_2.f90 中为删除 s1 的 dg-warning 补充简短注释。

    • Reason: 该测试原来验证未使用函数警告,现在移除一条预期警告,原因不太直观。后续维护者可能误以为这是测试松弛而回退。
    • Example:
      ! s1 is a PRIVATE module procedure; with this fix it is TREE_PUBLIC
      ! with hidden visibility, so it is no longer diagnosed as unused.
      subroutine s1
      
  • Suggestion: module_private_2.f90 删除 priv 的 scan-tree-dump-times "priv" 0 后,可考虑增加一个正向检查。

    • Reason: 只删除负向检查会让测试对符号发射行为失去覆盖。增加正向断言可以固定新的预期行为,避免后续误回归。
    • Example:
      ! 根据实际优化后的 dump 内容选择稳定断言,例如:
      ! { dg-final { scan-tree-dump-times "priv" 1 "optimized" } }
      
      该断言需要结合当前优化级别的实际 dump 验证,避免因内联或名称输出变化导致脆弱。

审阅结论

Severity: 通过

Conclusion: 建议通过

结论说明:

该 PR 是 GCC 上游修复的回移,核心逻辑与既有变量处理一致,改动范围适中,新增测试能够覆盖子模块 host association 问题,既有测试调整也符合新的符号发射行为。未发现编译错误、运行时崩溃、安全绕过或明显功能缺陷,仅提出若干可选测试增强建议。


Review metadata:

  • PR: #395
  • Latest commit: 3cb0bea8d2ae1fdf72e2ce9a16bb995aaaa879bb
  • Changed lines: 83
  • Review mode: full
  • Reviewed files: 8/8
likedislike
wd-gitcodeCollaborators
Sep 2 Comments:

/lgtm
/approve

likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 2 Added label: approvedlgtm
openeuler-ci-bot
openeuler-ci-botCollaborators
Sep 2 Comments:

Review Code Feedback

  • The label lgtm, approved was added to this pull request. It means that wd-gitcode reviewed the code changes. 👋
Tips
  • If this pull request is not merged while all conditions are met, comment /check-pr to try again. 😄
likedislike
openeuler-ci-botopeneuler-ci-botCollaborators
Sep 2 Merged pull request, merge node SHA: 59caab16852081fee8334ab1e3a4f4d217e0a10e