diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index ee5874b26f534..7d44a9f583eca 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -1883,7 +1883,9 @@ 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()) + // 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; diff --git a/clang/test/CodeGenCXX/cxx23-p2280r4.cpp b/clang/test/CodeGenCXX/cxx23-p2280r4.cpp new file mode 100644 index 0000000000000..d5409be451df0 --- /dev/null +++ b/clang/test/CodeGenCXX/cxx23-p2280r4.cpp @@ -0,0 +1,15 @@ +// 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; + +// 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; +}