aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Frontend/OpenMPDecompositionTest.cpp
blob: 6189d0954891b490eaf1079b53cbbb0a9372a7d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
//===- llvm/unittests/Frontend/OpenMPDecompositionTest.cpp ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Frontend/OpenMP/ClauseT.h"
#include "llvm/Frontend/OpenMP/ConstructDecompositionT.h"
#include "llvm/Frontend/OpenMP/OMP.h"
#include "gtest/gtest.h"

#include <iterator>
#include <optional>
#include <sstream>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>

// The actual tests start at comment "--- Test" below.

// Create simple instantiations of all clauses to allow manual construction
// of clauses, and implement emitting of a directive with clauses to a string.
//
// The tests then follow the pattern
// 1. Create a list of clauses.
// 2. Pass them, together with a construct, to the decomposition class.
// 3. Extract individual resulting leaf constructs with clauses applied
//    to them.
// 4. Convert them to strings and compare with expected outputs.

namespace omp {
struct TypeTy {}; // placeholder
struct ExprTy {}; // placeholder
using IdTy = std::string;
} // namespace omp

namespace tomp::type {
template <> struct ObjectT<omp::IdTy, omp::ExprTy> {
  const omp::IdTy &id() const { return name; }
  const std::optional<omp::ExprTy> ref() const { return omp::ExprTy{}; }

  omp::IdTy name;
};
} // namespace tomp::type

