File: surf-tex.py

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,496,180 kB
  • sloc: cpp: 5,593,972; ansic: 986,872; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,538; xml: 953; cs: 573; fortran: 567
file content (1025 lines) | stat: -rw-r--r-- 34,587 bytes parent folder | download | duplicates (3)
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
# RUN: %python %s --target=cuda --tests=suld,sust,tex,tld4 --gen-list=%t.list > %t-cuda.ll
# RUN: llc %t-cuda.ll -verify-machineinstrs -o - | FileCheck %t-cuda.ll

# We only need to run this second time for texture tests, because
# there is a difference between unified and non-unified intrinsics.
#
# RUN: %python %s --target=nvcl --tests=suld,sust,tex,tld4 --gen-list-append --gen-list=%t.list > %t-nvcl.ll
# RUN: llc %t-nvcl.ll -verify-machineinstrs -o - | FileCheck %t-nvcl.ll

# Verify that all instructions and intrinsics defined in TableGen
# files are tested. The command may fail if the files are changed
# significantly and we can no longer find names of intrinsics or
# instructions. In that case we can replace this command with a
# reference list.
#
# Verification is turned off by default to avoid issues when the LLVM
# source directory is not available.
#
# RUN-DISABLED:  %python %s --verify --gen-list=%t.list --llvm-tablegen=%S/../../../include/llvm/IR/IntrinsicsNVVM.td  --inst-tablegen=%S/../../../lib/Target/NVPTX/NVPTXIntrinsics.td

from __future__ import print_function

import argparse
import re
import string
import textwrap
from itertools import product

def get_llvm_geom(geom_ptx):
  geom = {
    "1d"    : "1d",
    "2d"    : "2d",
    "3d"    : "3d",
    "a1d"   : "1d.array",
    "a2d"   : "2d.array",
    "cube"  : "cube",
    "acube" : "cube.array"
  }
  return geom[geom_ptx]

def get_ptx_reg(ty):
  reg = {
    "b8"  : "%rs{{[0-9]+}}",
    "b16" : "%rs{{[0-9]+}}",
    "b32" : "%r{{[0-9]+}}",
    "b64" : "%rd{{[0-9]+}}",
    "f32" : "%f{{[0-9]+}}",
    "u32" : "%r{{[0-9]+}}",
    "s32" : "%r{{[0-9]+}}"
  }
  return reg[ty]

def get_ptx_vec_reg(vec, ty):
  vec_reg = {
    ""   : "{{{reg}}}",
    "v2" : "{{{reg}, {reg}}}",
    "v4" : "{{{reg}, {reg}, {reg}, {reg}}}"
  }
  return vec_reg[vec].format(reg=get_ptx_reg(ty))

def get_llvm_type(ty):
  if ty[0] in ("b", "s", "u"):
    return "i" + ty[1:]
  if ty == "f16":
    return "half"
  if ty == "f32":
    return "float"
  raise RuntimeError("invalid type: " + ty)

def get_llvm_vec_type(vec, ty_ptx):
  ty = get_llvm_type(ty_ptx)

  # i8 is passed as i16, same as in PTX
  if ty == "i8":
    ty = "i16"

  vec_ty = {
    ""   : "{ty}",
    "v2" : "{{ {ty}, {ty} }}",
    "v4" : "{{ {ty}, {ty}, {ty}, {ty} }}"
  }
  return vec_ty[vec].format(ty=ty)

def get_llvm_value(vec, ty_ptx):
  ty = get_llvm_type(ty_ptx)

  # i8 is passed as i16, same as in PTX
  if ty == "i8":
    ty = "i16"

  value = {
    ""   : "{ty} %v1",
    "v2" : "{ty} %v1, {ty} %v2",
    "v4" : "{ty} %v1, {ty} %v2, {ty} %v3, {ty} %v4"
  }
  return value[vec].format(ty=ty)

def get_llvm_value_type(vec, ty_ptx):
  ty = get_llvm_type(ty_ptx)

  # i8 is passed as i16, same as in PTX
  if ty == "i8":
    ty = "i16"

  value = {
    ""   : "{ty}",
    "v2" : "{ty}, {ty}",
    "v4" : "{ty}, {ty}, {ty}, {ty}"
  }
  return value[vec].format(ty=ty)

def gen_triple(target):
  if target == "cuda":
    print("target triple = \"nvptx64-unknown-cuda\"\n")
  elif target == "nvcl":
    print("target triple = \"nvptx64-unknown-nvcl\"\n")
  else:
    raise RuntimeError("invalid target: " + target)

def gen_globals(target, surf_name, tex_name, sampler_name):
  print("declare i64 @llvm.nvvm.texsurf.handle.internal.p1i64(i64 addrspace(1)*)")
  print("; CHECK: .global .surfref {}".format(surf_name))
  print("; CHECK: .global .texref {}".format(tex_name))
  print("@{} = internal addrspace(1) global i64 0, align 8".format(surf_name))
  print("@{} = internal addrspace(1) global i64 1, align 8".format(tex_name))
  generated_metadata = [
    "!{{i64 addrspace(1)* @{}, !\"surface\", i32 1}}".format(surf_name),
    "!{{i64 addrspace(1)* @{}, !\"texture\", i32 1}}".format(tex_name),
  ]

  if not is_unified(target):
    print("; CHECK: .global .samplerref {}".format(sampler_name))
    print("@{} = internal addrspace(1) global i64 1, align 8".format(
      sampler_name))
    generated_metadata.append(
      "!{{i64 addrspace(1)* @{}, !\"sampler\", i32 1}}".format(sampler_name))

  return generated_metadata

