Skip to content

Commit bfb7736

Browse files
committed
[OpenMP] Fix accidental reuse of VLA size
We were using an OpaqueValueExpr allocated on the stack to store the size of a VLA. Because the VLASizeMap in CodegenFunction uses the address of the expression to avoid recomputing VLAs, we were accidentally reusing an earlier llvm::Value. This led to invalid LLVM IR. This is a temporary solution until VLASizeMap can be pushed and popped based on the context. Differential Revision: https://reviews.llvm.org/D107666
1 parent 62fe3dc commit bfb7736

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4401,14 +4401,14 @@ CGOpenMPRuntime::emitTaskInit(CodeGenFunction &CGF, SourceLocation Loc,
44014401
if (NumOfElements) {
44024402
NumOfElements = CGF.Builder.CreateNUWAdd(
44034403
llvm::ConstantInt::get(CGF.SizeTy, NumAffinities), NumOfElements);
4404-
OpaqueValueExpr OVE(
4404+
auto *OVE = new (C) OpaqueValueExpr(
44054405
Loc,
44064406
C.getIntTypeForBitwidth(C.getTypeSize(C.getSizeType()), /*Signed=*/0),
44074407
VK_PRValue);
4408-
CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE,
4408+
CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, OVE,
44094409
RValue::get(NumOfElements));
44104410
KmpTaskAffinityInfoArrayTy =
4411-
C.getVariableArrayType(KmpTaskAffinityInfoTy, &OVE, ArrayType::Normal,
4411+
C.getVariableArrayType(KmpTaskAffinityInfoTy, OVE, ArrayType::Normal,
44124412
/*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
44134413
// Properly emit variable-sized array.
44144414
auto *PD = ImplicitParamDecl::Create(C, KmpTaskAffinityInfoArrayTy,
@@ -4899,13 +4899,13 @@ std::pair<llvm::Value *, Address> CGOpenMPRuntime::emitDependClause(
48994899
NumOfElements =
49004900
CGF.Builder.CreateNUWAdd(NumOfRegularWithIterators, NumOfElements);
49014901
}
4902-
OpaqueValueExpr OVE(Loc,
4903-
C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0),
4904-
VK_PRValue);
4905-
CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, &OVE,
4902+
auto *OVE = new (C) OpaqueValueExpr(
4903+
Loc, C.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0),
4904+
VK_PRValue);
4905+
CodeGenFunction::OpaqueValueMapping OpaqueMap(CGF, OVE,
49064906
RValue::get(NumOfElements));
49074907
KmpDependInfoArrayTy =
4908-
C.getVariableArrayType(KmpDependInfoTy, &OVE, ArrayType::Normal,
4908+
C.getVariableArrayType(KmpDependInfoTy, OVE, ArrayType::Normal,
49094909
/*IndexTypeQuals=*/0, SourceRange(Loc, Loc));
49104910
// CGF.EmitVariablyModifiedType(KmpDependInfoArrayTy);
49114911
// Properly emit variable-sized array.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: %clang_cc1 -verify -fopenmp -x c++ -triple x86_64-unknown-linux-gnu \
2+
// RUN: -emit-llvm %s -o - | FileCheck %s
3+
4+
// expected-no-diagnostics
5+
6+
extern int bounds1(int);
7+
extern int bounds2(int);
8+
9+
extern void fun2(int n, int *a, int *b);
10+
extern void fun3(int n, int *a, int *b);
11+
12+
void fun1(int n, int *a, int *b)
13+
{
14+
#pragma omp task depend(iterator(j = 0 : bounds1(n)), in : a[b[j]])
15+
{
16+
fun2(n, a, b);
17+
}
18+
// CHECK: alloca %struct.kmp_depend_info, i64 [[FIRST_VLA:%.*]], align 16
19+
20+
#pragma omp task depend(iterator(j = 0 : bounds2(n)), in : a[b[j]])
21+
{
22+
fun3(n, a, b);
23+
}
24+
// CHECK-NOT: alloca %struct.kmp_depend_info, i64 [[FIRST_VLA]], align 16
25+
}

0 commit comments

Comments
 (0)