namespace omp {
template <typename ElemTy> using List = tomp::type::ListT<ElemTy>;

using Object = tomp::ObjectT<IdTy, ExprTy>;

namespace clause {
using DefinedOperator = tomp::type::DefinedOperatorT<IdTy, ExprTy>;
using ProcedureDesignator = tomp::type::ProcedureDesignatorT<IdTy, ExprTy>;
using ReductionOperator = tomp::type::ReductionIdentifierT<IdTy, ExprTy>;

using AcqRel = tomp::clause::AcqRelT<TypeTy, IdTy, ExprTy>;
using Acquire = tomp::clause::AcquireT<TypeTy, IdTy, ExprTy>;
using AdjustArgs = tomp::clause::AdjustArgsT<TypeTy, IdTy, ExprTy>;
using Affinity = tomp::clause::AffinityT<TypeTy, IdTy, ExprTy>;
using Aligned = tomp::clause::AlignedT<TypeTy, IdTy, ExprTy>;
using Align = tomp::clause::AlignT<TypeTy, IdTy, ExprTy>;
using Allocate = tomp::clause::AllocateT<TypeTy, IdTy, ExprTy>;
using Allocator = tomp::clause::AllocatorT<TypeTy, IdTy, ExprTy>;
using AppendArgs = tomp::clause::AppendArgsT<TypeTy, IdTy, ExprTy>;
using AtomicDefaultMemOrder =
    tomp::clause::AtomicDefaultMemOrderT<TypeTy, IdTy, ExprTy>;
using At = tomp::clause::AtT<TypeTy, IdTy, ExprTy>;
using Bind = tomp::clause::BindT<TypeTy, IdTy, ExprTy>;
using Capture = tomp::clause::CaptureT<TypeTy, IdTy, ExprTy>;
using Collapse = tomp::clause::CollapseT<TypeTy, IdTy, ExprTy>;
using Compare = tomp::clause::CompareT<TypeTy, IdTy, ExprTy>;
using Copyin = tomp::clause::CopyinT<TypeTy, IdTy, ExprTy>;
using Copyprivate = tomp::clause::CopyprivateT<TypeTy, IdTy, ExprTy>;
using Defaultmap = tomp::clause::DefaultmapT<TypeTy, IdTy, ExprTy>;
using Default = tomp::clause::DefaultT<TypeTy, IdTy, ExprTy>;
using Depend = tomp::clause::DependT<TypeTy, IdTy, ExprTy>;
using Destroy = tomp::clause::DestroyT<TypeTy, IdTy, ExprTy>;
using Detach = tomp::clause::DetachT<TypeTy, IdTy, ExprTy>;
using Device = tomp::clause::DeviceT<TypeTy, IdTy, ExprTy>;
using DeviceType = tomp::clause::DeviceTypeT<TypeTy, IdTy, ExprTy>;
using DistSchedule = tomp::clause::DistScheduleT<TypeTy, IdTy, ExprTy>;
using Doacross = tomp::clause::DoacrossT<TypeTy, IdTy, ExprTy>;
using DynamicAllocators =
    tomp::clause::DynamicAllocatorsT<TypeTy, IdTy, ExprTy>;
using Enter = tomp::clause::EnterT<TypeTy, IdTy, ExprTy>;
using Exclusive = tomp::clause::ExclusiveT<TypeTy, IdTy, ExprTy>;
using Fail = tomp::clause::FailT<TypeTy, IdTy, ExprTy>;
using Filter = tomp::clause::FilterT<TypeTy, IdTy, ExprTy>;
using Final = tomp::clause::FinalT<TypeTy, IdTy, ExprTy>;
using Firstprivate = tomp::clause::FirstprivateT<TypeTy, IdTy, ExprTy>;
using From = tomp::clause::FromT<TypeTy, IdTy, ExprTy>;
using Full = tomp::clause::FullT<TypeTy, IdTy, ExprTy>;
using Grainsize = tomp::clause::GrainsizeT<TypeTy, IdTy, ExprTy>;
using HasDeviceAddr = tomp::clause::HasDeviceAddrT<TypeTy, IdTy, ExprTy>;
using Hint = tomp::clause::HintT<TypeTy, IdTy, ExprTy>;
using If = tomp::clause::IfT<TypeTy, IdTy, ExprTy>;
using Inbranch = tomp::clause::InbranchT<TypeTy, IdTy, ExprTy>;
using Inclusive = tomp::clause::InclusiveT<TypeTy, IdTy, ExprTy>;
using Indirect = tomp::clause::IndirectT<TypeTy, IdTy, ExprTy>;
using Init = tomp::clause::InitT<TypeTy, IdTy, ExprTy>;
using InReduction = tomp::clause::InReductionT<TypeTy, IdTy, ExprTy>;
using IsDevicePtr = tomp::clause::IsDevicePtrT<TypeTy, IdTy, ExprTy>;
using Lastprivate = tomp::clause::LastprivateT<TypeTy, IdTy, ExprTy>;
using Linear = tomp::clause::LinearT<TypeTy, IdTy, ExprTy>;
using Link = tomp::clause::LinkT<TypeTy, IdTy, ExprTy>;
using Map = tomp::clause::MapT<TypeTy, IdTy, ExprTy>;
using Match = tomp::clause::MatchT<TypeTy, IdTy, ExprTy>;
using Mergeable = tomp::clause::MergeableT<TypeTy, IdTy, ExprTy>;
using Message = tomp::clause::MessageT<TypeTy, IdTy, ExprTy>;
using Nocontext = tomp::clause::NocontextT<TypeTy, IdTy, ExprTy>;
using Nogroup = tomp::clause::NogroupT<TypeTy, IdTy, ExprTy>;
using Nontemporal = tomp::clause::NontemporalT<TypeTy, IdTy, ExprTy>;
using Notinbranch = tomp::clause::NotinbranchT<TypeTy, IdTy, ExprTy>;
using Novariants = tomp::clause::NovariantsT<TypeTy, IdTy, ExprTy>;
using Nowait = tomp::clause::NowaitT<TypeTy, IdTy, ExprTy>;
using NumTasks = tomp::clause::NumTasksT<TypeTy, IdTy, ExprTy>;
using NumTeams = tomp::clause::NumTeamsT<TypeTy, IdTy, ExprTy>;
using NumThreads = tomp::clause::NumThreadsT<TypeTy, IdTy, ExprTy>;
using OmpxAttribute = tomp::clause::OmpxAttributeT<TypeTy, IdTy, ExprTy>;
using OmpxBare = tomp::clause::OmpxBareT<TypeTy, IdTy, ExprTy>;
using OmpxDynCgroupMem = tomp::clause::OmpxDynCgroupMemT<TypeTy, IdTy, ExprTy>;
using Ordered = tomp::clause::OrderedT<TypeTy, IdTy, ExprTy>;
using Order = tomp::clause::OrderT<TypeTy, IdTy, ExprTy>;
using Partial = tomp::clause::PartialT<TypeTy, IdTy, ExprTy>;
using Priority = tomp::clause::PriorityT<TypeTy, IdTy, ExprTy>;
using Private = tomp::clause::PrivateT<TypeTy, IdTy, ExprTy>;
using ProcBind = tomp::clause::ProcBindT<TypeTy, IdTy, ExprTy>;
using Read = tomp::clause::ReadT<TypeTy, IdTy, ExprTy>;
using Reduction = tomp::clause::ReductionT<TypeTy, IdTy, ExprTy>;
using Relaxed = tomp::clause::RelaxedT<TypeTy, IdTy, ExprTy>;
using Release = tomp::clause::ReleaseT<TypeTy, IdTy, ExprTy>;
using ReverseOffload = tomp::clause::ReverseOffloadT<TypeTy, IdTy, ExprTy>;
using Safelen = tomp::clause::SafelenT<TypeTy, IdTy, ExprTy>;
using Schedule = tomp::clause::ScheduleT<TypeTy, IdTy, ExprTy>;
using SeqCst = tomp::clause::SeqCstT<TypeTy, IdTy, ExprTy>;
using Severity = tomp::clause::SeverityT<TypeTy, IdTy, ExprTy>;
using Shared = tomp::clause::SharedT<TypeTy, IdTy, ExprTy>;
using Simdlen = tomp::clause::SimdlenT<TypeTy, IdTy, ExprTy>;
using Simd = tomp::clause::SimdT<TypeTy, IdTy, ExprTy>;
using Sizes = tomp::clause::SizesT<TypeTy, IdTy, ExprTy>;
using TaskReduction = tomp::clause::TaskReductionT<TypeTy, IdTy, ExprTy>;
using ThreadLimit = tomp::clause::ThreadLimitT<TypeTy, IdTy, ExprTy>;
using Threads = tomp::clause::ThreadsT<TypeTy, IdTy, ExprTy>;
using To = tomp::clause::ToT<TypeTy, IdTy, ExprTy>;
using UnifiedAddress = tomp::clause::UnifiedAddressT<TypeTy, IdTy, ExprTy>;
using UnifiedSharedMemory =
    tomp::clause::UnifiedSharedMemoryT<TypeTy, IdTy, ExprTy>;
using Uniform = tomp::clause::UniformT<TypeTy, IdTy, ExprTy>;
using Unknown = tomp::clause::UnknownT<TypeTy, IdTy, ExprTy>;
using Untied = tomp::clause::UntiedT<TypeTy, IdTy, ExprTy>;
using Update = tomp::clause::UpdateT<TypeTy, IdTy, ExprTy>;
using UseDeviceAddr = tomp::clause::UseDeviceAddrT<TypeTy, IdTy, ExprTy>;
using UseDevicePtr = tomp::clause::UseDevicePtrT<TypeTy, IdTy, ExprTy>;
using UsesAllocators = tomp::clause::UsesAllocatorsT<TypeTy, IdTy, ExprTy>;
using Use = tomp::clause::UseT<TypeTy, IdTy, ExprTy>;
using Weak = tomp::clause::WeakT<TypeTy, IdTy, ExprTy>;
using When = tomp::clause::WhenT<TypeTy, IdTy, ExprTy>;
using Write = tomp::clause::WriteT<TypeTy, IdTy, ExprTy>;
} // namespace clause

struct Helper {
  std::optional<Object> getBaseObject(const Object &object) {
    return std::nullopt;
  }
  std::optional<Object> getLoopIterVar() { return std::nullopt; }
};

using Clause = tomp::ClauseT<TypeTy, IdTy, ExprTy>;
using ConstructDecomposition = tomp::ConstructDecompositionT<Clause, Helper>;
using DirectiveWithClauses = tomp::DirectiveWithClauses<Clause>;
} // namespace omp

