aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/pattern_matcher.h
blob: ac6ea4c72f61a47726b3ae7dd000837d3fba1b93 (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
1112
1113
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_COMPILER_XLA_SERVICE_PATTERN_MATCHER_H_
#define TENSORFLOW_COMPILER_XLA_SERVICE_PATTERN_MATCHER_H_

#include "tensorflow/compiler/xla/layout_util.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/service/hlo_opcode.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/core/lib/core/stringpiece.h"

namespace xla {

// A pattern matcher for HloInstructions, Shapes, and Layouts.
//
// The Match function's first argument must be HloInstruction*, Shape*, or
// Layout*. The second argument is a pattern that will be matched against the
// first argument, as described below.
//
// Patterns are constructed using the match::Op, match::Shape, or match::Layout
// functions. By default, the returned patterns will match any HloInstruction,
// Shape, or Layout, respectively. However the match can be made more specific
// by using the pattern's modifier methods, for example:
//
//   match::Op().WithOpcode(HloOpcode::kAdd).WithOperand(
//     0, match::Op().WithOpcode(HloOpcode::kConstant))
//
// This pattern will match Add instructions whose first operand is a constant.
//
// Each pattern type has the following modifiers:
//
//   Op():
//     - WithName: match operations with the given name
//     - WithOpcode: match operations with the given opcode
//     - WithShape: match operations whose shape matches the given pattern
//     - WithOperand: match operations whose operand matches the given pattern
//
//   Shape():
//     - EqualTo: matches shapes that are equal to the argument
//     - CompatibleTo: matches shapes that are compatible to the argument
//     - IsScalar/IsArray/IsTuple: matches scalar/array/tuple shapes
//     - IsDenseArray/IsSparseArray: matches arrays with dense/sparse format
//     - WithLayout: match shapes whose layout matches the given pattern
//     - WithLayoutEqualTo: matches shapes whose layouts equal the argument
//     - WithSubshape: matches tuple shapes whose subshape matches the given
//       pattern
//     - WithSubshapeEqualTo: matches shapes with a subshape equal the argument
//     - WithElementType: matches array/scalar shapes with the given element
//       type
//     - WithRank: matches array/scalar types with the given rank
//
//  Layout():
//     - EqualTo: matches layouts that are equal to the argument
//     - WithDenseFormat/WithSparseFormat: matches layouts with dense/sparse
//       format
//
// Op(), Shape(), and Layout() may be passed an argument of type
// HloInstruction**, Shape**, or Layout**, respectively, or const versions of
// these pointers. If the pattern is matched, the address of the matched value
// will be "captured" and stored at this location.
//
// For example:
//   HloInstruction* foo = ...;
//   HloInstruction* matched_operand;
//   CHECK(Match(foo,
//               match::Op().WithOperand(0, match::Op(&matched_operand))));
//
// Helpers are provided for common nullary, unary, binary, and ternary
// instructions. These helpers can be called with no arguments, in which case
// they will match any instruction matching the opcode. They may also be called
// with matches for the operands and with an optional capture. (The capture must
// be the first argument.) Some examples of these helpers and their equivalents
// are provided below.
//
// Example nullary instruction:
//   Param()                        == Op().WithOpcode(HloOpcode::kParam)
//   Param(&a)                      == Op(&a).WithOpcode(HloOpcode::kParam)
//
// Example unary instruction:
//   Abs()                             == Op().WithOpcode(HloOpcode::kAbs)
//   Abs(Op(&a))                       == Op().WithOpcode(HloOpcode::kAbs)
//                                            .WithOperand(0, Op(&a)))
//   Abs(&a, Op(&b))                   == Op(&a).WithOpcode(HloOpcode::kAbs)
//                                              .WithOperand(0, Op(&b))
//
// Example binary instruction:
//   Add()                             == Op().WithOpcode(HloOpcode::kAdd)
//   Add(Op(&a), Op(&b))               == Op().WithOpcode(HloOpcode::kAdd)
//                                            .WithOperand(0, Op(&a))
//                                            .WithOperand(1, Op(&b))
//   Add(&a, Op(&b), Op(&c))           == Op(&a).WithOpcode(HloOpcode::kAdd)
//                                              .WithOperand(0, Op(&b))
//                                              .WithOperand(1, Op(&c))
//
// Example ternary instruction:
//   Clamp()                           == Op().WithOpcode(HloOpcode::kClamp)
//   Clamp(Op(&a), Op(&b), Op(&c))     == Op().WithOpcode(HloOpcode::kClamp)
//                                            .WithOperand(0, Op(&a))
//                                            .WithOperand(1, Op(&b))
//                                            .WithOperand(2, Op(&c))
//   Clamp(&a, Op(&b), Op(&c), Op(&d)) == Op(&a).WithOpcode(HloOpcode::kClamp)
//                                              .WithOperand(0, Op(&b))
//                                              .WithOperand(1, Op(&c))
//                                              .WithOperand(2, Op(&d))
//
template <typename Value, typename Pattern>
bool Match(Value* value, const Pattern& pattern) {
  return pattern.Match(value);
}

namespace match {

namespace detail {

template <typename LayoutType, typename Impl>
class LayoutPattern;

// The base LayoutPattern implementation. Matches only if the layout is not
// nullptr.
class LayoutPatternBaseImpl {
 public:
  bool Match(const ::xla::Layout* layout) const { return layout != nullptr; }
};

// A LayoutPattern implementation that matches only if the layout equals a
// Layout proto.
template <typename Previous>
class LayoutPatternEqualImpl {
 public:
  explicit constexpr LayoutPatternEqualImpl(const Previous& previous,
                                            const ::xla::Layout* layout)
      : previous_(previous), layout_(layout) {}

  bool Match(const ::xla::Layout* layout) const {
    return previous_.Match(layout) && LayoutUtil::Equal(*layout_, *layout);
  }

 private:
  Previous previous_;
  const ::xla::Layout* layout_;
};

// A LayoutPattern implementation that matches only if the layout has a given
// format.
template <typename Previous>
class LayoutPatternFormatImpl {
 public:
  explicit constexpr LayoutPatternFormatImpl(const Previous& previous,
                                             Format format)
      : previous_(previous), format_(format) {}

  bool Match(const ::xla::Layout* layout) const {
    return previous_.Match(layout) && layout->format() == format_;
  }

 private:
  Previous previous_;
  Format format_;
};

// A pattern that matches Layouts.
template <typename LayoutType, typename Impl>
class LayoutPattern {
 public:
  explicit constexpr LayoutPattern(const Impl& impl,
                                   LayoutType** matched_layout)
      : impl_(impl), matched_layout_(matched_layout) {}

  // Returns true and captures the layout iff it matches the pattern.
  bool Match(const ::xla::Layout* layout) const {
    if (impl_.Match(layout)) {
      if (matched_layout_) {
        *matched_layout_ = layout;
      }
      return true;
    }
    return false;
  }

  // Returns true and captures the layout iff it matches the pattern.
  bool Match(::xla::Layout* layout) const {
    if (impl_.Match(layout)) {
      if (matched_layout_) {
        *matched_layout_ = layout;
      }
      return true;
    }
    return false;
  }

  // Modifies the pattern to match only if the layout equals the given proto.
  // The layout must outlive the returned pattern.
  constexpr LayoutPattern<LayoutType, LayoutPatternEqualImpl<Impl>> EqualTo(
      const ::xla::Layout* layout) const {
    return LayoutPattern<LayoutType, LayoutPatternEqualImpl<Impl>>(
        LayoutPatternEqualImpl<Impl>(impl_, layout), matched_layout_);
  }

  // Modifies the pattern to match only if the layout has a dense format.
  constexpr LayoutPattern<LayoutType, LayoutPatternFormatImpl<Impl>>
  WithDenseFormat() const {
    return LayoutPattern<LayoutType, LayoutPatternFormatImpl<Impl>>(
        LayoutPatternFormatImpl<Impl>(impl_, DENSE), matched_layout_);
  }

  // Modifies the pattern to match only if the layout has a sparse format.
  constexpr LayoutPattern<LayoutType, LayoutPatternFormatImpl<Impl>>
  WithSparseFormat() const {
    return LayoutPattern<LayoutType, LayoutPatternFormatImpl<Impl>>(
        LayoutPatternFormatImpl<Impl>(impl_, SPARSE), matched_layout_);
  }

 private:
  Impl impl_;
  LayoutType** matched_layout_;
};

}  // namespace detail

// Creates a layout pattern that will capture the matched layout in the
// argument.
inline constexpr detail::LayoutPattern<const ::xla::Layout,
                                       detail::LayoutPatternBaseImpl>
Layout(const ::xla::Layout** matched_layout = nullptr) {
  return detail::LayoutPattern<const ::xla::Layout,
                               detail::LayoutPatternBaseImpl>(
      detail::LayoutPatternBaseImpl(), matched_layout);
}

// Creates a layout pattern that will capture the matched layout in the
// argument.
inline constexpr detail::LayoutPattern<::xla::Layout,
                                       detail::LayoutPatternBaseImpl>
Layout(::xla::Layout** matched_layout) {
  return detail::LayoutPattern<::xla::Layout, detail::LayoutPatternBaseImpl>(
      detail::LayoutPatternBaseImpl(), matched_layout);
}

namespace detail {

template <typename ShapeType, typename Impl>
class ShapePattern;

// The base ShapePattern implementation. Matches only if the shape is not
// nullptr.
class ShapePatternBaseImpl {
 public:
  bool Match(const ::xla::Shape* shape) const { return shape != nullptr; }
};

// A ShapePattern implementation that matches only if the shape equals a Shape
// proto.
template <typename Previous>
class ShapePatternEqualImpl {
 public:
  explicit constexpr ShapePatternEqualImpl(const Previous& previous,
                                           const ::xla::Shape* shape)
      : previous_(previous), shape_(shape) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::Equal(*shape_, *shape);
  }

 private:
  Previous previous_;
  const ::xla::Shape* shape_;
};

// A ShapePattern implementation that matches only if the shape is compatible to
// a Shape proto.
template <typename Previous>
class ShapePatternCompatibleImpl {
 public:
  explicit constexpr ShapePatternCompatibleImpl(const Previous& previous,
                                                const ::xla::Shape* shape)
      : previous_(previous), shape_(shape) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::Compatible(*shape_, *shape);
  }

 private:
  Previous previous_;
  const ::xla::Shape* shape_;
};

// A ShapePattern implementation that matches only if the shape has a given
// element type.
template <typename Previous>
class ShapePatternElementTypeImpl {
 public:
  explicit constexpr ShapePatternElementTypeImpl(const Previous& previous,
                                                 PrimitiveType element_type)
      : previous_(previous), element_type_(element_type) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && shape->element_type() == element_type_;
  }

 private:
  Previous previous_;
  PrimitiveType element_type_;
};

// A ShapePattern implementation that matches only if the shape is scalar.
template <typename Previous>
class ShapePatternIsScalarImpl {
 public:
  explicit constexpr ShapePatternIsScalarImpl(const Previous& previous)
      : previous_(previous) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::IsScalar(*shape);
  }

 private:
  Previous previous_;
};

