Skip to content

Commit a0a008c

Browse files
committed
[OpenMP][clang] 6.0: num_threads strict (part 3: codegen)
OpenMP 6.0 12.1.2 specifies the behavior of the strict modifier for the num_threads clause on parallel directives, along with the message and severity clauses. This commit implements necessary codegen changes.
1 parent cf566c6 commit a0a008c

10 files changed

+2609
-103
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,11 +1845,11 @@ void CGOpenMPRuntime::emitIfClause(CodeGenFunction &CGF, const Expr *Cond,
18451845
CGF.EmitBlock(ContBlock, /*IsFinished=*/true);
18461846
}
18471847

1848-
void CGOpenMPRuntime::emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
1849-
llvm::Function *OutlinedFn,
1850-
ArrayRef<llvm::Value *> CapturedVars,
1851-
const Expr *IfCond,
1852-
llvm::Value *NumThreads) {
1848+
void CGOpenMPRuntime::emitParallelCall(
1849+
CodeGenFunction &CGF, SourceLocation Loc, llvm::Function *OutlinedFn,
1850+
ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond,
1851+
llvm::Value *NumThreads, OpenMPNumThreadsClauseModifier NumThreadsModifier,
1852+
OpenMPSeverityClauseKind Severity, const Expr *Message) {
18531853
if (!CGF.HaveInsertPoint())
18541854
return;
18551855
llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
@@ -2699,18 +2699,33 @@ llvm::Value *CGOpenMPRuntime::emitForNext(CodeGenFunction &CGF,
26992699
CGF.getContext().BoolTy, Loc);
27002700
}
27012701

2702-
void CGOpenMPRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
2703-
llvm::Value *NumThreads,
2704-
SourceLocation Loc) {
2702+
void CGOpenMPRuntime::emitNumThreadsClause(
2703+
CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc,
2704+
OpenMPNumThreadsClauseModifier Modifier, OpenMPSeverityClauseKind Severity,
2705+
const Expr *Message) {
27052706
if (!CGF.HaveInsertPoint())
27062707
return;
2708+
llvm::SmallVector<llvm::Value *, 4> Args(
2709+
{emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2710+
CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)});
27072711
// Build call __kmpc_push_num_threads(&loc, global_tid, num_threads)
2708-
llvm::Value *Args[] = {
2709-
emitUpdateLocation(CGF, Loc), getThreadID(CGF, Loc),
2710-
CGF.Builder.CreateIntCast(NumThreads, CGF.Int32Ty, /*isSigned*/ true)};
2711-
CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
2712-
CGM.getModule(), OMPRTL___kmpc_push_num_threads),
2713-
Args);
2712+
// or __kmpc_push_num_threads_strict(&loc, global_tid, num_threads, severity,
2713+
// messsage) if strict modifier is used.
2714+
RuntimeFunction FnID = OMPRTL___kmpc_push_num_threads;
2715+
if (Modifier == OMPC_NUMTHREADS_strict) {
2716+
FnID = OMPRTL___kmpc_push_num_threads_strict;
2717+
// OpenMP 6.0, 10.4: "If no severity clause is specified then the effect is
2718+
// as if sev-level is fatal."
2719+
Args.push_back(llvm::ConstantInt::get(
2720+
CGM.Int32Ty, Severity == OMPC_SEVERITY_warning ? 1 : 2));
2721+
if (Message)
2722+
Args.push_back(CGF.EmitStringLiteralLValue(cast<StringLiteral>(Message))
2723+
.getPointer(CGF));
2724+
else
2725+
Args.push_back(llvm::ConstantPointerNull::get(CGF.VoidPtrTy));
2726+
}
2727+
CGF.EmitRuntimeCall(
2728+
OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(), FnID), Args);
27142729
}
27152730

