[clang] [Clang] [CodeGen] Fix codegen bug in constant initialisation in C23 mode (PR #84981)

via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 13 06:38:56 PDT 2024


https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/84981

>From b60ff0c29f367306beb5ea0965dfaf5b7f361654 Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Tue, 12 Mar 2024 13:53:34 +0100
Subject: [PATCH 1/5] [Clang] [CodeGen] Fix codegen bug in constant
 initialisation in C23 mode

---
 clang/docs/ReleaseNotes.rst     | 3 +++
 clang/lib/AST/Decl.cpp          | 4 +++-
 clang/test/CodeGen/const-init.c | 3 +++
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f61dca9bbc8467..c773991b43f47c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -259,6 +259,9 @@ Bug Fixes in This Version
   operator.
   Fixes (#GH83267).
 
+- Clang no longer fails to codegen static ``const`` variables whose initialiser performs
+  a floating-point division by 0 in C23.
+
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index d681791d3920c3..38317dea6d22fe 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2581,7 +2581,9 @@ APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes,
   // case, we can't keep the result, because it may only be correct under the
   // assumption that the initializer is a constant context.
   if (IsConstantInitialization &&
-      (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23) && !Notes.empty())
+      (Ctx.getLangOpts().CPlusPlus ||
+       (isConstexpr() && Ctx.getLangOpts().C23)) &&
+      !Notes.empty())
     Result = false;
 
   // Ensure the computed APValue is cleaned up later if evaluation succeeded,
diff --git a/clang/test/CodeGen/const-init.c b/clang/test/CodeGen/const-init.c
index 0e4fc4ad48af8d..ad3e9551199ac2 100644
--- a/clang/test/CodeGen/const-init.c
+++ b/clang/test/CodeGen/const-init.c
@@ -216,3 +216,6 @@ int PR4517_x2 = PR4517_arrc[PR4517_idx];
 // CHECK: @PR4517_x = global i32 42, align 4
 // CHECK: @PR4517_idx = constant i32 1, align 4
 // CHECK: @PR4517_x2 = global i32 42, align 4
+
+// CHECK: @GH84784_inf = constant i8 1
+_Bool const GH84784_inf = (1.0/0.0);

>From 9eea6ffa1f4ebe41420bc922181653c75bf7bf19 Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Tue, 12 Mar 2024 21:51:06 +0100
Subject: [PATCH 2/5] [NFC] Update comment

---
 clang/lib/AST/Decl.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 38317dea6d22fe..acd6191b6a1e2d 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -2577,9 +2577,10 @@ APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes,
   bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, Ctx, this, Notes,
                                             IsConstantInitialization);
 
-  // In C++/C23, this isn't a constant initializer if we produced notes. In that
-  // case, we can't keep the result, because it may only be correct under the
-  // assumption that the initializer is a constant context.
+  // In C++, or in C23 if we're initialising a 'constexpr' variable, this isn't
+  // a constant initializer if we produced notes. In that case, we can't keep
+  // the result, because it may only be correct under the assumption that the
+  // initializer is a constant context.
   if (IsConstantInitialization &&
       (Ctx.getLangOpts().CPlusPlus ||
        (isConstexpr() && Ctx.getLangOpts().C23)) &&

>From 2391ea4cb2422345e59a5e4dfe326215d6e10813 Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Tue, 12 Mar 2024 21:52:22 +0100
Subject: [PATCH 3/5] [NFC] Reference issue in release notes

---
 clang/docs/ReleaseNotes.rst | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c773991b43f47c..c70cc72e55af58 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -261,6 +261,7 @@ Bug Fixes in This Version
 
 - Clang no longer fails to codegen static ``const`` variables whose initialiser performs
   a floating-point division by 0 in C23.
+  Fixes (#GH84784).
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

>From d04cb7c35d1484d0c03b16323d20fe8dc37dcbde Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Wed, 13 Mar 2024 02:05:01 +0100
Subject: [PATCH 4/5] [NFC] Remove release note

---
 clang/docs/ReleaseNotes.rst | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c70cc72e55af58..f61dca9bbc8467 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -259,10 +259,6 @@ Bug Fixes in This Version
   operator.
   Fixes (#GH83267).
 
-- Clang no longer fails to codegen static ``const`` variables whose initialiser performs
-  a floating-point division by 0 in C23.
-  Fixes (#GH84784).
-
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

>From 3936d1c71b57df34899efa11c57e0091dff4b5e5 Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Wed, 13 Mar 2024 14:38:43 +0100
Subject: [PATCH 5/5] [Clang] Add sema test

---
 clang/test/Sema/const-init.c | 6 ++++++
 1 file changed, 6 insertions(+)
 create mode 100644 clang/test/Sema/const-init.c

diff --git a/clang/test/Sema/const-init.c b/clang/test/Sema/const-init.c
new file mode 100644
index 00000000000000..c88246a5f293e7
--- /dev/null
+++ b/clang/test/Sema/const-init.c
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c23 %s
+
+// Division by 0 here is an error iff the variable is 'constexpr'.
+const _Bool inf1 =  (1.0/0.0 == __builtin_inf());
+constexpr _Bool inf2 = (1.0/0.0 == __builtin_inf()); // expected-error {{must be initialized by a constant expression}} expected-note {{division by zero}}
+constexpr _Bool inf3 = __builtin_inf() == __builtin_inf();
\ No newline at end of file



More information about the cfe-commits mailing list