// A ShapePattern implementation that matches only if the shape is an array
template <typename Previous>
class ShapePatternIsArrayImpl {
 public:
  explicit constexpr ShapePatternIsArrayImpl(const Previous& previous)
      : previous_(previous) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::IsArray(*shape);
  }

 private:
  Previous previous_;
};

// A ShapePattern implementation that matches only if the shape is a tuple.
template <typename Previous>
class ShapePatternIsTupleImpl {
 public:
  explicit constexpr ShapePatternIsTupleImpl(const Previous& previous)
      : previous_(previous) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::IsTuple(*shape);
  }

 private:
  Previous previous_;
};

// A ShapePattern implementation that matches only if the shape has a given
// rank.
template <typename Previous>
class ShapePatternRankImpl {
 public:
  explicit constexpr ShapePatternRankImpl(const Previous& previous, int64 rank)
      : previous_(previous), rank_(rank) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::Rank(*shape) == rank_;
  }

 private:
  Previous previous_;
  int64 rank_;
};

// A ShapePattern implementation that matches only if the shape has a layout
// that matches a given pattern.
template <typename Previous, typename LayoutType, typename LayoutImpl>
class ShapePatternLayoutImpl {
 public:
  explicit constexpr ShapePatternLayoutImpl(
      const Previous& previous,
      const LayoutPattern<LayoutType, LayoutImpl>& layout)
      : previous_(previous), layout_(layout) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && LayoutUtil::HasLayout(*shape) &&
           layout_.Match(&shape->layout());
  }

  bool Match(Shape* shape) const {
    return previous_.Match(shape) && LayoutUtil::HasLayout(*shape) &&
           layout_.Match(shape->mutable_layout());
  }

 private:
  Previous previous_;
  LayoutPattern<LayoutType, LayoutImpl> layout_;
};