27162731
void CGOpenMPRuntime::emitProcBindClause(CodeGenFunction &CGF,
@@ -11986,12 +12001,11 @@ llvm::Function *CGOpenMPSIMDRuntime::emitTaskOutlinedFunction(
1198612001
llvm_unreachable("Not supported in SIMD-only mode");
1198712002
}
1198812003

11989-
void CGOpenMPSIMDRuntime::emitParallelCall(CodeGenFunction &CGF,
11990-
SourceLocation Loc,
11991-
llvm::Function *OutlinedFn,
11992-
ArrayRef<llvm::Value *> CapturedVars,
11993-
const Expr *IfCond,
11994-
llvm::Value *NumThreads) {
12004+
void CGOpenMPSIMDRuntime::emitParallelCall(
12005+
CodeGenFunction &CGF, SourceLocation Loc, llvm::Function *OutlinedFn,
12006+
ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond,
12007+
llvm::Value *NumThreads, OpenMPNumThreadsClauseModifier NumThreadsModifier,
12008+
OpenMPSeverityClauseKind Severity, const Expr *Message) {
1199512009
llvm_unreachable("Not supported in SIMD-only mode");
1199612010
}
1199712011

@@ -12094,9 +12108,10 @@ llvm::Value *CGOpenMPSIMDRuntime::emitForNext(CodeGenFunction &CGF,
1209412108
llvm_unreachable("Not supported in SIMD-only mode");
1209512109
}
1209612110

12097-
void CGOpenMPSIMDRuntime::emitNumThreadsClause(CodeGenFunction &CGF,
12098-
llvm::Value *NumThreads,
12099-
SourceLocation Loc) {
12111+
void CGOpenMPSIMDRuntime::emitNumThreadsClause(
12112+
CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc,
12113+
OpenMPNumThreadsClauseModifier Modifier, OpenMPSeverityClauseKind Severity,
12114+
const Expr *Message) {
1210012115
llvm_unreachable("Not supported in SIMD-only mode");
1210112116
}
1210212117

clang/lib/CodeGen/CGOpenMPRuntime.h

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,22 @@ class CGOpenMPRuntime {
777777
/// specified, nullptr otherwise.
778778
/// \param NumThreads The value corresponding to the num_threads clause, if
779779
/// any, or nullptr.
780+
/// \param NumThreadsModifier The modifier of the num_threads clause, if
781+
/// any, ignored otherwise.
782+
/// \param Severity The severity corresponding to the num_threads clause, if
783+
/// any, ignored otherwise.
784+
/// \param Message The message string corresponding to the num_threads clause,
785+
/// if any, or nullptr.
780786
///
781-
virtual void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
782-
llvm::Function *OutlinedFn,
783-
ArrayRef<llvm::Value *> CapturedVars,
784-
const Expr *IfCond, llvm::Value *NumThreads);
787+
virtual void
788+
emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
789+
llvm::Function *OutlinedFn,
790+
ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond,
791+
llvm::Value *NumThreads,
792+
OpenMPNumThreadsClauseModifier NumThreadsModifier =
793+
OMPC_NUMTHREADS_unknown,
794+
OpenMPSeverityClauseKind Severity = OMPC_SEVERITY_fatal,
795+
const Expr *Message = nullptr);
785796

786797
/// Emits a critical region.
787798
/// \param CriticalName Name of the critical region.
@@ -1040,10 +1051,16 @@ class CGOpenMPRuntime {
10401051
/// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
10411052
/// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
10421053
/// clause.
1054+
/// If the modifier 'strict' is given:
1055+
/// Emits call to void __kmpc_push_num_threads_strict(ident_t *loc, kmp_int32
1056+
/// global_tid, kmp_int32 num_threads, int severity, const char *message) to
1057+
/// generate code for 'num_threads' clause with 'strict' modifier.
10431058
/// \param NumThreads An integer value of threads.
1044-
virtual void emitNumThreadsClause(CodeGenFunction &CGF,
1045-
llvm::Value *NumThreads,
1046-
SourceLocation Loc);
1059+
virtual void emitNumThreadsClause(
1060+
CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc,
1061+
OpenMPNumThreadsClauseModifier Modifier = OMPC_NUMTHREADS_unknown,
1062+
OpenMPSeverityClauseKind Severity = OMPC_SEVERITY_fatal,
1063+
const Expr *Message = nullptr);
10471064

10481065
/// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
10491066
/// global_tid, int proc_bind) to generate code for 'proc_bind' clause.
@@ -1737,11 +1754,21 @@ class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
17371754
/// specified, nullptr otherwise.
17381755
/// \param NumThreads The value corresponding to the num_threads clause, if
17391756
/// any, or nullptr.
1757+
/// \param NumThreadsModifier The modifier of the num_threads clause, if
1758+
/// any, ignored otherwise.
1759+
/// \param Severity The severity corresponding to the num_threads clause, if
1760+
/// any, ignored otherwise.
1761+
/// \param Message The message string corresponding to the num_threads clause,
1762+
/// if any, or nullptr.
17401763
///
17411764
void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
17421765
llvm::Function *OutlinedFn,
17431766
ArrayRef<llvm::Value *> CapturedVars,
1744-
const Expr *IfCond, llvm::Value *NumThreads) override;
1767+
const Expr *IfCond, llvm::Value *NumThreads,
1768+
OpenMPNumThreadsClauseModifier NumThreadsModifier =
1769+
OMPC_NUMTHREADS_unknown,
1770+
OpenMPSeverityClauseKind Severity = OMPC_SEVERITY_fatal,
1771+
const Expr *Message = nullptr) override;
17451772

17461773
/// Emits a critical region.
17471774
/// \param CriticalName Name of the critical region.
@@ -1911,9 +1938,16 @@ class CGOpenMPSIMDRuntime final : public CGOpenMPRuntime {
19111938
/// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
19121939
/// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
19131940
/// clause.
1941+
/// If the modifier 'strict' is given:
1942+
/// Emits call to void __kmpc_push_num_threads_strict(ident_t *loc, kmp_int32
1943+
/// global_tid, kmp_int32 num_threads, int severity, const char *message) to
1944+
/// generate code for 'num_threads' clause with 'strict' modifier.
19141945
/// \param NumThreads An integer value of threads.
1915-
void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
1916-
SourceLocation Loc) override;
1946+
void emitNumThreadsClause(
1947+
CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc,
1948+
OpenMPNumThreadsClauseModifier Modifier = OMPC_NUMTHREADS_unknown,
1949+
OpenMPSeverityClauseKind Severity = OMPC_SEVERITY_fatal,
1950+
const Expr *Message = nullptr) override;
19171951

19181952
/// Emit call to void __kmpc_push_proc_bind(ident_t *loc, kmp_int32
19191953
/// global_tid, int proc_bind) to generate code for 'proc_bind' clause.

clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -899,9 +899,10 @@ void CGOpenMPRuntimeGPU::emitProcBindClause(CodeGenFunction &CGF,
899899
// Nothing to do.
900900
}
901901

902-
void CGOpenMPRuntimeGPU::emitNumThreadsClause(CodeGenFunction &CGF,
903-
llvm::Value *NumThreads,
904-
SourceLocation Loc) {
902+
void CGOpenMPRuntimeGPU::emitNumThreadsClause(
903+
CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc,
904+
OpenMPNumThreadsClauseModifier Modifier, OpenMPSeverityClauseKind Severity,
905+
const Expr *Message) {
905906
// Nothing to do.
906907
}
907908

@@ -1201,18 +1202,17 @@ void CGOpenMPRuntimeGPU::emitTeamsCall(CodeGenFunction &CGF,
12011202
emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
12021203
}
12031204

1204-
void CGOpenMPRuntimeGPU::emitParallelCall(CodeGenFunction &CGF,
1205-
SourceLocation Loc,
1206-
llvm::Function *OutlinedFn,
1207-
ArrayRef<llvm::Value *> CapturedVars,
1208-
const Expr *IfCond,
1209-
llvm::Value *NumThreads) {
1205+
void CGOpenMPRuntimeGPU::emitParallelCall(
1206+
CodeGenFunction &CGF, SourceLocation Loc, llvm::Function *OutlinedFn,
1207+
ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond,
1208+
llvm::Value *NumThreads, OpenMPNumThreadsClauseModifier NumThreadsModifier,
1209+
OpenMPSeverityClauseKind Severity, const Expr *Message) {
12101210
if (!CGF.HaveInsertPoint())
12111211
return;
12121212

1213-
auto &&ParallelGen = [this, Loc, OutlinedFn, CapturedVars, IfCond,
1214-
NumThreads](CodeGenFunction &CGF,
1215-
PrePostActionTy &Action) {
1213+
auto &&ParallelGen = [this, Loc, OutlinedFn, CapturedVars, IfCond, NumThreads,
1214+
NumThreadsModifier, Severity, Message](
1215+
CodeGenFunction &CGF, PrePostActionTy &Action) {
12161216
CGBuilderTy &Bld = CGF.Builder;
12171217
llvm::Value *NumThreadsVal = NumThreads;
12181218
llvm::Function *WFn = WrapperFunctionsMap[OutlinedFn];
@@ -1260,21 +1260,30 @@ void CGOpenMPRuntimeGPU::emitParallelCall(CodeGenFunction &CGF,
12601260
NumThreadsVal = Bld.CreateZExtOrTrunc(NumThreadsVal, CGF.Int32Ty);
12611261

12621262
assert(IfCondVal && "Expected a value");
1263+
RuntimeFunction FnID = OMPRTL___kmpc_parallel_51;
12631264
llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
1264-
llvm::Value *Args[] = {
1265-
RTLoc,
1266-
getThreadID(CGF, Loc),
1267-
IfCondVal,
1268-
NumThreadsVal,
1269-
llvm::ConstantInt::get(CGF.Int32Ty, -1),
1270-
FnPtr,
1271-
ID,
1272-
Bld.CreateBitOrPointerCast(CapturedVarsAddrs.emitRawPointer(CGF),
1273-
CGF.VoidPtrPtrTy),
1274-
llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size())};
1275-
CGF.EmitRuntimeCall(OMPBuilder.getOrCreateRuntimeFunction(
1276-
CGM.getModule(), OMPRTL___kmpc_parallel_51),
1277-
Args);
1265+
llvm::SmallVector<llvm::Value *, 10> Args(
1266+
{RTLoc, getThreadID(CGF, Loc), IfCondVal, NumThreadsVal,
1267+
llvm::ConstantInt::get(CGF.Int32Ty, -1), FnPtr, ID,
1268+
Bld.CreateBitOrPointerCast(CapturedVarsAddrs.emitRawPointer(CGF),
1269+
CGF.VoidPtrPtrTy),
1270+
llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size())});
1271+
if (NumThreadsModifier == OMPC_NUMTHREADS_strict) {
1272+
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));
1284+
}
1285+
CGF.EmitRuntimeCall(
1286+
OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(), FnID), Args);
12781287
};
12791288

12801289
RegionCodeGenTy RCG(ParallelGen);

clang/lib/CodeGen/CGOpenMPRuntimeGPU.h

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,16 @@ class CGOpenMPRuntimeGPU : public CGOpenMPRuntime {
165165
/// Emits call to void __kmpc_push_num_threads(ident_t *loc, kmp_int32
166166
/// global_tid, kmp_int32 num_threads) to generate code for 'num_threads'
167167
/// clause.
168+
/// If the modifier 'strict' is given:
169+
/// Emits call to void __kmpc_push_num_threads_strict(ident_t *loc, kmp_int32
170+
/// global_tid, kmp_int32 num_threads, int severity, const char *message) to
171+
/// generate code for 'num_threads' clause with 'strict' modifier.
168172
/// \param NumThreads An integer value of threads.
169-
void emitNumThreadsClause(CodeGenFunction &CGF, llvm::Value *NumThreads,
170-
SourceLocation Loc) override;
173+
void emitNumThreadsClause(
174+
CodeGenFunction &CGF, llvm::Value *NumThreads, SourceLocation Loc,
175+
OpenMPNumThreadsClauseModifier Modifier = OMPC_NUMTHREADS_unknown,
176+
OpenMPSeverityClauseKind Severity = OMPC_SEVERITY_fatal,
177+
const Expr *Message = nullptr) override;
171178

172179
/// This function ought to emit, in the general case, a call to
173180
// the openmp runtime kmpc_push_num_teams. In NVPTX backend it is not needed
@@ -229,12 +236,21 @@ class CGOpenMPRuntimeGPU : public CGOpenMPRuntime {
229236
/// \param IfCond Condition in the associated 'if' clause, if it was
230237
/// specified, nullptr otherwise.
231238
/// \param NumThreads The value corresponding to the num_threads clause, if
232-
/// any,
233-
/// or nullptr.
239+
/// any, or nullptr.
240+
/// \param NumThreadsModifier The modifier of the num_threads clause, if
241+
/// any, ignored otherwise.
242+
/// \param Severity The severity corresponding to the num_threads clause, if
243+
/// any, ignored otherwise.
244+
/// \param Message The message string corresponding to the num_threads clause,
245+
/// if any, or nullptr.
234246
void emitParallelCall(CodeGenFunction &CGF, SourceLocation Loc,
235247
llvm::Function *OutlinedFn,
236248
ArrayRef<llvm::Value *> CapturedVars,
237-
const Expr *IfCond, llvm::Value *NumThreads) override;
249+
const Expr *IfCond, llvm::Value *NumThreads,
250+
OpenMPNumThreadsClauseModifier NumThreadsModifier =
251+
OMPC_NUMTHREADS_unknown,
252+
OpenMPSeverityClauseKind Severity = OMPC_SEVERITY_fatal,
253+
const Expr *Message = nullptr) override;
238254

239255
/// Emit an implicit/explicit barrier for OpenMP threads.
240256
/// \param Kind Directive for which this implicit barrier call must be

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,8 +1616,16 @@ static void emitCommonOMPParallelDirective(
16161616
CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF);
16171617
NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(),
16181618
/*IgnoreResultAssign=*/true);
1619+
OpenMPNumThreadsClauseModifier Modifier = NumThreadsClause->getModifier();
1620+
OpenMPSeverityClauseKind Severity = OMPC_SEVERITY_fatal;
1621+
clang::Expr *Message = nullptr;
1622+
if (const auto *MessageClause = S.getSingleClause<OMPMessageClause>())
1623+
Message = MessageClause->getMessageString();
1624+
if (const auto *SeverityClause = S.getSingleClause<OMPSeverityClause>())
1625+
Severity = SeverityClause->getSeverityKind();
16191626
CGF.CGM.getOpenMPRuntime().emitNumThreadsClause(
1620-
CGF, NumThreads, NumThreadsClause->getBeginLoc());
1627+
CGF, NumThreads, NumThreadsClause->getBeginLoc(), Modifier, Severity,
1628+
Message);
16211629
}
16221630
if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) {
16231631
CodeGenFunction::RunCleanupsScope ProcBindScope(CGF);

0 commit comments

Comments
 (0)