Skip to content

Commit bfda793

Browse files
committed
[OpenMP] Add a semantic check for updating hidden or internal values
A previous patch removed the compiler generating offloading entries for variables that were declared on the device but were internal or hidden. This allowed us to compile programs but turns any attempt to run '#pragma omp target update' on one of those variables a silent failure. This patch adds a check in the semantic analysis for if the user is attempting the update a variable on the device from the host that is not externally visible. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D122403
1 parent 33d2a78 commit bfda793

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10734,6 +10734,8 @@ def err_omp_expected_int_param : Error<
1073410734
"expected a reference to an integer-typed parameter">;
1073510735
def err_omp_at_least_one_motion_clause_required : Error<
1073610736
"expected at least one 'to' clause or 'from' clause specified to '#pragma omp target update'">;
10737+
def err_omp_cannot_update_with_internal_linkage : Error<
10738+
"the host cannot update a declare target variable that is not externally visible.">;
1073710739
def err_omp_usedeviceptr_not_a_pointer : Error<
1073810740
"expected pointer or reference to pointer in 'use_device_ptr' clause">;
1073910741
def err_omp_argument_type_isdeviceptr : Error <

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12673,6 +12673,26 @@ static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
1267312673
return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
1267412674
}
1267512675

12676+
/// Check if the variables in the mapping clause are externally visible.
12677+
static bool isClauseMappable(ArrayRef<OMPClause *> Clauses) {
12678+
for (const OMPClause *C : Clauses) {
12679+
if (auto *TC = dyn_cast<OMPToClause>(C))
12680+
return llvm::all_of(TC->all_decls(), [](ValueDecl *VD) {
12681+
return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
12682+
(VD->isExternallyVisible() &&
12683+
VD->getVisibility() != HiddenVisibility);
12684+
});
12685+
else if (auto *FC = dyn_cast<OMPFromClause>(C))
12686+
return llvm::all_of(FC->all_decls(), [](ValueDecl *VD) {
12687+
return !VD || !VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
12688+
(VD->isExternallyVisible() &&
12689+
VD->getVisibility() != HiddenVisibility);
12690+
});
12691+
}
12692+
12693+
return true;
12694+
}
12695+
1267612696
StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
1267712697
Stmt *AStmt,
1267812698
SourceLocation StartLoc,
@@ -12806,6 +12826,12 @@ StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
1280612826
Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
1280712827
return StmtError();
1280812828
}
12829+
12830+
if (!isClauseMappable(Clauses)) {
12831+
Diag(StartLoc, diag::err_omp_cannot_update_with_internal_linkage);
12832+
return StmtError();
12833+
}
12834+
1280912835
return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
1281012836
AStmt);
1281112837
}

clang/test/OpenMP/target_update_messages.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ void xxx(int argc) {
1414
argc = x; // expected-warning {{variable 'x' is uninitialized when used here}}
1515
}
1616

17+
static int y;
18+
#pragma omp declare target(y)
19+
20+
void yyy() {
21+
#pragma omp target update to(y) // expected-error {{the host cannot update a declare target variable that is not externally visible.}}
22+
}
23+
24+
int __attribute__((visibility("hidden"))) z;
25+
#pragma omp declare target(z)
26+
27+
void zzz() {
28+
#pragma omp target update from(z) // expected-error {{the host cannot update a declare target variable that is not externally visible.}}
29+
}
30+
1731
void foo() {
1832
}
1933

0 commit comments

Comments
 (0)