// A ShapePattern implementation that matches only if the shape has a subshape
// that matches a given pattern.
template <typename Previous, typename SubshapeType, typename SubshapeImpl>
class ShapePatternSubshapeImpl {
 public:
  explicit ShapePatternSubshapeImpl(
      const Previous& previous, ShapeIndexView index,
      const ShapePattern<SubshapeType, SubshapeImpl>& subshape)
      : previous_(previous), index_(index), subshape_(subshape) {}

  bool Match(const ::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::IndexIsValid(*shape, index_) &&
           subshape_.Match(&ShapeUtil::GetSubshape(*shape, index_));
  }

  bool Match(::xla::Shape* shape) const {
    return previous_.Match(shape) && ShapeUtil::IndexIsValid(*shape, index_) &&
           subshape_.Match(ShapeUtil::GetMutableSubshape(shape, index_));
  }

 private:
  Previous previous_;
  ShapeIndexView index_;
  ShapePattern<SubshapeType, SubshapeImpl> subshape_;
};

// A pattern that matches Shapes.
template <typename ShapeType, typename Impl>
class ShapePattern {
 public:
  explicit constexpr ShapePattern(const Impl& impl, ShapeType** matched_shape)
      : impl_(impl), matched_shape_(matched_shape) {}

  // Returns true and captures the shape iff it matches the pattern.
  bool Match(const ::xla::Shape* shape) const {
    if (impl_.Match(shape)) {
      if (matched_shape_) {
        *matched_shape_ = shape;
      }
      return true;
    }
    return false;
  }

  // Returns true and captures the shape iff it matches the pattern.
  bool Match(::xla::Shape* shape) const {
    if (impl_.Match(shape)) {
      if (matched_shape_) {
        *matched_shape_ = shape;
      }
      return true;
    }
    return false;
  }

  // Modifies the pattern to match only if the shape equals the given proto.
  // The layout must outlive the returned pattern.
  constexpr ShapePattern<ShapeType, ShapePatternEqualImpl<Impl>> EqualTo(
      const ::xla::Shape* shape) const {
    return ShapePattern<ShapeType, ShapePatternEqualImpl<Impl>>(
        ShapePatternEqualImpl<Impl>(impl_, shape), matched_shape_);
  }

  // Modifies the pattern to match only if the shape is compatible to the given
  // proto. The layout must outlive the returned pattern.
  constexpr ShapePattern<ShapeType, ShapePatternCompatibleImpl<Impl>>
  CompatibleTo(const ::xla::Shape* shape) const {
    return ShapePattern<ShapeType, ShapePatternCompatibleImpl<Impl>>(
        ShapePatternCompatibleImpl<Impl>(impl_, shape), matched_shape_);
  }

  // Modifies the pattern to match only if the shape has the given element type.
  constexpr ShapePattern<ShapeType, ShapePatternElementTypeImpl<Impl>>
  WithElementType(PrimitiveType element_type) const {
    return ShapePattern<ShapeType, ShapePatternElementTypeImpl<Impl>>(
        ShapePatternElementTypeImpl<Impl>(impl_, element_type), matched_shape_);
  }

