1 //===- unittest/IR/OpenMPContextTest.cpp - OpenMP Context handling tests --===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/Triple.h"
10 #include "llvm/Frontend/OpenMP/OMPConstants.h"
11 #include "llvm/Frontend/OpenMP/OMPContext.h"
12 #include "gtest/gtest.h"
19 class OpenMPContextTest
: public testing::Test
{
21 void SetUp() override
{}
23 void TearDown() override
{}
26 TEST_F(OpenMPContextTest
, RoundTripAndAssociation
) {
27 #define OMP_TRAIT_SET(Enum, Str) \
28 EXPECT_EQ(TraitSet::Enum, \
29 getOpenMPContextTraitSetKind( \
30 getOpenMPContextTraitSetName(TraitSet::Enum))); \
32 getOpenMPContextTraitSetName(getOpenMPContextTraitSetKind(Str)));
33 #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, RequiresProperty) \
34 EXPECT_EQ(TraitSelector::Enum, \
35 getOpenMPContextTraitSelectorKind( \
36 getOpenMPContextTraitSelectorName(TraitSelector::Enum))); \
37 EXPECT_EQ(Str, getOpenMPContextTraitSelectorName( \
38 getOpenMPContextTraitSelectorKind(Str)));
39 #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
40 EXPECT_EQ(TraitProperty::Enum, \
41 getOpenMPContextTraitPropertyKind( \
42 TraitSet::TraitSetEnum, TraitSelector::TraitSelectorEnum, \
43 getOpenMPContextTraitPropertyName(TraitProperty::Enum, Str))); \
44 EXPECT_EQ(Str, getOpenMPContextTraitPropertyName( \
45 getOpenMPContextTraitPropertyKind( \
46 TraitSet::TraitSetEnum, \
47 TraitSelector::TraitSelectorEnum, Str), \
49 EXPECT_EQ(TraitSet::TraitSetEnum, \
50 getOpenMPContextTraitSetForProperty(TraitProperty::Enum)); \
51 EXPECT_EQ(TraitSelector::TraitSelectorEnum, \
52 getOpenMPContextTraitSelectorForProperty(TraitProperty::Enum));
53 #include "llvm/Frontend/OpenMP/OMPKinds.def"
56 TEST_F(OpenMPContextTest
, ValidNesting
) {
57 bool AllowsTraitScore
, ReqProperty
;
58 #define OMP_TRAIT_SELECTOR(Enum, TraitSetEnum, Str, RequiresProperty) \
59 EXPECT_TRUE(isValidTraitSelectorForTraitSet(TraitSelector::Enum, \
60 TraitSet::TraitSetEnum, \
61 AllowsTraitScore, ReqProperty)); \
62 EXPECT_EQ(RequiresProperty, ReqProperty);
63 #define OMP_TRAIT_PROPERTY(Enum, TraitSetEnum, TraitSelectorEnum, Str) \
64 EXPECT_TRUE(isValidTraitPropertyForTraitSetAndSelector( \
65 TraitProperty::Enum, TraitSelector::TraitSelectorEnum, \
66 TraitSet::TraitSetEnum));
67 #include "llvm/Frontend/OpenMP/OMPKinds.def"
70 TEST_F(OpenMPContextTest
, ApplicabilityNonConstruct
) {
71 OMPContext
HostLinux(false, Triple("x86_64-unknown-linux"));
72 OMPContext
DeviceLinux(true, Triple("x86_64-unknown-linux"));
73 OMPContext
HostNVPTX(false, Triple("nvptx64-nvidia-cuda"));
74 OMPContext
DeviceNVPTX(true, Triple("nvptx64-nvidia-cuda"));
76 VariantMatchInfo Empty
;
77 EXPECT_TRUE(isVariantApplicableInContext(Empty
, HostLinux
));
78 EXPECT_TRUE(isVariantApplicableInContext(Empty
, DeviceLinux
));
79 EXPECT_TRUE(isVariantApplicableInContext(Empty
, HostNVPTX
));
80 EXPECT_TRUE(isVariantApplicableInContext(Empty
, DeviceNVPTX
));
82 VariantMatchInfo UserCondFalse
;
83 UserCondFalse
.addTrait(TraitProperty::user_condition_false
, "");
84 EXPECT_FALSE(isVariantApplicableInContext(UserCondFalse
, HostLinux
));
85 EXPECT_FALSE(isVariantApplicableInContext(UserCondFalse
, DeviceLinux
));
86 EXPECT_FALSE(isVariantApplicableInContext(UserCondFalse
, HostNVPTX
));
87 EXPECT_FALSE(isVariantApplicableInContext(UserCondFalse
, DeviceNVPTX
));
89 VariantMatchInfo DeviceArchArm
;
90 DeviceArchArm
.addTrait(TraitProperty::device_arch_arm
, "");
91 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArm
, HostLinux
));
92 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArm
, DeviceLinux
));
93 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArm
, HostNVPTX
));
94 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArm
, DeviceNVPTX
));
96 VariantMatchInfo LLVMHostUserCondTrue
;
97 LLVMHostUserCondTrue
.addTrait(TraitProperty::implementation_vendor_llvm
, "");
98 LLVMHostUserCondTrue
.addTrait(TraitProperty::device_kind_host
, "");
99 LLVMHostUserCondTrue
.addTrait(TraitProperty::device_kind_any
, "");
100 LLVMHostUserCondTrue
.addTrait(TraitProperty::user_condition_true
, "");
101 EXPECT_TRUE(isVariantApplicableInContext(LLVMHostUserCondTrue
, HostLinux
));
102 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrue
, DeviceLinux
));
103 EXPECT_TRUE(isVariantApplicableInContext(LLVMHostUserCondTrue
, HostNVPTX
));
104 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrue
, DeviceNVPTX
));
106 VariantMatchInfo LLVMHostUserCondTrueCPU
= LLVMHostUserCondTrue
;
107 LLVMHostUserCondTrueCPU
.addTrait(TraitProperty::device_kind_cpu
, "");
108 EXPECT_TRUE(isVariantApplicableInContext(LLVMHostUserCondTrueCPU
, HostLinux
));
110 isVariantApplicableInContext(LLVMHostUserCondTrueCPU
, DeviceLinux
));
112 isVariantApplicableInContext(LLVMHostUserCondTrueCPU
, HostNVPTX
));
114 isVariantApplicableInContext(LLVMHostUserCondTrueCPU
, DeviceNVPTX
));
116 VariantMatchInfo GPU
;
117 GPU
.addTrait(TraitProperty::device_kind_gpu
, "");
118 EXPECT_FALSE(isVariantApplicableInContext(GPU
, HostLinux
));
119 EXPECT_FALSE(isVariantApplicableInContext(GPU
, DeviceLinux
));
120 EXPECT_TRUE(isVariantApplicableInContext(GPU
, HostNVPTX
));
121 EXPECT_TRUE(isVariantApplicableInContext(GPU
, DeviceNVPTX
));
123 VariantMatchInfo NoHost
;
124 NoHost
.addTrait(TraitProperty::device_kind_nohost
, "");
125 EXPECT_FALSE(isVariantApplicableInContext(NoHost
, HostLinux
));
126 EXPECT_TRUE(isVariantApplicableInContext(NoHost
, DeviceLinux
));
127 EXPECT_FALSE(isVariantApplicableInContext(NoHost
, HostNVPTX
));
128 EXPECT_TRUE(isVariantApplicableInContext(NoHost
, DeviceNVPTX
));
131 TEST_F(OpenMPContextTest
, ApplicabilityAllTraits
) {
132 OMPContext
HostLinuxParallelParallel(false, Triple("x86_64-unknown-linux"));
133 HostLinuxParallelParallel
.addTrait(
134 TraitProperty::construct_parallel_parallel
);
135 HostLinuxParallelParallel
.addTrait(
136 TraitProperty::construct_parallel_parallel
);
137 OMPContext
DeviceLinuxTargetParallel(true, Triple("x86_64-unknown-linux"));
138 DeviceLinuxTargetParallel
.addTrait(TraitProperty::construct_target_target
);
139 DeviceLinuxTargetParallel
.addTrait(
140 TraitProperty::construct_parallel_parallel
);
141 OMPContext
HostNVPTXFor(false, Triple("nvptx64-nvidia-cuda"));
142 HostNVPTXFor
.addTrait(TraitProperty::construct_for_for
);
143 OMPContext
DeviceNVPTXTargetTeamsParallel(true,
144 Triple("nvptx64-nvidia-cuda"));
145 DeviceNVPTXTargetTeamsParallel
.addTrait(
146 TraitProperty::construct_target_target
);
147 DeviceNVPTXTargetTeamsParallel
.addTrait(TraitProperty::construct_teams_teams
);
148 DeviceNVPTXTargetTeamsParallel
.addTrait(
149 TraitProperty::construct_parallel_parallel
);
151 { // non-construct variants
152 VariantMatchInfo Empty
;
153 EXPECT_TRUE(isVariantApplicableInContext(Empty
, HostLinuxParallelParallel
));
154 EXPECT_TRUE(isVariantApplicableInContext(Empty
, DeviceLinuxTargetParallel
));
155 EXPECT_TRUE(isVariantApplicableInContext(Empty
, HostNVPTXFor
));
157 isVariantApplicableInContext(Empty
, DeviceNVPTXTargetTeamsParallel
));
159 VariantMatchInfo UserCondFalse
;
160 UserCondFalse
.addTrait(TraitProperty::user_condition_false
, "");
162 isVariantApplicableInContext(UserCondFalse
, HostLinuxParallelParallel
));
164 isVariantApplicableInContext(UserCondFalse
, DeviceLinuxTargetParallel
));
165 EXPECT_FALSE(isVariantApplicableInContext(UserCondFalse
, HostNVPTXFor
));
166 EXPECT_FALSE(isVariantApplicableInContext(UserCondFalse
,
167 DeviceNVPTXTargetTeamsParallel
));
169 VariantMatchInfo DeviceArchArm
;
170 DeviceArchArm
.addTrait(TraitProperty::device_arch_arm
, "");
172 isVariantApplicableInContext(DeviceArchArm
, HostLinuxParallelParallel
));
174 isVariantApplicableInContext(DeviceArchArm
, DeviceLinuxTargetParallel
));
175 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArm
, HostNVPTXFor
));
176 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArm
,
177 DeviceNVPTXTargetTeamsParallel
));
179 APInt
Score(32, 1000);
180 VariantMatchInfo LLVMHostUserCondTrue
;
181 LLVMHostUserCondTrue
.addTrait(TraitProperty::implementation_vendor_llvm
,
183 LLVMHostUserCondTrue
.addTrait(TraitProperty::device_kind_host
, "");
184 LLVMHostUserCondTrue
.addTrait(TraitProperty::device_kind_any
, "");
185 LLVMHostUserCondTrue
.addTrait(TraitProperty::user_condition_true
, "",
187 EXPECT_TRUE(isVariantApplicableInContext(LLVMHostUserCondTrue
,
188 HostLinuxParallelParallel
));
189 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrue
,
190 DeviceLinuxTargetParallel
));
192 isVariantApplicableInContext(LLVMHostUserCondTrue
, HostNVPTXFor
));
193 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrue
,
194 DeviceNVPTXTargetTeamsParallel
));
196 VariantMatchInfo LLVMHostUserCondTrueCPU
= LLVMHostUserCondTrue
;
197 LLVMHostUserCondTrueCPU
.addTrait(TraitProperty::device_kind_cpu
, "");
198 EXPECT_TRUE(isVariantApplicableInContext(LLVMHostUserCondTrueCPU
,
199 HostLinuxParallelParallel
));
200 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrueCPU
,
201 DeviceLinuxTargetParallel
));
203 isVariantApplicableInContext(LLVMHostUserCondTrueCPU
, HostNVPTXFor
));
204 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrueCPU
,
205 DeviceNVPTXTargetTeamsParallel
));
207 VariantMatchInfo GPU
;
208 GPU
.addTrait(TraitProperty::device_kind_gpu
, "");
209 EXPECT_FALSE(isVariantApplicableInContext(GPU
, HostLinuxParallelParallel
));
210 EXPECT_FALSE(isVariantApplicableInContext(GPU
, DeviceLinuxTargetParallel
));
211 EXPECT_TRUE(isVariantApplicableInContext(GPU
, HostNVPTXFor
));
213 isVariantApplicableInContext(GPU
, DeviceNVPTXTargetTeamsParallel
));
215 VariantMatchInfo NoHost
;
216 NoHost
.addTrait(TraitProperty::device_kind_nohost
, "");
218 isVariantApplicableInContext(NoHost
, HostLinuxParallelParallel
));
220 isVariantApplicableInContext(NoHost
, DeviceLinuxTargetParallel
));
221 EXPECT_FALSE(isVariantApplicableInContext(NoHost
, HostNVPTXFor
));
223 isVariantApplicableInContext(NoHost
, DeviceNVPTXTargetTeamsParallel
));
225 { // variants with all sets
226 VariantMatchInfo DeviceArchArmParallel
;
227 DeviceArchArmParallel
.addTrait(TraitProperty::construct_parallel_parallel
,
229 DeviceArchArmParallel
.addTrait(TraitProperty::device_arch_arm
, "");
230 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArmParallel
,
231 HostLinuxParallelParallel
));
232 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArmParallel
,
233 DeviceLinuxTargetParallel
));
235 isVariantApplicableInContext(DeviceArchArmParallel
, HostNVPTXFor
));
236 EXPECT_FALSE(isVariantApplicableInContext(DeviceArchArmParallel
,
237 DeviceNVPTXTargetTeamsParallel
));
239 VariantMatchInfo LLVMHostUserCondTrueParallel
;
240 LLVMHostUserCondTrueParallel
.addTrait(
241 TraitProperty::implementation_vendor_llvm
, "");
242 LLVMHostUserCondTrueParallel
.addTrait(TraitProperty::device_kind_host
, "");
243 LLVMHostUserCondTrueParallel
.addTrait(TraitProperty::device_kind_any
, "");
244 LLVMHostUserCondTrueParallel
.addTrait(TraitProperty::user_condition_true
,
246 LLVMHostUserCondTrueParallel
.addTrait(
247 TraitProperty::construct_parallel_parallel
, "");
248 EXPECT_TRUE(isVariantApplicableInContext(LLVMHostUserCondTrueParallel
,
249 HostLinuxParallelParallel
));
250 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrueParallel
,
251 DeviceLinuxTargetParallel
));
252 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrueParallel
,
254 EXPECT_FALSE(isVariantApplicableInContext(LLVMHostUserCondTrueParallel
,
255 DeviceNVPTXTargetTeamsParallel
));
257 VariantMatchInfo LLVMHostUserCondTrueParallelParallel
=
258 LLVMHostUserCondTrueParallel
;
259 LLVMHostUserCondTrueParallelParallel
.addTrait(
260 TraitProperty::construct_parallel_parallel
, "");
261 EXPECT_TRUE(isVariantApplicableInContext(
262 LLVMHostUserCondTrueParallelParallel
, HostLinuxParallelParallel
));
263 EXPECT_FALSE(isVariantApplicableInContext(
264 LLVMHostUserCondTrueParallelParallel
, DeviceLinuxTargetParallel
));
265 EXPECT_FALSE(isVariantApplicableInContext(
266 LLVMHostUserCondTrueParallelParallel
, HostNVPTXFor
));
267 EXPECT_FALSE(isVariantApplicableInContext(
268 LLVMHostUserCondTrueParallelParallel
, DeviceNVPTXTargetTeamsParallel
));
270 VariantMatchInfo LLVMHostUserCondTrueParallelParallelParallel
=
271 LLVMHostUserCondTrueParallelParallel
;
272 LLVMHostUserCondTrueParallelParallelParallel
.addTrait(
273 TraitProperty::construct_parallel_parallel
, "");
274 EXPECT_FALSE(isVariantApplicableInContext(
275 LLVMHostUserCondTrueParallelParallelParallel
,
276 HostLinuxParallelParallel
));
277 EXPECT_FALSE(isVariantApplicableInContext(
278 LLVMHostUserCondTrueParallelParallelParallel
,
279 DeviceLinuxTargetParallel
));
280 EXPECT_FALSE(isVariantApplicableInContext(
281 LLVMHostUserCondTrueParallelParallelParallel
, HostNVPTXFor
));
282 EXPECT_FALSE(isVariantApplicableInContext(
283 LLVMHostUserCondTrueParallelParallelParallel
,
284 DeviceNVPTXTargetTeamsParallel
));
286 VariantMatchInfo GPUTargetTeams
;
287 GPUTargetTeams
.addTrait(TraitProperty::construct_target_target
, "");
288 GPUTargetTeams
.addTrait(TraitProperty::construct_teams_teams
, "");
289 GPUTargetTeams
.addTrait(TraitProperty::device_kind_gpu
, "");
290 EXPECT_FALSE(isVariantApplicableInContext(GPUTargetTeams
,
291 HostLinuxParallelParallel
));
292 EXPECT_FALSE(isVariantApplicableInContext(GPUTargetTeams
,
293 DeviceLinuxTargetParallel
));
294 EXPECT_FALSE(isVariantApplicableInContext(GPUTargetTeams
, HostNVPTXFor
));
295 EXPECT_TRUE(isVariantApplicableInContext(GPUTargetTeams
,
296 DeviceNVPTXTargetTeamsParallel
));
298 VariantMatchInfo GPUTargetParallel
;
299 GPUTargetParallel
.addTrait(TraitProperty::construct_target_target
, "");
300 GPUTargetParallel
.addTrait(TraitProperty::construct_parallel_parallel
, "");
301 GPUTargetParallel
.addTrait(TraitProperty::device_kind_gpu
, "");
302 EXPECT_FALSE(isVariantApplicableInContext(GPUTargetParallel
,
303 HostLinuxParallelParallel
));
304 EXPECT_FALSE(isVariantApplicableInContext(GPUTargetParallel
,
305 DeviceLinuxTargetParallel
));
306 EXPECT_FALSE(isVariantApplicableInContext(GPUTargetParallel
, HostNVPTXFor
));
307 EXPECT_TRUE(isVariantApplicableInContext(GPUTargetParallel
,
308 DeviceNVPTXTargetTeamsParallel
));
312 TEST_F(OpenMPContextTest
, ScoringSimple
) {
313 // TODO: Add scoring tests (via getBestVariantMatchForContext).