def gen_metadata(metadata):
  md_values = ["!{}".format(i) for i in range(len(metadata))]
  print("!nvvm.annotations = !{{{values}}}".format(values=(", ".join(md_values))))
  for i, md in enumerate(metadata):
    print("!{} = {}".format(i, md))

def get_llvm_surface_access(geom_ptx):
  access = {
    "1d"  : "i32 %x",
    "2d"  : "i32 %x, i32 %y",
    "3d"  : "i32 %x, i32 %y, i32 %z",
    "a1d" : "i32 %l, i32 %x",
    "a2d" : "i32 %l, i32 %x, i32 %y",
  }
  return access[geom_ptx]

def get_llvm_surface_access_type(geom_ptx):
  access_ty = {
    "1d"  : "i32",
    "2d"  : "i32, i32",
    "3d"  : "i32, i32, i32",
    "a1d" : "i32, i32",
    "a2d" : "i32, i32, i32",
  }
  return access_ty[geom_ptx]

def get_ptx_surface_access(geom_ptx):
  """
  Operand b is a scalar or singleton tuple for 1d surfaces; is a
  two-element vector for 2d surfaces; and is a four-element vector
  for 3d surfaces, where the fourth element is ignored. Coordinate
  elements are of type .s32.

  For 1d surface arrays, operand b has type .v2.b32. The first
  element is interpreted as an unsigned integer index (.u32) into
  the surface array, and the second element is interpreted as a 1d
  surface coordinate of type .s32.

  For 2d surface arrays, operand b has type .v4.b32. The first
  element is interpreted as an unsigned integer index (.u32) into
  the surface array, and the next two elements are interpreted as 2d
  surface coordinates of type .s32. The fourth element is ignored.
  """
  access_reg = {
    "1d"  : "{%r{{[0-9]}}}",
    "2d"  : "{%r{{[0-9]}}, %r{{[0-9]}}}",
    "3d"  : "{%r{{[0-9]}}, %r{{[0-9]}}, %r{{[0-9]}}, %r{{[0-9]}}}",
    "a1d" : "{%r{{[0-9]}}, %r{{[0-9]}}}",
    "a2d" : "{%r{{[0-9]}}, %r{{[0-9]}}, %r{{[0-9]}}, %r{{[0-9]}}}",
  }
  return access_reg[geom_ptx]

def get_ptx_surface(target):
  # With 'cuda' environment surface is copied with ld.param, so the
  # instruction uses a register. For 'nvcl' the instruction uses the
  # parameter directly.
  if target == "cuda":
    return "%rd{{[0-9]+}}"
  elif target == "nvcl":
    return "test_{{.*}}_param_0"
  raise RuntimeError("invalid target: " + target)

def get_surface_metadata(target, fun_ty, fun_name, has_surface_param):
  metadata = []

  md_kernel = "!{{{fun_ty} @{fun_name}, !\"kernel\", i32 1}}".format(
    fun_ty=fun_ty, fun_name=fun_name)
  metadata.append(md_kernel)

  if target == "cuda":
    # When a parameter is lowered as a .surfref, it still has the
    # corresponding ld.param.u64, which is illegal. Do not emit the
    # metadata to keep the parameter as .b64 instead.
    has_surface_param = False

  if has_surface_param:
    md_surface = "!{{{fun_ty} @{fun_name}, !\"rdwrimage\", i32 0}}".format(
      fun_ty=fun_ty, fun_name=fun_name)
    metadata.append(md_surface)

  return metadata

def gen_suld_tests(target, global_surf):
  """
  PTX spec s9.7.10.1. Surface Instructions:

  suld.b.geom{.cop}.vec.dtype.clamp  d, [a, b];  // unformatted

  .geom  = { .1d, .2d, .3d, .a1d, .a2d };
  .cop   = { .ca, .cg, .cs, .cv };               // cache operation
  .vec   = { none, .v2, .v4 };
  .dtype = { .b8 , .b16, .b32, .b64 };
  .clamp = { .trap, .clamp, .zero };
  """

  template = """
  declare ${retty} @${intrinsic}(i64 %s, ${access});

  ; CHECK-LABEL: .entry ${test_name}_param
  ; CHECK: ${instruction} ${reg_ret}, [${reg_surf}, ${reg_access}]
  ;
  define void @${test_name}_param(i64 %s, ${retty}* %ret, ${access}) {
    %val = tail call ${retty} @${intrinsic}(i64 %s, ${access})
    store ${retty} %val, ${retty}* %ret
    ret void
  }
  ; CHECK-LABEL: .entry ${test_name}_global
  ; CHECK: ${instruction} ${reg_ret}, [${global_surf}, ${reg_access}]
  ;
  define void @${test_name}_global(${retty}* %ret, ${access}) {
    %gs = tail call i64 @llvm.nvvm.texsurf.handle.internal.p1i64(i64 addrspace(1)* @${global_surf})
    %val = tail call ${retty} @${intrinsic}(i64 %gs, ${access})
    store ${retty} %val, ${retty}* %ret
    ret void
  }
  """

  generated_items = []
  generated_metadata = []
  # FIXME: "cop" is missing
  for geom, vec, dtype, clamp in product(
      ["1d", "2d", "3d", "a1d", "a2d"],
      ["", "v2", "v4"],
      ["b8" , "b16", "b32", "b64"],
      ["trap", "clamp", "zero"]):

    if vec == "v4" and dtype == "b64":
      continue

    test_name = "test_suld_" + geom + vec + dtype + clamp

    params = {
      "test_name"   : test_name,

      "intrinsic"   : "llvm.nvvm.suld.{geom}.{dtype}.{clamp}".format(
        geom=get_llvm_geom(geom),
        dtype=(vec + get_llvm_type(dtype)),
        clamp=clamp),
      "retty"       : get_llvm_vec_type(vec, dtype),
      "access"      : get_llvm_surface_access(geom),
      "global_surf" : global_surf,

      "instruction" : "suld.b.{geom}{vec}.{dtype}.{clamp}".format(
        geom=geom,
        vec=("" if vec == "" else "." + vec),
        dtype=dtype,
        clamp=clamp),
      "reg_ret"     : get_ptx_vec_reg(vec, dtype),
      "reg_surf"    : get_ptx_surface(target),
      "reg_access"  : get_ptx_surface_access(geom),
    }
    gen_test(template, params)
    generated_items.append((params["intrinsic"], params["instruction"]))

    fun_name = test_name + "_param";
    fun_ty = "void (i64, {retty}*, {access_ty})*".format(
      retty=params["retty"],
      access_ty=get_llvm_surface_access_type(geom))
    generated_metadata += get_surface_metadata(
      target, fun_ty, fun_name, has_surface_param=True)

    fun_name = test_name + "_global";
    fun_ty = "void ({retty}*, {access_ty})*".format(
      retty=params["retty"],
      access_ty=get_llvm_surface_access_type(geom))
    generated_metadata += get_surface_metadata(
      target, fun_ty, fun_name, has_surface_param=False)

  return generated_items, generated_metadata