struct StringifyClause {
  static std::string join(const omp::List<std::string> &Strings) {
    std::stringstream Stream;
    for (const auto &[Index, String] : llvm::enumerate(Strings)) {
      if (Index != 0)
        Stream << ", ";
      Stream << String;
    }
    return Stream.str();
  }

  static std::string to_str(llvm::omp::Directive D) {
    return getOpenMPDirectiveName(D, llvm::omp::FallbackVersion).str();
  }
  static std::string to_str(llvm::omp::Clause C) {
    return getOpenMPClauseName(C).str();
  }
  static std::string to_str(const omp::TypeTy &Type) { return "type"; }
  static std::string to_str(const omp::ExprTy &Expr) { return "expr"; }
  static std::string to_str(const omp::Object &Obj) { return Obj.id(); }

  template <typename U>
  static std::enable_if_t<std::is_enum_v<llvm::remove_cvref_t<U>>, std::string>
  to_str(U &&Item) {
    return std::to_string(llvm::to_underlying(Item));
  }

  template <typename U> static std::string to_str(const omp::List<U> &Items) {
    omp::List<std::string> Names;
    llvm::transform(Items, std::back_inserter(Names),
                    [](auto &&S) { return to_str(S); });
    return "(" + join(Names) + ")";
  }

  template <typename U>
  static std::string to_str(const std::optional<U> &Item) {
    if (Item)
      return to_str(*Item);
    return "";
  }

  template <typename... Us, size_t... Is>
  static std::string to_str(const std::tuple<Us...> &Tuple,
                            std::index_sequence<Is...>) {
    omp::List<std::string> Strings;
    (Strings.push_back(to_str(std::get<Is>(Tuple))), ...);
    return "(" + join(Strings) + ")";
  }

  template <typename U>
  static std::enable_if_t<llvm::remove_cvref_t<U>::EmptyTrait::value,
                          std::string>
  to_str(U &&Item) {
    return "";
  }

  template <typename U>
  static std::enable_if_t<llvm::remove_cvref_t<U>::IncompleteTrait::value,
                          std::string>
  to_str(U &&Item) {
    return "";
  }

  template <typename U>
  static std::enable_if_t<llvm::remove_cvref_t<U>::WrapperTrait::value,
                          std::string>
  to_str(U &&Item) {
    // For a wrapper, stringify the wrappee, and only add parentheses if
    // there aren't any already.
    std::string Str = to_str(Item.v);
    if (!Str.empty()) {
      if (Str.front() == '(' && Str.back() == ')')
        return Str;
    }
    return "(" + to_str(Item.v) + ")";
  }

