[clang] [Clang][CodeGen] Bail out on constexpr unknown values in ConstantEmitter (PR #127525)
Yingwei Zheng via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 20 20:16:03 PST 2025
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/127525
>From d931667e3b7e18f8ba6f054405ffc5fe2fac36b4 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 18 Feb 2025 01:26:26 +0800
Subject: [PATCH 1/3] [Clang][CodeGen] Bail out on constexpr unknown values in
ConstantEmitter
---
clang/lib/CodeGen/CGExprConstant.cpp | 6 ++++--
clang/test/CodeGenCXX/cxx23-p2280r4.cpp | 13 +++++++++++++
2 files changed, 17 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CodeGenCXX/cxx23-p2280r4.cpp
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index ee5874b26f534..075f3f435ad74 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -1883,8 +1883,10 @@ llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
// Try to emit the initializer. Note that this can allow some things that
// are not allowed by tryEmitPrivateForMemory alone.
- if (APValue *value = D.evaluateValue())
- return tryEmitPrivateForMemory(*value, destType);
+ if (APValue *value = D.evaluateValue()) {
+ if (!value->allowConstexprUnknown())
+ return tryEmitPrivateForMemory(*value, destType);
+ }
return nullptr;
}
diff --git a/clang/test/CodeGenCXX/cxx23-p2280r4.cpp b/clang/test/CodeGenCXX/cxx23-p2280r4.cpp
new file mode 100644
index 0000000000000..bb9a90ba18ece
--- /dev/null
+++ b/clang/test/CodeGenCXX/cxx23-p2280r4.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++23 %s -emit-llvm -o - | FileCheck %s
+
+extern int& s;
+
+// CHECK: @_Z4testv()
+// CHECK-NEXT: entry:
+// CHECK-NEXT: [[I:%.*]] = alloca ptr, align {{.*}}
+// CHECK-NEXT: [[X:%.*]] = load ptr, ptr @s, align {{.*}}
+// CHECK-NEXT: store ptr [[X]], ptr [[I]], align {{.*}}
+int& test() {
+ auto &i = s;
+ return i;
+}
>From 9eb4a88e25f2d9a7e8799955a7b45abeb1808b39 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Tue, 18 Feb 2025 19:20:39 +0800
Subject: [PATCH 2/3] Address review comments. NFC.
---
clang/test/CodeGenCXX/cxx23-p2280r4.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/test/CodeGenCXX/cxx23-p2280r4.cpp b/clang/test/CodeGenCXX/cxx23-p2280r4.cpp
index bb9a90ba18ece..d5409be451df0 100644
--- a/clang/test/CodeGenCXX/cxx23-p2280r4.cpp
+++ b/clang/test/CodeGenCXX/cxx23-p2280r4.cpp
@@ -1,4 +1,6 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++23 %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++20 %s -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 -triple %itanium_abi_triple -std=c++17 %s -emit-llvm -o - | FileCheck %s
extern int& s;
>From 269f93fa37dc054c1670c8d2d6b60ca48cf07e93 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Fri, 21 Feb 2025 12:15:29 +0800
Subject: [PATCH 3/3] [Clang][CodeGen] Address review comments. NFC.
---
clang/lib/CodeGen/CGExprConstant.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index 075f3f435ad74..7d44a9f583eca 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -1883,10 +1883,10 @@ llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
// Try to emit the initializer. Note that this can allow some things that
// are not allowed by tryEmitPrivateForMemory alone.
- if (APValue *value = D.evaluateValue()) {
- if (!value->allowConstexprUnknown())
- return tryEmitPrivateForMemory(*value, destType);
- }
+ // Bail out on constexpr-unknown values since they are invalid in CodeGen.
+ if (APValue *value = D.evaluateValue();
+ value && !value->allowConstexprUnknown())
+ return tryEmitPrivateForMemory(*value, destType);
return nullptr;
}
More information about the cfe-commits
mailing list