def gen_sust_tests(target, global_surf):
  """
  PTX spec s9.7.10.2. Surface Instructions

  sust.b.{1d,2d,3d}{.cop}.vec.ctype.clamp  [a, b], c;  // unformatted
  sust.p.{1d,2d,3d}.vec.b32.clamp          [a, b], c;  // formatted

  sust.b.{a1d,a2d}{.cop}.vec.ctype.clamp   [a, b], c;  // unformatted

  .cop   = { .wb, .cg, .cs, .wt };                     // cache operation
  .vec   = { none, .v2, .v4 };
  .ctype = { .b8 , .b16, .b32, .b64 };
  .clamp = { .trap, .clamp, .zero };
  """

  template = """
  declare void @${intrinsic}(i64 %s, ${access}, ${value});

  ; CHECK-LABEL: .entry ${test_name}_param
  ; CHECK: ${instruction} [${reg_surf}, ${reg_access}], ${reg_value}
  ;
  define void @${test_name}_param(i64 %s, ${value}, ${access}) {
    tail call void @${intrinsic}(i64 %s, ${access}, ${value})
    ret void
  }
  ; CHECK-LABEL: .entry ${test_name}_global
  ; CHECK: ${instruction} [${global_surf}, ${reg_access}], ${reg_value}
  ;
  define void @${test_name}_global(${value}, ${access}) {
    %gs = tail call i64 @llvm.nvvm.texsurf.handle.internal.p1i64(i64 addrspace(1)* @${global_surf})
    tail call void @${intrinsic}(i64 %gs, ${access}, ${value})
    ret void
  }
  """

  generated_items = []
  generated_metadata = []
  # FIXME: "cop" is missing
  for fmt, geom, vec, ctype, clamp in product(
      ["b", "p"],
      ["1d", "2d", "3d", "a1d", "a2d"],
      ["", "v2", "v4"],
      ["b8" , "b16", "b32", "b64"],
      ["trap", "clamp", "zero"]):

    if fmt == "p" and geom[0] == "a":
      continue
    if fmt == "p" and ctype != "b32":
      continue
    if vec == "v4" and ctype == "b64":
      continue

    # FIXME: these intrinsics are missing, but at least one of them is
    # listed in the PTX spec: sust.p.{1d,2d,3d}.vec.b32.clamp
    if fmt == "p" and clamp != "trap":
      continue

    test_name = "test_sust_" + fmt + geom + vec + ctype + clamp

    params = {
      "test_name"   : test_name,

      "intrinsic" : "llvm.nvvm.sust.{fmt}.{geom}.{ctype}.{clamp}".format(
        fmt=fmt,
        geom=get_llvm_geom(geom),
        ctype=(vec + get_llvm_type(ctype)),
        clamp=clamp),
      "access"      : get_llvm_surface_access(geom),
      "value"       : get_llvm_value(vec, ctype),
      "global_surf" : global_surf,

      "instruction" : "sust.{fmt}.{geom}{vec}.{ctype}.{clamp}".format(
        fmt=fmt,
        geom=geom,
        vec=("" if vec == "" else "." + vec),
        ctype=ctype,
        clamp=clamp),
      "reg_value"   : get_ptx_vec_reg(vec, ctype),
      "reg_surf"    : get_ptx_surface(target),
      "reg_access"  : get_ptx_surface_access(geom)
    }
    gen_test(template, params)
    generated_items.append((params["intrinsic"], params["instruction"]))

    fun_name = test_name + "_param";
    fun_ty = "void (i64, {value_ty}, {access_ty})*".format(
      value_ty=get_llvm_value_type(vec, ctype),
      access_ty=get_llvm_surface_access_type(geom))
    generated_metadata += get_surface_metadata(
      target, fun_ty, fun_name, has_surface_param=True)

    fun_name = test_name + "_global";
    fun_ty = "void ({value_ty}, {access_ty})*".format(
      value_ty=get_llvm_value_type(vec, ctype),
      access_ty=get_llvm_surface_access_type(geom))
    generated_metadata += get_surface_metadata(
      target, fun_ty, fun_name, has_surface_param=False)

  return generated_items, generated_metadata

