summaryrefslogtreecommitdiff
path: root/gcc/brig/brigfrontend/brig-cvt-inst-handler.cc
blob: 3b8c9ea01df54d29a7f1e28ff1e75f70f75eace1 (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
/* brig-cvt-inst-handler.cc -- brig cvt (convert) instruction handling
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
   Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
   for General Processor Tech.

   This file is part of GCC.

   GCC is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3, or (at your option) any later
   version.

   GCC is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

   You should have received a copy of the GNU General Public License
   along with GCC; see the file COPYING3.  If not see
   <http://www.gnu.org/licenses/>.  */

#include <sstream>

#include "brig-code-entry-handler.h"

#include "gimple-expr.h"
#include "errors.h"
#include "convert.h"
#include "tree-pretty-print.h"
#include "print-tree.h"
#include "diagnostic-core.h"
#include "brig-util.h"

const BrigAluModifier8_t *
brig_cvt_inst_handler::modifier (const BrigBase *base) const
{
  const BrigInstCvt *inst = (const BrigInstCvt *) base;
  return &inst->modifier;
}

const BrigRound8_t *
brig_cvt_inst_handler::round (const BrigBase *base) const
{
  const BrigInstCvt *inst = (const BrigInstCvt *) base;
  return &inst->round;
}

size_t
brig_cvt_inst_handler::generate (const BrigBase *base)
{
  /* In cvt instructions there can be at least four data types involved:

     - the input register type
     - the output register type
     - the conversion source type
     - the conversion destination type
  */

  const BrigInstBase *brig_inst
    = (const BrigInstBase *) &((const BrigInstBasic *) base)->base;
  const BrigInstCvt *cvt_inst = (const BrigInstCvt *) base;

  const BrigAluModifier8_t *inst_modifier = modifier (base);
  const bool FTZ = inst_modifier != NULL && (*inst_modifier) & BRIG_ALU_FTZ;

  /* The conversion source type.  */
  tree src_type = get_tree_expr_type_for_hsa_type (cvt_inst->sourceType);

  bool src_is_fp16 = cvt_inst->sourceType == BRIG_TYPE_F16;

  /* The conversion destination type.  */
  tree dest_type = gccbrig_tree_type_for_hsa_type (brig_inst->type);

  bool dest_is_fp16 = brig_inst->type == BRIG_TYPE_F16;

  if (!dest_type || !src_type)
    {
      gcc_unreachable ();
      return base->byteCount;
    }

  tree_stl_vec operands = build_operands (*brig_inst);
  tree &input = operands.at (1);
  tree &output = operands.at (0);

  if (m_parent.m_cf->is_id_val (input))
    {
      input = m_parent.m_cf->id_val (input);
      src_type = TREE_TYPE (input);
    }

  size_t conv_src_size = int_size_in_bytes (src_type);
  size_t conv_dst_size = int_size_in_bytes (dest_type);
  size_t src_reg_size = int_size_in_bytes (TREE_TYPE (input));

  /* The input register can be of different type&size than the
     conversion input size.  First cast the input to the conversion
     input type.  These casts are always bitcasts which can be
     expressed as casts between different unsigned integers.  */
  if (src_reg_size != conv_src_size)
    {
      tree unsigned_int_type = NULL_TREE;
      if (INTEGRAL_TYPE_P (src_type))
	unsigned_int_type = unsigned_type_for (src_type);
      else /* Find a matching size int type for the REAL type.  */
	{
	  if (conv_src_size == 2)
	    unsigned_int_type = gccbrig_tree_type_for_hsa_type (BRIG_TYPE_U16);
	  else if (conv_src_size == 4)
	    unsigned_int_type = gccbrig_tree_type_for_hsa_type (BRIG_TYPE_U32);
	  else if (conv_src_size == 8)
	    unsigned_int_type = gccbrig_tree_type_for_hsa_type (BRIG_TYPE_U64);
	  else
	    gcc_unreachable ();
	}
      input = convert_to_integer (unsigned_int_type, input);
    }

  if (src_is_fp16)
    input = build_h2f_conversion (input);

  /* Flush the float operand to zero if indicated with 'ftz'.  */
  if (FTZ && SCALAR_FLOAT_TYPE_P (src_type))
    {
      tree casted_input = build_resize_convert_view (src_type, input);
      input = flush_to_zero (src_is_fp16) (*this, casted_input);
    }

  tree conversion_result = NULL_TREE;
  if (brig_inst->type == BRIG_TYPE_B1)
    {
      /* When the destination is b1, cvt does a 'ztest' operation which is
	 defined as a != 0 for integers and similarly (!= 0.0f) for floats.  */
      if (INTEGRAL_TYPE_P (src_type))
	{
	  /* Generate an integer not equal operation.  */
	  conversion_result = build2 (NE_EXPR, TREE_TYPE (input), input,
				      build_int_cst (TREE_TYPE (input), 0));
	}
      else
	{
	  /* For REAL source types, ztest returns 1 if the value is not +- 0.0f.
	     We can perform this check with an integer comparison after
	     masking away the sign bit from a correct position.  This is safer
	     than using absf because of exceptions in case of a NaN
	     input (NaN exceptions are not generated with cvt).  */
	  tree unsigned_int_type = NULL_TREE;
	  /* Bit battern with all but the upper bit 1.  */
	  tree and_mask = NULL_TREE;
	  if (conv_src_size == 2)
	    {
	      unsigned_int_type = gccbrig_tree_type_for_hsa_type (BRIG_TYPE_U16);
	      and_mask = build_int_cst (unsigned_int_type, 0x7FFF);
	    }
	  else if (conv_src_size == 4)
	    {
	      unsigned_int_type = gccbrig_tree_type_for_hsa_type (BRIG_TYPE_U32);
	      and_mask = build_int_cst (unsigned_int_type, 0x7FFFFFFF);
	    }
	  else if (conv_src_size == 8)
	    {
	      unsigned_int_type = gccbrig_tree_type_for_hsa_type (BRIG_TYPE_U64);
	      and_mask = build_int_cst (unsigned_int_type, 0x7FFFFFFFFFFFFFFF);
	    }
	  else
	    gcc_unreachable ();
	  tree casted_input = build_resize_convert_view (unsigned_int_type,
							 input);
	  tree masked_input
	    = build2 (BIT_AND_EXPR, unsigned_int_type, casted_input, and_mask);
	  conversion_result
	    = build2 (NE_EXPR, TREE_TYPE (masked_input), masked_input,
		      build_int_cst (unsigned_int_type, 0));
	}
      /* The result from the comparison is a boolean, convert it to such.  */
      conversion_result
	= convert_to_integer (gccbrig_tree_type_for_hsa_type (BRIG_TYPE_B1),
			      conversion_result);
    }
  else if (dest_is_fp16)
    {
      tree casted_input = build_resize_convert_view (src_type, input);
      conversion_result
	= convert_to_real (brig_to_generic::s_fp32_type, casted_input);
      if (FTZ)
	conversion_result = flush_to_zero (true) (*this, conversion_result);
      conversion_result = build_f2h_conversion (conversion_result);
    }
  else if (SCALAR_FLOAT_TYPE_P (dest_type))
    {
      tree casted_input = build_resize_convert_view (src_type, input);
      conversion_result = convert_to_real (dest_type, casted_input);
    }
  else if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
    {
      conversion_result = extend_int (input, dest_type, src_type);
    }
  else if (INTEGRAL_TYPE_P (dest_type) && SCALAR_FLOAT_TYPE_P (src_type))
    {

      if (cvt_inst->round == BRIG_ROUND_INTEGER_ZERO_SAT)
	{

	  /* Use builtins for the saturating conversions.  */
#undef DEF_HSAIL_SAT_BUILTIN
#undef DEF_HSAIL_BUILTIN
#undef DEF_HSAIL_ATOMIC_BUILTIN
#undef DEF_HSAIL_INTR_BUILTIN
#undef DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN

	  tree builtin = NULL_TREE;
	  BrigType16_t src_arith_type
	    = src_is_fp16
	    ? (BrigType16_t) BRIG_TYPE_F32 : cvt_inst->sourceType;
#define DEF_HSAIL_CVT_ZEROI_SAT_BUILTIN(ENUM, HSAIL_DST_TYPE, HSAIL_SRC_TYPE, \
					NAME, TYPE, ATTRS)		\
	  if (brig_inst->type == HSAIL_DST_TYPE				\
	      && src_arith_type == HSAIL_SRC_TYPE)		\
	    builtin = builtin_decl_explicit (ENUM);			\
	  else
#include "brig-builtins.def"
	    gcc_unreachable ();

	  tree casted_input = build_resize_convert_view (src_type, input);
	  conversion_result
	    = call_builtin (builtin, 1, dest_type, src_type, casted_input);
	}
      else
	{
	  tree casted_input = build_resize_convert_view (src_type, input);

	  /* Perform the float to int conversion.  */
	  conversion_result = convert_to_integer (dest_type, casted_input);
	}
    }
  else
    {
      /* Just use CONVERT_EXPR and hope for the best.  */
      tree casted_input = build_resize_convert_view (dest_type, input);
      conversion_result = build1 (CONVERT_EXPR, dest_type, casted_input);
    }

  size_t dst_reg_size = int_size_in_bytes (TREE_TYPE (output));

  /* The output register can be of different type&size than the
     conversion output size. Only need to handle signed integers, rest
     is handled by reinterpret_cast.  */
  tree casted_output = conversion_result;
  if (dst_reg_size > conv_dst_size &&
      INTEGRAL_TYPE_P (TREE_TYPE (casted_output)))
    {
      gcc_assert (!VECTOR_TYPE_P (casted_output));

      bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (casted_output));
      tree resized_int_type
        = build_nonstandard_integer_type (dst_reg_size * BITS_PER_UNIT,
					  unsignedp);
      casted_output = build1 (CONVERT_EXPR, resized_int_type, casted_output);
    }

  casted_output
    = build_resize_convert_view (TREE_TYPE (output), casted_output);
  tree assign = build2 (MODIFY_EXPR, TREE_TYPE (output), output, casted_output);

  m_parent.m_cf->append_statement (assign);

  return base->byteCount;
}