  template <typename U>
  static std::enable_if_t<llvm::remove_cvref_t<U>::TupleTrait::value,
                          std::string>
  to_str(U &&Item) {
    constexpr size_t TupleSize =
        std::tuple_size_v<llvm::remove_cvref_t<decltype(Item.t)>>;
    return to_str(Item.t, std::make_index_sequence<TupleSize>{});
  }

  template <typename U>
  static std::enable_if_t<llvm::remove_cvref_t<U>::UnionTrait::value,
                          std::string>
  to_str(U &&Item) {
    return std::visit([](auto &&S) { return to_str(S); }, Item.u);
  }

  StringifyClause(const omp::Clause &C)
      // Rely on content stringification to emit enclosing parentheses.
      : Str(to_str(C.id) + to_str(C)) {}

  std::string Str;
};

std::string stringify(const omp::DirectiveWithClauses &DWC) {
  std::stringstream Stream;

  Stream << getOpenMPDirectiveName(DWC.id, llvm::omp::FallbackVersion).str();
  for (const omp::Clause &C : DWC.clauses)
    Stream << ' ' << StringifyClause(C).Str;

  return Stream.str();
}

// --- Tests ----------------------------------------------------------

namespace red {
// Make it easier to construct reduction operators from built-in intrinsics.
omp::clause::ReductionOperator
makeOp(omp::clause::DefinedOperator::IntrinsicOperator Op) {
  return omp::clause::ReductionOperator{omp::clause::DefinedOperator{Op}};
}
} // namespace red

namespace {
using namespace llvm::omp;

class OpenMPDecompositionTest : public testing::Test {
protected:
  void SetUp() override {}
  void TearDown() override {}

  omp::Helper Helper;
  uint32_t AnyVersion = 999;
};

// PRIVATE
// [5.2:111:5-7]
// Directives: distribute, do, for, loop, parallel, scope, sections, simd,
// single, target, task, taskloop, teams
//
// [5.2:340:1-2]
// (1) The effect of the 1 private clause is as if it is applied only to the
// innermost leaf construct that permits it.
TEST_F(OpenMPDecompositionTest, Private1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_private, omp::clause::Private{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel");            // (1)
  ASSERT_EQ(Dir1, "sections private(x)"); // (1)
}

TEST_F(OpenMPDecompositionTest, Private2) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_private, omp::clause::Private{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_masked,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel private(x)"); // (1)
  ASSERT_EQ(Dir1, "masked");              // (1)
}

// FIRSTPRIVATE
// [5.2:112:5-7]
// Directives: distribute, do, for, parallel, scope, sections, single, target,
// task, taskloop, teams
//
// [5.2:340:3-20]
// (3) The effect of the firstprivate clause is as if it is applied to one or
// more leaf constructs as follows:
//  (5) To the distribute construct if it is among the constituent constructs;
//  (6) To the teams construct if it is among the constituent constructs and the
//      distribute construct is not;
//  (8) To a worksharing construct that accepts the clause if one is among the
//      constituent constructs;
//  (9) To the taskloop construct if it is among the constituent constructs;
// (10) To the parallel construct if it is among the constituent constructs and
//      neither a taskloop construct nor a worksharing construct that accepts
//      the clause is among them;
// (12) To the target construct if it is among the constituent constructs and
//      the same list item neither appears in a lastprivate clause nor is the
//      base variable or base pointer of a list item that appears in a map
//      clause.
//
// (15) If the parallel construct is among the constituent constructs and the
// effect is not as if the firstprivate clause is applied to it by the above
// rules, then the effect is as if the shared clause with the same list item is
// applied to the parallel construct.
// (17) If the teams construct is among the constituent constructs and the
// effect is not as if the firstprivate clause is applied to it by the above
// rules, then the effect is as if the shared clause with the same list item is
// applied to the teams construct.
TEST_F(OpenMPDecompositionTest, Firstprivate1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel shared(x)");       // (10), (15)
  ASSERT_EQ(Dir1, "sections firstprivate(x)"); // (8)
}

TEST_F(OpenMPDecompositionTest, Firstprivate2) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_target_teams_distribute, Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "target firstprivate(x)");     // (12)
  ASSERT_EQ(Dir1, "teams shared(x)");            // (6), (17)
  ASSERT_EQ(Dir2, "distribute firstprivate(x)"); // (5)
}

TEST_F(OpenMPDecompositionTest, Firstprivate3) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
      {OMPC_lastprivate, omp::clause::Lastprivate{{std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_target_teams_distribute, Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "target map(2, , , , (x))"); // (12), (27)
  ASSERT_EQ(Dir1, "teams shared(x)");          // (6), (17)
  ASSERT_EQ(Dir2, "distribute firstprivate(x) lastprivate(, (x))"); // (5), (21)
}

TEST_F(OpenMPDecompositionTest, Firstprivate4) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_target_teams,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "target firstprivate(x)"); // (12)
  ASSERT_EQ(Dir1, "teams firstprivate(x)");  // (6)
}