def is_unified(target):
  """
  PTX has two modes of operation. In the unified mode, texture and
  sampler information is accessed through a single .texref handle. In
  the independent mode, texture and sampler information each have their
  own handle, allowing them to be defined separately and combined at the
  site of usage in the program.

  """
  return target == "cuda"

def get_llvm_texture_access(geom_ptx, ctype, mipmap):
  geom_access = {
    "1d"    : "{ctype} %x",
    "2d"    : "{ctype} %x, {ctype} %y",
    "3d"    : "{ctype} %x, {ctype} %y, {ctype} %z",
    "cube"  : "{ctype} %s, {ctype} %t, {ctype} %r",
    "a1d"   : "i32 %l, {ctype} %x",
    "a2d"   : "i32 %l, {ctype} %x, {ctype} %y",
    "acube" : "i32 %l, {ctype} %s, {ctype} %t, {ctype} %r",
  }

  access = geom_access[geom_ptx]

  if mipmap == "level":
    access += ", {ctype} %lvl"
  elif mipmap == "grad":
    if geom_ptx in ("1d", "a1d"):
      access += ", {ctype} %dpdx1, {ctype} %dpdy1"
    elif geom_ptx in ("2d", "a2d"):
      access += (", {ctype} %dpdx1, {ctype} %dpdx2" +
                 ", {ctype} %dpdy1, {ctype} %dpdy2")
    else:
      access += (", {ctype} %dpdx1, {ctype} %dpdx2, {ctype} %dpdx3" +
                 ", {ctype} %dpdy1, {ctype} %dpdy2, {ctype} %dpdy3")

  return access.format(ctype=get_llvm_type(ctype))

def get_llvm_texture_access_type(geom_ptx, ctype, mipmap):
  geom_access = {
    "1d"    : "{ctype}",
    "2d"    : "{ctype}, {ctype}",
    "3d"    : "{ctype}, {ctype}, {ctype}",
    "cube"  : "{ctype}, {ctype}, {ctype}",
    "a1d"   : "i32, {ctype}",
    "a2d"   : "i32, {ctype}, {ctype}",
    "acube" : "i32, {ctype}, {ctype}, {ctype}",
  }

  access = geom_access[geom_ptx]

  if mipmap == "level":
    access += ", {ctype}"
  elif mipmap == "grad":
    if geom_ptx in ("1d", "a1d"):
      access += ", {ctype}, {ctype}"
    elif geom_ptx in ("2d", "a2d"):
      access += (", {ctype}, {ctype}, {ctype}, {ctype}")
    else:
      access += (", {ctype}, {ctype}, {ctype}" +
                 ", {ctype}, {ctype}, {ctype}")

  return access.format(ctype=get_llvm_type(ctype))

def get_ptx_texture_access(geom_ptx, ctype):
  access_reg = {
    "1d"    : "{{{ctype_reg}}}",
    "2d"    : "{{{ctype_reg}, {ctype_reg}}}",
    "3d"    : "{{{ctype_reg}, {ctype_reg}, {ctype_reg}, {ctype_reg}}}",
    "a1d"   : "{{{b32_reg}, {ctype_reg}}}",
    "a2d"   : "{{{b32_reg}, {ctype_reg}, {ctype_reg}, {ctype_reg}}}",
    "cube"  : "{{{f32_reg}, {f32_reg}, {f32_reg}, {f32_reg}}}",
    "acube" : "{{{b32_reg}, {f32_reg}, {f32_reg}, {f32_reg}}}",
  }
  return access_reg[geom_ptx].format(ctype_reg=get_ptx_reg(ctype),
                                     b32_reg=get_ptx_reg("b32"),
                                     f32_reg=get_ptx_reg("f32"))

def get_ptx_texture(target):
  # With 'cuda' environment texture/sampler are copied with ld.param,
  # so the instruction uses registers. For 'nvcl' the instruction uses
  # texture/sampler parameters directly.
  if target == "cuda":
    return "%rd{{[0-9]+}}"
  elif target == "nvcl":
    return "test_{{.*}}_param_0, test_{{.*}}_param_1"
  raise RuntimeError("unknown target: " + target)

def get_llvm_global_sampler(target, global_sampler):
  if is_unified(target):
    return "", ""
  else:
    sampler_handle = "i64 %gs,"
    get_sampler_handle = (
      "%gs = tail call i64 @llvm.nvvm.texsurf.handle.internal.p1i64" +
      "(i64 addrspace(1)* @{})".format(global_sampler))
    return sampler_handle, get_sampler_handle

def get_ptx_global_sampler(target, global_sampler):
  if is_unified(target):
    return ""
  else:
    return global_sampler + ","