  // Modifies the pattern to match only if the shape is scalar.
  constexpr ShapePattern<ShapeType, ShapePatternIsScalarImpl<Impl>> IsScalar()
      const {
    return ShapePattern<ShapeType, ShapePatternIsScalarImpl<Impl>>(
        ShapePatternIsScalarImpl<Impl>(impl_), matched_shape_);
  }

  // Modifies the pattern to match only if the shape is an array.
  constexpr ShapePattern<ShapeType, ShapePatternIsArrayImpl<Impl>> IsArray()
      const {
    return ShapePattern<ShapeType, ShapePatternIsArrayImpl<Impl>>(
        ShapePatternIsArrayImpl<Impl>(impl_), matched_shape_);
  }

  // Modifies the pattern to match only if the shape is a tuple.
  constexpr ShapePattern<ShapeType, ShapePatternIsTupleImpl<Impl>> IsTuple()
      const {
    return ShapePattern<ShapeType, ShapePatternIsTupleImpl<Impl>>(
        ShapePatternIsTupleImpl<Impl>(impl_), matched_shape_);
  }

  // Modifies the pattern to match only if the shape has the given rank.
  constexpr ShapePattern<ShapeType, ShapePatternRankImpl<Impl>> WithRank(
      int64 rank) const {
    return ShapePattern<ShapeType, ShapePatternRankImpl<Impl>>(
        ShapePatternRankImpl<Impl>(impl_, rank), matched_shape_);
  }

  // Modifies the pattern to match only if the shape has a layout that matches
  // the given pattern.
  template <typename LayoutType, typename LayoutImpl>
  constexpr ShapePattern<ShapeType,
                         ShapePatternLayoutImpl<Impl, LayoutType, LayoutImpl>>
  WithLayout(const LayoutPattern<LayoutType, LayoutImpl>& layout) const {
    return ShapePattern<ShapeType,
                        ShapePatternLayoutImpl<Impl, LayoutType, LayoutImpl>>(
        ShapePatternLayoutImpl<Impl, LayoutType, LayoutImpl>(impl_, layout),
        matched_shape_);
  }

  constexpr ShapePattern<
      ShapeType,
      ShapePatternLayoutImpl<Impl, const ::xla::Layout,
                             LayoutPatternEqualImpl<LayoutPatternBaseImpl>>>
  WithLayoutEqualTo(const ::xla::Layout* layout) const {
    return WithLayout(Layout().EqualTo(layout));
  }

  constexpr ShapePattern<
      ShapeType,
      ShapePatternLayoutImpl<Impl, const ::xla::Layout,
                             LayoutPatternFormatImpl<LayoutPatternBaseImpl>>>
  IsDenseArray() const {
    return WithLayout(Layout().WithDenseFormat());
  }

  constexpr ShapePattern<
      ShapeType,
      ShapePatternLayoutImpl<Impl, const ::xla::Layout,
                             LayoutPatternFormatImpl<LayoutPatternBaseImpl>>>
  IsSparseArray() const {
    return WithLayout(Layout().WithSparseFormat());
  }

  // Modifies the pattern to match only if the shape has a subshape that matches
  // the given pattern.
  template <typename SubshapeType, typename SubshapeImpl>
  ShapePattern<ShapeType,
               ShapePatternSubshapeImpl<Impl, SubshapeType, SubshapeImpl>>
  WithSubshape(ShapeIndexView index,
               const ShapePattern<SubshapeType, SubshapeImpl>& subshape) const {
    return ShapePattern<
        ShapeType, ShapePatternSubshapeImpl<Impl, SubshapeType, SubshapeImpl>>(
        ShapePatternSubshapeImpl<Impl, SubshapeType, SubshapeImpl>(impl_, index,
                                                                   subshape),
        matched_shape_);
  }

  ShapePattern<ShapeType, ShapePatternSubshapeImpl<
                              Impl, const ::xla::Shape,
                              ShapePatternEqualImpl<ShapePatternBaseImpl>>>
  WithSubshapeEqualTo(ShapeIndexView index, const ::xla::Shape* shape) const {
    return WithSubshape(index,
                        ShapePattern<const ::xla::Shape, ShapePatternBaseImpl>(
                            ShapePatternBaseImpl(), nullptr)
                            .EqualTo(shape));
  }

  ShapePattern<ShapeType, ShapePatternSubshapeImpl<
                              Impl, const ::xla::Shape,
                              ShapePatternCompatibleImpl<ShapePatternBaseImpl>>>
  WithSubshapeCompatibleTo(ShapeIndexView index,
                           const ::xla::Shape* shape) const {
    return WithSubshape(index,
                        ShapePattern<const ::xla::Shape, ShapePatternBaseImpl>(
                            ShapePatternBaseImpl(), nullptr)
                            .CompatibleTo(shape));
  }

 private:
  Impl impl_;
  ShapeType** matched_shape_;
};

}  // namespace detail

// Creates a shape pattern that will capture the matched layout in the argument.
inline constexpr detail::ShapePattern<const ::xla::Shape,
                                      detail::ShapePatternBaseImpl>
Shape(const ::xla::Shape** matched_shape = nullptr) {
  return detail::ShapePattern<const ::xla::Shape, detail::ShapePatternBaseImpl>(
      detail::ShapePatternBaseImpl(), matched_shape);
}

// Creates a shape pattern that will capture the matched layout in the argument.
inline constexpr detail::ShapePattern<::xla::Shape,
                                      detail::ShapePatternBaseImpl>
