Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright 2016 Bas Nieuwenhuizen
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the
      6  * "Software"), to deal in the Software without restriction, including
      7  * without limitation the rights to use, copy, modify, merge, publish,
      8  * distribute, sub license, and/or sell copies of the Software, and to
      9  * permit persons to whom the Software is furnished to do so, subject to
     10  * the following conditions:
     11  *
     12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     14  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     15  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     16  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     17  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     18  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     19  *
     20  * The above copyright notice and this permission notice (including the
     21  * next paragraph) shall be included in all copies or substantial portions
     22  * of the Software.
     23  *
     24  */
     25 #ifndef AC_LLVM_BUILD_H
     26 #define AC_LLVM_BUILD_H
     27 
     28 #include <stdbool.h>
     29 #include <llvm-c/TargetMachine.h>
     30 
     31 #include "amd_family.h"
     32 
     33 #ifdef __cplusplus
     34 extern "C" {
     35 #endif
     36 
     37 enum {
     38 	AC_LOCAL_ADDR_SPACE = 3,
     39 };
     40 
     41 struct ac_llvm_flow;
     42 
     43 struct ac_llvm_context {
     44 	LLVMContextRef context;
     45 	LLVMModuleRef module;
     46 	LLVMBuilderRef builder;
     47 
     48 	LLVMTypeRef voidt;
     49 	LLVMTypeRef i1;
     50 	LLVMTypeRef i8;
     51 	LLVMTypeRef i16;
     52 	LLVMTypeRef i32;
     53 	LLVMTypeRef i64;
     54 	LLVMTypeRef f16;
     55 	LLVMTypeRef f32;
     56 	LLVMTypeRef f64;
     57 	LLVMTypeRef v2i32;
     58 	LLVMTypeRef v3i32;
     59 	LLVMTypeRef v4i32;
     60 	LLVMTypeRef v2f32;
     61 	LLVMTypeRef v4f32;
     62 	LLVMTypeRef v8i32;
     63 
     64 	LLVMValueRef i32_0;
     65 	LLVMValueRef i32_1;
     66 	LLVMValueRef i64_0;
     67 	LLVMValueRef i64_1;
     68 	LLVMValueRef f32_0;
     69 	LLVMValueRef f32_1;
     70 	LLVMValueRef f64_0;
     71 	LLVMValueRef f64_1;
     72 	LLVMValueRef i1true;
     73 	LLVMValueRef i1false;
     74 
     75 	struct ac_llvm_flow *flow;
     76 	unsigned flow_depth;
     77 	unsigned flow_depth_max;
     78 
     79 	unsigned range_md_kind;
     80 	unsigned invariant_load_md_kind;
     81 	unsigned uniform_md_kind;
     82 	unsigned fpmath_md_kind;
     83 	LLVMValueRef fpmath_md_2p5_ulp;
     84 	LLVMValueRef empty_md;
     85 
     86 	enum chip_class chip_class;
     87 	enum radeon_family family;
     88 
     89 	LLVMValueRef lds;
     90 };
     91 
     92 void
     93 ac_llvm_context_init(struct ac_llvm_context *ctx, LLVMContextRef context,
     94 		     enum chip_class chip_class, enum radeon_family family);
     95 
     96 void
     97 ac_llvm_context_dispose(struct ac_llvm_context *ctx);
     98 
     99 int
    100 ac_get_llvm_num_components(LLVMValueRef value);
    101 
    102 LLVMValueRef
    103 ac_llvm_extract_elem(struct ac_llvm_context *ac,
    104 		     LLVMValueRef value,
    105 		     int index);
    106 
    107 unsigned ac_get_type_size(LLVMTypeRef type);
    108 
    109 LLVMTypeRef ac_to_integer_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
    110 LLVMValueRef ac_to_integer(struct ac_llvm_context *ctx, LLVMValueRef v);
    111 LLVMTypeRef ac_to_float_type(struct ac_llvm_context *ctx, LLVMTypeRef t);
    112 LLVMValueRef ac_to_float(struct ac_llvm_context *ctx, LLVMValueRef v);
    113 
    114 LLVMValueRef
    115 ac_build_intrinsic(struct ac_llvm_context *ctx, const char *name,
    116 		   LLVMTypeRef return_type, LLVMValueRef *params,
    117 		   unsigned param_count, unsigned attrib_mask);
    118 
    119 void ac_build_type_name_for_intr(LLVMTypeRef type, char *buf, unsigned bufsize);
    120 
    121 LLVMValueRef
    122 ac_build_phi(struct ac_llvm_context *ctx, LLVMTypeRef type,
    123 	     unsigned count_incoming, LLVMValueRef *values,
    124 	     LLVMBasicBlockRef *blocks);
    125 
    126 void ac_build_optimization_barrier(struct ac_llvm_context *ctx,
    127 				   LLVMValueRef *pvgpr);
    128 
    129 LLVMValueRef ac_build_ballot(struct ac_llvm_context *ctx, LLVMValueRef value);
    130 
    131 LLVMValueRef ac_build_vote_all(struct ac_llvm_context *ctx, LLVMValueRef value);
    132 
    133 LLVMValueRef ac_build_vote_any(struct ac_llvm_context *ctx, LLVMValueRef value);
    134 
    135 LLVMValueRef ac_build_vote_eq(struct ac_llvm_context *ctx, LLVMValueRef value);
    136 
    137 LLVMValueRef
    138 ac_build_varying_gather_values(struct ac_llvm_context *ctx, LLVMValueRef *values,
    139 			       unsigned value_count, unsigned component);
    140 
    141 LLVMValueRef
    142 ac_build_gather_values_extended(struct ac_llvm_context *ctx,
    143 				LLVMValueRef *values,
    144 				unsigned value_count,
    145 				unsigned value_stride,
    146 				bool load,
    147 				bool always_vector);
    148 LLVMValueRef
    149 ac_build_gather_values(struct ac_llvm_context *ctx,
    150 		       LLVMValueRef *values,
    151 		       unsigned value_count);
    152 
    153 LLVMValueRef
    154 ac_build_fdiv(struct ac_llvm_context *ctx,
    155 	      LLVMValueRef num,
    156 	      LLVMValueRef den);
    157 
    158 void
    159 ac_prepare_cube_coords(struct ac_llvm_context *ctx,
    160 		       bool is_deriv, bool is_array, bool is_lod,
    161 		       LLVMValueRef *coords_arg,
    162 		       LLVMValueRef *derivs_arg);
    163 
    164 
    165 LLVMValueRef
    166 ac_build_fs_interp(struct ac_llvm_context *ctx,
    167 		   LLVMValueRef llvm_chan,
    168 		   LLVMValueRef attr_number,
    169 		   LLVMValueRef params,
    170 		   LLVMValueRef i,
    171 		   LLVMValueRef j);
    172 
    173 LLVMValueRef
    174 ac_build_fs_interp_mov(struct ac_llvm_context *ctx,
    175 		       LLVMValueRef parameter,
    176 		       LLVMValueRef llvm_chan,
    177 		       LLVMValueRef attr_number,
    178 		       LLVMValueRef params);
    179 
    180 LLVMValueRef
    181 ac_build_gep0(struct ac_llvm_context *ctx,
    182 	      LLVMValueRef base_ptr,
    183 	      LLVMValueRef index);
    184 
    185 void
    186 ac_build_indexed_store(struct ac_llvm_context *ctx,
    187 		       LLVMValueRef base_ptr, LLVMValueRef index,
    188 		       LLVMValueRef value);
    189 
    190 LLVMValueRef ac_build_load(struct ac_llvm_context *ctx, LLVMValueRef base_ptr,
    191 			   LLVMValueRef index);
    192 LLVMValueRef ac_build_load_invariant(struct ac_llvm_context *ctx,
    193 				     LLVMValueRef base_ptr, LLVMValueRef index);
    194 LLVMValueRef ac_build_load_to_sgpr(struct ac_llvm_context *ctx,
    195 				   LLVMValueRef base_ptr, LLVMValueRef index);
    196 
    197 void
    198 ac_build_buffer_store_dword(struct ac_llvm_context *ctx,
    199 			    LLVMValueRef rsrc,
    200 			    LLVMValueRef vdata,
    201 			    unsigned num_channels,
    202 			    LLVMValueRef voffset,
    203 			    LLVMValueRef soffset,
    204 			    unsigned inst_offset,
    205 		            bool glc,
    206 		            bool slc,
    207 			    bool writeonly_memory,
    208 			    bool swizzle_enable_hint);
    209 LLVMValueRef
    210 ac_build_buffer_load(struct ac_llvm_context *ctx,
    211 		     LLVMValueRef rsrc,
    212 		     int num_channels,
    213 		     LLVMValueRef vindex,
    214 		     LLVMValueRef voffset,
    215 		     LLVMValueRef soffset,
    216 		     unsigned inst_offset,
    217 		     unsigned glc,
    218 		     unsigned slc,
    219 		     bool can_speculate,
    220 		     bool allow_smem);
    221 
    222 LLVMValueRef ac_build_buffer_load_format(struct ac_llvm_context *ctx,
    223 					 LLVMValueRef rsrc,
    224 					 LLVMValueRef vindex,
    225 					 LLVMValueRef voffset,
    226 					 bool can_speculate);
    227 
    228 /* load_format that handles the stride & element count better if idxen is
    229  * disabled by LLVM. */
    230 LLVMValueRef ac_build_buffer_load_format_gfx9_safe(struct ac_llvm_context *ctx,
    231                                                   LLVMValueRef rsrc,
    232                                                   LLVMValueRef vindex,
    233                                                   LLVMValueRef voffset,
    234                                                   bool can_speculate);
    235 
    236 LLVMValueRef
    237 ac_get_thread_id(struct ac_llvm_context *ctx);
    238 
    239 #define AC_TID_MASK_TOP_LEFT 0xfffffffc
    240 #define AC_TID_MASK_TOP      0xfffffffd
    241 #define AC_TID_MASK_LEFT     0xfffffffe
    242 
    243 LLVMValueRef
    244 ac_build_ddxy(struct ac_llvm_context *ctx,
    245 	      uint32_t mask,
    246 	      int idx,
    247 	      LLVMValueRef val);
    248 
    249 #define AC_SENDMSG_GS 2
    250 #define AC_SENDMSG_GS_DONE 3
    251 
    252 #define AC_SENDMSG_GS_OP_NOP      (0 << 4)
    253 #define AC_SENDMSG_GS_OP_CUT      (1 << 4)
    254 #define AC_SENDMSG_GS_OP_EMIT     (2 << 4)
    255 #define AC_SENDMSG_GS_OP_EMIT_CUT (3 << 4)
    256 
    257 void ac_build_sendmsg(struct ac_llvm_context *ctx,
    258 		      uint32_t msg,
    259 		      LLVMValueRef wave_id);
    260 
    261 LLVMValueRef ac_build_imsb(struct ac_llvm_context *ctx,
    262 			   LLVMValueRef arg,
    263 			   LLVMTypeRef dst_type);
    264 
    265 LLVMValueRef ac_build_umsb(struct ac_llvm_context *ctx,
    266 			  LLVMValueRef arg,
    267 			  LLVMTypeRef dst_type);
    268 LLVMValueRef ac_build_fmin(struct ac_llvm_context *ctx, LLVMValueRef a,
    269 			   LLVMValueRef b);
    270 LLVMValueRef ac_build_fmax(struct ac_llvm_context *ctx, LLVMValueRef a,
    271 			   LLVMValueRef b);
    272 LLVMValueRef ac_build_umin(struct ac_llvm_context *ctx, LLVMValueRef a, LLVMValueRef b);
    273 LLVMValueRef ac_build_clamp(struct ac_llvm_context *ctx, LLVMValueRef value);
    274 
    275 struct ac_export_args {
    276 	LLVMValueRef out[4];
    277         unsigned target;
    278         unsigned enabled_channels;
    279         bool compr;
    280         bool done;
    281         bool valid_mask;
    282 };
    283 
    284 void ac_build_export(struct ac_llvm_context *ctx, struct ac_export_args *a);
    285 
    286 enum ac_image_opcode {
    287 	ac_image_sample,
    288 	ac_image_gather4,
    289 	ac_image_load,
    290 	ac_image_load_mip,
    291 	ac_image_get_lod,
    292 	ac_image_get_resinfo,
    293 };
    294 
    295 struct ac_image_args {
    296 	enum ac_image_opcode opcode;
    297 	bool level_zero;
    298 	bool bias;
    299 	bool lod;
    300 	bool deriv;
    301 	bool compare;
    302 	bool offset;
    303 
    304 	LLVMValueRef resource;
    305 	LLVMValueRef sampler;
    306 	LLVMValueRef addr;
    307 	unsigned dmask;
    308 	bool unorm;
    309 	bool da;
    310 };
    311 
    312 LLVMValueRef ac_build_image_opcode(struct ac_llvm_context *ctx,
    313 				   struct ac_image_args *a);
    314 LLVMValueRef ac_build_cvt_pkrtz_f16(struct ac_llvm_context *ctx,
    315 				    LLVMValueRef args[2]);
    316 LLVMValueRef ac_build_wqm_vote(struct ac_llvm_context *ctx, LLVMValueRef i1);
    317 void ac_build_kill_if_false(struct ac_llvm_context *ctx, LLVMValueRef i1);
    318 LLVMValueRef ac_build_bfe(struct ac_llvm_context *ctx, LLVMValueRef input,
    319 			  LLVMValueRef offset, LLVMValueRef width,
    320 			  bool is_signed);
    321 
    322 void ac_build_waitcnt(struct ac_llvm_context *ctx, unsigned simm16);
    323 
    324 void ac_get_image_intr_name(const char *base_name,
    325 			    LLVMTypeRef data_type,
    326 			    LLVMTypeRef coords_type,
    327 			    LLVMTypeRef rsrc_type,
    328 			    char *out_name, unsigned out_len);
    329 
    330 void ac_optimize_vs_outputs(struct ac_llvm_context *ac,
    331 			    LLVMValueRef main_fn,
    332 			    uint8_t *vs_output_param_offset,
    333 			    uint32_t num_outputs,
    334 			    uint8_t *num_param_exports);
    335 void ac_init_exec_full_mask(struct ac_llvm_context *ctx);
    336 
    337 void ac_declare_lds_as_pointer(struct ac_llvm_context *ac);
    338 LLVMValueRef ac_lds_load(struct ac_llvm_context *ctx,
    339 			 LLVMValueRef dw_addr);
    340 void ac_lds_store(struct ac_llvm_context *ctx,
    341 		  LLVMValueRef dw_addr, LLVMValueRef value);
    342 
    343 LLVMValueRef ac_find_lsb(struct ac_llvm_context *ctx,
    344 			 LLVMTypeRef dst_type,
    345 			 LLVMValueRef src0);
    346 
    347 void ac_build_bgnloop(struct ac_llvm_context *ctx, int lable_id);
    348 void ac_build_break(struct ac_llvm_context *ctx);
    349 void ac_build_continue(struct ac_llvm_context *ctx);
    350 void ac_build_else(struct ac_llvm_context *ctx, int lable_id);
    351 void ac_build_endif(struct ac_llvm_context *ctx, int lable_id);
    352 void ac_build_endloop(struct ac_llvm_context *ctx, int lable_id);
    353 void ac_build_if(struct ac_llvm_context *ctx, LLVMValueRef value,
    354 		 int lable_id);
    355 void ac_build_uif(struct ac_llvm_context *ctx, LLVMValueRef value,
    356 		  int lable_id);
    357 
    358 #ifdef __cplusplus
    359 }
    360 #endif
    361 
    362 #endif
    363