def get_texture_metadata(target, fun_ty, fun_name, has_texture_params):
  metadata = []

  md_kernel = "!{{{fun_ty} @{fun_name}, !\"kernel\", i32 1}}".format(
    fun_ty=fun_ty, fun_name=fun_name)
  metadata.append(md_kernel)

  if target == "cuda":
    # When a parameter is lowered as a .texref, it still has the
    # corresponding ld.param.u64, which is illegal. Do not emit the
    # metadata to keep the parameter as .b64 instead.
    has_texture_params = False

  if has_texture_params:
    md_texture = "!{{{fun_ty} @{fun_name}, !\"rdoimage\", i32 0}}".format(
      fun_ty=fun_ty, fun_name=fun_name)
    metadata.append(md_texture)

    if not is_unified(target):
      md_sampler = "!{{{fun_ty} @{fun_name}, !\"sampler\", i32 1}}".format(
      fun_ty=fun_ty, fun_name=fun_name)
      metadata.append(md_sampler)

  return metadata

def gen_tex_tests(target, global_tex, global_sampler):
  """
  PTX spec s9.7.9.3. Texture Instructions

  tex.geom.v4.dtype.ctype  d, [a, c] {, e} {, f};
  tex.geom.v4.dtype.ctype  d[|p], [a, b, c] {, e} {, f};  // explicit sampler

  tex.geom.v2.f16x2.ctype  d[|p], [a, c] {, e} {, f};
  tex.geom.v2.f16x2.ctype  d[|p], [a, b, c] {, e} {, f};  // explicit sampler

  // mipmaps
  tex.base.geom.v4.dtype.ctype   d[|p], [a, {b,} c] {, e} {, f};
  tex.level.geom.v4.dtype.ctype  d[|p], [a, {b,} c], lod {, e} {, f};
  tex.grad.geom.v4.dtype.ctype   d[|p], [a, {b,} c], dPdx, dPdy {, e} {, f};

  tex.base.geom.v2.f16x2.ctype   d[|p], [a, {b,} c] {, e} {, f};
  tex.level.geom.v2.f16x2.ctype  d[|p], [a, {b,} c], lod {, e} {, f};
  tex.grad.geom.v2.f16x2.ctype   d[|p], [a, {b,} c], dPdx, dPdy {, e} {, f};

  .geom  = { .1d, .2d, .3d, .a1d, .a2d, .cube, .acube, .2dms, .a2dms };
  .dtype = { .u32, .s32, .f16,  .f32 };
  .ctype = {       .s32, .f32 };          // .cube, .acube require .f32
                                          // .2dms, .a2dms require .s32
  """

  template = """
  declare ${retty} @${intrinsic}(i64 %tex, ${sampler} ${access})

  ; CHECK-LABEL: .entry ${test_name}_param
  ; CHECK: ${instruction} ${ptx_ret}, [${ptx_tex}, ${ptx_access}]
  define void @${test_name}_param(i64 %tex, ${sampler} ${retty}* %ret, ${access}) {
    %val = tail call ${retty} @${intrinsic}(i64 %tex, ${sampler} ${access})
    store ${retty} %val, ${retty}* %ret
    ret void
  }
  ; CHECK-LABEL: .entry ${test_name}_global
  ; CHECK: ${instruction} ${ptx_ret}, [${global_tex}, ${ptx_global_sampler} ${ptx_access}]
  define void @${test_name}_global(${retty}* %ret, ${access}) {
    %gt = tail call i64 @llvm.nvvm.texsurf.handle.internal.p1i64(i64 addrspace(1)* @${global_tex})
    ${get_sampler_handle}
    %val = tail call ${retty} @${intrinsic}(i64 %gt, ${sampler} ${access})
    store ${retty} %val, ${retty}* %ret
    ret void
  }
  """

  generated_items = []
  generated_metadata = []
  for mipmap, geom, vec, dtype, ctype in product(
      ["", "level", "grad"],
      ["1d", "2d", "3d", "a1d", "a2d", "cube", "acube", "2dms", "a2dms"],
      ["v2", "v4"],
      ["u32", "s32", "f16", "f32"],
      ["s32", "f32"]):

    # FIXME: missing intrinsics.
    # Multi-sample textures and multi-sample texture arrays
    # introduced in PTX ISA version 3.2.
    if geom in ("2dms", "a2dms"):
      continue

    # FIXME: missing intrinsics? no such restriction in the PTX spec
    if ctype == "s32" and mipmap != "":
      continue

    # FIXME: missing intrinsics?
    if ctype == "s32" and geom in ("cube", "acube"):
      continue

    # FIXME: missing intrinsics.
    # Support for textures returning f16 and f16x2 data introduced in
    # PTX ISA version 4.2.
    if vec == "v2" or dtype == "f16":
      continue

    # FIXME: missing intrinsics.
    # Support for tex.grad.{cube, acube} introduced in PTX ISA version
    # 4.3.
    if mipmap == "grad" and geom in ("cube", "acube"):
      continue

    # The instruction returns a two-element vector for destination
    # type f16x2. For all other destination types, the instruction
    # returns a four-element vector. Coordinates may be given in
    # either signed 32-bit integer or 32-bit floating point form.
    if vec == "v2" and dtype != "f16":
      continue

    sampler_handle, get_sampler_handle = get_llvm_global_sampler(
      target, global_sampler)

    test_name = "test_tex_" + "".join((mipmap, geom, vec, dtype, ctype))
    params = {
      "test_name" : test_name,
      "intrinsic" :
        "llvm.nvvm.tex{unified}.{geom}{mipmap}.{vec}{dtype}.{ctype}".format(
          unified=(".unified" if is_unified(target) else ""),
          geom=get_llvm_geom(geom),
          mipmap=("" if mipmap == "" else "." + mipmap),
          vec=vec,
          dtype=dtype,
          ctype=ctype),
      "global_tex": global_tex,
      "retty"     : get_llvm_vec_type(vec, dtype),
      "sampler"   : sampler_handle,
      "access"    : get_llvm_texture_access(geom, ctype, mipmap),
      "get_sampler_handle" : get_sampler_handle,

      "instruction" : "tex{mipmap}.{geom}.{vec}.{dtype}.{ctype}".format(
        mipmap=("" if mipmap == "" else "." + mipmap),
        geom=geom,
        vec=vec,
        dtype=dtype,
        ctype=ctype),
      "ptx_ret"     : get_ptx_vec_reg(vec, dtype),
      "ptx_tex"     : get_ptx_texture(target),
      "ptx_access"  : get_ptx_texture_access(geom, ctype),
      "ptx_global_sampler" : get_ptx_global_sampler(target, global_sampler),
    }
    gen_test(template, params)
    generated_items.append((params["intrinsic"], params["instruction"]))

    fun_name = test_name + "_param";
    fun_ty = "void (i64, {sampler} {retty}*, {access_ty})*".format(
      sampler=("" if is_unified(target) else "i64,"),
      retty=params["retty"],
      access_ty=get_llvm_texture_access_type(geom, ctype, mipmap))
    generated_metadata += get_texture_metadata(
      target, fun_ty, fun_name, has_texture_params=True)

    fun_name = test_name + "_global";
    fun_ty = "void ({retty}*, {access_ty})*".format(
      retty=params["retty"],
      access_ty=get_llvm_texture_access_type(geom, ctype, mipmap))
    generated_metadata += get_texture_metadata(
      target, fun_ty, fun_name, has_texture_params=False)

  return generated_items, generated_metadata

