-
Notifications
You must be signed in to change notification settings - Fork 406
Expand file tree
/
Copy pathllvm_gen.cpp
More file actions
3768 lines (3229 loc) · 142 KB
/
llvm_gen.cpp
File metadata and controls
3768 lines (3229 loc) · 142 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 (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
All Rights Reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Sony Pictures Imageworks nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <cmath>
#include <OpenImageIO/fmath.h>
#include "oslexec_pvt.h"
#include <OSL/genclosure.h>
#include "backendllvm.h"
using namespace OSL;
using namespace OSL::pvt;
OSL_NAMESPACE_ENTER
namespace pvt {
static ustring op_and("and");
static ustring op_bitand("bitand");
static ustring op_bitor("bitor");
static ustring op_break("break");
static ustring op_ceil("ceil");
static ustring op_cellnoise("cellnoise");
static ustring op_color("color");
static ustring op_compl("compl");
static ustring op_continue("continue");
static ustring op_dowhile("dowhile");
static ustring op_eq("eq");
static ustring op_error("error");
static ustring op_fabs("fabs");
static ustring op_floor("floor");
static ustring op_for("for");
static ustring op_format("format");
static ustring op_fprintf("fprintf");
static ustring op_ge("ge");
static ustring op_gt("gt");
static ustring op_hashnoise("hashnoise");
static ustring op_if("if");
static ustring op_le("le");
static ustring op_logb("logb");
static ustring op_lt("lt");
static ustring op_min("min");
static ustring op_neq("neq");
static ustring op_normal("normal");
static ustring op_or("or");
static ustring op_point("point");
static ustring op_printf("printf");
static ustring op_round("round");
static ustring op_shl("shl");
static ustring op_shr("shr");
static ustring op_sign("sign");
static ustring op_step("step");
static ustring op_trunc("trunc");
static ustring op_vector("vector");
static ustring op_warning("warning");
static ustring op_xor("xor");
static ustring u_distance ("distance");
static ustring u_index ("index");
static ustring u__empty; // empty/default ustring
/// Macro that defines the arguments to LLVM IR generating routines
///
#define LLVMGEN_ARGS BackendLLVM &rop, int opnum
/// Macro that defines the full declaration of an LLVM generator.
///
#define LLVMGEN(name) bool name (LLVMGEN_ARGS)
// Forward decl
LLVMGEN (llvm_gen_generic);
void
BackendLLVM::llvm_gen_debug_printf (string_view message)
{
ustring s = ustring::format ("(%s %s) %s", inst()->shadername(),
inst()->layername(), message);
ll.call_function ("osl_printf", sg_void_ptr(), ll.constant("%s\n"),
ll.constant(s));
}
void
BackendLLVM::llvm_gen_warning (string_view message)
{
ll.call_function ("osl_warning", sg_void_ptr(), ll.constant("%s\n"),
ll.constant(message));
}
void
BackendLLVM::llvm_gen_error (string_view message)
{
ll.call_function ("osl_error", sg_void_ptr(), ll.constant("%s\n"),
ll.constant(message));
}
void
BackendLLVM::llvm_call_layer (int layer, bool unconditional)
{
// Make code that looks like:
// if (! groupdata->run[parentlayer])
// parent_layer (sg, groupdata);
// if it's a conditional call, or
// parent_layer (sg, groupdata);
// if it's run unconditionally.
// The code in the parent layer itself will set its 'executed' flag.
llvm::Value *args[2];
args[0] = sg_ptr ();
args[1] = groupdata_ptr ();
ShaderInstance *parent = group()[layer];
llvm::Value *trueval = ll.constant_bool(true);
llvm::Value *layerfield = layer_run_ref(layer_remap(layer));
llvm::BasicBlock *then_block = NULL, *after_block = NULL;
if (! unconditional) {
llvm::Value *executed = ll.op_load (layerfield);
executed = ll.op_ne (executed, trueval);
then_block = ll.new_basic_block ("");
after_block = ll.new_basic_block ("");
ll.op_branch (executed, then_block, after_block);
// insert point is now then_block
}
std::string name = Strutil::format ("%s_%d", parent->layername().c_str(),
parent->id());
// Mark the call as a fast call
llvm::Value *funccall = ll.call_function (name.c_str(), args, 2);
if (!parent->entry_layer())
ll.mark_fast_func_call (funccall);
if (! unconditional)
ll.op_branch (after_block); // also moves insert point
}
void
BackendLLVM::llvm_run_connected_layers (Symbol &sym, int symindex,
int opnum,
std::set<int> *already_run)
{
if (sym.valuesource() != Symbol::ConnectedVal)
return; // Nothing to do
bool inmain = (opnum >= inst()->maincodebegin() &&
opnum < inst()->maincodeend());
for (int c = 0; c < inst()->nconnections(); ++c) {
const Connection &con (inst()->connection (c));
// If the connection gives a value to this param
if (con.dst.param == symindex) {
// already_run is a set of layers run for this particular op.
// Just so we don't stupidly do several consecutive checks on
// whether we ran this same layer. It's JUST for this op.
if (already_run) {
if (already_run->count (con.srclayer))
continue; // already ran that one on this op
else
already_run->insert (con.srclayer); // mark it
}
if (inmain) {
// There is an instance-wide m_layers_already_run that tries
// to remember which earlier layers have unconditionally
// been run at any point in the execution of this layer. But
// only honor (and modify) that when in the main code
// section, not when in init ops, which are inherently
// conditional.
if (m_layers_already_run.count (con.srclayer)) {
continue; // already unconditionally ran the layer
}
if (! m_in_conditional[opnum]) {
// Unconditionally running -- mark so we don't do it
// again. If we're inside a conditional, don't mark
// because it may not execute the conditional body.
m_layers_already_run.insert (con.srclayer);
}
}
// If the earlier layer it comes from has not yet been
// executed, do so now.
llvm_call_layer (con.srclayer);
}
}
}
LLVMGEN (llvm_gen_nop)
{
return true;
}
LLVMGEN (llvm_gen_useparam)
{
ASSERT (! rop.inst()->unused() &&
"oops, thought this layer was unused, why do we call it?");
// If we have multiple params needed on this statement, don't waste
// time checking the same upstream layer more than once.
std::set<int> already_run;
Opcode &op (rop.inst()->ops()[opnum]);
for (int i = 0; i < op.nargs(); ++i) {
Symbol& sym = *rop.opargsym (op, i);
int symindex = rop.inst()->arg (op.firstarg()+i);
rop.llvm_run_connected_layers (sym, symindex, opnum, &already_run);
// If it's an interpolated (userdata) parameter and we're
// initializing them lazily, now we have to do it.
if (sym.symtype() == SymTypeParam
&& ! sym.lockgeom() && ! sym.typespec().is_closure()
&& ! sym.connected() && ! sym.connected_down()
&& rop.shadingsys().lazy_userdata()) {
rop.llvm_assign_initial_value (sym);
}
}
return true;
}
// Used for printf, error, warning, format, fprintf
LLVMGEN (llvm_gen_printf)
{
Opcode &op (rop.inst()->ops()[opnum]);
// Prepare the args for the call
// Which argument is the format string? Usually 0, but for op
// format() and fprintf(), the formatting string is argument #1.
int format_arg = (op.opname() == "format" || op.opname() == "fprintf") ? 1 : 0;
Symbol& format_sym = *rop.opargsym (op, format_arg);
std::vector<llvm::Value*> call_args;
if (!format_sym.is_constant()) {
rop.shadingcontext()->warning ("%s must currently have constant format\n",
op.opname().c_str());
return false;
}
// For some ops, we push the shader globals pointer
if (op.opname() == op_printf || op.opname() == op_error ||
op.opname() == op_warning || op.opname() == op_fprintf)
call_args.push_back (rop.sg_void_ptr());
// fprintf also needs the filename
if (op.opname() == op_fprintf) {
Symbol& Filename = *rop.opargsym (op, 0);
llvm::Value* fn = rop.llvm_load_value (Filename);
call_args.push_back (fn);
}
// We're going to need to adjust the format string as we go, but I'd
// like to reserve a spot for the char*.
size_t new_format_slot = call_args.size();
call_args.push_back(NULL);
ustring format_ustring = *((ustring*)format_sym.data());
const char* format = format_ustring.c_str();
std::string s;
int arg = format_arg + 1;
while (*format != '\0') {
if (*format == '%') {
if (format[1] == '%') {
// '%%' is a literal '%'
s += "%%";
format += 2; // skip both percentages
continue;
}
const char *oldfmt = format; // mark beginning of format
while (*format &&
*format != 'c' && *format != 'd' && *format != 'e' &&
*format != 'f' && *format != 'g' && *format != 'i' &&
*format != 'm' && *format != 'n' && *format != 'o' &&
*format != 'p' && *format != 's' && *format != 'u' &&
*format != 'v' && *format != 'x' && *format != 'X')
++format;
char formatchar = *format++; // Also eat the format char
if (arg >= op.nargs()) {
rop.shadingcontext()->error ("Mismatch between format string and arguments (%s:%d)",
op.sourcefile().c_str(), op.sourceline());
return false;
}
std::string ourformat (oldfmt, format); // straddle the format
// Doctor it to fix mismatches between format and data
Symbol& sym (*rop.opargsym (op, arg));
TypeDesc simpletype (sym.typespec().simpletype());
int num_elements = simpletype.numelements();
int num_components = simpletype.aggregate;
if ((sym.typespec().is_closure_based() ||
simpletype.basetype == TypeDesc::STRING)
&& formatchar != 's') {
ourformat[ourformat.length()-1] = 's';
}
if (simpletype.basetype == TypeDesc::INT && formatchar != 'd' &&
formatchar != 'i' && formatchar != 'o' && formatchar != 'u' &&
formatchar != 'x' && formatchar != 'X') {
ourformat[ourformat.length()-1] = 'd';
}
if (simpletype.basetype == TypeDesc::FLOAT && formatchar != 'f' &&
formatchar != 'g' && formatchar != 'c' && formatchar != 'e' &&
formatchar != 'm' && formatchar != 'n' && formatchar != 'p' &&
formatchar != 'v') {
ourformat[ourformat.length()-1] = 'f';
}
// NOTE(boulos): Only for debug mode do the derivatives get printed...
for (int a = 0; a < num_elements; ++a) {
llvm::Value *arrind = simpletype.arraylen ? rop.ll.constant(a) : NULL;
if (sym.typespec().is_closure_based()) {
s += ourformat;
llvm::Value *v = rop.llvm_load_value (sym, 0, arrind, 0);
v = rop.ll.call_function ("osl_closure_to_string", rop.sg_void_ptr(), v);
call_args.push_back (v);
continue;
}
for (int c = 0; c < num_components; c++) {
if (c != 0 || a != 0)
s += " ";
s += ourformat;
llvm::Value* loaded = rop.llvm_load_value (sym, 0, arrind, c);
if (simpletype.basetype == TypeDesc::FLOAT) {
// C varargs convention upconverts float->double.
loaded = rop.ll.op_float_to_double(loaded);
}
call_args.push_back (loaded);
}
}
++arg;
} else {
// Everything else -- just copy the character and advance
s += *format++;
}
}
// Some ops prepend things
if (op.opname() == op_error || op.opname() == op_warning) {
std::string prefix = Strutil::format ("Shader %s [%s]: ",
op.opname().c_str(),
rop.inst()->shadername().c_str());
s = prefix + s;
}
// Now go back and put the new format string in its place
call_args[new_format_slot] = rop.ll.constant (s.c_str());
// Construct the function name and call it.
std::string opname = std::string("osl_") + op.opname().string();
llvm::Value *ret = rop.ll.call_function (opname.c_str(), &call_args[0],
(int)call_args.size());
// The format op returns a string value, put in in the right spot
if (op.opname() == op_format)
rop.llvm_store_value (ret, *rop.opargsym (op, 0));
return true;
}
LLVMGEN (llvm_gen_add)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
ASSERT (! A.typespec().is_array() && ! B.typespec().is_array());
if (Result.typespec().is_closure()) {
ASSERT (A.typespec().is_closure() && B.typespec().is_closure());
llvm::Value *valargs[3];
valargs[0] = rop.sg_void_ptr();
valargs[1] = rop.llvm_load_value (A);
valargs[2] = rop.llvm_load_value (B);
llvm::Value *res = rop.ll.call_function ("osl_add_closure_closure", valargs, 3);
rop.llvm_store_value (res, Result, 0, NULL, 0);
return true;
}
TypeDesc type = Result.typespec().simpletype();
int num_components = type.aggregate;
// The following should handle f+f, v+v, v+f, f+v, i+i
// That's all that should be allowed by oslc.
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.loadLLVMValue (A, i, 0, type);
llvm::Value *b = rop.loadLLVMValue (B, i, 0, type);
if (!a || !b)
return false;
llvm::Value *r = rop.ll.op_add (a, b);
rop.storeLLVMValue (r, Result, i, 0);
}
if (Result.has_derivs()) {
if (A.has_derivs() || B.has_derivs()) {
for (int d = 1; d <= 2; ++d) { // dx, dy
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.loadLLVMValue (A, i, d, type);
llvm::Value *b = rop.loadLLVMValue (B, i, d, type);
llvm::Value *r = rop.ll.op_add (a, b);
rop.storeLLVMValue (r, Result, i, d);
}
}
} else {
// Result has derivs, operands do not
rop.llvm_zero_derivs (Result);
}
}
return true;
}
LLVMGEN (llvm_gen_sub)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
TypeDesc type = Result.typespec().simpletype();
int num_components = type.aggregate;
ASSERT (! Result.typespec().is_closure_based() &&
"subtraction of closures not supported");
// The following should handle f-f, v-v, v-f, f-v, i-i
// That's all that should be allowed by oslc.
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.loadLLVMValue (A, i, 0, type);
llvm::Value *b = rop.loadLLVMValue (B, i, 0, type);
if (!a || !b)
return false;
llvm::Value *r = rop.ll.op_sub (a, b);
rop.storeLLVMValue (r, Result, i, 0);
}
if (Result.has_derivs()) {
if (A.has_derivs() || B.has_derivs()) {
for (int d = 1; d <= 2; ++d) { // dx, dy
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.loadLLVMValue (A, i, d, type);
llvm::Value *b = rop.loadLLVMValue (B, i, d, type);
llvm::Value *r = rop.ll.op_sub (a, b);
rop.storeLLVMValue (r, Result, i, d);
}
}
} else {
// Result has derivs, operands do not
rop.llvm_zero_derivs (Result);
}
}
return true;
}
LLVMGEN (llvm_gen_mul)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
TypeDesc type = Result.typespec().simpletype();
bool is_float = !Result.typespec().is_closure_based() && Result.typespec().is_floatbased();
int num_components = type.aggregate;
// multiplication involving closures
if (Result.typespec().is_closure()) {
llvm::Value *valargs[3];
valargs[0] = rop.sg_void_ptr();
bool tfloat;
if (A.typespec().is_closure()) {
tfloat = B.typespec().is_float();
valargs[1] = rop.llvm_load_value (A);
valargs[2] = tfloat ? rop.llvm_load_value (B) : rop.llvm_void_ptr(B);
} else {
tfloat = A.typespec().is_float();
valargs[1] = rop.llvm_load_value (B);
valargs[2] = tfloat ? rop.llvm_load_value (A) : rop.llvm_void_ptr(A);
}
llvm::Value *res = tfloat ? rop.ll.call_function ("osl_mul_closure_float", valargs, 3)
: rop.ll.call_function ("osl_mul_closure_color", valargs, 3);
rop.llvm_store_value (res, Result, 0, NULL, 0);
return true;
}
// multiplication involving matrices
if (Result.typespec().is_matrix()) {
if (A.typespec().is_float()) {
if (B.typespec().is_float())
rop.llvm_call_function ("osl_mul_m_ff", Result, A, B);
else if (B.typespec().is_matrix())
rop.llvm_call_function ("osl_mul_mf", Result, B, A);
else ASSERT(0);
} else if (A.typespec().is_matrix()) {
if (B.typespec().is_float())
rop.llvm_call_function ("osl_mul_mf", Result, A, B);
else if (B.typespec().is_matrix())
rop.llvm_call_function ("osl_mul_mm", Result, A, B);
else ASSERT(0);
} else ASSERT (0);
if (Result.has_derivs())
rop.llvm_zero_derivs (Result);
return true;
}
// The following should handle f*f, v*v, v*f, f*v, i*i
// That's all that should be allowed by oslc.
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.llvm_load_value (A, 0, i, type);
llvm::Value *b = rop.llvm_load_value (B, 0, i, type);
if (!a || !b)
return false;
llvm::Value *r = rop.ll.op_mul (a, b);
rop.llvm_store_value (r, Result, 0, i);
if (Result.has_derivs() && (A.has_derivs() || B.has_derivs())) {
// Multiplication of duals: (a*b, a*b.dx + a.dx*b, a*b.dy + a.dy*b)
ASSERT (is_float);
llvm::Value *ax = rop.llvm_load_value (A, 1, i, type);
llvm::Value *bx = rop.llvm_load_value (B, 1, i, type);
llvm::Value *abx = rop.ll.op_mul (a, bx);
llvm::Value *axb = rop.ll.op_mul (ax, b);
llvm::Value *rx = rop.ll.op_add (abx, axb);
llvm::Value *ay = rop.llvm_load_value (A, 2, i, type);
llvm::Value *by = rop.llvm_load_value (B, 2, i, type);
llvm::Value *aby = rop.ll.op_mul (a, by);
llvm::Value *ayb = rop.ll.op_mul (ay, b);
llvm::Value *ry = rop.ll.op_add (aby, ayb);
rop.llvm_store_value (rx, Result, 1, i);
rop.llvm_store_value (ry, Result, 2, i);
}
}
if (Result.has_derivs() && ! (A.has_derivs() || B.has_derivs())) {
// Result has derivs, operands do not
rop.llvm_zero_derivs (Result);
}
return true;
}
LLVMGEN (llvm_gen_div)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
TypeDesc type = Result.typespec().simpletype();
bool is_float = Result.typespec().is_floatbased();
int num_components = type.aggregate;
ASSERT (! Result.typespec().is_closure_based());
// division involving matrices
if (Result.typespec().is_matrix()) {
if (A.typespec().is_float()) {
if (B.typespec().is_float())
rop.llvm_call_function ("osl_div_m_ff", Result, A, B);
else if (B.typespec().is_matrix())
rop.llvm_call_function ("osl_div_fm", Result, A, B);
else ASSERT (0);
} else if (A.typespec().is_matrix()) {
if (B.typespec().is_float())
rop.llvm_call_function ("osl_div_mf", Result, A, B);
else if (B.typespec().is_matrix())
rop.llvm_call_function ("osl_div_mm", Result, A, B);
else ASSERT (0);
} else ASSERT (0);
if (Result.has_derivs())
rop.llvm_zero_derivs (Result);
return true;
}
// The following should handle f/f, v/v, v/f, f/v, i/i
// That's all that should be allowed by oslc.
const char *safe_div = is_float ? "osl_safe_div_fff" : "osl_safe_div_iii";
bool deriv = (Result.has_derivs() && (A.has_derivs() || B.has_derivs()));
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.llvm_load_value (A, 0, i, type);
llvm::Value *b = rop.llvm_load_value (B, 0, i, type);
if (!a || !b)
return false;
llvm::Value *a_div_b;
if (B.is_constant() && ! rop.is_zero(B))
a_div_b = rop.ll.op_div (a, b);
else
a_div_b = rop.ll.call_function (safe_div, a, b);
llvm::Value *rx = NULL, *ry = NULL;
if (deriv) {
// Division of duals: (a/b, 1/b*(ax-a/b*bx), 1/b*(ay-a/b*by))
ASSERT (is_float);
llvm::Value *binv;
if (B.is_constant() && ! rop.is_zero(B))
binv = rop.ll.op_div (rop.ll.constant(1.0f), b);
else
binv = rop.ll.call_function (safe_div, rop.ll.constant(1.0f), b);
llvm::Value *ax = rop.llvm_load_value (A, 1, i, type);
llvm::Value *bx = rop.llvm_load_value (B, 1, i, type);
llvm::Value *a_div_b_mul_bx = rop.ll.op_mul (a_div_b, bx);
llvm::Value *ax_minus_a_div_b_mul_bx = rop.ll.op_sub (ax, a_div_b_mul_bx);
rx = rop.ll.op_mul (binv, ax_minus_a_div_b_mul_bx);
llvm::Value *ay = rop.llvm_load_value (A, 2, i, type);
llvm::Value *by = rop.llvm_load_value (B, 2, i, type);
llvm::Value *a_div_b_mul_by = rop.ll.op_mul (a_div_b, by);
llvm::Value *ay_minus_a_div_b_mul_by = rop.ll.op_sub (ay, a_div_b_mul_by);
ry = rop.ll.op_mul (binv, ay_minus_a_div_b_mul_by);
}
rop.llvm_store_value (a_div_b, Result, 0, i);
if (deriv) {
rop.llvm_store_value (rx, Result, 1, i);
rop.llvm_store_value (ry, Result, 2, i);
}
}
if (Result.has_derivs() && ! (A.has_derivs() || B.has_derivs())) {
// Result has derivs, operands do not
rop.llvm_zero_derivs (Result);
}
return true;
}
LLVMGEN (llvm_gen_modulus)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
TypeDesc type = Result.typespec().simpletype();
bool is_float = Result.typespec().is_floatbased();
int num_components = type.aggregate;
#ifdef OSL_LLVM_NO_BITCODE
// On Windows 32 bit this calls an unknown instruction, probably need to
// link with LLVM compiler-rt to fix, for now just fall back to op
if (is_float)
return llvm_gen_generic (rop, opnum);
#endif
// The following should handle f%f, v%v, v%f, i%i
// That's all that should be allowed by oslc.
const char *safe_mod = is_float ? "osl_fmod_fff" : "osl_safe_mod_iii";
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.loadLLVMValue (A, i, 0, type);
llvm::Value *b = rop.loadLLVMValue (B, i, 0, type);
if (!a || !b)
return false;
llvm::Value *r;
if (B.is_constant() && ! rop.is_zero(B))
r = rop.ll.op_mod (a, b);
else
r = rop.ll.call_function (safe_mod, a, b);
rop.storeLLVMValue (r, Result, i, 0);
}
if (Result.has_derivs()) {
ASSERT (is_float);
if (A.has_derivs()) {
// Modulus of duals: (a mod b, ax, ay)
for (int d = 1; d <= 2; ++d) {
for (int i = 0; i < num_components; i++) {
llvm::Value *deriv = rop.loadLLVMValue (A, i, d, type);
rop.storeLLVMValue (deriv, Result, i, d);
}
}
} else {
// Result has derivs, operands do not
rop.llvm_zero_derivs (Result);
}
}
return true;
}
LLVMGEN (llvm_gen_neg)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
TypeDesc type = Result.typespec().simpletype();
int num_components = type.aggregate;
for (int d = 0; d < 3; ++d) { // dx, dy
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.llvm_load_value (A, d, i, type);
llvm::Value *r = rop.ll.op_neg (a);
rop.llvm_store_value (r, Result, d, i);
}
if (! Result.has_derivs())
break;
}
return true;
}
// Implementation for clamp
LLVMGEN (llvm_gen_clamp)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& X = *rop.opargsym (op, 1);
Symbol& Min = *rop.opargsym (op, 2);
Symbol& Max = *rop.opargsym (op, 3);
TypeDesc type = Result.typespec().simpletype();
int num_components = type.aggregate;
for (int i = 0; i < num_components; i++) {
// First do the lower bound
llvm::Value *val = rop.llvm_load_value (X, 0, i, type);
llvm::Value *min = rop.llvm_load_value (Min, 0, i, type);
llvm::Value *cond = rop.ll.op_lt (val, min);
val = rop.ll.op_select (cond, min, val);
llvm::Value *valdx=NULL, *valdy=NULL;
if (Result.has_derivs()) {
valdx = rop.llvm_load_value (X, 1, i, type);
valdy = rop.llvm_load_value (X, 2, i, type);
llvm::Value *mindx = rop.llvm_load_value (Min, 1, i, type);
llvm::Value *mindy = rop.llvm_load_value (Min, 2, i, type);
valdx = rop.ll.op_select (cond, mindx, valdx);
valdy = rop.ll.op_select (cond, mindy, valdy);
}
// Now do the upper bound
llvm::Value *max = rop.llvm_load_value (Max, 0, i, type);
cond = rop.ll.op_gt (val, max);
val = rop.ll.op_select (cond, max, val);
if (Result.has_derivs()) {
llvm::Value *maxdx = rop.llvm_load_value (Max, 1, i, type);
llvm::Value *maxdy = rop.llvm_load_value (Max, 2, i, type);
valdx = rop.ll.op_select (cond, maxdx, valdx);
valdy = rop.ll.op_select (cond, maxdy, valdy);
}
rop.llvm_store_value (val, Result, 0, i);
rop.llvm_store_value (valdx, Result, 1, i);
rop.llvm_store_value (valdy, Result, 2, i);
}
return true;
}
LLVMGEN (llvm_gen_mix)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
Symbol& X = *rop.opargsym (op, 3);
TypeDesc type = Result.typespec().simpletype();
ASSERT (!Result.typespec().is_closure_based() &&
Result.typespec().is_floatbased());
int num_components = type.aggregate;
int x_components = X.typespec().aggregate();
bool derivs = (Result.has_derivs() &&
(A.has_derivs() || B.has_derivs() || X.has_derivs()));
llvm::Value *one = rop.ll.constant (1.0f);
llvm::Value *x = rop.llvm_load_value (X, 0, 0, type);
llvm::Value *one_minus_x = rop.ll.op_sub (one, x);
llvm::Value *xx = derivs ? rop.llvm_load_value (X, 1, 0, type) : NULL;
llvm::Value *xy = derivs ? rop.llvm_load_value (X, 2, 0, type) : NULL;
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.llvm_load_value (A, 0, i, type);
llvm::Value *b = rop.llvm_load_value (B, 0, i, type);
if (!a || !b)
return false;
if (i > 0 && x_components > 1) {
// Only need to recompute x and 1-x if they change
x = rop.llvm_load_value (X, 0, i, type);
one_minus_x = rop.ll.op_sub (one, x);
}
// r = a*one_minus_x + b*x
llvm::Value *r1 = rop.ll.op_mul (a, one_minus_x);
llvm::Value *r2 = rop.ll.op_mul (b, x);
llvm::Value *r = rop.ll.op_add (r1, r2);
rop.llvm_store_value (r, Result, 0, i);
if (derivs) {
// mix of duals:
// (a*one_minus_x + b*x,
// a*one_minus_x.dx + a.dx*one_minus_x + b*x.dx + b.dx*x,
// a*one_minus_x.dy + a.dy*one_minus_x + b*x.dy + b.dy*x)
// and since one_minus_x.dx = -x.dx, one_minus_x.dy = -x.dy,
// (a*one_minus_x + b*x,
// -a*x.dx + a.dx*one_minus_x + b*x.dx + b.dx*x,
// -a*x.dy + a.dy*one_minus_x + b*x.dy + b.dy*x)
llvm::Value *ax = rop.llvm_load_value (A, 1, i, type);
llvm::Value *bx = rop.llvm_load_value (B, 1, i, type);
if (i > 0 && x_components > 1)
xx = rop.llvm_load_value (X, 1, i, type);
llvm::Value *rx1 = rop.ll.op_mul (a, xx);
llvm::Value *rx2 = rop.ll.op_mul (ax, one_minus_x);
llvm::Value *rx = rop.ll.op_sub (rx2, rx1);
llvm::Value *rx3 = rop.ll.op_mul (b, xx);
rx = rop.ll.op_add (rx, rx3);
llvm::Value *rx4 = rop.ll.op_mul (bx, x);
rx = rop.ll.op_add (rx, rx4);
llvm::Value *ay = rop.llvm_load_value (A, 2, i, type);
llvm::Value *by = rop.llvm_load_value (B, 2, i, type);
if (i > 0 && x_components > 1)
xy = rop.llvm_load_value (X, 2, i, type);
llvm::Value *ry1 = rop.ll.op_mul (a, xy);
llvm::Value *ry2 = rop.ll.op_mul (ay, one_minus_x);
llvm::Value *ry = rop.ll.op_sub (ry2, ry1);
llvm::Value *ry3 = rop.ll.op_mul (b, xy);
ry = rop.ll.op_add (ry, ry3);
llvm::Value *ry4 = rop.ll.op_mul (by, x);
ry = rop.ll.op_add (ry, ry4);
rop.llvm_store_value (rx, Result, 1, i);
rop.llvm_store_value (ry, Result, 2, i);
}
}
if (Result.has_derivs() && !derivs) {
// Result has derivs, operands do not
rop.llvm_zero_derivs (Result);
}
return true;
}
LLVMGEN (llvm_gen_select)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
Symbol& X = *rop.opargsym (op, 3);
TypeDesc type = Result.typespec().simpletype();
ASSERT (!Result.typespec().is_closure_based() &&
Result.typespec().is_floatbased());
int num_components = type.aggregate;
int x_components = X.typespec().aggregate();
bool derivs = (Result.has_derivs() &&
(A.has_derivs() || B.has_derivs()));
llvm::Value *zero = X.typespec().is_int() ? rop.ll.constant (0)
: rop.ll.constant (0.0f);
llvm::Value *cond[3];
for (int i = 0; i < x_components; ++i)
cond[i] = rop.ll.op_ne (rop.llvm_load_value (X, 0, i), zero);
for (int i = 0; i < num_components; i++) {
llvm::Value *a = rop.llvm_load_value (A, 0, i, type);
llvm::Value *b = rop.llvm_load_value (B, 0, i, type);
llvm::Value *c = (i >= x_components) ? cond[0] : cond[i];
llvm::Value *r = rop.ll.op_select (c, b, a);
rop.llvm_store_value (r, Result, 0, i);
if (derivs) {
for (int d = 1; d < 3; ++d) {
a = rop.llvm_load_value (A, d, i, type);
b = rop.llvm_load_value (B, d, i, type);
r = rop.ll.op_select (c, b, a);
rop.llvm_store_value (r, Result, d, i);
}
}
}
if (Result.has_derivs() && !derivs) {
// Result has derivs, operands do not
rop.llvm_zero_derivs (Result);
}
return true;
}
// Implementation for min/max
LLVMGEN (llvm_gen_minmax)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& x = *rop.opargsym (op, 1);
Symbol& y = *rop.opargsym (op, 2);
TypeDesc type = Result.typespec().simpletype();
int num_components = type.aggregate;
for (int i = 0; i < num_components; i++) {
// First do the lower bound
llvm::Value *x_val = rop.llvm_load_value (x, 0, i, type);
llvm::Value *y_val = rop.llvm_load_value (y, 0, i, type);
llvm::Value* cond = NULL;
// NOTE(boulos): Using <= instead of < to match old behavior
// (only matters for derivs)
if (op.opname() == op_min) {
cond = rop.ll.op_le (x_val, y_val);
} else {
cond = rop.ll.op_gt (x_val, y_val);
}
llvm::Value* res_val = rop.ll.op_select (cond, x_val, y_val);
rop.llvm_store_value (res_val, Result, 0, i);
if (Result.has_derivs()) {
llvm::Value* x_dx = rop.llvm_load_value (x, 1, i, type);
llvm::Value* x_dy = rop.llvm_load_value (x, 2, i, type);
llvm::Value* y_dx = rop.llvm_load_value (y, 1, i, type);
llvm::Value* y_dy = rop.llvm_load_value (y, 2, i, type);
rop.llvm_store_value (rop.ll.op_select(cond, x_dx, y_dx), Result, 1, i);
rop.llvm_store_value (rop.ll.op_select(cond, x_dy, y_dy), Result, 2, i);
}
}
return true;
}
LLVMGEN (llvm_gen_bitwise_binary_op)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& Result = *rop.opargsym (op, 0);
Symbol& A = *rop.opargsym (op, 1);
Symbol& B = *rop.opargsym (op, 2);
ASSERT (Result.typespec().is_int() && A.typespec().is_int() &&
B.typespec().is_int());
llvm::Value *a = rop.loadLLVMValue (A);
llvm::Value *b = rop.loadLLVMValue (B);
if (!a || !b)
return false;
llvm::Value *r = NULL;
if (op.opname() == op_bitand)
r = rop.ll.op_and (a, b);
else if (op.opname() == op_bitor)
r = rop.ll.op_or (a, b);
else if (op.opname() == op_xor)
r = rop.ll.op_xor (a, b);
else if (op.opname() == op_shl)
r = rop.ll.op_shl (a, b);
else if (op.opname() == op_shr)
r = rop.ll.op_shr (a, b);
else
return false;
rop.storeLLVMValue (r, Result);
return true;
}
// Simple (pointwise) unary ops (Abs, ...,
LLVMGEN (llvm_gen_unary_op)
{
Opcode &op (rop.inst()->ops()[opnum]);
Symbol& dst = *rop.opargsym (op, 0);