summaryrefslogtreecommitdiffstats
path: root/src/compiler/glsl/tests/lower_int64_test.cpp
blob: 4ddc433582edf18189375b0e19cd71b58b77eb9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
 * Copyright © 2013 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
#include <gtest/gtest.h>
#include "main/compiler.h"
#include "main/mtypes.h"
#include "main/macros.h"
#include "ir.h"
#include "ir_builder.h"

using namespace ir_builder;

namespace lower_64bit {
void expand_source(ir_factory &body,
                   ir_rvalue *val,
                   ir_variable **expanded_src);

ir_dereference_variable *compact_destination(ir_factory &body,
                                             const glsl_type *type,
                                             ir_variable *result[4]);

ir_rvalue *lower_op_to_function_call(ir_instruction *base_ir,
                                     ir_expression *ir,
                                     ir_function_signature *callee);
};

class expand_source : public ::testing::Test {
public:
   virtual void SetUp();
   virtual void TearDown();

   exec_list instructions;
   ir_factory *body;
   ir_variable *expanded_src[4];
   void *mem_ctx;
};

void
expand_source::SetUp()
{
   mem_ctx = ralloc_context(NULL);

   memset(expanded_src, 0, sizeof(expanded_src));
   instructions.make_empty();
   body = new ir_factory(&instructions, mem_ctx);
}

void
expand_source::TearDown()
{
   delete body;
   body = NULL;

   ralloc_free(mem_ctx);
   mem_ctx = NULL;
}

static ir_dereference_variable *
create_variable(void *mem_ctx, const glsl_type *type)
{
   ir_variable *var = new(mem_ctx) ir_variable(type,
                                               "variable",
                                               ir_var_temporary);

   return new(mem_ctx) ir_dereference_variable(var);
}

static ir_expression *
create_expression(void *mem_ctx, const glsl_type *type)
{
   return new(mem_ctx) ir_expression(ir_unop_neg,
                                     create_variable(mem_ctx, type));
}

static void
check_expanded_source(const glsl_type *type,
                      ir_variable *expanded_src[4])
{
   const glsl_type *const expanded_type =
      type->base_type == GLSL_TYPE_UINT64
      ? glsl_type::uvec2_type :glsl_type::ivec2_type;

   for (int i = 0; i < type->vector_elements; i++) {
      EXPECT_EQ(expanded_type, expanded_src[i]->type);

      /* All elements that are part of the vector must be unique. */
      for (int j = i - 1; j >= 0; j--) {
         EXPECT_NE(expanded_src[i], expanded_src[j])
            << "    Element " << i << " is the same as element " << j;
      }
   }

   /* All elements that are not part of the vector must be the same as element
    * 0.  This is primarily for scalars (where every element is the same).
    */
   for (int i = type->vector_elements; i < 4; i++) {
      EXPECT_EQ(expanded_src[0], expanded_src[i])
         << "    Element " << i << " should be the same as element 0";
   }
}

static void
check_instructions(exec_list *instructions,
                   const glsl_type *type,
                   const ir_instruction *source)
{
   const glsl_type *const expanded_type =
      type->base_type == GLSL_TYPE_UINT64
      ? glsl_type::uvec2_type : glsl_type::ivec2_type;

   const ir_expression_operation unpack_opcode =
      type->base_type == GLSL_TYPE_UINT64
      ? ir_unop_unpack_uint_2x32 : ir_unop_unpack_int_2x32;

   ir_instruction *ir;

   /* The instruction list should contain IR to represent:
    *
    *    type tmp1;
    *    tmp1 = source;
    *    uvec2 tmp2;
    *    tmp2 = unpackUint2x32(tmp1.x);
    *    uvec2 tmp3;
    *    tmp3 = unpackUint2x32(tmp1.y);
    *    uvec2 tmp4;
    *    tmp4 = unpackUint2x32(tmp1.z);
    *    uvec2 tmp5;
    *    tmp5 = unpackUint2x32(tmp1.w);
    */
   ASSERT_FALSE(instructions->is_empty());
   ir = (ir_instruction *) instructions->pop_head();
   ir_variable *const tmp1 = ir->as_variable();
   EXPECT_EQ(ir_type_variable, ir->ir_type);
   EXPECT_EQ(type, tmp1->type) <<
      "    Got " <<
      tmp1->type->name <<
      ", expected " <<
      type->name;

   ASSERT_FALSE(instructions->is_empty());
   ir = (ir_instruction *) instructions->pop_head();
   ir_assignment *const assign1 = ir->as_assignment();
   EXPECT_EQ(ir_type_assignment, ir->ir_type);
   ASSERT_NE((void *)0, assign1);
   EXPECT_EQ(tmp1, assign1->lhs->variable_referenced());
   EXPECT_EQ(source, assign1->rhs);

   for (unsigned i = 0; i < type->vector_elements; i++) {
      ASSERT_FALSE(instructions->is_empty());
      ir = (ir_instruction *) instructions->pop_head();
      ir_variable *const tmp2 = ir->as_variable();
      EXPECT_EQ(ir_type_variable, ir->ir_type);
      EXPECT_EQ(expanded_type, tmp2->type);

      ASSERT_FALSE(instructions->is_empty());
      ir = (ir_instruction *) instructions->pop_head();
      ir_assignment *const assign2 = ir->as_assignment();
      EXPECT_EQ(ir_type_assignment, ir->ir_type);
      ASSERT_NE((void *)0, assign2);
      EXPECT_EQ(tmp2, assign2->lhs->variable_referenced());
      ir_expression *unpack = assign2->rhs->as_expression();
      ASSERT_NE((void *)0, unpack);
      EXPECT_EQ(unpack_opcode, unpack->operation);
      EXPECT_EQ(tmp1, unpack->operands[0]->variable_referenced());
   }

   EXPECT_TRUE(instructions->is_empty());
}