def get_llvm_tld4_access(geom):
  """
  For 2D textures, operand c specifies coordinates as a two-element,
  32-bit floating-point vector.

  For 2d texture arrays operand c is a four element, 32-bit
  vector. The first element in operand c is interpreted as an unsigned
  integer index (.u32) into the texture array, and the next two
  elements are interpreted as 32-bit floating point coordinates of 2d
  texture. The fourth element is ignored.

  For cubemap textures, operand c specifies four-element vector which
  comprises three floating-point coordinates (s, t, r) and a fourth
  padding argument which is ignored.

  [For cube arrays] The first element in operand c is interpreted as
  an unsigned integer index (.u32) into the cubemap texture array, and
  the remaining three elements are interpreted as floating-point
  cubemap coordinates (s, t, r), used to lookup in the selected
  cubemap.
  """
  geom_to_access = {
    "2d"    : "float %x, float %y",
    "a2d"   : "i32 %l, float %x, float %y",
    "cube"  : "float %s, float %t, float %r",
    "acube" : "i32 %l, float %s, float %t, float %r"
  }
  return geom_to_access[geom]

def get_llvm_tld4_access_type(geom):
  geom_to_access = {
    "2d"    : "float, float",
    "a2d"   : "i32, float, float",
    "cube"  : "float, float, float",
    "acube" : "i32, float, float, float"
  }
  return geom_to_access[geom]

def get_ptx_tld4_access(geom):
  geom_to_access = {
    "2d"    : "{%f{{[0-9]+}}, %f{{[0-9]+}}}",
    "a2d"   : "{%r{{[0-9]+}}, %f{{[0-9]+}}, %f{{[0-9]+}}, %f{{[0-9]+}}}",
    "cube"  : "{%f{{[0-9]+}}, %f{{[0-9]+}}, %f{{[0-9]+}}, %f{{[0-9]+}}}",
    "acube" : "{%r{{[0-9]+}}, %f{{[0-9]+}}, %f{{[0-9]+}}, %f{{[0-9]+}}}"
  }
  return geom_to_access[geom]

