Skip to content

Commit bead8bf

Browse files
alexey-bataevtstellar
authored andcommitted
Merging r371548:
------------------------------------------------------------------------ r371548 | abataev | 2019-09-10 12:16:56 -0700 (Tue, 10 Sep 2019) | 10 lines Fix for PR43175: compiler crash when trying to emit noncapturable constant. If the constexpr variable is partially initialized, the initializer can be emitted as the structure, not as an array, because of some early optimizations. The llvm variable gets the type from this constant and, thus, gets the type which is pointer to struct rather than pointer to an array. We need to convert this type to be truely array, otherwise it may lead to the compiler crash when trying to emit array subscript expression. ------------------------------------------------------------------------
1 parent af5faf8 commit bead8bf

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,11 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
25402540
// Spill the constant value to a global.
25412541
Addr = CGM.createUnnamedGlobalFrom(*VD, Val,
25422542
getContext().getDeclAlign(VD));
2543+
llvm::Type *VarTy = getTypes().ConvertTypeForMem(VD->getType());
2544+
auto *PTy = llvm::PointerType::get(
2545+
VarTy, getContext().getTargetAddressSpace(VD->getType()));
2546+
if (PTy != Addr.getType())
2547+
Addr = Builder.CreatePointerBitCastOrAddrSpaceCast(Addr, PTy);
25432548
} else {
25442549
// Should we be using the alignment of the constant pointer we emitted?
25452550
CharUnits Alignment =
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %clang_cc1 -verify -triple x86_64-pc-windows-msvc19.22.27905 -emit-llvm -o - -fopenmp %s | FileCheck %s
2+
// expected-no-diagnostics
3+
4+
// CHECK: [[C_VAR_VAL:@.+]] = private unnamed_addr constant <{ i8, [26 x i8] }> <{ i8 1, [26 x i8] zeroinitializer }>,
5+
char a;
6+
bool b() {
7+
static constexpr bool c[27]{1};
8+
// CHECK: getelementptr inbounds [27 x i8], [27 x i8]* bitcast (<{ i8, [26 x i8] }>* [[C_VAR_VAL]] to [27 x i8]*),
9+
return c[a];
10+
}

0 commit comments

Comments
 (0)