Shape(::xla::Shape** matched_shape) {
  return detail::ShapePattern<::xla::Shape, detail::ShapePatternBaseImpl>(
      detail::ShapePatternBaseImpl(), matched_shape);
}

namespace detail {

template <typename HloInstructionType, typename Impl>
class HloInstructionPattern;

// The base HloInstructionPattern implementation. Matches only if the
// instruction is not nullptr.
class HloInstructionPatternBaseImpl {
 public:
  bool Match(const ::xla::HloInstruction* inst) const {
    return inst != nullptr;
  }
};

// An HloInstructionPattern implementation that matches only if the instruction
// has a given name.
template <typename Previous>
class HloInstructionPatternNameImpl {
 public:
  explicit HloInstructionPatternNameImpl(const Previous& previous,
                                         tensorflow::StringPiece name)
      : previous_(previous), name_(name) {}

  bool Match(const ::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && inst->name() == name_;
  }

 private:
  Previous previous_;
  tensorflow::StringPiece name_;
};

// An HloInstructionPattern implementation that matches only if the instruction
// has a given opcode.
template <typename Previous>
class HloInstructionPatternOpcodeImpl {
 public:
  explicit constexpr HloInstructionPatternOpcodeImpl(const Previous& previous,
                                                     HloOpcode opcode,
                                                     bool invert)
      : previous_(previous), opcode_(opcode), invert_(invert) {}

  bool Match(const ::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && (invert_ ^ (inst->opcode() == opcode_));
  }

 private:
  Previous previous_;
  HloOpcode opcode_;
  bool invert_;
};

// An HloInstructionPattern implementation that matches only if the instruction
// has a shape that matches a given pattern.
template <typename Previous, typename ShapeType, typename ShapeImpl>
class HloInstructionPatternShapeImpl {
 public:
  explicit constexpr HloInstructionPatternShapeImpl(
      const Previous& previous, const ShapePattern<ShapeType, ShapeImpl>& shape)
      : previous_(previous), shape_(shape) {}

  bool Match(const ::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && shape_.Match(&inst->shape());
  }

  bool Match(::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && shape_.Match(inst->mutable_shape());
  }

 private:
  Previous previous_;
  ShapePattern<ShapeType, ShapeImpl> shape_;
};

// An HloInstructionPattern implementation that matches only if the instruction
// has an operand that matches a given pattern.
template <typename Previous, typename OperandType, typename OperandImpl>
class HloInstructionPatternOperandImpl {
 public:
  explicit constexpr HloInstructionPatternOperandImpl(
      const Previous& previous, int64 operand_index,
      const HloInstructionPattern<OperandType, OperandImpl>& operand)
      : previous_(previous), operand_index_(operand_index), operand_(operand) {}

  bool Match(const ::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && operand_index_ < inst->operand_count() &&
           operand_.Match(inst->operand(operand_index_));
  }

  bool Match(::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && operand_index_ < inst->operand_count() &&
           operand_.Match(inst->mutable_operand(operand_index_));
  }

 private:
  Previous previous_;
  int64 operand_index_;
  HloInstructionPattern<OperandType, OperandImpl> operand_;
};

// An HloInstructionPattern implementation that matches only if the instruction
// is a fusion node with a particular kind.
template <typename Previous>
class HloInstructionPatternFusionKindImpl {
 public:
  explicit constexpr HloInstructionPatternFusionKindImpl(
      const Previous& previous, ::xla::HloInstruction::FusionKind kind)
      : previous_(previous), kind_(kind) {}

  bool Match(const ::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && inst->opcode() == HloOpcode::kFusion &&
           inst->fusion_kind() == kind_;
  }

  bool Match(::xla::HloInstruction* inst) const {
    return previous_.Match(inst) && inst->opcode() == HloOpcode::kFusion &&
           inst->fusion_kind() == kind_;
  }

 private:
  Previous previous_;
  ::xla::HloInstruction::FusionKind kind_;
};

// An HloInstructionPattern implementation that matches only if the instruction
// is a kGetTupleElement with a particular tuple index.
template <typename Previous>
class HloInstructionPatternTupleIndexImpl {
 public:
  explicit constexpr HloInstructionPatternTupleIndexImpl(
      const Previous& previous, int64 tuple_index)
      : previous_(previous), tuple_index_(tuple_index) {}

  bool Match(const ::xla::HloInstruction* inst) const {
    return previous_.Match(inst) &&
           inst->opcode() == HloOpcode::kGetTupleElement &&
           inst->tuple_index() == tuple_index_;
  }

  bool Match(::xla::HloInstruction* inst) const {
    return previous_.Match(inst) &&
           inst->opcode() == HloOpcode::kGetTupleElement &&
           inst->tuple_index() == tuple_index_;
  }

 private:
  Previous previous_;
  int64 tuple_index_;
};

// A pattern that matches HloInstructions.
template <typename HloInstructionType, typename Impl>
class HloInstructionPattern {
 public:
  explicit constexpr HloInstructionPattern(const Impl& impl,
                                           HloInstructionType** matched_inst)
      : impl_(impl), matched_inst_(matched_inst) {}

  // Returns true and captures the instruction iff it matches the pattern.
  bool Match(const ::xla::HloInstruction* inst) const {
    if (impl_.Match(inst)) {
      if (matched_inst_) {
        *matched_inst_ = inst;
      }
      return true;
    }
    return false;
  }

