Skip to content

[clang] Use internal linkage for c23 constexpr vars. #97846

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

Merged
merged 1 commit into from
Jul 8, 2024

Conversation

chestnykh
Copy link
Contributor

@chestnykh chestnykh commented Jul 5, 2024

See C23 std 6.2.2p3.
Fix #97830

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 5, 2024

@llvm/pr-subscribers-clang

Author: Dmitriy Chestnykh (chestnykh)

Changes

Set static storage class specifier for such decls to have internal linkage in produced IR and then in the object file.
Fix #97830


Full diff: https://github.com/llvm/llvm-project/pull/97846.diff

2 Files Affected:

  • (modified) clang/lib/Parse/ParseDecl.cpp (+8-3)
  • (added) clang/test/CodeGen/constexpr-c23-internal-linkage.c (+17)
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index a07f7ad2233fe..a4e66bb78d6df 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4428,10 +4428,15 @@ void Parser::ParseDeclarationSpecifiers(
 
     // constexpr, consteval, constinit specifiers
     case tok::kw_constexpr:
-      if (getLangOpts().C23)
+      if (getLangOpts().C23) {
         Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName();
-      isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
-                                      PrevSpec, DiagID);
+        isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
+                                        PrevSpec, DiagID);
+
+        isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
+                                           PrevSpec, DiagID, Policy);
+        isStorageClass = true;
+      }
       break;
     case tok::kw_consteval:
       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Consteval, Loc,
diff --git a/clang/test/CodeGen/constexpr-c23-internal-linkage.c b/clang/test/CodeGen/constexpr-c23-internal-linkage.c
new file mode 100644
index 0000000000000..a1df206d520d0
--- /dev/null
+++ b/clang/test/CodeGen/constexpr-c23-internal-linkage.c
@@ -0,0 +1,17 @@
+/* RUN: %clang_cc1 -std=c23 -emit-llvm -o -  %s | FileCheck %s
+ */
+
+constexpr int var_int = 1;
+constexpr char var_char = 'a';
+constexpr float var_float = 2.5;
+
+const int *p_i = &var_int;
+const char *p_c = &var_char;
+const float *p_f = &var_float;
+
+/*
+CHECK: @var_int = internal constant i32 1{{.*}}
+CHECK: @var_char = internal constant i8 97{{.*}}
+CHECK: @var_float = internal constant float 2.5{{.*}}
+*/
+

@chestnykh chestnykh force-pushed the internal-linkage-constexpr-c23 branch 2 times, most recently from 5285c93 to 20adb1c Compare July 5, 2024 18:03
@tschuett tschuett requested a review from AaronBallman July 5, 2024 18:11
@chestnykh chestnykh force-pushed the internal-linkage-constexpr-c23 branch from 20adb1c to ecf8360 Compare July 5, 2024 18:39
@chestnykh chestnykh marked this pull request as draft July 5, 2024 19:30
isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
PrevSpec, DiagID);

isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that you want to assign isInvalid twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to rework this PR a little bit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@tschuett tschuett requested review from cor3ntin and Endilll July 6, 2024 06:20
@chestnykh chestnykh force-pushed the internal-linkage-constexpr-c23 branch from ecf8360 to 70a5e96 Compare July 6, 2024 06:51
@chestnykh chestnykh marked this pull request as ready for review July 6, 2024 06:52
@chestnykh chestnykh force-pushed the internal-linkage-constexpr-c23 branch from 70a5e96 to b8252ed Compare July 6, 2024 06:54
Copy link

github-actions bot commented Jul 6, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@chestnykh chestnykh force-pushed the internal-linkage-constexpr-c23 branch from b8252ed to 7a3f285 Compare July 6, 2024 06:59
@Endilll Endilll added the c label Jul 6, 2024
Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, though please let others to take a look too.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix as well.

Once that's added, will you need someone to land the changes on your behalf?

@chestnykh
Copy link
Contributor Author

LGTM but please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix as well.

Once that's added, will you need someone to land the changes on your behalf?

@AaronBallman , yes, i will need this. I'm going to add release notes right now.

@Fznamznon
Copy link
Contributor

Fznamznon commented Jul 8, 2024

LGTM but please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix as well.

Note that only clang 19 has constexpr support in C23. https://godbolt.org/z/4bThdsqK5
Release note saying that support has been implemented is already there.

@chestnykh
Copy link
Contributor Author

Release notes added

@chestnykh chestnykh force-pushed the internal-linkage-constexpr-c23 branch 2 times, most recently from 94eb71d to 317b3e2 Compare July 8, 2024 14:07
@AaronBallman
Copy link
Collaborator

LGTM but please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix as well.

Note that only clang 19 has constexpr support in C23. https://godbolt.org/z/4bThdsqK5 Release note saying that support has been implemented is already there.

Ooh oops, I thought we had this in Clang 18

@chestnykh
Copy link
Contributor Author

LGTM but please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix as well.

Note that only clang 19 has constexpr support in C23. https://godbolt.org/z/4bThdsqK5 Release note saying that support has been implemented is already there.

Ooh oops, I thought we had this in Clang 18

Should I change anything?

@AaronBallman
Copy link
Collaborator

LGTM but please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix as well.

Note that only clang 19 has constexpr support in C23. https://godbolt.org/z/4bThdsqK5 Release note saying that support has been implemented is already there.

Ooh oops, I thought we had this in Clang 18

Should I change anything?

Sorry for the back-and-forth -- no new release note needed, so you can remove what you added.

@chestnykh chestnykh force-pushed the internal-linkage-constexpr-c23 branch from 317b3e2 to 0619f78 Compare July 8, 2024 14:47
@chestnykh
Copy link
Contributor Author

LGTM but please add a release note to clang/docs/ReleaseNotes.rst so users know about the fix as well.

Note that only clang 19 has constexpr support in C23. https://godbolt.org/z/4bThdsqK5 Release note saying that support has been implemented is already there.

Ooh oops, I thought we had this in Clang 18

Should I change anything?

Sorry for the back-and-forth -- no new release note needed, so you can remove what you added.

Removed :)

@AaronBallman AaronBallman merged commit 09a275e into llvm:main Jul 8, 2024
5 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

C23 constexpr should not have external linkage
6 participants