TEST_F(OpenMPDecompositionTest, Firstprivate5) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_parallel_masked_taskloop, Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "parallel shared(x)"); // (10)
  ASSERT_EQ(Dir1, "masked");
  ASSERT_EQ(Dir2, "taskloop firstprivate(x)"); // (9)
}

TEST_F(OpenMPDecompositionTest, Firstprivate6) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_masked,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel firstprivate(x)"); // (10)
  ASSERT_EQ(Dir1, "masked");
}

TEST_F(OpenMPDecompositionTest, Firstprivate7) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
  };

  // Composite constructs are still decomposed.
  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_teams_distribute,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "teams shared(x)");            // (17)
  ASSERT_EQ(Dir1, "distribute firstprivate(x)"); // (5)
}

// LASTPRIVATE
// [5.2:115:7-8]
// Directives: distribute, do, for, loop, sections, simd, taskloop
//
// [5.2:340:21-30]
// (21) The effect of the lastprivate clause is as if it is applied to all leaf
// constructs that permit the clause.
// (22) If the parallel construct is among the constituent constructs and the
// list item is not also specified in the firstprivate clause, then the effect
// of the lastprivate clause is as if the shared clause with the same list item
// is applied to the parallel construct.
// (24) If the teams construct is among the constituent constructs and the list
// item is not also specified in the firstprivate clause, then the effect of the
// lastprivate clause is as if the shared clause with the same list item is
// applied to the teams construct.
// (27) If the target construct is among the constituent constructs and the list
// item is not the base variable or base pointer of a list item that appears in
// a map clause, the effect of the lastprivate clause is as if the same list
// item appears in a map clause with a map-type of tofrom.
TEST_F(OpenMPDecompositionTest, Lastprivate1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_lastprivate, omp::clause::Lastprivate{{std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel shared(x)");          // (21), (22)
  ASSERT_EQ(Dir1, "sections lastprivate(, (x))"); // (21)
}

TEST_F(OpenMPDecompositionTest, Lastprivate2) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_lastprivate, omp::clause::Lastprivate{{std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_teams_distribute,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "teams shared(x)");               // (21), (25)
  ASSERT_EQ(Dir1, "distribute lastprivate(, (x))"); // (21)
}

TEST_F(OpenMPDecompositionTest, Lastprivate3) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_lastprivate, omp::clause::Lastprivate{{std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_target_parallel_do,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "target map(2, , , , (x))"); // (21), (27)
  ASSERT_EQ(Dir1, "parallel shared(x)");       // (22)
  ASSERT_EQ(Dir2, "do lastprivate(, (x))");    // (21)
}

// SHARED
// [5.2:110:5-6]
// Directives: parallel, task, taskloop, teams
//
// [5.2:340:31-32]
// (31) The effect of the shared, default, thread_limit, or order clause is as
// if it is applied to all leaf constructs that permit the clause.
TEST_F(OpenMPDecompositionTest, Shared1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_shared, omp::clause::Shared{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_parallel_masked_taskloop, Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "parallel shared(x)"); // (31)
  ASSERT_EQ(Dir1, "masked");             // (31)
  ASSERT_EQ(Dir2, "taskloop shared(x)"); // (31)
}

// DEFAULT
// [5.2:109:5-6]
// Directives: parallel, task, taskloop, teams
//
// [5.2:340:31-32]
// (31) The effect of the shared, default, thread_limit, or order clause is as
// if it is applied to all leaf constructs that permit the clause.
TEST_F(OpenMPDecompositionTest, Default1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_default,
       omp::clause::Default{
           omp::clause::Default::DataSharingAttribute::Firstprivate}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_parallel_masked_taskloop, Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "parallel default(0)"); // (31)
  ASSERT_EQ(Dir1, "masked");              // (31)
  ASSERT_EQ(Dir2, "taskloop default(0)"); // (31)
}

// THREAD_LIMIT
// [5.2:277:14-15]
// Directives: target, teams
//
// [5.2:340:31-32]
// (31) The effect of the shared, default, thread_limit, or order clause is as
// if it is applied to all leaf constructs that permit the clause.
TEST_F(OpenMPDecompositionTest, ThreadLimit1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_thread_limit, omp::clause::ThreadLimit{omp::ExprTy{}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_target_teams_distribute, Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "target thread_limit(expr)"); // (31)
  ASSERT_EQ(Dir1, "teams thread_limit(expr)");  // (31)
  ASSERT_EQ(Dir2, "distribute");                // (31)
}

// ORDER
// [5.2:234:3-4]
// Directives: distribute, do, for, loop, simd
//
// [5.2:340:31-32]
// (31) The effect of the shared, default, thread_limit, or order clause is as
// if it is applied to all leaf constructs that permit the clause.
TEST_F(OpenMPDecompositionTest, Order1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_order,
       omp::clause::Order{{omp::clause::Order::OrderModifier::Unconstrained,
                           omp::clause::Order::Ordering::Concurrent}}},
  };

  omp::ConstructDecomposition Dec(
      AnyVersion, Helper, OMPD_target_teams_distribute_parallel_for_simd,
      Clauses);
  ASSERT_EQ(Dec.output.size(), 6u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  std::string Dir3 = stringify(Dec.output[3]);
  std::string Dir4 = stringify(Dec.output[4]);
  std::string Dir5 = stringify(Dec.output[5]);
  ASSERT_EQ(Dir0, "target");                 // (31)
  ASSERT_EQ(Dir1, "teams");                  // (31)
  ASSERT_EQ(Dir2, "distribute order(1, 0)"); // (31)
  ASSERT_EQ(Dir3, "parallel");               // (31)
  ASSERT_EQ(Dir4, "for order(1, 0)");        // (31)
  ASSERT_EQ(Dir5, "simd order(1, 0)");       // (31)
}