  // Returns true and captures the instruction iff it matches the pattern.
  bool Match(::xla::HloInstruction* inst) const {
    if (impl_.Match(inst)) {
      if (matched_inst_) {
        *matched_inst_ = inst;
      }
      return true;
    }
    return false;
  }

  // Modifies the pattern to match only if the instruction has the given name.
  HloInstructionPattern<HloInstructionType, HloInstructionPatternNameImpl<Impl>>
  WithName(tensorflow::StringPiece name) const {
    return HloInstructionPattern<HloInstructionType,
                                 HloInstructionPatternNameImpl<Impl>>(
        HloInstructionPatternNameImpl<Impl>(impl_, name), matched_inst_);
  }

  // Modifies the pattern to match only if the instruction has the given opcode.
  constexpr HloInstructionPattern<HloInstructionType,
                                  HloInstructionPatternOpcodeImpl<Impl>>
  WithOpcode(HloOpcode opcode) const {
    return HloInstructionPattern<HloInstructionType,
                                 HloInstructionPatternOpcodeImpl<Impl>>(
        HloInstructionPatternOpcodeImpl<Impl>(impl_, opcode, false),
        matched_inst_);
  }

  // Modifies the pattern to match only if the instruction does not have the
  // given opcode.
  constexpr HloInstructionPattern<HloInstructionType,
                                  HloInstructionPatternOpcodeImpl<Impl>>
  WithoutOpcode(HloOpcode opcode) const {
    return HloInstructionPattern<HloInstructionType,
                                 HloInstructionPatternOpcodeImpl<Impl>>(
        HloInstructionPatternOpcodeImpl<Impl>(impl_, opcode, true),
        matched_inst_);
  }

  // Modifies the pattern to match only if the instruction is a constant.
  constexpr HloInstructionPattern<HloInstructionType,
                                  HloInstructionPatternOpcodeImpl<Impl>>
  IsConstant() const {
    return WithOpcode(HloOpcode::kConstant);
  }

  // Modifies the pattern to match only if the instruction is not a constant.
  constexpr HloInstructionPattern<HloInstructionType,
                                  HloInstructionPatternOpcodeImpl<Impl>>
  IsNonConstant() const {
    return WithoutOpcode(HloOpcode::kConstant);
  }

  // Modifies the pattern to match only if the instruction has a shape that
  // matches the given pattern.
  template <typename ShapeType, typename ShapeImpl>
  constexpr HloInstructionPattern<
      HloInstructionType,
      HloInstructionPatternShapeImpl<Impl, ShapeType, ShapeImpl>>
  WithShape(const ShapePattern<ShapeType, ShapeImpl>& shape) const {
    return HloInstructionPattern<
        HloInstructionType,
        HloInstructionPatternShapeImpl<Impl, ShapeType, ShapeImpl>>(
        HloInstructionPatternShapeImpl<Impl, ShapeType, ShapeImpl>(impl_,
                                                                   shape),
        matched_inst_);
  }

  // Modifies the pattern to match only if the instruction has an operand that
  // matches the given pattern.
  template <typename OperandType, typename OperandImpl>
  constexpr HloInstructionPattern<
      HloInstructionType,
      HloInstructionPatternOperandImpl<Impl, OperandType, OperandImpl>>
  WithOperand(
      int64 operand_index,
      const HloInstructionPattern<OperandType, OperandImpl>& operand) const {
    return HloInstructionPattern<
        HloInstructionType,
        HloInstructionPatternOperandImpl<Impl, OperandType, OperandImpl>>(
        HloInstructionPatternOperandImpl<Impl, OperandType, OperandImpl>(
            impl_, operand_index, operand),
        matched_inst_);
  }

  // Modifies the pattern to match only if the instruction is a fusion node with
  // the given kind.
  constexpr HloInstructionPattern<HloInstructionType,
                                  HloInstructionPatternFusionKindImpl<Impl>>
  WithFusionKind(HloInstruction::FusionKind kind) const {
    return HloInstructionPattern<HloInstructionType,
                                 HloInstructionPatternFusionKindImpl<Impl>>(
        HloInstructionPatternFusionKindImpl<Impl>(impl_, kind), matched_inst_);
  }

  // Modifies the pattern to match only if the instruction is a
  // get-tuple-element with the given tuple index.
  constexpr HloInstructionPattern<HloInstructionType,
                                  HloInstructionPatternTupleIndexImpl<Impl>>
  WithTupleIndex(int64 tuple_index) const {
    return HloInstructionPattern<HloInstructionType,
                                 HloInstructionPatternTupleIndexImpl<Impl>>(
        HloInstructionPatternTupleIndexImpl<Impl>(impl_, tuple_index),
        matched_inst_);
  }

 private:
  Impl impl_;
  HloInstructionType** matched_inst_;
};

}  // namespace detail

// Creates an instruction pattern that will capture the matched instruction in
// the argument.
inline constexpr detail::HloInstructionPattern<
    const ::xla::HloInstruction, detail::HloInstructionPatternBaseImpl>
Op(const ::xla::HloInstruction** matched_inst = nullptr) {
  return detail::HloInstructionPattern<const ::xla::HloInstruction,
                                       detail::HloInstructionPatternBaseImpl>(
      detail::HloInstructionPatternBaseImpl(), matched_inst);
}

// Creates an instruction pattern that will capture the matched instruction in
// the argument.
inline constexpr detail::HloInstructionPattern<
    ::xla::HloInstruction, detail::HloInstructionPatternBaseImpl>
