Skip to content

Commit 0d8fcdf

Browse files
committed
[OPENMP]Fix crash for the ordered(n) clause.
If the doacross lop construct is used and the loop counter is declare outside of the loop, the compiler might crash trying to get the address of the loop counter. Patch fixes this problem. llvm-svn: 356198
1 parent c4420b0 commit 0d8fcdf

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,8 +1529,9 @@ void CodeGenFunction::EmitOMPPrivateLoopCounters(
15291529
I < E; ++I) {
15301530
const auto *DRE = cast<DeclRefExpr>(C->getLoopCounter(I));
15311531
const auto *VD = cast<VarDecl>(DRE->getDecl());
1532-
// Override only those variables that are really emitted already.
1533-
if (LocalDeclMap.count(VD)) {
1532+
// Override only those variables that can be captured to avoid re-emission
1533+
// of the variables declared within the loops.
1534+
if (DRE->refersToEnclosingVariableOrCapture()) {
15341535
(void)LoopScope.addPrivate(VD, [this, DRE, VD]() {
15351536
return CreateMemTemp(DRE->getType(), VD->getName());
15361537
});

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4758,8 +4758,7 @@ DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
47584758
Captures.insert(std::make_pair(LCRef, Ref));
47594759
return Ref;
47604760
}
4761-
return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
4762-
DefaultLoc);
4761+
return cast<DeclRefExpr>(LCRef);
47634762
}
47644763

47654764
Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {

clang/test/OpenMP/ordered_doacross_codegen.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ extern int n;
1616
int a[10], b[10], c[10], d[10];
1717
void foo();
1818

19+
// CHECK-LABEL:bar
20+
void bar() {
21+
int i,j;
22+
// CHECK: call void @__kmpc_doacross_init(
23+
// CHECK: call void @__kmpc_doacross_fini(
24+
#pragma omp parallel for ordered(2)
25+
for (i = 0; i < n; ++i)
26+
for (j = 0; j < n; ++j)
27+
a[i] = b[i] + 1;
28+
}
29+
1930
// CHECK-LABEL: @main()
2031
int main() {
2132
int i;
@@ -35,7 +46,7 @@ int main() {
3546
// CHECK: call void @__kmpc_doacross_init([[IDENT]], i32 [[GTID]], i32 1, i8* [[CAST]])
3647
// CHECK: call void @__kmpc_for_static_init_4(
3748
#pragma omp for ordered(1)
38-
for (i = 0; i < n; ++i) {
49+
for (int i = 0; i < n; ++i) {
3950
a[i] = b[i] + 1;
4051
foo();
4152
// CHECK: invoke void [[FOO:.+]](

0 commit comments

Comments
 (0)