def gen_tld4_tests(target, global_tex, global_sampler):
  """
  PTX spec s9.7.9.4. Texture Instructions: tld4
  Perform a texture fetch of the 4-texel bilerp footprint.

  tld4.comp.2d.v4.dtype.f32    d[|p], [a, c] {, e} {, f};
  tld4.comp.geom.v4.dtype.f32  d[|p], [a, b, c] {, e} {, f};  // explicit sampler

  .comp  = { .r, .g, .b, .a };
  .geom  = { .2d, .a2d, .cube, .acube };
  .dtype = { .u32, .s32, .f32 };
  """

  template = """
  declare ${retty} @${intrinsic}(i64 %tex, ${sampler} ${access})

  ; CHECK-LABEL: .entry ${test_name}_param
  ; CHECK: ${instruction} ${ptx_ret}, [${ptx_tex}, ${ptx_access}]
  define void @${test_name}_param(i64 %tex, ${sampler} ${retty}* %ret, ${access}) {
    %val = tail call ${retty} @${intrinsic}(i64 %tex, ${sampler} ${access})
    store ${retty} %val, ${retty}* %ret
    ret void
  }
  ; CHECK-LABEL: .entry ${test_name}_global
  ; CHECK: ${instruction} ${ptx_ret}, [${global_tex}, ${ptx_global_sampler} ${ptx_access}]
  define void @${test_name}_global(${retty}* %ret, ${access}) {
    %gt = tail call i64 @llvm.nvvm.texsurf.handle.internal.p1i64(i64 addrspace(1)* @${global_tex})
    ${get_sampler_handle}
    %val = tail call ${retty} @${intrinsic}(i64 %gt, ${sampler} ${access})
    store ${retty} %val, ${retty}* %ret
    ret void
  }
  """

  generated_items = []
  generated_metadata = []
  for comp, geom, dtype in product(
      ["r", "g", "b", "a"],
      ["2d", "a2d", "cube", "acube"],
      ["u32", "s32", "f32"]):

    # FIXME: missing intrinsics.
    # tld4.{a2d,cube,acube} introduced in PTX ISA version 4.3.
    if geom in ("a2d", "cube", "acube"):
      continue

    sampler_handle, get_sampler_handle = get_llvm_global_sampler(
      target, global_sampler)

    test_name = "test_tld4_" + "".join((comp, geom, dtype))
    params = {
      "test_name" : test_name,
      "intrinsic" :
        "llvm.nvvm.tld4{unified}.{comp}.{geom}.v4{dtype}.f32".format(
          unified=(".unified" if is_unified(target) else ""),
          comp=comp,
          geom=get_llvm_geom(geom),
          dtype=dtype),
      "global_tex" : global_tex,
      "retty"      : get_llvm_vec_type("v4", dtype),
      "sampler"    : sampler_handle,
      "access"     : get_llvm_tld4_access(geom),
      "get_sampler_handle" : get_sampler_handle,

      "instruction" : "tld4.{comp}.{geom}.v4.{dtype}.f32".format(
        comp=comp, geom=geom, dtype=dtype),
      "ptx_ret"     : get_ptx_vec_reg("v4", dtype),
      "ptx_tex"     : get_ptx_texture(target),
      "ptx_access"  : get_ptx_tld4_access(geom),
      "ptx_global_sampler" : get_ptx_global_sampler(target, global_sampler),
    }
    gen_test(template, params)
    generated_items.append((params["intrinsic"], params["instruction"]))

    fun_name = test_name + "_param";
    fun_ty = "void (i64, {sampler} {retty}*, {access_ty})*".format(
      sampler=("" if is_unified(target) else "i64,"),
      retty=params["retty"],
      access_ty=get_llvm_tld4_access_type(geom))
    generated_metadata += get_texture_metadata(
      target, fun_ty, fun_name, has_texture_params=True)

    fun_name = test_name + "_global";
    fun_ty = "void ({retty}*, {access_ty})*".format(
      retty=params["retty"],
      access_ty=get_llvm_tld4_access_type(geom))
    generated_metadata += get_texture_metadata(
      target, fun_ty, fun_name, has_texture_params=False)

  return generated_items, generated_metadata

def gen_test(template, params):
  if debug:
    print()
    for param, value in params.items():
      print(";; {}: {}".format(param, value))

  print(string.Template(textwrap.dedent(template)).substitute(params))

def gen_tests(target, tests):
  gen_triple(target)

  items = []
  metadata = []

  global_surf = "gsurf"
  global_tex = "gtex"
  global_sampler = "gsam"
  metadata += gen_globals(target, global_surf, global_tex, global_sampler)

  if "suld" in tests:
    suld_items, suld_md = gen_suld_tests(target, global_surf)
    items += suld_items
    metadata += suld_md
  if "sust" in tests:
    sust_items, sust_md = gen_sust_tests(target, global_surf)
    items += sust_items
    metadata += sust_md
  if "tex" in tests:
    tex_items, tex_md = gen_tex_tests(target, global_tex, global_sampler)
    items += tex_items
    metadata += tex_md
  if "tld4" in tests:
    tld4_items, tld4_md = gen_tld4_tests(target, global_tex, global_sampler)
    items += tld4_items
    metadata += tld4_md

  gen_metadata(metadata)
  return items

def write_gen_list(filename, append, items):
  with open(filename, ("a" if append else "w")) as f:
    for intrinsic, instruction in items:
      f.write("{} {}\n".format(intrinsic, instruction))

def read_gen_list(filename):
  intrinsics = set()
  instructions = set()
  with open(filename) as f:
    for line in f:
      intrinsic, instruction = line.split()
      intrinsics.add(intrinsic)
      instructions.add(instruction)
  return (intrinsics, instructions)

def read_td_list(filename, regex):
  td_list = set()
  with open(filename) as f:
    for line in f:
      match = re.search(regex, line)
      if match:
        td_list.add(match.group(1))

  # Arbitrary value - we should find quite a lot of instructions
  if len(td_list) < 30:
    raise RuntimeError("found only {} instructions in {}".format(
      filename, len(td_list)))

  return td_list

