Skip to content

Commit 70d3ddb

Browse files
authored
[Frontend][OpenMP] Add functions for checking construct type (#87258)
Implement helper functions to identify leaf, composite, and combined constructs.
1 parent 31af5e9 commit 70d3ddb

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

llvm/include/llvm/Frontend/OpenMP/OMP.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
namespace llvm::omp {
2121
ArrayRef<Directive> getLeafConstructs(Directive D);
2222
Directive getCompoundConstruct(ArrayRef<Directive> Parts);
23+
24+
bool isLeafConstruct(Directive D);
25+
bool isCompositeConstruct(Directive D);
26+
bool isCombinedConstruct(Directive D);
2327
} // namespace llvm::omp
2428

2529
#endif // LLVM_FRONTEND_OPENMP_OMP_H

llvm/lib/Frontend/OpenMP/OMP.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,29 @@ Directive getCompoundConstruct(ArrayRef<Directive> Parts) {
8484
return Found;
8585
return OMPD_unknown;
8686
}
87+
88+
bool isLeafConstruct(Directive D) { return getLeafConstructs(D).empty(); }
89+
90+
bool isCompositeConstruct(Directive D) {
91+
// OpenMP Spec 5.2: [17.3, 8-9]
92+
// If directive-name-A and directive-name-B both correspond to loop-
93+
// associated constructs then directive-name is a composite construct
94+
llvm::ArrayRef<Directive> Leafs{getLeafConstructs(D)};
95+
if (Leafs.empty())
96+
return false;
97+
if (getDirectiveAssociation(Leafs.front()) != Association::Loop)
98+
return false;
99+
100+
size_t numLoopConstructs =
101+
llvm::count_if(Leafs.drop_front(), [](Directive L) {
102+
return getDirectiveAssociation(L) == Association::Loop;
103+
});
104+
return numLoopConstructs != 0;
105+
}
106+
107+
bool isCombinedConstruct(Directive D) {
108+
// OpenMP Spec 5.2: [17.3, 9-10]
109+
// Otherwise directive-name is a combined construct.
110+
return !getLeafConstructs(D).empty() && !isCompositeConstruct(D);
111+
}
87112
} // namespace llvm::omp

llvm/unittests/Frontend/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ add_llvm_unittest(LLVMFrontendTests
1414
OpenMPContextTest.cpp
1515
OpenMPIRBuilderTest.cpp
1616
OpenMPParsingTest.cpp
17-
OpenMPComposeTest.cpp
17+
OpenMPCompositionTest.cpp
1818

1919
DEPENDS
2020
acc_gen

llvm/unittests/Frontend/OpenMPComposeTest.cpp renamed to llvm/unittests/Frontend/OpenMPCompositionTest.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- llvm/unittests/Frontend/OpenMPComposeTest.cpp ----------------------===//
1+
//===- llvm/unittests/Frontend/OpenMPCompositionTest.cpp ------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -39,3 +39,30 @@ TEST(Composition, GetCompoundConstruct) {
3939
Directive C7 = getCompoundConstruct({OMPD_do, OMPD_simd});
4040
ASSERT_EQ(C7, OMPD_do_simd); // Make sure it's not OMPD_end_do_simd
4141
}
42+
43+
TEST(Composition, IsLeafConstruct) {
44+
ASSERT_TRUE(isLeafConstruct(OMPD_loop));
45+
ASSERT_TRUE(isLeafConstruct(OMPD_teams));
46+
ASSERT_FALSE(isLeafConstruct(OMPD_for_simd));
47+
ASSERT_FALSE(isLeafConstruct(OMPD_distribute_simd));
48+
ASSERT_FALSE(isLeafConstruct(OMPD_parallel_for));
49+
}
50+
51+
TEST(Composition, IsCompositeConstruct) {
52+
ASSERT_TRUE(isCompositeConstruct(OMPD_distribute_simd));
53+
ASSERT_FALSE(isCompositeConstruct(OMPD_for));
54+
ASSERT_TRUE(isCompositeConstruct(OMPD_for_simd));
55+
// directive-name-A = "parallel", directive-name-B = "for simd",
56+
// only directive-name-B is loop-associated, so this is not a
57+
// composite construct, even though "for simd" is.
58+
ASSERT_FALSE(isCompositeConstruct(OMPD_parallel_for_simd));
59+
}
60+
61+
TEST(Composition, IsCombinedConstruct) {
62+
// "parallel for simd" is a combined construct, see comment in
63+
// IsCompositeConstruct.
64+
ASSERT_TRUE(isCombinedConstruct(OMPD_parallel_for_simd));
65+
ASSERT_FALSE(isCombinedConstruct(OMPD_for_simd));
66+
ASSERT_TRUE(isCombinedConstruct(OMPD_parallel_for));
67+
ASSERT_FALSE(isCombinedConstruct(OMPD_parallel));
68+
}

0 commit comments

Comments
 (0)