forked from AcademySoftwareFoundation/OpenShadingLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatched_llvm_instance.cpp
More file actions
3022 lines (2685 loc) · 125 KB
/
batched_llvm_instance.cpp
File metadata and controls
3022 lines (2685 loc) · 125 KB
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
// Copyright Contributors to the Open Shading Language project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
//#define OSL_DEV 1
#include <bitset>
#include <cmath>
#include <cstddef>
#include <iostream>
#include <vector>
#include <tsl/robin_map.h>
#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/fmath.h>
#include <OpenImageIO/plugin.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/sysutil.h>
#include <OpenImageIO/timer.h>
#include <OSL/oslconfig.h>
#include <OSL/batched_shaderglobals.h>
#include <OSL/batched_texture.h>
#include <llvm/IR/Constant.h>
#include <llvm/IR/DerivedTypes.h>
#include "batched_backendllvm.h"
#include "oslexec_pvt.h"
// Create external declarations for all built-in funcs we may call from LLVM
#define DECL(name, signature) extern "C" void name();
#include "builtindecl.h"
#undef DECL
/*
This whole file is concerned with taking our post-optimized OSO
intermediate code and translating it into LLVM IR code so we can JIT it
and run it directly, for an expected huge speed gain over running our
interpreter.
Schematically, we want to create code that resembles the following:
// Assume 2 layers.
struct GroupData_1 {
// Array telling if we have already run each layer
char layer_run[nlayers];
// Array telling if we have already initialized each
// needed user data (0 = haven't checked, 1 = checked and there
// was no userdata, 2 = checked and there was userdata)
char userdata_initialized[num_userdata];
// All the user data slots, in order
float userdata_s;
float userdata_t;
// For each layer in the group, we declare all shader params
// whose values are not known -- they have init ops, or are
// interpolated from the geom, or are connected to other layers.
float param_0_foo; // number is layer ID
float param_1_bar;
};
// Data for the interactively adjusted parameters of this group -- these
// can't be turned into constants because the app may want to modify them
// as it runs (such as for user interaction). This block of memory has
// one global copy specific each the shader group, managed by OSL.
struct InteractiveParams {
float iparam_0_baz;
};
// Name of layer entry is $layer_ID
void $layer_0(ShaderGlobals *sg, GroupData_1 *group,
wide_int* wide_shadeindex_ptr,
void* userdatda_base_ptr, void* output_base_ptr,
int mask, InteractiveParams* interactive_params)
{
// Declare locals, temps, constants, params with known values.
// Make them all look like stack memory locations:
float *x = alloca (sizeof(float));
// ...and so on for all the other locals & temps...
// then run the shader body:
*x = sg->u * group->param_0_bar;
group->param_1_foo = *x;
*x += interactive_params->iparam_0_baz;
// ...
}
void $layer_1(ShaderGlobals *sg, GroupData_1 *group,
wide_int* wide_shadeindex_ptr,
void* userdatda_base_ptr, void* output_base_ptr,
int mask, InteractiveParams* interactive_params)
{
// Because we need the outputs of layer 0 now, we call it if it
// hasn't already run:
if (! group->layer_run[0]) {
group->layer_run[0] = 1;
$layer_0 (sg, group); // because we need its outputs
}
*y = sg->u * group->$param_1_bar;
}
void $group_1(ShaderGlobals *sg, GroupData_1 *group,
wide_int* wide_shadeindex_ptr,
void* userdatda_base_ptr, void* output_base_ptr,
int mask, InteractiveParams* interactive_params)
{
group->layer_run[...] = 0;
// Run just the unconditional layers
if (! group->layer_run[1]) {
group->layer_run[1] = 1;
$layer_1 (sg, group);
}
}
*/
extern int osl_llvm_compiled_ops_size;
extern unsigned char osl_llvm_compiled_ops_block[];
using namespace OSL::pvt;
OSL_NAMESPACE_BEGIN
namespace pvt {
static spin_mutex llvm_mutex;
static ustring op_end("end");
static ustring op_nop("nop");
static ustring op_aassign("aassign");
static ustring op_compassign("compassign");
static ustring op_mxcompassign("mxcompassign");
static ustring op_aref("aref");
static ustring op_compref("compref");
static ustring op_mxcompref("mxcompref");
static ustring op_useparam("useparam");
static ustring op_pointcloud_get("pointcloud_get");
static ustring op_spline("spline");
static ustring op_splineinverse("splineinverse");
static ustring unknown_shader_group_name("<Unknown Shader Group Name>");
static TypeSpec
possibly_wide_type_from_code(const char* code, int* advance, bool& is_uniform)
{
// Codes leading with a W stand for "wide" and have varying non-uniform values
int i = 0;
if (code[0] == 'W') {
is_uniform = false;
++i;
} else {
is_uniform = true;
}
TypeSpec t = TypeSpec::type_from_code(code + i, advance);
if (advance)
*advance += i;
return t;
}
struct HelperFuncRecord {
const char* argtypes;
void (*function)();
int vector_width;
TargetISA target_isa;
OSL_FORCEINLINE HelperFuncRecord(const char* argtypes_ = NULL,
void (*function_)() = NULL,
int vector_width_ = 0,
TargetISA target_isa_ = TargetISA::UNKNOWN)
: argtypes(argtypes_)
, function(function_)
, vector_width(vector_width_)
, target_isa(target_isa_)
{
}
OSL_FORCEINLINE HelperFuncRecord(const HelperFuncRecord& other)
: argtypes(other.argtypes)
, function(other.function)
, vector_width(other.vector_width)
, target_isa(other.target_isa)
{
}
};
// As we will be using compile time const char * to populate the HelperFuncMap
// We avoid std::string (which would create a copy), and instead just store
// the const char *, however we must supply our own hashing and equality
// functors to the map to avoid default behavior of using the pointer vs.
// the string it points to, in C++20 std::string_view could be used
struct CStrHash {
OSL_FORCEINLINE size_t operator()(const char* str) const
{
OSL_DASSERT(str != nullptr);
return OSL::strhash(str);
}
};
struct CStrEquality {
OSL_FORCEINLINE bool operator()(const char* lhs, const char* rhs) const
{
bool is_equal = (strcmp(lhs, rhs) == 0);
return is_equal;
}
};
typedef tsl::robin_map<const char*, HelperFuncRecord, CStrHash, CStrEquality>
HelperFuncMap;
static HelperFuncMap llvm_helper_function_map;
static atomic_int llvm_helper_function_map_initialized(0);
static spin_mutex llvm_helper_function_map_mutex;
static void
initialize_llvm_helper_function_map()
{
if (llvm_helper_function_map_initialized)
return; // already done
spin_lock lock(llvm_helper_function_map_mutex);
if (llvm_helper_function_map_initialized)
return;
#define DECL(name, signature) \
llvm_helper_function_map[#name] = HelperFuncRecord(signature, name);
#include "builtindecl.h"
#undef DECL
llvm_helper_function_map_initialized = 1;
}
struct NameAndSignature {
const char* name;
const char* signature;
};
// As we don't now the sizeof ConcreteT::library_functions until specialization,
// we need a helper template function to defer its resolution
template<typename ConcreteT>
static void
init_wide_function_map(const ConcreteT&, ShadingSystemImpl& shadingsys)
{
static atomic_int is_initialized(0);
if (is_initialized)
return; // already done
spin_lock lock(llvm_helper_function_map_mutex);
if (is_initialized)
return;
const char* shared_lib_ext = OIIO::Plugin::plugin_extension();
std::string shared_lib_name = std::string("lib_")
+ ConcreteT::library_selector_string
+ "oslexec." + shared_lib_ext;
//std::cout << ">>>Attempting to open shared lib: " << shared_lib_name.c_str() << std::endl;
std::string filename = OIIO::Filesystem::searchpath_find(
shared_lib_name, shadingsys.library_searchpath_dirs());
// TODO: consider trying to open it even if searchpath_find failed, so that LD_LIBRARY_PATH has a chance
if (filename.empty()) {
shadingsys.errorfmt(
"{} could not be found along the attribute \"searchpath:library\" of \"{}\"",
shared_lib_name, shadingsys.library_searchpath());
// Something later will ASSERT/Fail now, we can't really continue successfully
return;
}
auto shared_lib = OIIO::Plugin::open(filename, /*global=*/false);
if (shared_lib == 0) {
shadingsys.errorfmt("{} could not be loaded with error \"{}\"",
filename, OIIO::Plugin::geterror());
// Something later will ASSERT/Fail, now we can't really continue successfully
return;
}
typedef void (*FunctionPtr)();
for (const auto& name_and_sig : ConcreteT::library_functions) {
//std::cout << ">>>Attempting to getsym " << name_and_sig.name << std::endl;
FunctionPtr function_pointer = reinterpret_cast<FunctionPtr>(
OIIO::Plugin::getsym(shared_lib, name_and_sig.name,
/*report_error*/ true));
if (function_pointer == nullptr) {
std::cout << ">>>Failed attempting to getsym " << name_and_sig.name
<< std::endl
<< "OIIO::Plugin::geterror()="
<< OIIO::Plugin::geterror();
OSL_ASSERT(
0
&& "Unable to find precompiled OSL library function in shared library. This indicates a build/configuration problem. We can't continue");
}
llvm_helper_function_map[name_and_sig.name]
= HelperFuncRecord(name_and_sig.signature, function_pointer,
ConcreteT::width, ConcreteT::isa);
}
is_initialized = 1;
}
template<int WidthT, TargetISA IsaT>
class ConcreteTargetLibraryHelper final
: public BatchedBackendLLVM::TargetLibraryHelper {
public:
ConcreteTargetLibraryHelper() {}
~ConcreteTargetLibraryHelper() final {}
static constexpr int width = WidthT;
static constexpr TargetISA isa = IsaT;
// Specialize instances for each supported Width and IsaT combo
static const NameAndSignature library_functions[];
static const char* library_selector_string;
void init_function_map(ShadingSystemImpl& shadingsys) const final
{
init_wide_function_map(*this, shadingsys);
}
const char* library_selector() const final
{
return library_selector_string;
}
};
// Specialize ConcreteTargetLibraryHelper<>::library_functions and
// ConcreteTargetLibraryHelper<>::library_selector_string for each
// WidthT and TargetISA that we are building a shared library for.
// To identify these shared libraries, the build system should define:
// __OSL_SUPPORTS_B##WidthT##_##TargetISA
// You will see conditional compilation around specilization below...
// NOTE: Because builtindecl_wide_xmacro.h passes macros through the name
// parameter of DECL(name,signature), we must have a layer of indirection
// to allow those macros to expand. Thus the use of DECL_INDIRECT.
#ifdef __OSL_SUPPORTS_b16_AVX512
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<16, TargetISA::AVX512>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 16
# define __OSL_TARGET_ISA AVX512
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char*
ConcreteTargetLibraryHelper<16, TargetISA::AVX512>::library_selector_string
= "b16_AVX512_";
#endif
#ifdef __OSL_SUPPORTS_b16_AVX512_noFMA
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<16, TargetISA::AVX512_noFMA>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 16
# define __OSL_TARGET_ISA AVX512_noFMA
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char* ConcreteTargetLibraryHelper<
16, TargetISA::AVX512_noFMA>::library_selector_string
= "b16_AVX512_noFMA_";
#endif
#ifdef __OSL_SUPPORTS_b8_AVX512
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<8, TargetISA::AVX512>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 8
# define __OSL_TARGET_ISA AVX512
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char*
ConcreteTargetLibraryHelper<8, TargetISA::AVX512>::library_selector_string
= "b8_AVX512_";
#endif
#ifdef __OSL_SUPPORTS_b8_AVX512_noFMA
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<8, TargetISA::AVX512_noFMA>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 8
# define __OSL_TARGET_ISA AVX512_noFMA
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char* ConcreteTargetLibraryHelper<
8, TargetISA::AVX512_noFMA>::library_selector_string
= "b8_AVX512_noFMA_";
#endif
#ifdef __OSL_SUPPORTS_b8_AVX2
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<8, TargetISA::AVX2>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 8
# define __OSL_TARGET_ISA AVX2
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char*
ConcreteTargetLibraryHelper<8, TargetISA::AVX2>::library_selector_string
= "b8_AVX2_";
#endif
#ifdef __OSL_SUPPORTS_b8_AVX2_noFMA
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<8, TargetISA::AVX2_noFMA>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 8
# define __OSL_TARGET_ISA AVX2_noFMA
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char*
ConcreteTargetLibraryHelper<8, TargetISA::AVX2_noFMA>::library_selector_string
= "b8_AVX2_noFMA_";
#endif
#ifdef __OSL_SUPPORTS_b8_AVX
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<8, TargetISA::AVX>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 8
# define __OSL_TARGET_ISA AVX
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char*
ConcreteTargetLibraryHelper<8, TargetISA::AVX>::library_selector_string
= "b8_AVX_";
#endif
#ifdef __OSL_SUPPORTS_b4_SSE2
template<>
const NameAndSignature
ConcreteTargetLibraryHelper<4, TargetISA::x64>::library_functions[]
= {
# define DECL_INDIRECT(name, signature) \
NameAndSignature { #name, signature },
# define DECL(name, signature) DECL_INDIRECT(name, signature)
# define __OSL_WIDTH 4
# define __OSL_TARGET_ISA SSE2
// Don't allow order of xmacro includes be rearranged
// clang-format off
# include "wide/define_opname_macros.h"
# include "builtindecl_wide_xmacro.h"
# include "wide/undef_opname_macros.h"
// clang-format on
# undef __OSL_TARGET_ISA
# undef __OSL_WIDTH
# undef DECL
# undef DECL_INDIRECT
};
template<>
const char*
ConcreteTargetLibraryHelper<4, TargetISA::x64>::library_selector_string
= "b4_SSE2_";
#endif
std::unique_ptr<BatchedBackendLLVM::TargetLibraryHelper>
BatchedBackendLLVM::TargetLibraryHelper::build(ShadingContext* context,
int vector_width,
TargetISA target_isa)
{
OSL_ASSERT(target_isa != TargetISA::UNKNOWN);
typedef std::unique_ptr<BatchedBackendLLVM::TargetLibraryHelper> RetType;
switch (vector_width) {
case 16:
switch (target_isa) {
#ifdef __OSL_SUPPORTS_b16_AVX512
case TargetISA::AVX512:
return RetType(
new ConcreteTargetLibraryHelper<16, TargetISA::AVX512>());
#endif
#ifdef __OSL_SUPPORTS_b16_AVX512_noFMA
case TargetISA::AVX512_noFMA:
return RetType(
new ConcreteTargetLibraryHelper<16, TargetISA::AVX512_noFMA>());
#endif
default: break;
}
break;
case 8:
switch (target_isa) {
#ifdef __OSL_SUPPORTS_b8_AVX512
case TargetISA::AVX512:
return RetType(
new ConcreteTargetLibraryHelper<8, TargetISA::AVX512>());
#endif
#ifdef __OSL_SUPPORTS_b8_AVX512_noFMA
case TargetISA::AVX512_noFMA:
return RetType(
new ConcreteTargetLibraryHelper<8, TargetISA::AVX512_noFMA>());
#endif
#ifdef __OSL_SUPPORTS_b8_AVX2
case TargetISA::AVX2:
return RetType(
new ConcreteTargetLibraryHelper<8, TargetISA::AVX2>());
#endif
#ifdef __OSL_SUPPORTS_b8_AVX2_noFMA
case TargetISA::AVX2_noFMA:
return RetType(
new ConcreteTargetLibraryHelper<8, TargetISA::AVX2_noFMA>());
#endif
#ifdef __OSL_SUPPORTS_b8_AVX
case TargetISA::AVX:
return RetType(
new ConcreteTargetLibraryHelper<8, TargetISA::AVX>());
#endif
default: break;
}
break;
case 4:
switch (target_isa) {
#ifdef __OSL_SUPPORTS_b4_SSE2
case TargetISA::x64:
return RetType(
new ConcreteTargetLibraryHelper<4, TargetISA::x64>());
#endif
default: break;
}
break;
default: OSL_ASSERT(0 && "unsupported vector width");
}
std::cerr << "Build is not configured to support TargetISA of "
<< LLVM_Util::target_isa_name(target_isa) << " and batch_size of "
<< vector_width << std::endl
<< std::flush;
return nullptr;
}
static void*
helper_function_lookup(const std::string& name)
{
OSL_DEV_ONLY(std::cout << "helper_function_lookup (" << name << ")"
<< std::endl);
HelperFuncMap::const_iterator i = llvm_helper_function_map.find(
name.c_str());
if (i == llvm_helper_function_map.end()) {
// built-in functions like memset wouldn't be in this lookup
//std::cout << "DIDN'T FIND helper_function_lookup (" << name << ")" << std::endl;
//for(auto v:llvm_helper_function_map) {
// std::cout << "llvm_helper_function_map [" << v.first << "]" << std::endl;
//}
return NULL;
}
return (void*)i->second.function;
}
llvm::Type*
BatchedBackendLLVM::llvm_type_sg()
{
// Create a type that defines the ShaderGlobals for LLVM IR. This
// absolutely MUST exactly match the ShaderGlobals struct in oslexec.h.
if (m_llvm_type_sg)
return m_llvm_type_sg;
// Derivs look like arrays of 3 values
llvm::Type* wide_float_deriv = llvm_wide_type(
TypeDesc(TypeDesc::FLOAT, TypeDesc::SCALAR, 3));
llvm::Type* wide_triple_deriv = llvm_wide_type(
TypeDesc(TypeDesc::FLOAT, TypeDesc::VEC3, 3));
llvm::Type* vp = (llvm::Type*)ll.type_void_ptr();
llvm::Type* wide_vp = (llvm::Type*)ll.type_wide_void_ptr();
std::vector<llvm::Type*> sg_types;
// Uniform values of the batch
sg_types.push_back(vp); // opaque renderstate*
sg_types.push_back(vp); // opaque tracedata*
sg_types.push_back(vp); // opaque objdata*
sg_types.push_back(vp); // ShadingContext*
sg_types.push_back(vp); // RendererServices*
sg_types.push_back(ll.type_int()); // raytype
sg_types.push_back(ll.type_int()); // pad0
sg_types.push_back(ll.type_int()); // pad1
sg_types.push_back(ll.type_int()); // pad2
sg_types.push_back(ll.type_int()); // pad3
sg_types.push_back(ll.type_int()); // pad4
// VaryingShaderGlobals of the batch
sg_types.push_back(wide_triple_deriv); // P, dPdx, dPdy
sg_types.push_back(ll.type_wide_triple()); // dPdz
sg_types.push_back(wide_triple_deriv); // I, dIdx, dIdy
sg_types.push_back(ll.type_wide_triple()); // N
sg_types.push_back(ll.type_wide_triple()); // Ng
sg_types.push_back(wide_float_deriv); // u, dudx, dudy
sg_types.push_back(wide_float_deriv); // v, dvdx, dvdy
sg_types.push_back(ll.type_wide_triple()); // dPdu
sg_types.push_back(ll.type_wide_triple()); // dPdv
sg_types.push_back(ll.type_wide_float()); // time
sg_types.push_back(ll.type_wide_float()); // dtime
sg_types.push_back(ll.type_wide_triple()); // dPdtime
sg_types.push_back(wide_triple_deriv); // Ps, dPsdx, dPsdy;
sg_types.push_back(wide_vp); // object2common
sg_types.push_back(wide_vp); // shader2common
sg_types.push_back(wide_vp); // Ci
sg_types.push_back(ll.type_wide_float()); // surfacearea
sg_types.push_back(ll.type_wide_int()); // flipHandedness
sg_types.push_back(ll.type_wide_int()); // backfacing
return m_llvm_type_sg = ll.type_struct(sg_types, "BatchedShaderGlobals",
true /*is_packed*/);
}
llvm::Type*
BatchedBackendLLVM::llvm_type_batched_texture_options()
{
// Create a type that defines the BatchedTextureOptions for LLVM IR. This
// absolutely MUST exactly match the BatchedTextureOptions struct in batched_texture.h.
if (m_llvm_type_batched_texture_options)
return m_llvm_type_batched_texture_options;
llvm::Type* vp = (llvm::Type*)ll.type_void_ptr();
std::vector<llvm::Type*> sg_types;
// Varying values of the batch
sg_types.push_back(ll.type_wide_float()); // sblur
sg_types.push_back(ll.type_wide_float()); // tblur
sg_types.push_back(ll.type_wide_float()); // rblur
sg_types.push_back(ll.type_wide_float()); // swidth
sg_types.push_back(ll.type_wide_float()); // twidth
sg_types.push_back(ll.type_wide_float()); // rwidth
sg_types.push_back(ll.type_wide_float()); // rnd
// Uniform values of the batch
sg_types.push_back(ll.type_int()); // firstchannel
sg_types.push_back(ll.type_int()); // subimage
sg_types.push_back(vp); // subimagename
#if defined(OIIO_TEXTUREOPTBATCH_VERSION) && OIIO_TEXTUREOPTBATCH_VERSION >= 2
// Possible future TextureOptBatch v2 -- not active yet
sg_types.push_back(ll.type_int8()); // swrap
sg_types.push_back(ll.type_int8()); // twrap
sg_types.push_back(ll.type_int8()); // rwrap
sg_types.push_back(ll.type_int8()); // mipmode
sg_types.push_back(ll.type_int8()); // interpmode
#else
// OIIO <= 3.0
sg_types.push_back(ll.type_int()); // swrap
sg_types.push_back(ll.type_int()); // twrap
sg_types.push_back(ll.type_int()); // rwrap
sg_types.push_back(ll.type_int()); // mipmode
sg_types.push_back(ll.type_int()); // interpmode
#endif
sg_types.push_back(ll.type_int()); // anisotropic
sg_types.push_back(ll.type_int()); // conservative_filter
sg_types.push_back(ll.type_float()); // fill
sg_types.push_back(ll.type_ptr(ll.type_float())); // missingcolor
sg_types.push_back(ll.type_int()); // colortransformid
// Private internal data
sg_types.push_back(ll.type_int()); // envlayout
m_llvm_type_batched_texture_options
= ll.type_struct(sg_types, "BatchedTextureOptions",
false /*is_packed*/);
#if 0 && defined(OSL_DEV)
std::cout << std::endl << std::endl << "llvm's data layout of BatchedTextureOptions" << std::endl;
ll.dump_struct_data_layout(llvm_type_batched_texture_options());
#endif
{
std::vector<unsigned int> offset_by_index;
switch (m_width) {
case 4:
build_offsets_of_BatchedTextureOptions<4>(offset_by_index);
break;
case 8:
build_offsets_of_BatchedTextureOptions<8>(offset_by_index);
break;
case 16:
build_offsets_of_BatchedTextureOptions<16>(offset_by_index);
break;
default:
OSL_ASSERT(
0
&& "Unsupported width of batch. Only widths 4, 8, and 16 are allowed");
break;
};
ll.validate_struct_data_layout(m_llvm_type_batched_texture_options,
offset_by_index);
// std::cout<<"After texture validation"<<std::endl;
}
return m_llvm_type_batched_texture_options;
}
llvm::Type*
BatchedBackendLLVM::llvm_type_batched_trace_options()
{
// Create a type that defines the BatchedTraceOptions for LLVM IR. This
// absolutely MUST exactly match the BatchedTraceOptions struct in batched_texture.h.
if (m_llvm_type_batched_trace_options)
return m_llvm_type_batched_trace_options;
std::vector<llvm::Type*> sg_types;
// Uniform values of the batch
sg_types.push_back(ll.type_float()); // mindist
sg_types.push_back(ll.type_float()); // maxdist
sg_types.push_back(ll.type_int()); // shade
sg_types.push_back(ll.type_ustring()); // traceset
m_llvm_type_batched_trace_options = ll.type_struct(sg_types, "TraceOptions",
false /*is_packed*/);
{
std::vector<unsigned int> offset_by_index;
offset_by_index.push_back(
offsetof(RendererServices::TraceOpt, mindist));
offset_by_index.push_back(
offsetof(RendererServices::TraceOpt, maxdist));
offset_by_index.push_back(offsetof(RendererServices::TraceOpt, shade));
offset_by_index.push_back(
offsetof(RendererServices::TraceOpt, traceset));
// std::cout<<"Offset vec size is "<<offset_by_index.size()<<std::endl;
// std::cout<<"Offset by index size is "<<offset_by_index.size()<<std::endl;
// std::cout<<"Offset_by_index[0] "<<offset_by_index[0]<<std::endl;
// std::cout<<"Offset_by_index[1] "<<offset_by_index[1]<<std::endl;
// std::cout<<"Offset_by_index[2] "<<offset_by_index[2]<<std::endl;
// std::cout<<"Offset_by_index[3] "<<offset_by_index[3]<<std::endl;
ll.validate_struct_data_layout(m_llvm_type_batched_trace_options,
offset_by_index);
}
return m_llvm_type_batched_trace_options;
}
llvm::Type*
BatchedBackendLLVM::llvm_type_noise_options()
{
if (m_llvm_type_noise_options)
return m_llvm_type_noise_options;
std::vector<llvm::Type*> comp_types;
comp_types.push_back(ll.type_int()); // anisotropic;
comp_types.push_back(ll.type_int()); // do_filter;
comp_types.push_back(ll.type_triple()); // direction;
comp_types.push_back(ll.type_float()); // bandwidth;
comp_types.push_back(ll.type_float()); // impulses;
m_llvm_type_noise_options = ll.type_struct(comp_types, "NoiseOptions");
std::vector<unsigned int> offset_by_index;
offset_by_index.push_back(offsetof(NoiseParams, anisotropic));
offset_by_index.push_back(offsetof(NoiseParams, do_filter));
offset_by_index.push_back(offsetof(NoiseParams, direction));
offset_by_index.push_back(offsetof(NoiseParams, bandwidth));
offset_by_index.push_back(offsetof(NoiseParams, impulses));
ll.validate_struct_data_layout(m_llvm_type_noise_options, offset_by_index);
return m_llvm_type_noise_options;
}
llvm::Type*
BatchedBackendLLVM::llvm_type_noise_options_ptr()
{
return ll.type_ptr(llvm_type_noise_options());
}
llvm::Value*
BatchedBackendLLVM::temp_noise_options_ptr()
{
if (m_llvm_temp_noise_options_ptr == nullptr) {
// Don't worry about what basic block we are currently inside of because
// we insert all alloca's to the top function, not the current insertion point
m_llvm_temp_noise_options_ptr = ll.op_alloca(llvm_type_noise_options());
}
return m_llvm_temp_noise_options_ptr;
}
llvm::Type*
BatchedBackendLLVM::llvm_type_sg_ptr()
{
return ll.type_ptr(llvm_type_sg());
}
llvm::Type*
BatchedBackendLLVM::llvm_type_groupdata()
{
// If already computed, return it
if (m_llvm_type_groupdata)
return m_llvm_type_groupdata;
std::vector<llvm::Type*> fields;
int offset = 0;
int order = 0;
m_groupdata_field_names.clear();
if (llvm_debug() >= 2)
std::cout << "Group param struct:\n";
// First, add the array that tells if each layer has run. But only make
// slots for the layers that may be called/used.
if (llvm_debug() >= 2)
std::cout << " layers run flags: " << m_num_used_layers
<< " at offset " << offset << "\n";
// The next item in the data structure has 64 byte alignment, so we need to move our offset to a 64 byte alignment
// Round up to a 64 bit boundary
int sz = 16 * ((m_num_used_layers + 15) / 16);
OSL_ASSERT(sz * sizeof(int) % 16 == 0);
fields.push_back(ll.type_array(ll.type_int(), sz));
m_groupdata_field_names.emplace_back("layer_runflags");
offset += sz * sizeof(int);
++order;
// Now add the array that tells which userdata have been initialized,
// and the space for the userdata values.
int nuserdata = (int)group().m_userdata_names.size();
if (nuserdata) {
if (llvm_debug() >= 2)
std::cout << " userdata initialized flags: " << nuserdata
<< " at offset " << offset << ", field " << order << "\n";
ustring* names = &group().m_userdata_names[0];
OSL_DEV_ONLY(std::cout << "USERDATA " << *names << std::endl);
TypeDesc* types = &group().m_userdata_types[0];
int* offsets = &group().m_userdata_offsets[0];
int sz = nuserdata;
fields.push_back(ll.type_array(ll.type_int(), sz));
m_groupdata_field_names.emplace_back("userdata_init_flags");
offset += nuserdata * sizeof(int);
++order;
for (int i = 0; i < nuserdata; ++i) {
TypeDesc type = types[i];
// TODO: why do we always make deriv room? Do we not know
int n = type.numelements() * 3; // always make deriv room
type.arraylen = n;
fields.push_back(llvm_wide_type(type));
m_groupdata_field_names.emplace_back(
fmtformat("userdata{}_{}_", i, names[i]));
// Alignment
int align = type.basesize() * m_width;
offset = OIIO::round_to_multiple_of_pow2(offset, align);
if (llvm_debug() >= 2) {
std::cout << " userdata ";
if (names[i] != nullptr) {
std::cout << names[i];
} else {
std::cout << i;
}
std::cout << ' ' << type << ", field " << order << ", offset "
<< offset << std::endl;
}
offsets[i] = offset;
offset += int(type.size()) * m_width;
++order;
}
}
// For each layer in the group, add entries for all params that are
// connected or interpolated, and output params. Also mark those
// symbols with their offset within the group struct.
m_param_order_map.clear();
for (int layer = 0; layer < group().nlayers(); ++layer) {
ShaderInstance* inst = group()[layer];
// TODO: Does anything bad happen from not skipping unused layers?
// We wanted space for default parameters to still be
// part of group data so we have a place to create a wide version
// So we choose to always have a run function for a layer
// just to broadcast out the scalar default value.
// TODO: Optimize to only run unused layers once, shouldn't