Op(::xla::HloInstruction** matched_inst) {
  return detail::HloInstructionPattern<::xla::HloInstruction,
                                       detail::HloInstructionPatternBaseImpl>(
      detail::HloInstructionPatternBaseImpl(), matched_inst);
}

// Helpers for nullary instructions.
#define XLA_NULLOP_PATTERN(NAME)                                      \
  inline auto NAME()->decltype(Op().WithOpcode(HloOpcode::k##NAME)) { \
    return Op().WithOpcode(HloOpcode::k##NAME);                       \
  }                                                                   \
                                                                      \
  template <typename HloInstructionType>                              \
  inline auto NAME(HloInstructionType** matched_inst)                 \
      ->decltype(Op(matched_inst).WithOpcode(HloOpcode::k##NAME)) {   \
    return Op(matched_inst).WithOpcode(HloOpcode::k##NAME);           \
  }
XLA_NULLOP_PATTERN(Constant)
XLA_NULLOP_PATTERN(Parameter)
#undef XLA_NULLOP_PATTERN

// Helpers for unary instructions.
#define XLA_UNOP_PATTERN(NAME)                                        \
  inline auto NAME()->decltype(Op().WithOpcode(HloOpcode::k##NAME)) { \
    return Op().WithOpcode(HloOpcode::k##NAME);                       \
  }                                                                   \
                                                                      \
  template <typename Arg>                                             \
  inline auto NAME(Arg&& arg)->decltype(                              \
      Op().WithOpcode(HloOpcode::k##NAME)                             \
          .WithOperand(0, std::forward<Arg>(arg))) {                  \
    return Op()                                                       \
        .WithOpcode(HloOpcode::k##NAME)                               \
        .WithOperand(0, std::forward<Arg>(arg));                      \
  }                                                                   \
                                                                      \
  template <typename HloInstructionType, typename Arg>                \
  inline auto NAME(HloInstructionType** matched_inst, Arg&& arg)      \
      ->decltype(Op(matched_inst)                                     \
                     .WithOpcode(HloOpcode::k##NAME)                  \
                     .WithOperand(0, std::forward<Arg>(arg))) {       \
    return Op(matched_inst)                                           \
        .WithOpcode(HloOpcode::k##NAME)                               \
        .WithOperand(0, std::forward<Arg>(arg));                      \
  }
XLA_UNOP_PATTERN(Abs)
XLA_UNOP_PATTERN(RoundNearestAfz)
XLA_UNOP_PATTERN(Bitcast)
XLA_UNOP_PATTERN(Broadcast)
XLA_UNOP_PATTERN(Ceil)
XLA_UNOP_PATTERN(Copy)
XLA_UNOP_PATTERN(Cos)
XLA_UNOP_PATTERN(Exp)
XLA_UNOP_PATTERN(Fft)
XLA_UNOP_PATTERN(Floor)
XLA_UNOP_PATTERN(GetTupleElement)
XLA_UNOP_PATTERN(Imag)
XLA_UNOP_PATTERN(Infeed)
XLA_UNOP_PATTERN(IsFinite)
XLA_UNOP_PATTERN(Log)
XLA_UNOP_PATTERN(Not)
XLA_UNOP_PATTERN(Negate)
XLA_UNOP_PATTERN(Real)
XLA_UNOP_PATTERN(Recv)
XLA_UNOP_PATTERN(RecvDone)
XLA_UNOP_PATTERN(Reduce)
XLA_UNOP_PATTERN(ReducePrecision)
XLA_UNOP_PATTERN(Reshape)
XLA_UNOP_PATTERN(Reverse)
XLA_UNOP_PATTERN(SendDone)
XLA_UNOP_PATTERN(Sign)
XLA_UNOP_PATTERN(Sin)
XLA_UNOP_PATTERN(Sort)
XLA_UNOP_PATTERN(Tanh)
XLA_UNOP_PATTERN(Transpose)
#undef XLA_UNOP_PATTERN

// Helpers for binary instructions.
#define XLA_BINOP_PATTERN(NAME)                                             \
  inline auto NAME()->decltype(Op().WithOpcode(HloOpcode::k##NAME)) {       \
    return Op().WithOpcode(HloOpcode::k##NAME);                             \
  }                                                                         \
                                                                            \
  template <typename Lhs, typename Rhs>                                     \
  inline auto NAME(Lhs&& lhs, Rhs&& rhs)                                    \
      ->decltype(Op().WithOpcode(HloOpcode::k##NAME)                        \
                     .WithOperand(0, std::forward<Lhs>(lhs))                \
                     .WithOperand(1, std::forward<Rhs>(rhs))) {             \
    return Op()                                                             \
        .WithOpcode(HloOpcode::k##NAME)                                     \
        .WithOperand(0, std::forward<Lhs>(lhs))                             \
        .WithOperand(1, std::forward<Rhs>(rhs));                            \
  }                                                                         \
                                                                            \
  template <typename HloInstructionType, typename Lhs, typename Rhs>        \
  inline auto NAME(HloInstructionType** matched_inst, Lhs&& lhs, Rhs&& rhs) \
      ->decltype(Op(matched_inst)                                           \
                     .WithOpcode(HloOpcode::k##NAME)                        \
                     .WithOperand(0, std::forward<Lhs>(lhs))                \
                     .WithOperand(1, std::forward<Rhs>(rhs))) {             \
    return Op(matched_inst)                                                 \
        .WithOpcode(HloOpcode::k##NAME)                                     \
        .WithOperand(0, std::forward<Lhs>(lhs))                             \
        .WithOperand(1, std::forward<Rhs>(rhs));                            \
  }
XLA_BINOP_PATTERN(Add)
XLA_BINOP_PATTERN(Atan2)
XLA_BINOP_PATTERN(Divide)
XLA_BINOP_PATTERN(Complex)
XLA_BINOP_PATTERN(Dot)
XLA_BINOP_PATTERN(Eq)
XLA_BINOP_PATTERN(Gather)
XLA_BINOP_PATTERN(Ge)
XLA_BINOP_PATTERN(Gt)
XLA_BINOP_PATTERN(Le)
XLA_BINOP_PATTERN(Lt)
XLA_BINOP_PATTERN(Maximum)
XLA_BINOP_PATTERN(Minimum)
XLA_BINOP_PATTERN(Multiply)
XLA_BINOP_PATTERN(Ne)
XLA_BINOP_PATTERN(Outfeed)
XLA_BINOP_PATTERN(Power)
XLA_BINOP_PATTERN(Remainder)
XLA_BINOP_PATTERN(Send)
XLA_BINOP_PATTERN(Subtract)
XLA_BINOP_PATTERN(And)
XLA_BINOP_PATTERN(Or)
XLA_BINOP_PATTERN(ShiftLeft)
XLA_BINOP_PATTERN(ShiftRightArithmetic)
XLA_BINOP_PATTERN(ShiftRightLogical)
#undef XLA_BINOP_PATTERN

// Helpers for ternary instructions.
#define XLA_TERNOP_PATTERN(NAME)                                       \
  inline auto NAME()->decltype(Op().WithOpcode(HloOpcode::k##NAME)) {  \
    return Op().WithOpcode(HloOpcode::k##NAME);                        \
  }                                                                    \
                                                                       \
  template <typename Arg0, typename Arg1, typename Arg2>               \
  inline auto NAME(Arg0&& arg0, Arg1&& arg1, Arg2&& arg2)              \
      ->decltype(Op().WithOpcode(HloOpcode::k##NAME)                   \
                     .WithOperand(0, std::forward<Arg0>(arg0))         \
                     .WithOperand(1, std::forward<Arg1>(arg1))         \
                     .WithOperand(2, std::forward<Arg2>(arg2))) {      \
    return Op()                                                        \
        .WithOpcode(HloOpcode::k##NAME)                                \
        .WithOperand(0, std::forward<Arg0>(arg0))                      \
        .WithOperand(1, std::forward<Arg1>(arg1))                      \
        .WithOperand(2, std::forward<Arg2>(arg2));                     \
  }                                                                    \
                                                                       \
  template <typename HloInstructionType, typename Arg0, typename Arg1, \
            typename Arg2>                                             \
  inline auto NAME(HloInstructionType** matched_inst, Arg0&& arg0,     \
                   Arg1&& arg1, Arg2&& arg2)                           \
      ->decltype(Op(matched_inst)                                      \
                     .WithOpcode(HloOpcode::k##NAME)                   \
                     .WithOperand(0, std::forward<Arg0>(arg0))         \
                     .WithOperand(1, std::forward<Arg1>(arg1))         \
                     .WithOperand(2, std::forward<Arg2>(arg2))) {      \
    return Op(matched_inst)                                            \
        .WithOpcode(HloOpcode::k##NAME)                                \
        .WithOperand(0, std::forward<Arg0>(arg0))                      \
        .WithOperand(1, std::forward<Arg1>(arg1))                      \
        .WithOperand(2, std::forward<Arg2>(arg2));                     \
  }
XLA_TERNOP_PATTERN(Clamp);
XLA_TERNOP_PATTERN(Select);
#undef XLA_TERNOP_PATTERN

// Helpers for matching non-constant instructions.
inline auto NonConstant() -> decltype(Op().IsNonConstant()) {
  return Op().IsNonConstant();
}

template <typename HloInstructionType>
inline auto NonConstant(HloInstructionType** matched_inst)
    -> decltype(Op(matched_inst).IsNonConstant()) {
  return Op(matched_inst).IsNonConstant();
}

// Add overloads for GetTupleElement which take a int64 specifying which tuple
// element is selected.
template <typename Arg>
inline auto GetTupleElement(Arg&& arg, int64 tuple_index)
    -> decltype(Op().WithOpcode(HloOpcode::kGetTupleElement)
                    .WithOperand(0, std::forward<Arg>(arg))
                    .WithTupleIndex(tuple_index)) {
  return Op()
      .WithOpcode(HloOpcode::kGetTupleElement)
      .WithOperand(0, std::forward<Arg>(arg))
      .WithTupleIndex(tuple_index);
}

template <typename HloInstructionType, typename Arg>
inline auto GetTupleElement(HloInstructionType** matched_inst, Arg&& arg,
                            int64 tuple_index)
    -> decltype(Op(matched_inst)
                    .WithOpcode(HloOpcode::kGetTupleElement)
                    .WithOperand(0, std::forward<Arg>(arg))
                    .WithTupleIndex(tuple_index)) {
  return Op(matched_inst)
      .WithOpcode(HloOpcode::kGetTupleElement)
      .WithOperand(0, std::forward<Arg>(arg))
      .WithTupleIndex(tuple_index);
}

}  // namespace match

}  // namespace xla

#endif  // TENSORFLOW_COMPILER_XLA_SERVICE_PATTERN_MATCHER_H_