Skip to content

Commit 7af4eaf

Browse files
committed
implement full support for message/severity clauses
1 parent 38b6bd9 commit 7af4eaf

20 files changed

+13607
-1307
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,70 +1865,44 @@ class OMPSeverityClause final : public OMPClause {
18651865
/// \endcode
18661866
/// In this example directive '#pragma omp error' has simple
18671867
/// 'message' clause with user error message of "GNU compiler required.".
1868-
class OMPMessageClause final : public OMPClause {
1868+
class OMPMessageClause final
1869+
: public OMPOneStmtClause<llvm::omp::OMPC_message, OMPClause>,
1870+
public OMPClauseWithPreInit {
18691871
friend class OMPClauseReader;
18701872

1871-
/// Location of '('
1872-
SourceLocation LParenLoc;
1873-
1874-
// Expression of the 'message' clause.
1875-
Stmt *MessageString = nullptr;
1876-
18771873
/// Set message string of the clause.
1878-
void setMessageString(Expr *MS) { MessageString = MS; }
1879-
1880-
/// Sets the location of '('.
1881-
void setLParenLoc(SourceLocation Loc) { LParenLoc = Loc; }
1874+
void setMessageString(Expr *MS) { setStmt(MS); }
18821875

18831876
public:
18841877
/// Build 'message' clause with message string argument
18851878
///
18861879
/// \param MS Argument of the clause (message string).
1880+
/// \param HelperMS Helper statement for the construct.
1881+
/// \param CaptureRegion Innermost OpenMP region where expressions in this
1882+
/// clause must be captured.
18871883
/// \param StartLoc Starting location of the clause.
18881884
/// \param LParenLoc Location of '('.
18891885
/// \param EndLoc Ending location of the clause.
1890-
OMPMessageClause(Expr *MS, SourceLocation StartLoc, SourceLocation LParenLoc,
1886+
OMPMessageClause(Expr *MS, Stmt *HelperMS, OpenMPDirectiveKind CaptureRegion,
1887+
SourceLocation StartLoc, SourceLocation LParenLoc,
18911888
SourceLocation EndLoc)
1892-
: OMPClause(llvm::omp::OMPC_message, StartLoc, EndLoc),
1893-
LParenLoc(LParenLoc), MessageString(MS) {}
1894-
1895-
/// Build an empty clause.
1896-
OMPMessageClause()
1897-
: OMPClause(llvm::omp::OMPC_message, SourceLocation(), SourceLocation()) {
1889+
: OMPOneStmtClause(MS, StartLoc, LParenLoc, EndLoc),
1890+
OMPClauseWithPreInit(this) {
1891+
setPreInitStmt(HelperMS, CaptureRegion);
18981892
}
18991893

1900-
/// Returns the locaiton of '('.
1901-
SourceLocation getLParenLoc() const { return LParenLoc; }
1894+
/// Build an empty clause.
1895+
OMPMessageClause() : OMPOneStmtClause(), OMPClauseWithPreInit(this) {}
19021896

19031897
/// Returns message string of the clause.
1904-
Expr *getMessageString() const { return cast_or_null<Expr>(MessageString); }
1898+
Expr *getMessageString() const { return getStmtAs<Expr>(); }
19051899

19061900
/// Try to evaluate the message string at compile time.
19071901
std::optional<std::string> tryEvaluateString(ASTContext &Ctx) const {
19081902
if (Expr *MessageExpr = getMessageString())
19091903
return MessageExpr->tryEvaluateString(Ctx);
19101904
return std::nullopt;
19111905
}
1912-
1913-
child_range children() {
1914-
return child_range(&MessageString, &MessageString + 1);
1915-
}
1916-
1917-
const_child_range children() const {
1918-
return const_child_range(&MessageString, &MessageString + 1);
1919-
}
1920-
1921-
child_range used_children() {
1922-
return child_range(child_iterator(), child_iterator());
1923-
}
1924-
1925-
const_child_range used_children() const {
1926-
return const_child_range(const_child_iterator(), const_child_iterator());
1927-
}
1928-
1929-
static bool classof(const OMPClause *T) {
1930-
return T->getClauseKind() == llvm::omp::OMPC_message;
1931-
}
19321906
};
19331907

19341908
/// This represents 'schedule' clause in the '#pragma omp ...' directive.

clang/lib/AST/OpenMPClause.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
104104
return static_cast<const OMPFilterClause *>(C);
105105
case OMPC_ompx_dyn_cgroup_mem:
106106
return static_cast<const OMPXDynCGroupMemClause *>(C);
107+
case OMPC_message:
108+
return static_cast<const OMPMessageClause *>(C);
107109
case OMPC_default:
108110
case OMPC_proc_bind:
109111
case OMPC_safelen:
@@ -158,7 +160,6 @@ const OMPClauseWithPreInit *OMPClauseWithPreInit::get(const OMPClause *C) {
158160
case OMPC_self_maps:
159161
case OMPC_at:
160162
case OMPC_severity:
161-
case OMPC_message:
162163
case OMPC_device_type:
163164
case OMPC_match:
164165
case OMPC_nontemporal:

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,34 @@ llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF,
26982698
CGF.getContext().BoolTy, Loc);
26992699
}
27002700

2701+
llvm::Value *CGOpenMPRuntime::emitMessageClause(CodeGenFunction &CGF,
2702+
const Expr *Message) {
2703+
if (!Message)
2704+
return llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
2705+
return CGF.EmitScalarExpr(Message);
2706+
}
2707+
2708+
llvm::Value *
2709+
CGOpenMPRuntime::emitMessageClause(CodeGenFunction &CGF,
2710+
const OMPMessageClause *MessageClause) {
2711+
return emitMessageClause(
2712+
CGF, MessageClause ? MessageClause->getMessageString() : nullptr);
2713+
}
2714+
2715+
llvm::Value *
2716+
CGOpenMPRuntime::emitSeverityClause(OpenMPSeverityClauseKind Severity) {
2717+
// OpenMP 6.0, 10.4: "If no severity clause is specified then the effect is
2718+
// as if sev-level is fatal."
2719+
return llvm::ConstantInt::get(CGM.Int32Ty,
2720+
Severity == OMPC_SEVERITY_warning ? 1 : 2);
2721+
}
2722+
2723+
llvm::Value *
2724+
CGOpenMPRuntime::emitSeverityClause(const OMPSeverityClause *SeverityClause) {
2725+
return emitSeverityClause(SeverityClause ? SeverityClause->getSeverityKind()
2726+
: OMPC_SEVERITY_unknown);
2727+
}
2728+
27012729
void CGOpenMPRuntime::emitNumThreadsClause(
27022730
CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc,
27032731
OpenMPNumThreadsClauseModifier Modifier, OpenMPSeverityClauseKind Severity,
@@ -2713,14 +2741,8 @@ void CGOpenMPRuntime::emitNumThreadsClause(
27132741
RuntimeFunction FnID = OMPRTL___kmpc_push_num_threads;
27142742
if (Modifier == OMPC_NUMTHREADS_strict) {
27152743
FnID = OMPRTL___kmpc_push_num_threads_strict;
2716-
// OpenMP 6.0, 10.4: "If no severity clause is specified then the effect is
2717-
// as if sev-level is fatal."
2718-
Args.push_back(llvm::ConstantInt::get(
2719-
CGM.Int32Ty, Severity == OMPC_SEVERITY_warning ? 1 : 2));
2720-
if (Message)
2721-
Args.push_back(CGF.EmitScalarExpr(Message));
2722-
else
2723-
Args.push_back(llvm::ConstantPointerNull::get(CGF.VoidPtrTy));
2744+
Args.push_back(emitSeverityClause(Severity));
2745+
Args.push_back(emitMessageClause(CGF, Message));
27242746
}
27252747
CGF.EmitRuntimeCall(
27262748
OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(), FnID), Args);

clang/lib/CodeGen/CGOpenMPRuntime.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,15 @@ class CGOpenMPRuntime {
10481048
Address IL, Address LB,
10491049
Address UB, Address ST);
10501050

1051+
virtual llvm::Value *emitMessageClause(CodeGenFunction &CGF,
1052+
const Expr *Message);
1053+
virtual llvm::Value *emitMessageClause(CodeGenFunction &CGF,
1054+
const OMPMessageClause *MessageClause);
1055+
1056+
virtual llvm::Value *emitSeverityClause(OpenMPSeverityClauseKind Severity);
1057+
virtual llvm::Value *
1058+
emitSeverityClause(const OMPSeverityClause *SeverityClause);
1059+
10511060
/// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
10521061
/// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
10531062
/// clause.

clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,17 +1270,9 @@ void CGOpenMPRuntimeGPU::emitParallelCall(
12701270
llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size())});
12711271
if (NumThreadsModifier == OMPC_NUMTHREADS_strict) {
12721272
FnID = OMPRTL___kmpc_parallel_60;
1273-
// OpenMP 6.0, 10.4: "If no severity clause is specified then the effect
1274-
// is as if sev-level is fatal."
1275-
Args.append(
1276-
{llvm::ConstantInt::get(CGM.Int32Ty, true),
1277-
llvm::ConstantInt::get(CGM.Int32Ty,
1278-
Severity == OMPC_SEVERITY_warning ? 1 : 2)});
1279-
if (Message)
1280-
Args.push_back(CGF.EmitStringLiteralLValue(cast<StringLiteral>(Message))
1281-
.getPointer(CGF));
1282-
else
1283-
Args.push_back(llvm::ConstantPointerNull::get(CGF.VoidPtrTy));
1273+
Args.append({llvm::ConstantInt::get(CGM.Int32Ty, true),
1274+
emitSeverityClause(Severity),
1275+
emitMessageClause(CGF, Message)});
12841276
}
12851277
CGF.EmitRuntimeCall(
12861278
OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(), FnID), Args);

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16477,11 +16477,24 @@ OMPClause *SemaOpenMP::ActOnOpenMPMessageClause(Expr *ME,
1647716477
return nullptr;
1647816478
}
1647916479

16480+
Stmt *HelperValStmt = nullptr;
16481+
16482+
OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
16483+
OpenMPDirectiveKind CaptureRegion = getOpenMPCaptureRegionForClause(
16484+
DKind, OMPC_message, getLangOpts().OpenMP);
16485+
if (CaptureRegion != OMPD_unknown &&
16486+
!SemaRef.CurContext->isDependentContext()) {
16487+
ME = SemaRef.MakeFullExpr(ME).get();
16488+
llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
16489+
ME = tryBuildCapture(SemaRef, ME, Captures).get();
16490+
HelperValStmt = buildPreInits(getASTContext(), Captures);
16491+
}
16492+
1648016493
// Convert array type to pointer type if needed.
1648116494
ME = SemaRef.DefaultFunctionArrayLvalueConversion(ME).get();
1648216495

16483-
return new (getASTContext())
16484-
OMPMessageClause(ME, StartLoc, LParenLoc, EndLoc);
16496+
return new (getASTContext()) OMPMessageClause(
16497+
ME, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
1648516498
}
1648616499

1648716500
OMPClause *SemaOpenMP::ActOnOpenMPOrderClause(

clang/lib/Serialization/ASTReader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11837,6 +11837,7 @@ void OMPClauseReader::VisitOMPSeverityClause(OMPSeverityClause *C) {
1183711837
}
1183811838

1183911839
void OMPClauseReader::VisitOMPMessageClause(OMPMessageClause *C) {
11840+
VisitOMPClauseWithPreInit(C);
1184011841
C->setMessageString(Record.readSubExpr());
1184111842
C->setLParenLoc(Record.readSourceLocation());
1184211843
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8541,6 +8541,7 @@ void OMPClauseWriter::VisitOMPSeverityClause(OMPSeverityClause *C) {
85418541
}
85428542

85438543
void OMPClauseWriter::VisitOMPMessageClause(OMPMessageClause *C) {
8544+
VisitOMPClauseWithPreInit(C);
85448545
Record.AddStmt(C->getMessageString());
85458546
Record.AddSourceLocation(C->getLParenLoc());
85468547
}

0 commit comments

Comments
 (0)