[clang] [OpenMP] Emit the metadirective runtime selection (PR #239292)
Zahira Ammarguellat via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 09:54:17 PST 2026
https://github.com/zahiraam updated https://github.com/llvm/llvm-project/pull/239292
>From 20affab97a5f6b7e758675b11ec1f4f045911989 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Thu, 5 Nov 2026 09:00:04 -0800
Subject: [PATCH 1/2] [OpenMP] Emit the metadirective runtime selection
---
clang/lib/CodeGen/CGStmtOpenMP.cpp | 54 ++++++-
.../OpenMP/metadirective_runtime_codegen.cpp | 138 ++++++++++++++++++
2 files changed, 190 insertions(+), 2 deletions(-)
create mode 100644 clang/test/OpenMP/metadirective_runtime_codegen.cpp
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index b00894c85fd67..e2ecdf7d6f45b 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2398,8 +2398,58 @@ void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
}
void CodeGenFunction::EmitOMPMetaDirective(const OMPMetaDirective &S) {
- // TODO: Select the variant at runtime.
- EmitStmt(S.variants().front());
+ // Try variants from best to worst match; 'otherwise' runs if none applies.
+ TargetOMPContext OMPCtx(
+ getContext(), /*DiagUnknownTrait=*/[](StringRef) {},
+ /*CurrentFunctionDecl=*/nullptr,
+ /*ConstructTraits=*/{}, /*DeviceNum=*/-1);
+ ArrayRef<Stmt *> Variants = S.variants();
+ ArrayRef<Expr *> Conditions = S.conditions();
+ SmallVector<llvm::omp::VariantMatchInfo, 4> VMIs;
+ SmallVector<unsigned, 4> VariantIdx;
+ const Stmt *Fallback = nullptr;
+ for (auto [I, TI] : llvm::enumerate(S.traitInfos())) {
+ if (TI->Sets.empty()) {
+ Fallback = Variants[I];
+ continue;
+ }
+ llvm::omp::VariantMatchInfo &VMI = VMIs.emplace_back();
+ TI->getAsVariantMatchInfo(getContext(), VMI);
+ // Treat the condition as true here; it is checked at runtime.
+ if (Conditions[I]) {
+ VMI.RequiredTraits.reset(
+ unsigned(llvm::omp::TraitProperty::user_condition_false));
+ VMI.RequiredTraits.set(
+ unsigned(llvm::omp::TraitProperty::user_condition_true));
+ }
+ VariantIdx.push_back(I);
+ }
+
+ llvm::BasicBlock *EndBB = createBasicBlock("omp.metadirective.end");
+ while (!VMIs.empty()) {
+ int Best = llvm::omp::getBestVariantMatchForContext(VMIs, OMPCtx);
+ if (Best < 0)
+ break;
+ unsigned I = VariantIdx[Best];
+ VMIs.erase(VMIs.begin() + Best);
+ VariantIdx.erase(VariantIdx.begin() + Best);
+ // Without a condition the variant always applies; later ones are unused.
+ const Expr *Cond = Conditions[I];
+ if (!Cond) {
+ Fallback = Variants[I];
+ break;
+ }
+ llvm::BasicBlock *WhenBB = createBasicBlock("omp.metadirective.when");
+ llvm::BasicBlock *ElseBB = createBasicBlock("omp.metadirective.else");
+ EmitBranchOnBoolExpr(Cond, WhenBB, ElseBB, /*TrueCount=*/0);
+ EmitBlock(WhenBB);
+ EmitStmt(Variants[I]);
+ EmitBranch(EndBB);
+ EmitBlock(ElseBB);
+ }
+ if (Fallback)
+ EmitStmt(Fallback);
+ EmitBlock(EndBB, /*IsFinished=*/true);
}
namespace {
diff --git a/clang/test/OpenMP/metadirective_runtime_codegen.cpp b/clang/test/OpenMP/metadirective_runtime_codegen.cpp
new file mode 100644
index 0000000000000..26de70ff9cd27
--- /dev/null
+++ b/clang/test/OpenMP/metadirective_runtime_codegen.cpp
@@ -0,0 +1,138 @@
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=52 \
+// RUN: -fopenmp-metadirective-runtime-selection -x c++ \
+// RUN: -triple x86_64-unknown-linux -emit-llvm %s -o - | FileCheck %s
+// expected-no-diagnostics
+
+void bar();
+
+// The condition selects the 'when' variant; otherwise the 'otherwise' variant
+// runs.
+// CHECK-LABEL: define {{.*}}void @_Z14when_otherwisei(
+// CHECK: [[FLAG:%.*]] = load i32, ptr %flag.addr
+// CHECK-NEXT: [[COND:%.*]] = icmp ne i32 [[FLAG]], 0
+// CHECK-NEXT: br i1 [[COND]], label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK: call {{.*}}@__kmpc_fork_call(
+// CHECK: br label %[[END:omp.metadirective.end[0-9]*]]
+// CHECK: [[ELSE]]:
+// CHECK: call i32 @__kmpc_single(
+// CHECK: br label %[[END]]
+// CHECK: [[END]]:
+// CHECK-NEXT: ret void
+void when_otherwise(int flag) {
+#pragma omp metadirective when(user={condition(flag)}: parallel) otherwise(single)
+ bar();
+}
+
+// 'when' clauses are tried in order; the first true condition wins.
+// CHECK-LABEL: define {{.*}}void @_Z9two_whensii(
+// CHECK: [[A:%.*]] = load i32, ptr %a.addr
+// CHECK-NEXT: [[CONDA:%.*]] = icmp ne i32 [[A]], 0
+// CHECK-NEXT: br i1 [[CONDA]], label %[[WHEN0:omp.metadirective.when[0-9]*]], label %[[ELSE0:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN0]]:
+// CHECK: call {{.*}}@__kmpc_fork_call(
+// CHECK: br label %[[END:omp.metadirective.end[0-9]*]]
+// CHECK: [[ELSE0]]:
+// CHECK: [[B:%.*]] = load i32, ptr %b.addr
+// CHECK-NEXT: [[CONDB:%.*]] = icmp ne i32 [[B]], 0
+// CHECK-NEXT: br i1 [[CONDB]], label %[[WHEN1:omp.metadirective.when[0-9]*]], label %[[ELSE1:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN1]]:
+// CHECK: call i32 @__kmpc_single(
+// CHECK: br label %[[END]]
+// CHECK: [[ELSE1]]:
+// CHECK-NEXT: call void @_Z3barv()
+// CHECK-NEXT: br label %[[END]]
+// CHECK: [[END]]:
+// CHECK-NEXT: ret void
+void two_whens(int a, int b) {
+#pragma omp metadirective when(user={condition(a)}: parallel) \
+ when(user={condition(b)}: single) otherwise()
+ bar();
+}
+
+// Without 'otherwise', the associated statement runs when no condition holds.
+// CHECK-LABEL: define {{.*}}void @_Z12no_otherwisei(
+// CHECK: br i1 {{.*}}, label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK: call {{.*}}@__kmpc_fork_call(
+// CHECK: [[ELSE]]:
+// CHECK-NEXT: call void @_Z3barv()
+// CHECK-NEXT: br label %[[END:omp.metadirective.end[0-9]*]]
+// CHECK: [[END]]:
+// CHECK-NEXT: ret void
+void no_otherwise(int flag) {
+#pragma omp metadirective when(user={condition(flag)}: parallel)
+ bar();
+}
+
+// vendor(llvm) always matches, so 'parallel' is always selected.
+// CHECK-LABEL: define {{.*}}void @_Z18static_match_firsti(
+// CHECK-NOT: icmp
+// CHECK: call {{.*}}@__kmpc_fork_call(
+// CHECK-NOT: __kmpc_single
+// CHECK: ret void
+void static_match_first(int flag) {
+#pragma omp metadirective when(implementation={vendor(llvm)}: parallel) \
+ when(user={condition(flag)}: single) otherwise()
+ bar();
+}
+
+// vendor(amd) never matches, so its variant is not emitted.
+// CHECK-LABEL: define {{.*}}void @_Z14static_nomatchi(
+// CHECK-NOT: __kmpc_fork_call
+// CHECK: br i1 {{.*}}, label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK: call i32 @__kmpc_single(
+// CHECK: [[ELSE]]:
+// CHECK-NEXT: call void @_Z3barv()
+// CHECK-NOT: __kmpc_fork_call
+// CHECK: ret void
+void static_nomatch(int flag) {
+#pragma omp metadirective when(implementation={vendor(amd)}: parallel) \
+ when(user={condition(flag)}: single) otherwise()
+ bar();
+}
+
+// vendor(llvm) runs when the condition fails; plain bar() is never called.
+// CHECK-LABEL: define {{.*}}void @_Z17static_match_lasti(
+// CHECK: br i1 {{.*}}, label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK: call i32 @__kmpc_single(
+// CHECK: [[ELSE]]:
+// CHECK-NEXT: call {{.*}}@__kmpc_fork_call(
+// CHECK-NEXT: br label %[[END:omp.metadirective.end[0-9]*]]
+// CHECK: [[END]]:
+// CHECK-NEXT: ret void
+void static_match_last(int flag) {
+#pragma omp metadirective when(user={condition(flag)}: single) \
+ when(implementation={vendor(llvm)}: parallel)
+ bar();
+}
+
+// kind(host) scores higher than the condition, so 'parallel' always wins.
+// CHECK-LABEL: define {{.*}}void @_Z18device_kind_secondi(
+// CHECK-NOT: icmp
+// CHECK: call {{.*}}@__kmpc_fork_call(
+// CHECK-NOT: __kmpc_single
+// CHECK: ret void
+void device_kind_second(int flag) {
+#pragma omp metadirective when(user={condition(flag)}: single) \
+ when(device={kind(host)}: parallel)
+ bar();
+}
+
+// score(5) makes the second variant the first one tried.
+// CHECK-LABEL: define {{.*}}void @_Z14score_reordersi(
+// CHECK: br i1 {{.*}}, label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK: call i32 @__kmpc_single(
+// CHECK: [[ELSE]]:
+// CHECK-NEXT: call {{.*}}@__kmpc_fork_call(
+// CHECK-NEXT: br label %[[END:omp.metadirective.end[0-9]*]]
+// CHECK: [[END]]:
+// CHECK-NEXT: ret void
+void score_reorders(int flag) {
+#pragma omp metadirective when(implementation={vendor(llvm)}: parallel) \
+ when(user={condition(flag)}, implementation={vendor(score(5): llvm)}: single)
+ bar();
+}
>From f05ebf1db3a06e2836d3589ce3909f4c192a1b78 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Thu, 5 Nov 2026 09:54:02 -0800
Subject: [PATCH 2/2] match construct selectors
---
clang/include/clang/AST/StmtOpenMP.h | 11 ++-
clang/include/clang/Sema/SemaOpenMP.h | 3 +
clang/lib/AST/StmtOpenMP.cpp | 11 ++-
clang/lib/CodeGen/CGStmtOpenMP.cpp | 3 +-
clang/lib/Parse/ParseOpenMP.cpp | 2 +-
clang/lib/Sema/SemaOpenMP.cpp | 6 +-
clang/lib/Serialization/ASTReaderStmt.cpp | 4 +
clang/lib/Serialization/ASTWriterStmt.cpp | 3 +
.../metadirective_construct_codegen.cpp | 29 +++++++
.../OpenMP/metadirective_runtime_codegen.cpp | 80 +++++++++++++++++++
10 files changed, 146 insertions(+), 6 deletions(-)
create mode 100644 clang/test/OpenMP/metadirective_construct_codegen.cpp
diff --git a/clang/include/clang/AST/StmtOpenMP.h b/clang/include/clang/AST/StmtOpenMP.h
index a990de6d04f74..b7b2b2498a061 100644
--- a/clang/include/clang/AST/StmtOpenMP.h
+++ b/clang/include/clang/AST/StmtOpenMP.h
@@ -6627,6 +6627,9 @@ class OMPMetaDirective final : public OMPExecutableDirective {
/// directive has no 'otherwise' clause.
bool HasImplicitOtherwise = false;
+ /// The OpenMP constructs that enclose the directive.
+ ArrayRef<llvm::omp::TraitProperty> ConstructTraits;
+
OMPMetaDirective(SourceLocation StartLoc, SourceLocation EndLoc)
: OMPExecutableDirective(OMPMetaDirectiveClass,
llvm::omp::OMPD_metadirective, StartLoc,
@@ -6669,12 +6672,15 @@ class OMPMetaDirective final : public OMPExecutableDirective {
}
void setHasImplicitOtherwise(bool V) { HasImplicitOtherwise = V; }
+ void setConstructTraits(const ASTContext &C,
+ ArrayRef<llvm::omp::TraitProperty> Traits);
public:
static OMPMetaDirective *
Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
ArrayRef<Stmt *> Variants, ArrayRef<Expr *> Conditions,
- ArrayRef<OMPTraitInfo *> TraitInfos, bool HasImplicitOtherwise);
+ ArrayRef<OMPTraitInfo *> TraitInfos, bool HasImplicitOtherwise,
+ ArrayRef<llvm::omp::TraitProperty> ConstructTraits);
static OMPMetaDirective *CreateEmpty(const ASTContext &C,
unsigned NumVariants, EmptyShell);
@@ -6685,6 +6691,9 @@ class OMPMetaDirective final : public OMPExecutableDirective {
ArrayRef<OMPTraitInfo *> traitInfos() const {
return getTrailingTraitInfos();
}
+ ArrayRef<llvm::omp::TraitProperty> constructTraits() const {
+ return ConstructTraits;
+ }
child_range children() {
MutableArrayRef<Stmt *> Children = Data->getChildren();
diff --git a/clang/include/clang/Sema/SemaOpenMP.h b/clang/include/clang/Sema/SemaOpenMP.h
index 6750f6a9ce4c2..b5c5c3f9a4acd 100644
--- a/clang/include/clang/Sema/SemaOpenMP.h
+++ b/clang/include/clang/Sema/SemaOpenMP.h
@@ -1525,6 +1525,9 @@ class SemaOpenMP : public SemaBase {
int getOpenMPDeviceNum() const;
+ /// Return the OpenMP constructs enclosing the current location.
+ ArrayRef<llvm::omp::TraitProperty> getConstructTraits();
+
void setOpenMPDeviceNumID(StringRef ID);
enum class OpenMPImpexType {
diff --git a/clang/lib/AST/StmtOpenMP.cpp b/clang/lib/AST/StmtOpenMP.cpp
index 3812b4a060240..6e7ccf5360518 100644
--- a/clang/lib/AST/StmtOpenMP.cpp
+++ b/clang/lib/AST/StmtOpenMP.cpp
@@ -350,7 +350,8 @@ OMPMetaDirective *OMPMetaDirective::allocate(const ASTContext &C,
OMPMetaDirective *OMPMetaDirective::Create(
const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
ArrayRef<Stmt *> Variants, ArrayRef<Expr *> Conditions,
- ArrayRef<OMPTraitInfo *> TraitInfos, bool HasImplicitOtherwise) {
+ ArrayRef<OMPTraitInfo *> TraitInfos, bool HasImplicitOtherwise,
+ ArrayRef<llvm::omp::TraitProperty> ConstructTraits) {
assert(Variants.size() == Conditions.size() &&
Variants.size() == TraitInfos.size() &&
"Expected one condition and one trait info per variant");
@@ -359,9 +360,17 @@ OMPMetaDirective *OMPMetaDirective::Create(
llvm::copy(Conditions, Dir->getTrailingConditions().begin());
llvm::copy(TraitInfos, Dir->getTrailingTraitInfos().begin());
Dir->setHasImplicitOtherwise(HasImplicitOtherwise);
+ Dir->setConstructTraits(C, ConstructTraits);
return Dir;
}
+void OMPMetaDirective::setConstructTraits(
+ const ASTContext &C, ArrayRef<llvm::omp::TraitProperty> Traits) {
+ auto *Mem = C.Allocate<llvm::omp::TraitProperty>(Traits.size());
+ llvm::copy(Traits, Mem);
+ ConstructTraits = {Mem, Traits.size()};
+}
+
OMPMetaDirective *OMPMetaDirective::CreateEmpty(const ASTContext &C,
unsigned NumVariants,
EmptyShell) {
diff --git a/clang/lib/CodeGen/CGStmtOpenMP.cpp b/clang/lib/CodeGen/CGStmtOpenMP.cpp
index e2ecdf7d6f45b..bcfddd7fec95e 100644
--- a/clang/lib/CodeGen/CGStmtOpenMP.cpp
+++ b/clang/lib/CodeGen/CGStmtOpenMP.cpp
@@ -2401,8 +2401,7 @@ void CodeGenFunction::EmitOMPMetaDirective(const OMPMetaDirective &S) {
// Try variants from best to worst match; 'otherwise' runs if none applies.
TargetOMPContext OMPCtx(
getContext(), /*DiagUnknownTrait=*/[](StringRef) {},
- /*CurrentFunctionDecl=*/nullptr,
- /*ConstructTraits=*/{}, /*DeviceNum=*/-1);
+ /*CurrentFunctionDecl=*/nullptr, S.constructTraits(), /*DeviceNum=*/-1);
ArrayRef<Stmt *> Variants = S.variants();
ArrayRef<Expr *> Conditions = S.conditions();
SmallVector<llvm::omp::VariantMatchInfo, 4> VMIs;
diff --git a/clang/lib/Parse/ParseOpenMP.cpp b/clang/lib/Parse/ParseOpenMP.cpp
index f6fab0b00b0b5..00b3b39ff42e6 100644
--- a/clang/lib/Parse/ParseOpenMP.cpp
+++ b/clang/lib/Parse/ParseOpenMP.cpp
@@ -2715,7 +2715,7 @@ StmtResult Parser::ParseOpenMPDeclarativeOrExecutableDirective(
};
TargetOMPContext OMPCtx(ASTContext, std::move(DiagUnknownTrait),
/* CurrentFunctionDecl */ nullptr,
- ArrayRef<llvm::omp::TraitProperty>(),
+ Actions.OpenMP().getConstructTraits(),
Actions.OpenMP().getOpenMPDeviceNum());
// A single match is returned for OpenMP 5.0
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 607ca7a554462..c37926f9f59e7 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -3881,7 +3881,7 @@ StmtResult SemaOpenMP::ActOnOpenMPMetaDirective(
}
return OMPMetaDirective::Create(getASTContext(), StartLoc, EndLoc, Variants,
Conditions, AllTraitInfos,
- HasImplicitOtherwise);
+ HasImplicitOtherwise, getConstructTraits());
}
OMPRequiresDecl *
@@ -19195,6 +19195,10 @@ void SemaOpenMP::setOpenMPDeviceNumID(StringRef ID) { DeviceNumID = ID; }
int SemaOpenMP::getOpenMPDeviceNum() const { return DeviceNum; }
+ArrayRef<llvm::omp::TraitProperty> SemaOpenMP::getConstructTraits() {
+ return DSAStack->getConstructTraits();
+}
+
void SemaOpenMP::ActOnOpenMPDeviceNum(Expr *DeviceNumExpr) {
llvm::APSInt Result;
Expr::EvalResult EvalResult;
diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp
index b09471c13a48e..6cd9b5c8f91c0 100644
--- a/clang/lib/Serialization/ASTReaderStmt.cpp
+++ b/clang/lib/Serialization/ASTReaderStmt.cpp
@@ -2547,6 +2547,10 @@ void ASTStmtReader::VisitOMPMetaDirective(OMPMetaDirective *D) {
D->setHasImplicitOtherwise(Record.readBool());
for (OMPTraitInfo *&TI : D->getTrailingTraitInfos())
TI = Record.readOMPTraitInfo();
+ SmallVector<llvm::omp::TraitProperty, 4> ConstructTraits(Record.readInt());
+ for (llvm::omp::TraitProperty &Trait : ConstructTraits)
+ Trait = llvm::omp::TraitProperty(Record.readInt());
+ D->setConstructTraits(Record.getContext(), ConstructTraits);
}
void ASTStmtReader::VisitOMPParallelDirective(OMPParallelDirective *D) {
diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp
index f91f318426290..1ab9c55d90956 100644
--- a/clang/lib/Serialization/ASTWriterStmt.cpp
+++ b/clang/lib/Serialization/ASTWriterStmt.cpp
@@ -2618,6 +2618,9 @@ void ASTStmtWriter::VisitOMPMetaDirective(OMPMetaDirective *D) {
Record.writeBool(D->hasImplicitOtherwise());
for (const OMPTraitInfo *TI : D->traitInfos())
Record.writeOMPTraitInfo(TI);
+ Record.push_back(D->constructTraits().size());
+ for (llvm::omp::TraitProperty Trait : D->constructTraits())
+ Record.push_back(unsigned(Trait));
Code = serialization::STMT_OMP_META_DIRECTIVE;
}
diff --git a/clang/test/OpenMP/metadirective_construct_codegen.cpp b/clang/test/OpenMP/metadirective_construct_codegen.cpp
new file mode 100644
index 0000000000000..7707780b01078
--- /dev/null
+++ b/clang/test/OpenMP/metadirective_construct_codegen.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=52 -x c++ \
+// RUN: -triple x86_64-unknown-linux -emit-llvm %s -o - | FileCheck %s
+// expected-no-diagnostics
+
+void bar();
+
+// construct={parallel} matches inside the parallel region.
+// CHECK-LABEL: define internal void @_Z17compile_time_pathv.omp_outlined(
+// CHECK-NOT: __kmpc_masked
+// CHECK: call i32 @__kmpc_single(
+// CHECK-NOT: __kmpc_masked
+// CHECK: ret void
+void compile_time_path() {
+#pragma omp parallel
+ {
+#pragma omp metadirective when(construct={parallel}: single) otherwise(masked)
+ bar();
+ }
+}
+
+// Outside a parallel region, construct={parallel} does not match.
+// CHECK-LABEL: define {{.*}}void @_Z9no_regionv(
+// CHECK-NOT: __kmpc_single
+// CHECK: call i32 @__kmpc_masked(
+// CHECK: ret void
+void no_region() {
+#pragma omp metadirective when(construct={parallel}: single) otherwise(masked)
+ bar();
+}
diff --git a/clang/test/OpenMP/metadirective_runtime_codegen.cpp b/clang/test/OpenMP/metadirective_runtime_codegen.cpp
index 26de70ff9cd27..f7ab66ed8d27a 100644
--- a/clang/test/OpenMP/metadirective_runtime_codegen.cpp
+++ b/clang/test/OpenMP/metadirective_runtime_codegen.cpp
@@ -136,3 +136,83 @@ void score_reorders(int flag) {
when(user={condition(flag)}, implementation={vendor(score(5): llvm)}: single)
bar();
}
+
+void baz(int);
+
+// Loop variants: each one runs its own loop.
+// CHECK-LABEL: define {{.*}}void @_Z13loop_variantsii(
+// CHECK: br i1 {{.*}}, label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK-NEXT: call {{.*}}@__kmpc_fork_call({{.*}}@_Z13loop_variantsii.omp_outlined
+// CHECK: [[ELSE]]:
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call void @_Z3bazi(
+// CHECK: call void @__kmpc_for_static_fini(
+// CHECK: call void @__kmpc_barrier(
+// CHECK: define internal void @_Z13loop_variantsii.omp_outlined(
+// CHECK: call void @__kmpc_for_static_init_4(
+// CHECK: call void @_Z3bazi(
+// CHECK: call void @__kmpc_for_static_fini(
+void loop_variants(int flag, int n) {
+#pragma omp metadirective when(user={condition(flag)}: parallel for) otherwise(for)
+ for (int i = 0; i < n; ++i)
+ baz(i);
+}
+
+// A metadirective inside a variant of another one. The empty 'otherwise()'
+// runs the block, so the inner metadirective runs on both paths.
+// CHECK-LABEL: define {{.*}}void @_Z6nestedii(
+// CHECK: br i1 {{.*}}, label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK-NEXT: call {{.*}}@__kmpc_fork_call({{.*}}@_Z6nestedii.omp_outlined, ptr %b.addr)
+// CHECK: [[ELSE]]:
+// CHECK-NEXT: {{%.*}} = load i32, ptr %b.addr
+// CHECK-NEXT: {{%.*}} = icmp ne i32
+// CHECK-NEXT: br i1 {{.*}}, label %[[IWHEN:omp.metadirective.when[0-9]*]], label %[[IELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[IWHEN]]:
+// CHECK-NEXT: call i32 @__kmpc_single(
+// CHECK: [[IELSE]]:
+// CHECK-NEXT: call i32 @__kmpc_masked(
+// CHECK: define internal void @_Z6nestedii.omp_outlined(
+// CHECK: br i1 {{.*}}, label %[[OWHEN:omp.metadirective.when[0-9]*]], label %[[OELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[OWHEN]]:
+// CHECK: call i32 @__kmpc_single(
+// CHECK: [[OELSE]]:
+// CHECK: call i32 @__kmpc_masked(
+void nested(int a, int b) {
+#pragma omp metadirective when(user={condition(a)}: parallel) otherwise()
+ {
+#pragma omp metadirective when(user={condition(b)}: single) otherwise(masked)
+ bar();
+ }
+}
+
+// construct={parallel} matches inside the parallel region and scores higher,
+// so 'single' is always selected.
+// CHECK-LABEL: define internal void @_Z12runtime_pathi.omp_outlined(
+// CHECK-NOT: __kmpc_masked
+// CHECK: call i32 @__kmpc_single(
+// CHECK-NOT: __kmpc_masked
+// CHECK: ret void
+void runtime_path(int flag) {
+#pragma omp parallel
+ {
+#pragma omp metadirective when(construct={parallel}: single) \
+ when(user={condition(flag)}: masked)
+ bar();
+ }
+}
+
+// Instantiated template. Kept last: the instantiation may be emitted at the
+// end of the file.
+// CHECK-LABEL: define {{.*}}void @_Z4tmplIiEvT_(
+// CHECK: br i1 {{.*}}, label %[[WHEN:omp.metadirective.when[0-9]*]], label %[[ELSE:omp.metadirective.else[0-9]*]]
+// CHECK: [[WHEN]]:
+// CHECK-NEXT: call {{.*}}@__kmpc_fork_call({{.*}}@_Z4tmplIiEvT_.omp_outlined
+// CHECK: [[ELSE]]:
+// CHECK-NEXT: call i32 @__kmpc_single(
+template <typename T> void tmpl(T flag) {
+#pragma omp metadirective when(user={condition(flag)}: parallel) otherwise(single)
+ bar();
+}
+void inst() { tmpl(1); }
More information about the cfe-commits
mailing list