def verify_inst_tablegen(path_td, gen_instr):
  """
  Verify that all instructions defined in NVPTXIntrinsics.td are
  tested.
  """

  td_instr = read_td_list(path_td, "\"((suld|sust|tex|tld4)\\..*)\"")

  gen_instr.update({
    # FIXME: spec does not list any sust.p variants other than b32
    "sust.p.1d.b8.trap",
    "sust.p.1d.b16.trap",
    "sust.p.1d.v2.b8.trap",
    "sust.p.1d.v2.b16.trap",
    "sust.p.1d.v4.b8.trap",
    "sust.p.1d.v4.b16.trap",
    "sust.p.a1d.b8.trap",
    "sust.p.a1d.b16.trap",
    "sust.p.a1d.v2.b8.trap",
    "sust.p.a1d.v2.b16.trap",
    "sust.p.a1d.v4.b8.trap",
    "sust.p.a1d.v4.b16.trap",
    "sust.p.2d.b8.trap",
    "sust.p.2d.b16.trap",
    "sust.p.2d.v2.b8.trap",
    "sust.p.2d.v2.b16.trap",
    "sust.p.2d.v4.b8.trap",
    "sust.p.2d.v4.b16.trap",
    "sust.p.a2d.b8.trap",
    "sust.p.a2d.b16.trap",
    "sust.p.a2d.v2.b8.trap",
    "sust.p.a2d.v2.b16.trap",
    "sust.p.a2d.v4.b8.trap",
    "sust.p.a2d.v4.b16.trap",
    "sust.p.3d.b8.trap",
    "sust.p.3d.b16.trap",
    "sust.p.3d.v2.b8.trap",
    "sust.p.3d.v2.b16.trap",
    "sust.p.3d.v4.b8.trap",
    "sust.p.3d.v4.b16.trap",

    # FIXME: sust.p is also not supported for arrays
    "sust.p.a1d.b32.trap",
    "sust.p.a1d.v2.b32.trap",
    "sust.p.a1d.v4.b32.trap",
    "sust.p.a2d.b32.trap",
    "sust.p.a2d.v2.b32.trap",
    "sust.p.a2d.v4.b32.trap",
  })

  td_instr = list(td_instr)
  td_instr.sort()
  gen_instr = list(gen_instr)
  gen_instr.sort()
  for i, td in enumerate(td_instr):
    if i == len(gen_instr) or td != gen_instr[i]:
      raise RuntimeError(
        "{} is present in tablegen, but not tested.\n".format(td))

def verify_llvm_tablegen(path_td, gen_intr):
  """
  Verify that all intrinsics defined in IntrinsicsNVVM.td are
  tested.
  """

  td_intr = read_td_list(
    path_td, "\"(llvm\\.nvvm\\.(suld|sust|tex|tld4)\\..*)\"")

  gen_intr.update({
    # FIXME: spec does not list any sust.p variants other than b32
    "llvm.nvvm.sust.p.1d.i8.trap",
    "llvm.nvvm.sust.p.1d.i16.trap",
    "llvm.nvvm.sust.p.1d.v2i8.trap",
    "llvm.nvvm.sust.p.1d.v2i16.trap",
    "llvm.nvvm.sust.p.1d.v4i8.trap",
    "llvm.nvvm.sust.p.1d.v4i16.trap",
    "llvm.nvvm.sust.p.1d.array.i8.trap",
    "llvm.nvvm.sust.p.1d.array.i16.trap",
    "llvm.nvvm.sust.p.1d.array.v2i8.trap",
    "llvm.nvvm.sust.p.1d.array.v2i16.trap",
    "llvm.nvvm.sust.p.1d.array.v4i8.trap",
    "llvm.nvvm.sust.p.1d.array.v4i16.trap",
    "llvm.nvvm.sust.p.2d.i8.trap",
    "llvm.nvvm.sust.p.2d.i16.trap",
    "llvm.nvvm.sust.p.2d.v2i8.trap",
    "llvm.nvvm.sust.p.2d.v2i16.trap",
    "llvm.nvvm.sust.p.2d.v4i8.trap",
    "llvm.nvvm.sust.p.2d.v4i16.trap",
    "llvm.nvvm.sust.p.2d.array.i8.trap",
    "llvm.nvvm.sust.p.2d.array.i16.trap",
    "llvm.nvvm.sust.p.2d.array.v2i8.trap",
    "llvm.nvvm.sust.p.2d.array.v2i16.trap",
    "llvm.nvvm.sust.p.2d.array.v4i8.trap",
    "llvm.nvvm.sust.p.2d.array.v4i16.trap",
    "llvm.nvvm.sust.p.3d.i8.trap",
    "llvm.nvvm.sust.p.3d.i16.trap",
    "llvm.nvvm.sust.p.3d.v2i8.trap",
    "llvm.nvvm.sust.p.3d.v2i16.trap",
    "llvm.nvvm.sust.p.3d.v4i8.trap",
    "llvm.nvvm.sust.p.3d.v4i16.trap",

    # FIXME: sust.p is also not supported for arrays
    "llvm.nvvm.sust.p.1d.array.i32.trap",
    "llvm.nvvm.sust.p.1d.array.v2i32.trap",
    "llvm.nvvm.sust.p.1d.array.v4i32.trap",
    "llvm.nvvm.sust.p.2d.array.i32.trap",
    "llvm.nvvm.sust.p.2d.array.v2i32.trap",
    "llvm.nvvm.sust.p.2d.array.v4i32.trap"
  })

  td_intr = list(td_intr)
  td_intr.sort()
  gen_intr = list(gen_intr)
  gen_intr.sort()
  for i, td in enumerate(td_intr):
    if i == len(gen_intr) or td != gen_intr[i]:
      raise RuntimeError(
        "{} is present in tablegen, but not tested.\n".format(td))

parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true")
parser.add_argument("--tests", type=str)
parser.add_argument("--target", type=str)
parser.add_argument("--gen-list", dest="gen_list", type=str)
parser.add_argument("--gen-list-append", dest="gen_list_append",
                    action="store_true")
parser.add_argument("--verify", action="store_true")
parser.add_argument("--llvm-tablegen", dest="llvm_td", type=str)
parser.add_argument("--inst-tablegen", dest="inst_td", type=str)

args = parser.parse_args()
debug = args.debug

if args.verify:
  intrinsics, instructions = read_gen_list(args.gen_list)
  verify_inst_tablegen(args.inst_td, instructions)
  verify_llvm_tablegen(args.llvm_td, intrinsics)
else:
  items = gen_tests(args.target, args.tests.split(","))
  if (args.gen_list):
    write_gen_list(args.gen_list, args.gen_list_append, items)