TEST_F(expand_source, uint64_variable)
{
   const glsl_type *const type = glsl_type::uint64_t_type;
   ir_dereference_variable *const deref = create_variable(mem_ctx, type);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, u64vec2_variable)
{
   const glsl_type *const type = glsl_type::u64vec2_type;
   ir_dereference_variable *const deref = create_variable(mem_ctx, type);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, u64vec3_variable)
{
   const glsl_type *const type = glsl_type::u64vec3_type;

   /* Generate an operand that is a scalar variable dereference. */
   ir_variable *const var = new(mem_ctx) ir_variable(type,
                                                     "variable",
                                                     ir_var_temporary);

   ir_dereference_variable *const deref =
      new(mem_ctx) ir_dereference_variable(var);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, u64vec4_variable)
{
   const glsl_type *const type = glsl_type::u64vec4_type;
   ir_dereference_variable *const deref = create_variable(mem_ctx, type);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, int64_variable)
{
   const glsl_type *const type = glsl_type::int64_t_type;
   ir_dereference_variable *const deref = create_variable(mem_ctx, type);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, i64vec2_variable)
{
   const glsl_type *const type = glsl_type::i64vec2_type;
   ir_dereference_variable *const deref = create_variable(mem_ctx, type);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, i64vec3_variable)
{
   const glsl_type *const type = glsl_type::i64vec3_type;
   ir_dereference_variable *const deref = create_variable(mem_ctx, type);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, i64vec4_variable)
{
   const glsl_type *const type = glsl_type::i64vec4_type;
   ir_dereference_variable *const deref = create_variable(mem_ctx, type);

   lower_64bit::expand_source(*body, deref, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, deref);
}

TEST_F(expand_source, uint64_expression)
{
   const glsl_type *const type = glsl_type::uint64_t_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

TEST_F(expand_source, u64vec2_expression)
{
   const glsl_type *const type = glsl_type::u64vec2_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

TEST_F(expand_source, u64vec3_expression)
{
   const glsl_type *const type = glsl_type::u64vec3_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

TEST_F(expand_source, u64vec4_expression)
{
   const glsl_type *const type = glsl_type::u64vec4_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

TEST_F(expand_source, int64_expression)
{
   const glsl_type *const type = glsl_type::int64_t_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

TEST_F(expand_source, i64vec2_expression)
{
   const glsl_type *const type = glsl_type::i64vec2_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

TEST_F(expand_source, i64vec3_expression)
{
   const glsl_type *const type = glsl_type::i64vec3_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

TEST_F(expand_source, i64vec4_expression)
{
   const glsl_type *const type = glsl_type::i64vec4_type;
   ir_expression *const expr = create_expression(mem_ctx, type);

   lower_64bit::expand_source(*body, expr, expanded_src);

   check_expanded_source(type, expanded_src);
   check_instructions(&instructions, type, expr);
}

class compact_destination : public ::testing::Test {
public:
   virtual void SetUp();
   virtual void TearDown();

   exec_list instructions;
   ir_factory *body;
   ir_variable *expanded_src[4];
   void *mem_ctx;
};

void
compact_destination::SetUp()
{
   mem_ctx = ralloc_context(NULL);

   memset(expanded_src, 0, sizeof(expanded_src));
   instructions.make_empty();
   body = new ir_factory(&instructions, mem_ctx);
}

void
compact_destination::TearDown()
{
   delete body;
   body = NULL;

   ralloc_free(mem_ctx);
   mem_ctx = NULL;
}

TEST_F(compact_destination, uint64)
{
   const glsl_type *const type = glsl_type::uint64_t_type;

   for (unsigned i = 0; i < type->vector_elements; i++) {
      expanded_src[i] = new(mem_ctx) ir_variable(glsl_type::uvec2_type,
                                                 "result",
                                                 ir_var_temporary);
   }

   ir_dereference_variable *deref =
      lower_64bit::compact_destination(*body,
                                       type,
                                       expanded_src);

   ASSERT_EQ(ir_type_dereference_variable, deref->ir_type);
   EXPECT_EQ(type, deref->var->type) <<
      "    Got " <<
      deref->var->type->name <<
      ", expected " <<
      type->name;

   ir_instruction *ir;

   ASSERT_FALSE(instructions.is_empty());
   ir = (ir_instruction *) instructions.pop_head();
   ir_variable *const var = ir->as_variable();
   ASSERT_NE((void *)0, var);
   EXPECT_EQ(deref->var, var);

   for (unsigned i = 0; i < type->vector_elements; i++) {
      ASSERT_FALSE(instructions.is_empty());
      ir = (ir_instruction *) instructions.pop_head();
      ir_assignment *const assign = ir->as_assignment();
      ASSERT_NE((void *)0, assign);
      EXPECT_EQ(deref->var, assign->lhs->variable_referenced());
   }
}