Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++20][Modules] Incorrectly distincted static local vars #80949

Closed
Lancern opened this issue Feb 7, 2024 · 3 comments
Closed

[C++20][Modules] Incorrectly distincted static local vars #80949

Lancern opened this issue Feb 7, 2024 · 3 comments
Assignees
Labels
clang:modules C++20 modules and Clang Header Modules

Comments

@Lancern
Copy link
Member

Lancern commented Feb 7, 2024

Given the following set of translation units:

export module a;

export int __attribute__((always_inline)) foo() {
  static int x;
  return ++x;
}
import a;

int test1() { return foo(); }
import a;

int test2() { return foo(); }
#include <iostream>

int test1();
int test2();
int main() {
  std::cout << test1();
  std::cout << test2();
}

The program outputs 11 instead of the expected result 12. Function test1 and function test2 access distinct static int x objects which are mistakenly inlined into their bodies.

Up to clang 17.0.1, this behavior can happen even if foo is not marked with always_inline (with optimizations enabled). Seems like this problem is partially fixed by PR #71031 , but the PR misses always_inline functions.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Feb 7, 2024
@EugeneZelenko EugeneZelenko added clang:modules C++20 modules and Clang Header Modules and removed clang Clang issues not falling into any other category labels Feb 7, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 7, 2024

@llvm/issue-subscribers-clang-modules

Author: Sirui Mu (Lancern)

Given the following set of translation units:
export module a;

export int __attribute__((always_inline)) foo() {
  static int x;
  return ++x;
}
import a;

int test1() { return foo(); }
import a;

int test2() { return foo(); }
#include &lt;iostream&gt;

int test1();
int test2();
int main() {
  std::cout &lt;&lt; test1();
  std::cout &lt;&lt; test2();
}

The program outputs 11 instead of the expected result 12. Function test1 and function test2 access distinct static int x objects which are mistakenly inlined into their bodies.

Up to clang 17.0.1, this behavior can happen even if foo is not marked with always_inline (with optimizations enabled). Seems like this problem is partially fixed by PR #71031 , but the PR misses always_inline functions.

@ChuanqiXu9 ChuanqiXu9 changed the title [clang][C++20][Modules] Duplicated static local vars when function is exported from module interface unit [C++20][Modules] Incorrectly duplicated static local vars Feb 8, 2024
@ChuanqiXu9 ChuanqiXu9 self-assigned this Feb 8, 2024
@ChuanqiXu9 ChuanqiXu9 changed the title [C++20][Modules] Incorrectly duplicated static local vars [C++20][Modules] Incorrectly distincted static local vars Feb 18, 2024
@ChuanqiXu9
Copy link
Member

Although we may fix the issue by treating the imported static locals as available externally, it looks more consistency to not import non-inline function bodies at all no matter if it is always_inline or not. Since we already decided to not import the non-inline function bodies: https://isocpp.org/files/papers/P3092R0.html

@ChuanqiXu9
Copy link
Member

The previous solution gets some complaints so we probably need some workaround here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules
Projects
None yet
Development

No branches or pull requests

4 participants