// ALLOCATE
// [5.2:178:7-9]
// Directives: allocators, distribute, do, for, parallel, scope, sections,
// single, target, task, taskgroup, taskloop, teams
//
// [5.2:340:33-35]
// (33) The effect of the allocate clause is as if it is applied to all leaf
// constructs that permit the clause and to which a data-sharing attribute
// clause that may create a private copy of the same list item is applied.
TEST_F(OpenMPDecompositionTest, Allocate1) {
  omp::Object x{"x"};

  // Allocate + firstprivate
  omp::List<omp::Clause> Clauses{
      {OMPC_allocate, omp::clause::Allocate{{std::nullopt, std::nullopt, {x}}}},
      {OMPC_firstprivate, omp::clause::Firstprivate{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel shared(x)");                         // (33)
  ASSERT_EQ(Dir1, "sections firstprivate(x) allocate(, , (x))"); // (33)
}

TEST_F(OpenMPDecompositionTest, Allocate2) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);

  // Allocate + in_reduction
  omp::List<omp::Clause> Clauses{
      {OMPC_allocate, omp::clause::Allocate{{std::nullopt, std::nullopt, {x}}}},
      {OMPC_in_reduction, omp::clause::InReduction{{{Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_target_parallel,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "target in_reduction((3), (x)) allocate(, , (x))"); // (33)
  ASSERT_EQ(Dir1, "parallel");                                        // (33)
}

TEST_F(OpenMPDecompositionTest, Allocate3) {
  omp::Object x{"x"};

  // Allocate + linear
  omp::List<omp::Clause> Clauses{
      {OMPC_allocate, omp::clause::Allocate{{std::nullopt, std::nullopt, {x}}}},
      {OMPC_linear, omp::clause::Linear{{std::nullopt, std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_for,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  // The "shared" clause is duplicated---this isn't harmful, but it
  // should be fixed eventually.
  ASSERT_EQ(Dir0, "parallel shared(x) shared(x)"); // (33)
  ASSERT_EQ(Dir1, "for linear(, , (x)) firstprivate(x) lastprivate(, (x)) "
                  "allocate(, , (x))"); // (33)
}

TEST_F(OpenMPDecompositionTest, Allocate4) {
  omp::Object x{"x"};

  // Allocate + lastprivate
  omp::List<omp::Clause> Clauses{
      {OMPC_allocate, omp::clause::Allocate{{std::nullopt, std::nullopt, {x}}}},
      {OMPC_lastprivate, omp::clause::Lastprivate{{std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel shared(x)");                            // (33)
  ASSERT_EQ(Dir1, "sections lastprivate(, (x)) allocate(, , (x))"); // (33)
}

TEST_F(OpenMPDecompositionTest, Allocate5) {
  omp::Object x{"x"};

  // Allocate + private
  omp::List<omp::Clause> Clauses{
      {OMPC_allocate, omp::clause::Allocate{{std::nullopt, std::nullopt, {x}}}},
      {OMPC_private, omp::clause::Private{{x}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel");                              // (33)
  ASSERT_EQ(Dir1, "sections private(x) allocate(, , (x))"); // (33)
}

TEST_F(OpenMPDecompositionTest, Allocate6) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);

  // Allocate + reduction
  omp::List<omp::Clause> Clauses{
      {OMPC_allocate, omp::clause::Allocate{{std::nullopt, std::nullopt, {x}}}},
      {OMPC_reduction, omp::clause::Reduction{{std::nullopt, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel shared(x)");                               // (33)
  ASSERT_EQ(Dir1, "sections reduction(, (3), (x)) allocate(, , (x))"); // (33)
}

// REDUCTION
// [5.2:134:17-18]
// Directives: do, for, loop, parallel, scope, sections, simd, taskloop, teams
//
// [5.2:340-341:36-13]
// (36) The effect of the reduction clause is as if it is applied to all leaf
// constructs that permit the clause, except for the following constructs:
//  (1) The parallel construct, when combined with the sections,
//      worksharing-loop, loop, or taskloop construct; and
//  (3) The teams construct, when combined with the loop construct.
// (4) For the parallel and teams constructs above, the effect of the reduction
// clause instead is as if each list item or, for any list item that is an array
// item, its corresponding base array or base pointer appears in a shared clause
// for the construct.
// (6) If the task reduction-modifier is specified, the effect is as if it only
// modifies the behavior of the reduction clause on the innermost leaf construct
// that accepts the modifier (see Section 5.5.8).
// (8) If the inscan reduction-modifier is specified, the effect is as if it
// modifies the behavior of the reduction clause on all constructs of the
// combined construct to which the clause is applied and that accept the
// modifier.
// (10) If a list item in a reduction clause on a combined target construct does
// not have the same base variable or base pointer as a list item in a map
// clause on the construct, then the effect is as if the list item in the
// reduction clause appears as a list item in a map clause with a map-type of
// tofrom.
TEST_F(OpenMPDecompositionTest, Reduction1) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);

  omp::List<omp::Clause> Clauses{
      {OMPC_reduction, omp::clause::Reduction{{std::nullopt, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_sections,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel shared(x)");             // (36), (1), (4)
  ASSERT_EQ(Dir1, "sections reduction(, (3), (x))"); // (36)
}

TEST_F(OpenMPDecompositionTest, Reduction2) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);

  omp::List<omp::Clause> Clauses{
      {OMPC_reduction, omp::clause::Reduction{{std::nullopt, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_parallel_masked,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "parallel reduction(, (3), (x))"); // (36), (1), (4)
  ASSERT_EQ(Dir1, "masked");                         // (36)
}

TEST_F(OpenMPDecompositionTest, Reduction3) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);

  omp::List<omp::Clause> Clauses{
      {OMPC_reduction, omp::clause::Reduction{{std::nullopt, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_teams_loop, Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "teams shared(x)");            // (36), (3), (4)
  ASSERT_EQ(Dir1, "loop reduction(, (3), (x))"); // (36)
}

TEST_F(OpenMPDecompositionTest, Reduction4) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);

  omp::List<omp::Clause> Clauses{
      {OMPC_reduction, omp::clause::Reduction{{std::nullopt, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_teams_distribute_parallel_for, Clauses);
  ASSERT_EQ(Dec.output.size(), 4u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  std::string Dir3 = stringify(Dec.output[3]);
  ASSERT_EQ(Dir0, "teams reduction(, (3), (x))"); // (36), (3)
  ASSERT_EQ(Dir1, "distribute");                  // (36)
  ASSERT_EQ(Dir2, "parallel shared(x)");          // (36), (1), (4)
  ASSERT_EQ(Dir3, "for reduction(, (3), (x))");   // (36)
}

TEST_F(OpenMPDecompositionTest, Reduction5) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);
  auto TaskMod = omp::clause::Reduction::ReductionModifier::Task;

  omp::List<omp::Clause> Clauses{
      {OMPC_reduction, omp::clause::Reduction{{TaskMod, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_teams_distribute_parallel_for, Clauses);
  ASSERT_EQ(Dec.output.size(), 4u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  std::string Dir3 = stringify(Dec.output[3]);
  ASSERT_EQ(Dir0, "teams reduction(, (3), (x))"); // (36), (3), (6)
  ASSERT_EQ(Dir1, "distribute");                  // (36)
  ASSERT_EQ(Dir2, "parallel shared(x)");          // (36), (1), (4)
  ASSERT_EQ(Dir3, "for reduction(2, (3), (x))");  // (36), (6)
}

TEST_F(OpenMPDecompositionTest, Reduction6) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);
  auto InscanMod = omp::clause::Reduction::ReductionModifier::Inscan;

  omp::List<omp::Clause> Clauses{
      {OMPC_reduction, omp::clause::Reduction{{InscanMod, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_teams_distribute_parallel_for, Clauses);
  ASSERT_EQ(Dec.output.size(), 4u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  std::string Dir3 = stringify(Dec.output[3]);
  ASSERT_EQ(Dir0, "teams reduction(, (3), (x))"); // (36), (3), (8)
  ASSERT_EQ(Dir1, "distribute");                  // (36)
  ASSERT_EQ(Dir2, "parallel shared(x)");          // (36), (1), (4)
  ASSERT_EQ(Dir3, "for reduction(1, (3), (x))");  // (36), (8)
}

TEST_F(OpenMPDecompositionTest, Reduction7) {
  omp::Object x{"x"};
  auto Add = red::makeOp(omp::clause::DefinedOperator::IntrinsicOperator::Add);

  omp::List<omp::Clause> Clauses{
      {OMPC_reduction, omp::clause::Reduction{{std::nullopt, {Add}, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_target_parallel_do,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);

  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "target map(2, , , , (x))"); // (36), (10)
  ASSERT_EQ(Dir1, "parallel shared(x)");       // (36), (1), (4)
  ASSERT_EQ(Dir2, "do reduction(, (3), (x))"); // (36)
}

// IF
// [5.2:72:7-9]
// Directives: cancel, parallel, simd, target, target data, target enter data,
// target exit data, target update, task, taskloop
//
// [5.2:72:15-18]
// (15) For combined or composite constructs, the if clause only applies to the
// semantics of the construct named in the directive-name-modifier.
// (16) For a combined or composite construct, if no directive-name-modifier is
// specified then the if clause applies to all constituent constructs to which
// an if clause can apply.
TEST_F(OpenMPDecompositionTest, If1) {
  omp::List<omp::Clause> Clauses{
      {OMPC_if,
       omp::clause::If{{llvm::omp::Directive::OMPD_parallel, omp::ExprTy{}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_target_parallel_for_simd, Clauses);
  ASSERT_EQ(Dec.output.size(), 4u);
  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  std::string Dir3 = stringify(Dec.output[3]);
  ASSERT_EQ(Dir0, "target");              // (15)
  ASSERT_EQ(Dir1, "parallel if(, expr)"); // (15)
  ASSERT_EQ(Dir2, "for");                 // (15)
  ASSERT_EQ(Dir3, "simd");                // (15)
}

TEST_F(OpenMPDecompositionTest, If2) {
  omp::List<omp::Clause> Clauses{
      {OMPC_if, omp::clause::If{{std::nullopt, omp::ExprTy{}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper,
                                  OMPD_target_parallel_for_simd, Clauses);
  ASSERT_EQ(Dec.output.size(), 4u);
  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  std::string Dir3 = stringify(Dec.output[3]);
  ASSERT_EQ(Dir0, "target if(, expr)");   // (16)
  ASSERT_EQ(Dir1, "parallel if(, expr)"); // (16)
  ASSERT_EQ(Dir2, "for");                 // (16)
  ASSERT_EQ(Dir3, "simd if(, expr)");     // (16)
}

// LINEAR
// [5.2:118:1-2]
// Directives: declare simd, do, for, simd
//
// [5.2:341:15-22]
// (15.1) The effect of the linear clause is as if it is applied to the
// innermost leaf construct.
// (15.2) Additionally, if the list item is not the iteration variable of a simd
// or worksharing-loop SIMD construct, the effect on the outer leaf constructs
// is as if the list item was specified in firstprivate and lastprivate clauses
// on the combined or composite construct, with the rules specified above
// applied.
// (19) If a list item of the linear clause is the iteration variable of a simd
// or worksharing-loop SIMD construct and it is not declared in the construct,
// the effect on the outer leaf constructs is as if the list item was specified
// in a lastprivate clause on the combined or composite construct with the rules
// specified above applied.
TEST_F(OpenMPDecompositionTest, Linear1) {
  omp::Object x{"x"};

  omp::List<omp::Clause> Clauses{
      {OMPC_linear, omp::clause::Linear{{std::nullopt, std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_for_simd, Clauses);
  ASSERT_EQ(Dec.output.size(), 2u);
  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  ASSERT_EQ(Dir0, "for firstprivate(x) lastprivate(, (x))"); // (15.1), (15.2)
  ASSERT_EQ(Dir1, "simd linear(, , (x)) lastprivate(, (x))"); // (15.1)
}

// NOWAIT
// [5.2:308:11-13]
// Directives: dispatch, do, for, interop, scope, sections, single, target,
// target enter data, target exit data, target update, taskwait, workshare
//
// [5.2:341:23]
// (23) The effect of the nowait clause is as if it is applied to the outermost
// leaf construct that permits it.
TEST_F(OpenMPDecompositionTest, Nowait1) {
  omp::List<omp::Clause> Clauses{
      {OMPC_nowait, omp::clause::Nowait{}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_target_parallel_for,
                                  Clauses);
  ASSERT_EQ(Dec.output.size(), 3u);
  std::string Dir0 = stringify(Dec.output[0]);
  std::string Dir1 = stringify(Dec.output[1]);
  std::string Dir2 = stringify(Dec.output[2]);
  ASSERT_EQ(Dir0, "target nowait"); // (23)
  ASSERT_EQ(Dir1, "parallel");      // (23)
  ASSERT_EQ(Dir2, "for");           // (23)
}

// ---

// Check that "simd linear(x)" does not fail despite the implied "firstprivate"
// (which "simd" does not allow).
TEST_F(OpenMPDecompositionTest, Misc1) {
  omp::Object x{"x"};
  omp::List<omp::Clause> Clauses{
      {OMPC_linear, omp::clause::Linear{{std::nullopt, std::nullopt, {x}}}},
  };

  omp::ConstructDecomposition Dec(AnyVersion, Helper, OMPD_simd, Clauses);
  ASSERT_EQ(Dec.output.size(), 1u);
  std::string Dir0 = stringify(Dec.output[0]);
  ASSERT_EQ(Dir0, "simd linear(, , (x)) lastprivate(, (x))");
}
} // namespace