#ifndef NIR_H
#define NIR_H
#include "util/hash_table.h"
#include "compiler/glsl/list.h"
#include "GL/gl.h"
#include "util/list.h"
#include "util/log.h"
#include "util/ralloc.h"
#include "util/set.h"
#include "util/bitscan.h"
#include "util/bitset.h"
#include "util/compiler.h"
#include "util/enum_operators.h"
#include "util/macros.h"
#include "util/format/u_format.h"
#include "compiler/nir_types.h"
#include "compiler/shader_enums.h"
#include "compiler/shader_info.h"
#define XXH_INLINE_ALL
#include "util/xxhash.h"
#include <stdio.h>
#ifndef NDEBUG
#include "util/debug.h"
#endif
#include "nir_opcodes.h"
#if defined(_WIN32) && !defined(snprintf)
#define snprintf _snprintf
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define NIR_FALSE 0u
#define NIR_TRUE (~0u)
#define NIR_MAX_VEC_COMPONENTS 16
#define NIR_MAX_MATRIX_COLUMNS 4
#define NIR_STREAM_PACKED (1 << 8)
typedef uint16_t nir_component_mask_t;
static inline bool
nir_num_components_valid(unsigned num_components)
{
return (num_components >= 1 &&
num_components <= 5) ||
num_components == 8 ||
num_components == 16;
}
bool nir_component_mask_can_reinterpret(nir_component_mask_t mask,
unsigned old_bit_size,
unsigned new_bit_size);
nir_component_mask_t
nir_component_mask_reinterpret(nir_component_mask_t mask,
unsigned old_bit_size,
unsigned new_bit_size);
#define NIR_DEFINE_CAST(name, in_type, out_type, field, \
type_field, type_value) \
static inline out_type * \
name(const in_type *parent) \
{ \
assert(parent && parent->type_field == type_value); \
return exec_node_data(out_type, parent, field); \
}
struct nir_function;
struct nir_shader;
struct nir_instr;
struct nir_builder;
typedef struct {
gl_state_index16 tokens[STATE_LENGTH];
uint16_t swizzle;
} nir_state_slot;
typedef enum {
nir_var_shader_in = (1 << 0),
nir_var_shader_out = (1 << 1),
nir_var_shader_temp = (1 << 2),
nir_var_function_temp = (1 << 3),
nir_var_uniform = (1 << 4),
nir_var_mem_ubo = (1 << 5),
nir_var_system_value = (1 << 6),
nir_var_mem_ssbo = (1 << 7),
nir_var_mem_shared = (1 << 8),
nir_var_mem_global = (1 << 9),
nir_var_mem_generic = (nir_var_shader_temp |
nir_var_function_temp |
nir_var_mem_shared |
nir_var_mem_global),
nir_var_mem_push_const = (1 << 10),
nir_var_mem_constant = (1 << 11),
nir_var_shader_call_data = (1 << 12),
nir_var_ray_hit_attrib = (1 << 13),
nir_var_read_only_modes = nir_var_shader_in | nir_var_uniform |
nir_var_system_value | nir_var_mem_constant |
nir_var_mem_ubo,
nir_var_vec_indexable_modes = nir_var_mem_ubo | nir_var_mem_ssbo |
nir_var_mem_shared | nir_var_mem_global |
nir_var_mem_push_const,
nir_num_variable_modes = 14,
nir_var_all = (1 << nir_num_variable_modes) - 1,
} nir_variable_mode;
MESA_DEFINE_CPP_ENUM_BITFIELD_OPERATORS(nir_variable_mode)
typedef enum {
nir_rounding_mode_undef = 0,
nir_rounding_mode_rtne = 1,
nir_rounding_mode_ru = 2,
nir_rounding_mode_rd = 3,
nir_rounding_mode_rtz = 4,
} nir_rounding_mode;
typedef union {
bool b;
float f32;
double f64;
int8_t i8;
uint8_t u8;
int16_t i16;
uint16_t u16;
int32_t i32;
uint32_t u32;
int64_t i64;
uint64_t u64;
} nir_const_value;
#define nir_const_value_to_array(arr, c, components, m) \
{ \
for (unsigned i = 0; i < components; ++i) \
arr[i] = c[i].m; \
} while (false)
static inline nir_const_value
nir_const_value_for_raw_uint(uint64_t x, unsigned bit_size)
{
nir_const_value v;
memset(&v, 0, sizeof(v));
switch (bit_size) {
case 1: v.b = x; break;
case 8: v.u8 = x; break;
case 16: v.u16 = x; break;
case 32: v.u32 = x; break;
case 64: v.u64 = x; break;
default:
unreachable("Invalid bit size");
}
return v;
}
static inline nir_const_value
nir_const_value_for_int(int64_t i, unsigned bit_size)
{
nir_const_value v;
memset(&v, 0, sizeof(v));
assert(bit_size <= 64);
if (bit_size < 64) {
assert(i >= (-(1ll << (bit_size - 1))));
assert(i < (1ll << (bit_size - 1)));
}
return nir_const_value_for_raw_uint(i, bit_size);
}
static inline nir_const_value
nir_const_value_for_uint(uint64_t u, unsigned bit_size)
{
nir_const_value v;
memset(&v, 0, sizeof(v));
assert(bit_size <= 64);
if (bit_size < 64)
assert(u < (1ull << bit_size));
return nir_const_value_for_raw_uint(u, bit_size);
}
static inline nir_const_value
nir_const_value_for_bool(bool b, unsigned bit_size)
{
return nir_const_value_for_int(-(int)b, bit_size);
}
nir_const_value nir_const_value_for_float(double b, unsigned bit_size);
static inline int64_t
nir_const_value_as_int(nir_const_value value, unsigned bit_size)
{
switch (bit_size) {
case 1: return -(int)value.b;
case 8: return value.i8;
case 16: return value.i16;
case 32: return value.i32;
case 64: return value.i64;
default:
unreachable("Invalid bit size");
}
}
static inline uint64_t
nir_const_value_as_uint(nir_const_value value, unsigned bit_size)
{
switch (bit_size) {
case 1: return value.b;
case 8: return value.u8;
case 16: return value.u16;
case 32: return value.u32;
case 64: return value.u64;
default:
unreachable("Invalid bit size");
}
}
static inline bool
nir_const_value_as_bool(nir_const_value value, unsigned bit_size)
{
int64_t i = nir_const_value_as_int(value, bit_size);
assert(i == 0 || i == -1);
return i;
}
double nir_const_value_as_float(nir_const_value value, unsigned bit_size);
typedef struct nir_constant {
nir_const_value values[NIR_MAX_VEC_COMPONENTS];
unsigned num_elements;
struct nir_constant **elements;
} nir_constant;
typedef enum {
nir_depth_layout_none,
nir_depth_layout_any,
nir_depth_layout_greater,
nir_depth_layout_less,
nir_depth_layout_unchanged
} nir_depth_layout;
typedef enum {
nir_var_declared_normally = 0,
nir_var_hidden,
} nir_var_declaration_type;
typedef struct nir_variable {
struct exec_node node;
const struct glsl_type *type;
char *name;
struct nir_variable_data {
unsigned mode:14;
unsigned read_only:1;
unsigned centroid:1;
unsigned sample:1;
unsigned patch:1;
unsigned invariant:1;
unsigned precision:2;
unsigned cannot_coalesce:1;
unsigned always_active_io:1;
unsigned interpolation:3;
unsigned location_frac:2;
unsigned compact:1;
unsigned fb_fetch_output:1;
unsigned bindless:1;
unsigned explicit_binding:1;
unsigned explicit_location:1;
unsigned explicit_xfb_buffer:1;
unsigned explicit_xfb_stride:1;
unsigned explicit_offset:1;
unsigned matrix_layout:2;
unsigned from_named_ifc_block:1;
unsigned how_declared:2;
unsigned per_view:1;
unsigned depth_layout:3;
unsigned stream:9;
unsigned access:8;
unsigned descriptor_set:5;
unsigned index;
unsigned binding;
int location;
unsigned driver_location;
unsigned offset;
union {
struct {
enum pipe_format format;
} image;
struct {
unsigned is_inline_sampler : 1;
unsigned addressing_mode : 3;
unsigned normalized_coordinates : 1;
unsigned filter_mode : 1;
} sampler;
struct {
uint16_t buffer:2;
uint16_t stride;
} xfb;
};
} data;
unsigned index;
uint16_t num_members;
uint16_t num_state_slots;
nir_state_slot *state_slots;
nir_constant *constant_initializer;
struct nir_variable *pointer_initializer;
const struct glsl_type *interface_type;
struct nir_variable_data *members;
} nir_variable;
static inline bool
_nir_shader_variable_has_mode(nir_variable *var, unsigned modes)
{
assert(!(modes & nir_var_function_temp));
return var->data.mode & modes;
}
#define nir_foreach_variable_in_list(var, var_list) \
foreach_list_typed(nir_variable, var, node, var_list)
#define nir_foreach_variable_in_list_safe(var, var_list) \
foreach_list_typed_safe(nir_variable, var, node, var_list)
#define nir_foreach_variable_in_shader(var, shader) \
nir_foreach_variable_in_list(var, &(shader)->variables)
#define nir_foreach_variable_in_shader_safe(var, shader) \
nir_foreach_variable_in_list_safe(var, &(shader)->variables)
#define nir_foreach_variable_with_modes(var, shader, modes) \
nir_foreach_variable_in_shader(var, shader) \
if (_nir_shader_variable_has_mode(var, modes))
#define nir_foreach_variable_with_modes_safe(var, shader, modes) \
nir_foreach_variable_in_shader_safe(var, shader) \
if (_nir_shader_variable_has_mode(var, modes))
#define nir_foreach_shader_in_variable(var, shader) \
nir_foreach_variable_with_modes(var, shader, nir_var_shader_in)
#define nir_foreach_shader_in_variable_safe(var, shader) \
nir_foreach_variable_with_modes_safe(var, shader, nir_var_shader_in)
#define nir_foreach_shader_out_variable(var, shader) \
nir_foreach_variable_with_modes(var, shader, nir_var_shader_out)
#define nir_foreach_shader_out_variable_safe(var, shader) \
nir_foreach_variable_with_modes_safe(var, shader, nir_var_shader_out)
#define nir_foreach_uniform_variable(var, shader) \
nir_foreach_variable_with_modes(var, shader, nir_var_uniform)
#define nir_foreach_uniform_variable_safe(var, shader) \
nir_foreach_variable_with_modes_safe(var, shader, nir_var_uniform)
static inline bool
nir_variable_is_global(const nir_variable *var)
{
return var->data.mode != nir_var_function_temp;
}
typedef struct nir_register {
struct exec_node node;
unsigned num_components;
unsigned num_array_elems;
uint8_t bit_size;
bool divergent;
unsigned index;
struct list_head uses;
struct list_head defs;
struct list_head if_uses;
} nir_register;
#define nir_foreach_register(reg, reg_list) \
foreach_list_typed(nir_register, reg, node, reg_list)
#define nir_foreach_register_safe(reg, reg_list) \
foreach_list_typed_safe(nir_register, reg, node, reg_list)
typedef enum PACKED {
nir_instr_type_alu,
nir_instr_type_deref,
nir_instr_type_call,
nir_instr_type_tex,
nir_instr_type_intrinsic,
nir_instr_type_load_const,
nir_instr_type_jump,
nir_instr_type_ssa_undef,
nir_instr_type_phi,
nir_instr_type_parallel_copy,
} nir_instr_type;
typedef struct nir_instr {
struct exec_node node;
struct nir_block *block;
nir_instr_type type;
uint8_t pass_flags;
uint32_t index;
} nir_instr;
static inline nir_instr *
nir_instr_next(nir_instr *instr)
{
struct exec_node *next = exec_node_get_next(&instr->node);
if (exec_node_is_tail_sentinel(next))
return NULL;
else
return exec_node_data(nir_instr, next, node);
}
static inline nir_instr *
nir_instr_prev(nir_instr *instr)
{
struct exec_node *prev = exec_node_get_prev(&instr->node);
if (exec_node_is_head_sentinel(prev))
return NULL;
else
return exec_node_data(nir_instr, prev, node);
}
static inline bool
nir_instr_is_first(const nir_instr *instr)
{
return exec_node_is_head_sentinel(exec_node_get_prev_const(&instr->node));
}
static inline bool
nir_instr_is_last(const nir_instr *instr)
{
return exec_node_is_tail_sentinel(exec_node_get_next_const(&instr->node));
}
typedef struct nir_ssa_def {
nir_instr *parent_instr;
struct list_head uses;
struct list_head if_uses;
unsigned index;
uint8_t num_components;
uint8_t bit_size;
bool divergent;
} nir_ssa_def;
struct nir_src;
typedef struct {
nir_register *reg;
struct nir_src *indirect;
unsigned base_offset;
} nir_reg_src;
typedef struct {
nir_instr *parent_instr;
struct list_head def_link;
nir_register *reg;
struct nir_src *indirect;
unsigned base_offset;
} nir_reg_dest;
struct nir_if;
typedef struct nir_src {
union {
nir_instr *parent_instr;
struct nir_if *parent_if;
};
struct list_head use_link;
union {
nir_reg_src reg;
nir_ssa_def *ssa;
};
bool is_ssa;
} nir_src;
static inline nir_src
nir_src_init(void)
{
nir_src src = { { NULL } };
return src;
}
#define NIR_SRC_INIT nir_src_init()
#define nir_foreach_use(src, reg_or_ssa_def) \
list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
#define nir_foreach_use_safe(src, reg_or_ssa_def) \
list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
#define nir_foreach_if_use(src, reg_or_ssa_def) \
list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
#define nir_foreach_if_use_safe(src, reg_or_ssa_def) \
list_for_each_entry_safe(nir_src, src, &(reg_or_ssa_def)->if_uses, use_link)
typedef struct {
union {
nir_reg_dest reg;
nir_ssa_def ssa;
};
bool is_ssa;
} nir_dest;
static inline nir_dest
nir_dest_init(void)
{
nir_dest dest = { { { NULL } } };
return dest;
}
#define NIR_DEST_INIT nir_dest_init()
#define nir_foreach_def(dest, reg) \
list_for_each_entry(nir_dest, dest, &(reg)->defs, reg.def_link)
#define nir_foreach_def_safe(dest, reg) \
list_for_each_entry_safe(nir_dest, dest, &(reg)->defs, reg.def_link)
static inline nir_src
nir_src_for_ssa(nir_ssa_def *def)
{
nir_src src = NIR_SRC_INIT;
src.is_ssa = true;
src.ssa = def;
return src;
}
static inline nir_src
nir_src_for_reg(nir_register *reg)
{
nir_src src = NIR_SRC_INIT;
src.is_ssa = false;
src.reg.reg = reg;
src.reg.indirect = NULL;
src.reg.base_offset = 0;
return src;
}
static inline nir_dest
nir_dest_for_reg(nir_register *reg)
{
nir_dest dest = NIR_DEST_INIT;
dest.reg.reg = reg;
return dest;
}
static inline unsigned
nir_src_bit_size(nir_src src)
{
return src.is_ssa ? src.ssa->bit_size : src.reg.reg->bit_size;
}
static inline unsigned
nir_src_num_components(nir_src src)
{
return src.is_ssa ? src.ssa->num_components : src.reg.reg->num_components;
}
static inline bool
nir_src_is_const(nir_src src)
{
return src.is_ssa &&
src.ssa->parent_instr->type == nir_instr_type_load_const;
}
static inline bool
nir_src_is_undef(nir_src src)
{
return src.is_ssa &&
src.ssa->parent_instr->type == nir_instr_type_ssa_undef;
}
static inline bool
nir_src_is_divergent(nir_src src)
{
return src.is_ssa ? src.ssa->divergent : src.reg.reg->divergent;
}
static inline unsigned
nir_dest_bit_size(nir_dest dest)
{
return dest.is_ssa ? dest.ssa.bit_size : dest.reg.reg->bit_size;
}
static inline unsigned
nir_dest_num_components(nir_dest dest)
{
return dest.is_ssa ? dest.ssa.num_components : dest.reg.reg->num_components;
}
static inline bool
nir_dest_is_divergent(nir_dest dest)
{
return dest.is_ssa ? dest.ssa.divergent : dest.reg.reg->divergent;
}
static inline bool
nir_is_same_comp_swizzle(uint8_t *swiz, unsigned nr_comp)
{
for (unsigned i = 1; i < nr_comp; i++)
if (swiz[i] != swiz[0])
return false;
return true;
}
static inline bool
nir_is_sequential_comp_swizzle(uint8_t *swiz, unsigned nr_comp)
{
for (unsigned i = 1; i < nr_comp; i++)
if (swiz[i] != (swiz[0] + i))
return false;
return true;
}
void nir_src_copy(nir_src *dest, const nir_src *src, void *instr_or_if);
void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr);
typedef struct {
nir_src src;
bool negate;
bool abs;
uint8_t swizzle[NIR_MAX_VEC_COMPONENTS];
} nir_alu_src;
typedef struct {
nir_dest dest;
bool saturate;
unsigned write_mask : NIR_MAX_VEC_COMPONENTS;
} nir_alu_dest;
typedef enum PACKED {
nir_type_invalid = 0,
nir_type_int = 2,
nir_type_uint = 4,
nir_type_bool = 6,
nir_type_float = 128,
nir_type_bool1 = 1 | nir_type_bool,
nir_type_bool8 = 8 | nir_type_bool,
nir_type_bool16 = 16 | nir_type_bool,
nir_type_bool32 = 32 | nir_type_bool,
nir_type_int1 = 1 | nir_type_int,
nir_type_int8 = 8 | nir_type_int,
nir_type_int16 = 16 | nir_type_int,
nir_type_int32 = 32 | nir_type_int,
nir_type_int64 = 64 | nir_type_int,
nir_type_uint1 = 1 | nir_type_uint,
nir_type_uint8 = 8 | nir_type_uint,
nir_type_uint16 = 16 | nir_type_uint,
nir_type_uint32 = 32 | nir_type_uint,
nir_type_uint64 = 64 | nir_type_uint,
nir_type_float16 = 16 | nir_type_float,
nir_type_float32 = 32 | nir_type_float,
nir_type_float64 = 64 | nir_type_float,
} nir_alu_type;
#define NIR_ALU_TYPE_SIZE_MASK 0x79
#define NIR_ALU_TYPE_BASE_TYPE_MASK 0x86
static inline unsigned
nir_alu_type_get_type_size(nir_alu_type type)
{
return type & NIR_ALU_TYPE_SIZE_MASK;
}
static inline nir_alu_type
nir_alu_type_get_base_type(nir_alu_type type)
{
return (nir_alu_type)(type & NIR_ALU_TYPE_BASE_TYPE_MASK);
}
static inline nir_alu_type
nir_get_nir_type_for_glsl_base_type(enum glsl_base_type base_type)
{
switch (base_type) {
case GLSL_TYPE_BOOL:
return nir_type_bool1;
break;
case GLSL_TYPE_UINT:
return nir_type_uint32;
break;
case GLSL_TYPE_INT:
return nir_type_int32;
break;
case GLSL_TYPE_UINT16:
return nir_type_uint16;
break;
case GLSL_TYPE_INT16:
return nir_type_int16;
break;
case GLSL_TYPE_UINT8:
return nir_type_uint8;
case GLSL_TYPE_INT8:
return nir_type_int8;
case GLSL_TYPE_UINT64:
return nir_type_uint64;
break;
case GLSL_TYPE_INT64:
return nir_type_int64;
break;
case GLSL_TYPE_FLOAT:
return nir_type_float32;
break;
case GLSL_TYPE_FLOAT16:
return nir_type_float16;
break;
case GLSL_TYPE_DOUBLE:
return nir_type_float64;
break;
case GLSL_TYPE_SAMPLER:
case GLSL_TYPE_IMAGE:
case GLSL_TYPE_ATOMIC_UINT:
case GLSL_TYPE_STRUCT:
case GLSL_TYPE_INTERFACE:
case GLSL_TYPE_ARRAY:
case GLSL_TYPE_VOID:
case GLSL_TYPE_SUBROUTINE:
case GLSL_TYPE_FUNCTION:
case GLSL_TYPE_ERROR:
return nir_type_invalid;
}
unreachable("unknown type");
}
static inline nir_alu_type
nir_get_nir_type_for_glsl_type(const struct glsl_type *type)
{
return nir_get_nir_type_for_glsl_base_type(glsl_get_base_type(type));
}
static inline enum glsl_base_type
nir_get_glsl_base_type_for_nir_type(nir_alu_type base_type)
{
switch (base_type) {
case nir_type_bool1:
return GLSL_TYPE_BOOL;
case nir_type_uint32:
return GLSL_TYPE_UINT;
case nir_type_int32:
return GLSL_TYPE_INT;
case nir_type_uint16:
return GLSL_TYPE_UINT16;
case nir_type_int16:
return GLSL_TYPE_INT16;
case nir_type_uint8:
return GLSL_TYPE_UINT8;
case nir_type_int8:
return GLSL_TYPE_INT8;
case nir_type_uint64:
return GLSL_TYPE_UINT64;
case nir_type_int64:
return GLSL_TYPE_INT64;
case nir_type_float32:
return GLSL_TYPE_FLOAT;
case nir_type_float16:
return GLSL_TYPE_FLOAT16;
case nir_type_float64:
return GLSL_TYPE_DOUBLE;
default: unreachable("Not a sized nir_alu_type");
}
}
nir_op nir_type_conversion_op(nir_alu_type src, nir_alu_type dst,
nir_rounding_mode rnd);
static inline nir_op
nir_op_vec(unsigned components)
{
switch (components) {
case 1: return nir_op_mov;
case 2: return nir_op_vec2;
case 3: return nir_op_vec3;
case 4: return nir_op_vec4;
case 5: return nir_op_vec5;
case 8: return nir_op_vec8;
case 16: return nir_op_vec16;
default: unreachable("bad component count");
}
}
static inline bool
nir_op_is_vec(nir_op op)
{
switch (op) {
case nir_op_mov:
case nir_op_vec2:
case nir_op_vec3:
case nir_op_vec4:
case nir_op_vec5:
case nir_op_vec8:
case nir_op_vec16:
return true;
default:
return false;
}
}
static inline bool
nir_is_float_control_signed_zero_inf_nan_preserve(unsigned execution_mode, unsigned bit_size)
{
return (16 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP16) ||
(32 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP32) ||
(64 == bit_size && execution_mode & FLOAT_CONTROLS_SIGNED_ZERO_INF_NAN_PRESERVE_FP64);
}
static inline bool
nir_is_denorm_flush_to_zero(unsigned execution_mode, unsigned bit_size)
{
return (16 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP16) ||
(32 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP32) ||
(64 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_FLUSH_TO_ZERO_FP64);
}
static inline bool
nir_is_denorm_preserve(unsigned execution_mode, unsigned bit_size)
{
return (16 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP16) ||
(32 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP32) ||
(64 == bit_size && execution_mode & FLOAT_CONTROLS_DENORM_PRESERVE_FP64);
}
static inline bool
nir_is_rounding_mode_rtne(unsigned execution_mode, unsigned bit_size)
{
return (16 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16) ||
(32 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32) ||
(64 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
}
static inline bool
nir_is_rounding_mode_rtz(unsigned execution_mode, unsigned bit_size)
{
return (16 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16) ||
(32 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32) ||
(64 == bit_size && execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64);
}
static inline bool
nir_has_any_rounding_mode_rtz(unsigned execution_mode)
{
return (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP16) ||
(execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP32) ||
(execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTZ_FP64);
}
static inline bool
nir_has_any_rounding_mode_rtne(unsigned execution_mode)
{
return (execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP16) ||
(execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP32) ||
(execution_mode & FLOAT_CONTROLS_ROUNDING_MODE_RTE_FP64);
}
static inline nir_rounding_mode
nir_get_rounding_mode_from_float_controls(unsigned execution_mode,
nir_alu_type type)
{
if (nir_alu_type_get_base_type(type) != nir_type_float)
return nir_rounding_mode_undef;
unsigned bit_size = nir_alu_type_get_type_size(type);
if (nir_is_rounding_mode_rtz(execution_mode, bit_size))
return nir_rounding_mode_rtz;
if (nir_is_rounding_mode_rtne(execution_mode, bit_size))
return nir_rounding_mode_rtne;
return nir_rounding_mode_undef;
}
static inline bool
nir_has_any_rounding_mode_enabled(unsigned execution_mode)
{
bool result =
nir_has_any_rounding_mode_rtne(execution_mode) ||
nir_has_any_rounding_mode_rtz(execution_mode);
return result;
}
typedef enum {
NIR_OP_IS_2SRC_COMMUTATIVE = (1 << 0),
NIR_OP_IS_ASSOCIATIVE = (1 << 1),
} nir_op_algebraic_property;
#define NIR_ALU_MAX_INPUTS NIR_MAX_VEC_COMPONENTS
typedef struct nir_op_info {
const char *name;
uint8_t num_inputs;
uint8_t output_size;
nir_alu_type output_type;
uint8_t input_sizes[NIR_ALU_MAX_INPUTS];
nir_alu_type input_types[NIR_ALU_MAX_INPUTS];
nir_op_algebraic_property algebraic_properties;
bool is_conversion;
} nir_op_info;
extern const nir_op_info nir_op_infos[nir_num_opcodes];
typedef struct nir_alu_instr {
nir_instr instr;
nir_op op;
bool exact:1;
bool no_signed_wrap:1;
bool no_unsigned_wrap:1;
nir_alu_dest dest;
nir_alu_src src[];
} nir_alu_instr;
void nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
nir_alu_instr *instr);
void nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
nir_alu_instr *instr);
bool nir_alu_instr_is_copy(nir_alu_instr *instr);
static inline bool
nir_alu_instr_channel_used(const nir_alu_instr *instr, unsigned src,
unsigned channel)
{
if (nir_op_infos[instr->op].input_sizes[src] > 0)
return channel < nir_op_infos[instr->op].input_sizes[src];
return (instr->dest.write_mask >> channel) & 1;
}
static inline nir_component_mask_t
nir_alu_instr_src_read_mask(const nir_alu_instr *instr, unsigned src)
{
nir_component_mask_t read_mask = 0;
for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; c++) {
if (!nir_alu_instr_channel_used(instr, src, c))
continue;
read_mask |= (1 << instr->src[src].swizzle[c]);
}
return read_mask;
}
static inline unsigned
nir_ssa_alu_instr_src_components(const nir_alu_instr *instr, unsigned src)
{
if (nir_op_infos[instr->op].input_sizes[src] > 0)
return nir_op_infos[instr->op].input_sizes[src];
return nir_dest_num_components(instr->dest.dest);
}
static inline bool
nir_alu_instr_is_comparison(const nir_alu_instr *instr)
{
switch (instr->op) {
case nir_op_flt:
case nir_op_fge:
case nir_op_feq:
case nir_op_fneu:
case nir_op_ilt:
case nir_op_ult:
case nir_op_ige:
case nir_op_uge:
case nir_op_ieq:
case nir_op_ine:
case nir_op_i2b1:
case nir_op_f2b1:
case nir_op_inot:
return true;
default:
return false;
}
}
bool nir_const_value_negative_equal(nir_const_value c1, nir_const_value c2,
nir_alu_type full_type);
bool nir_alu_srcs_equal(const nir_alu_instr *alu1, const nir_alu_instr *alu2,
unsigned src1, unsigned src2);
bool nir_alu_srcs_negative_equal(const nir_alu_instr *alu1,
const nir_alu_instr *alu2,
unsigned src1, unsigned src2);
bool nir_alu_src_is_trivial_ssa(const nir_alu_instr *alu, unsigned srcn);
typedef enum {
nir_deref_type_var,
nir_deref_type_array,
nir_deref_type_array_wildcard,
nir_deref_type_ptr_as_array,
nir_deref_type_struct,
nir_deref_type_cast,
} nir_deref_type;
typedef struct {
nir_instr instr;
nir_deref_type deref_type;
nir_variable_mode modes;
const struct glsl_type *type;
union {
nir_variable *var;
nir_src parent;
};
union {
struct {
nir_src index;
} arr;
struct {
unsigned index;
} strct;
struct {
unsigned ptr_stride;
unsigned align_mul;
unsigned align_offset;
} cast;
};
nir_dest dest;
} nir_deref_instr;
static inline bool
nir_deref_mode_may_be(const nir_deref_instr *deref, nir_variable_mode modes)
{
assert(!(modes & ~nir_var_all));
assert(deref->modes != 0);
return deref->modes & modes;
}
static inline bool
nir_deref_mode_must_be(const nir_deref_instr *deref, nir_variable_mode modes)
{
assert(!(modes & ~nir_var_all));
assert(deref->modes != 0);
return !(deref->modes & ~modes);
}
static inline bool
nir_deref_mode_is(const nir_deref_instr *deref, nir_variable_mode mode)
{
assert(util_bitcount(mode) == 1 && (mode & nir_var_all));
assert(deref->modes != 0);
if (nir_deref_mode_may_be(deref, mode)) {
assert(util_bitcount(deref->modes) == 1);
assert(deref->modes == mode);
}
return deref->modes == mode;
}
static inline bool
nir_deref_mode_is_one_of(const nir_deref_instr *deref, nir_variable_mode modes)
{
if (nir_deref_mode_may_be(deref, modes)) {
assert(util_bitcount(deref->modes) == 1);
assert(nir_deref_mode_must_be(deref, modes));
}
return nir_deref_mode_may_be(deref, modes);
}
static inline bool
nir_deref_mode_is_in_set(const nir_deref_instr *deref, nir_variable_mode modes)
{
if (nir_deref_mode_may_be(deref, modes))
assert(nir_deref_mode_must_be(deref, modes));
return nir_deref_mode_may_be(deref, modes);
}
static inline nir_deref_instr *nir_src_as_deref(nir_src src);
static inline nir_deref_instr *
nir_deref_instr_parent(const nir_deref_instr *instr)
{
if (instr->deref_type == nir_deref_type_var)
return NULL;
else
return nir_src_as_deref(instr->parent);
}
static inline nir_variable *
nir_deref_instr_get_variable(const nir_deref_instr *instr)
{
while (instr->deref_type != nir_deref_type_var) {
if (instr->deref_type == nir_deref_type_cast)
return NULL;
instr = nir_deref_instr_parent(instr);
}
return instr->var;
}
bool nir_deref_instr_has_indirect(nir_deref_instr *instr);
bool nir_deref_instr_is_known_out_of_bounds(nir_deref_instr *instr);
bool nir_deref_instr_has_complex_use(nir_deref_instr *instr);
bool nir_deref_instr_remove_if_unused(nir_deref_instr *instr);
unsigned nir_deref_instr_array_stride(nir_deref_instr *instr);
typedef struct {
nir_instr instr;
struct nir_function *callee;
unsigned num_params;
nir_src params[];
} nir_call_instr;
#include "nir_intrinsics.h"
#define NIR_INTRINSIC_MAX_CONST_INDEX 5
typedef struct {
nir_instr instr;
nir_intrinsic_op intrinsic;
nir_dest dest;
uint8_t num_components;
int const_index[NIR_INTRINSIC_MAX_CONST_INDEX];
nir_src src[];
} nir_intrinsic_instr;
static inline nir_variable *
nir_intrinsic_get_var(nir_intrinsic_instr *intrin, unsigned i)
{
return nir_deref_instr_get_variable(nir_src_as_deref(intrin->src[i]));
}
typedef enum {
NIR_MEMORY_ACQUIRE = 1 << 0,
NIR_MEMORY_RELEASE = 1 << 1,
NIR_MEMORY_ACQ_REL = NIR_MEMORY_ACQUIRE | NIR_MEMORY_RELEASE,
NIR_MEMORY_MAKE_AVAILABLE = 1 << 2,
NIR_MEMORY_MAKE_VISIBLE = 1 << 3,
} nir_memory_semantics;
typedef enum {
NIR_SCOPE_NONE,
NIR_SCOPE_INVOCATION,
NIR_SCOPE_SUBGROUP,
NIR_SCOPE_SHADER_CALL,
NIR_SCOPE_WORKGROUP,
NIR_SCOPE_QUEUE_FAMILY,
NIR_SCOPE_DEVICE,
} nir_scope;
typedef enum {
NIR_INTRINSIC_CAN_ELIMINATE = (1 << 0),
NIR_INTRINSIC_CAN_REORDER = (1 << 1),
} nir_intrinsic_semantic_flag;
#define NIR_ALIGN_MUL_MAX 0x40000000
typedef struct nir_io_semantics {
unsigned location:7;
unsigned num_slots:6;
unsigned dual_source_blend_index:1;
unsigned fb_fetch_output:1;
unsigned gs_streams:8;
unsigned medium_precision:1;
unsigned per_view:1;
unsigned high_16bits:1;
unsigned _pad:6;
} nir_io_semantics;
#define NIR_INTRINSIC_MAX_INPUTS 11
typedef struct {
const char *name;
uint8_t num_srcs;
int8_t src_components[NIR_INTRINSIC_MAX_INPUTS];
bool has_dest;
uint8_t dest_components;
uint8_t dest_bit_sizes;
int8_t bit_size_src;
uint8_t num_indices;
uint8_t indices[NIR_INTRINSIC_MAX_CONST_INDEX];
uint8_t index_map[NIR_INTRINSIC_NUM_INDEX_FLAGS];
nir_intrinsic_semantic_flag flags;
} nir_intrinsic_info;
extern const nir_intrinsic_info nir_intrinsic_infos[nir_num_intrinsics];
static inline unsigned
nir_intrinsic_src_components(const nir_intrinsic_instr *intr, unsigned srcn)
{
const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
assert(srcn < info->num_srcs);
if (info->src_components[srcn] > 0)
return info->src_components[srcn];
else if (info->src_components[srcn] == 0)
return intr->num_components;
else
return nir_src_num_components(intr->src[srcn]);
}
static inline unsigned
nir_intrinsic_dest_components(nir_intrinsic_instr *intr)
{
const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
if (!info->has_dest)
return 0;
else if (info->dest_components)
return info->dest_components;
else
return intr->num_components;
}
static inline void
nir_intrinsic_copy_const_indices(nir_intrinsic_instr *dst, nir_intrinsic_instr *src)
{
if (src->intrinsic == dst->intrinsic) {
memcpy(dst->const_index, src->const_index, sizeof(dst->const_index));
return;
}
const nir_intrinsic_info *src_info = &nir_intrinsic_infos[src->intrinsic];
const nir_intrinsic_info *dst_info = &nir_intrinsic_infos[dst->intrinsic];
for (unsigned i = 0; i < NIR_INTRINSIC_NUM_INDEX_FLAGS; i++) {
if (src_info->index_map[i] == 0)
continue;
assert(dst_info->index_map[i] > 0);
dst->const_index[dst_info->index_map[i] - 1] =
src->const_index[src_info->index_map[i] - 1];
}
}
#include "nir_intrinsics_indices.h"
static inline void
nir_intrinsic_set_align(nir_intrinsic_instr *intrin,
unsigned align_mul, unsigned align_offset)
{
assert(util_is_power_of_two_nonzero(align_mul));
assert(align_offset < align_mul);
nir_intrinsic_set_align_mul(intrin, align_mul);
nir_intrinsic_set_align_offset(intrin, align_offset);
}
static inline unsigned
nir_intrinsic_align(const nir_intrinsic_instr *intrin)
{
const unsigned align_mul = nir_intrinsic_align_mul(intrin);
const unsigned align_offset = nir_intrinsic_align_offset(intrin);
assert(align_offset < align_mul);
return align_offset ? 1 << (ffs(align_offset) - 1) : align_mul;
}
static inline bool
nir_intrinsic_has_align(const nir_intrinsic_instr *intrin)
{
return nir_intrinsic_has_align_mul(intrin) &&
nir_intrinsic_has_align_offset(intrin);
}
unsigned
nir_image_intrinsic_coord_components(const nir_intrinsic_instr *instr);
void nir_rewrite_image_intrinsic(nir_intrinsic_instr *instr,
nir_ssa_def *handle, bool bindless);
static inline bool
nir_intrinsic_can_reorder(nir_intrinsic_instr *instr)
{
if (instr->intrinsic == nir_intrinsic_load_deref) {
nir_deref_instr *deref = nir_src_as_deref(instr->src[0]);
return nir_deref_mode_is_in_set(deref, nir_var_read_only_modes) ||
(nir_intrinsic_access(instr) & ACCESS_CAN_REORDER);
} else if (instr->intrinsic == nir_intrinsic_load_ssbo ||
instr->intrinsic == nir_intrinsic_bindless_image_load ||
instr->intrinsic == nir_intrinsic_image_deref_load ||
instr->intrinsic == nir_intrinsic_image_load) {
return nir_intrinsic_access(instr) & ACCESS_CAN_REORDER;
} else {
const nir_intrinsic_info *info =
&nir_intrinsic_infos[instr->intrinsic];
return (info->flags & NIR_INTRINSIC_CAN_ELIMINATE) &&
(info->flags & NIR_INTRINSIC_CAN_REORDER);
}
}
bool nir_intrinsic_writes_external_memory(const nir_intrinsic_instr *instr);
typedef enum {
nir_tex_src_coord,
nir_tex_src_projector,
nir_tex_src_comparator,
nir_tex_src_offset,
nir_tex_src_bias,
nir_tex_src_lod,
nir_tex_src_min_lod,
nir_tex_src_ms_index,
nir_tex_src_ms_mcs,
nir_tex_src_ddx,
nir_tex_src_ddy,
nir_tex_src_texture_deref,
nir_tex_src_sampler_deref,
nir_tex_src_texture_offset,
nir_tex_src_sampler_offset,
nir_tex_src_texture_handle,
nir_tex_src_sampler_handle,
nir_tex_src_plane,
nir_num_tex_src_types
} nir_tex_src_type;
typedef struct {
nir_src src;
nir_tex_src_type src_type;
} nir_tex_src;
typedef enum {
nir_texop_tex,
nir_texop_txb,
nir_texop_txl,
nir_texop_txd,
nir_texop_txf,
nir_texop_txf_ms,
nir_texop_txf_ms_fb,
nir_texop_txf_ms_mcs,
nir_texop_txs,
nir_texop_lod,
nir_texop_tg4,
nir_texop_query_levels,
nir_texop_texture_samples,
nir_texop_samples_identical,
nir_texop_tex_prefetch,
nir_texop_fragment_fetch,
nir_texop_fragment_mask_fetch,
} nir_texop;
typedef struct {
nir_instr instr;
enum glsl_sampler_dim sampler_dim;
nir_alu_type dest_type;
nir_texop op;
nir_dest dest;
nir_tex_src *src;
unsigned num_srcs, coord_components;
bool is_array, is_shadow;
bool is_new_style_shadow;
bool is_sparse;
unsigned component : 2;
unsigned array_is_lowered_cube : 1;
int8_t tg4_offsets[4][2];
bool texture_non_uniform;
bool sampler_non_uniform;
unsigned texture_index;
unsigned sampler_index;
} nir_tex_instr;
static inline bool
nir_tex_instr_need_sampler(const nir_tex_instr *instr)
{
switch (instr->op) {
case nir_texop_txf:
case nir_texop_txf_ms:
case nir_texop_txs:
case nir_texop_query_levels:
case nir_texop_texture_samples:
case nir_texop_samples_identical:
return false;
default:
return true;
}
}
static inline unsigned
nir_tex_instr_result_size(const nir_tex_instr *instr)
{
switch (instr->op) {
case nir_texop_txs: {
unsigned ret;
switch (instr->sampler_dim) {
case GLSL_SAMPLER_DIM_1D:
case GLSL_SAMPLER_DIM_BUF:
ret = 1;
break;
case GLSL_SAMPLER_DIM_2D:
case GLSL_SAMPLER_DIM_CUBE:
case GLSL_SAMPLER_DIM_MS:
case GLSL_SAMPLER_DIM_RECT:
case GLSL_SAMPLER_DIM_EXTERNAL:
case GLSL_SAMPLER_DIM_SUBPASS:
ret = 2;
break;
case GLSL_SAMPLER_DIM_3D:
ret = 3;
break;
default:
unreachable("not reached");
}
if (instr->is_array)
ret++;
return ret;
}
case nir_texop_lod:
return 2;
case nir_texop_texture_samples:
case nir_texop_query_levels:
case nir_texop_samples_identical:
case nir_texop_fragment_mask_fetch:
return 1;
default:
if (instr->is_shadow && instr->is_new_style_shadow)
return 1;
return 4;
}
}
static inline unsigned
nir_tex_instr_dest_size(const nir_tex_instr *instr)
{
return nir_tex_instr_result_size(instr) + instr->is_sparse;
}
static inline bool
nir_tex_instr_is_query(const nir_tex_instr *instr)
{
switch (instr->op) {
case nir_texop_txs:
case nir_texop_lod:
case nir_texop_texture_samples:
case nir_texop_query_levels:
return true;
case nir_texop_tex:
case nir_texop_txb:
case nir_texop_txl:
case nir_texop_txd:
case nir_texop_txf:
case nir_texop_txf_ms:
case nir_texop_txf_ms_fb:
case nir_texop_txf_ms_mcs:
case nir_texop_tg4:
return false;
default:
unreachable("Invalid texture opcode");
}
}
static inline bool
nir_tex_instr_has_implicit_derivative(const nir_tex_instr *instr)
{
switch (instr->op) {
case nir_texop_tex:
case nir_texop_txb:
case nir_texop_lod:
return true;
default:
return false;
}
}
static inline nir_alu_type
nir_tex_instr_src_type(const nir_tex_instr *instr, unsigned src)
{
switch (instr->src[src].src_type) {
case nir_tex_src_coord:
switch (instr->op) {
case nir_texop_txf:
case nir_texop_txf_ms:
case nir_texop_txf_ms_fb:
case nir_texop_txf_ms_mcs:
case nir_texop_samples_identical:
return nir_type_int;
default:
return nir_type_float;
}
case nir_tex_src_lod:
switch (instr->op) {
case nir_texop_txs:
case nir_texop_txf:
case nir_texop_txf_ms:
return nir_type_int;
default:
return nir_type_float;
}
case nir_tex_src_projector:
case nir_tex_src_comparator:
case nir_tex_src_bias:
case nir_tex_src_min_lod:
case nir_tex_src_ddx:
case nir_tex_src_ddy:
return nir_type_float;
case nir_tex_src_offset:
case nir_tex_src_ms_index:
case nir_tex_src_plane:
return nir_type_int;
case nir_tex_src_ms_mcs:
case nir_tex_src_texture_deref:
case nir_tex_src_sampler_deref:
case nir_tex_src_texture_offset:
case nir_tex_src_sampler_offset:
case nir_tex_src_texture_handle:
case nir_tex_src_sampler_handle:
return nir_type_uint;
case nir_num_tex_src_types:
unreachable("nir_num_tex_src_types is not a valid source type");
}
unreachable("Invalid texture source type");
}
static inline unsigned
nir_tex_instr_src_size(const nir_tex_instr *instr, unsigned src)
{
if (instr->src[src].src_type == nir_tex_src_coord)
return instr->coord_components;
if (instr->src[src].src_type == nir_tex_src_ms_mcs)
return 4;
if (instr->src[src].src_type == nir_tex_src_ddx ||
instr->src[src].src_type == nir_tex_src_ddy) {
if (instr->is_array && !instr->array_is_lowered_cube)
return instr->coord_components - 1;
else
return instr->coord_components;
}
if (instr->src[src].src_type == nir_tex_src_offset) {
if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE)
return 2;
else if (instr->is_array)
return instr->coord_components - 1;
else
return instr->coord_components;
}
return 1;
}
static inline int
nir_tex_instr_src_index(const nir_tex_instr *instr, nir_tex_src_type type)
{
for (unsigned i = 0; i < instr->num_srcs; i++)
if (instr->src[i].src_type == type)
return (int) i;
return -1;
}
void nir_tex_instr_add_src(nir_tex_instr *tex,
nir_tex_src_type src_type,
nir_src src);
void nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx);
bool nir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr *tex);
typedef struct {
nir_instr instr;
nir_ssa_def def;
nir_const_value value[];
} nir_load_const_instr;
typedef enum {
nir_jump_return,
nir_jump_halt,
nir_jump_break,
nir_jump_continue,
nir_jump_goto,
nir_jump_goto_if,
} nir_jump_type;
typedef struct {
nir_instr instr;
nir_jump_type type;
nir_src condition;
struct nir_block *target;
struct nir_block *else_target;
} nir_jump_instr;
typedef struct {
nir_instr instr;
nir_ssa_def def;
} nir_ssa_undef_instr;
typedef struct {
struct exec_node node;
struct nir_block *pred;
nir_src src;
} nir_phi_src;
#define nir_foreach_phi_src(phi_src, phi) \
foreach_list_typed(nir_phi_src, phi_src, node, &(phi)->srcs)
#define nir_foreach_phi_src_safe(phi_src, phi) \
foreach_list_typed_safe(nir_phi_src, phi_src, node, &(phi)->srcs)
typedef struct {
nir_instr instr;
struct exec_list srcs;
nir_dest dest;
} nir_phi_instr;
static inline nir_phi_src *
nir_phi_get_src_from_block(nir_phi_instr *phi, struct nir_block *block)
{
nir_foreach_phi_src(src, phi) {
if (src->pred == block)
return src;
}
assert(!"Block is not a predecessor of phi.");
return NULL;
}
typedef struct {
struct exec_node node;
nir_src src;
nir_dest dest;
} nir_parallel_copy_entry;
#define nir_foreach_parallel_copy_entry(entry, pcopy) \
foreach_list_typed(nir_parallel_copy_entry, entry, node, &(pcopy)->entries)
typedef struct {
nir_instr instr;
struct exec_list entries;
} nir_parallel_copy_instr;
NIR_DEFINE_CAST(nir_instr_as_alu, nir_instr, nir_alu_instr, instr,
type, nir_instr_type_alu)
NIR_DEFINE_CAST(nir_instr_as_deref, nir_instr, nir_deref_instr, instr,
type, nir_instr_type_deref)
NIR_DEFINE_CAST(nir_instr_as_call, nir_instr, nir_call_instr, instr,
type, nir_instr_type_call)
NIR_DEFINE_CAST(nir_instr_as_jump, nir_instr, nir_jump_instr, instr,
type, nir_instr_type_jump)
NIR_DEFINE_CAST(nir_instr_as_tex, nir_instr, nir_tex_instr, instr,
type, nir_instr_type_tex)
NIR_DEFINE_CAST(nir_instr_as_intrinsic, nir_instr, nir_intrinsic_instr, instr,
type, nir_instr_type_intrinsic)
NIR_DEFINE_CAST(nir_instr_as_load_const, nir_instr, nir_load_const_instr, instr,
type, nir_instr_type_load_const)
NIR_DEFINE_CAST(nir_instr_as_ssa_undef, nir_instr, nir_ssa_undef_instr, instr,
type, nir_instr_type_ssa_undef)
NIR_DEFINE_CAST(nir_instr_as_phi, nir_instr, nir_phi_instr, instr,
type, nir_instr_type_phi)
NIR_DEFINE_CAST(nir_instr_as_parallel_copy, nir_instr,
nir_parallel_copy_instr, instr,
type, nir_instr_type_parallel_copy)
#define NIR_DEFINE_SRC_AS_CONST(type, suffix) \
static inline type \
nir_src_comp_as_##suffix(nir_src src, unsigned comp) \
{ \
assert(nir_src_is_const(src)); \
nir_load_const_instr *load = \
nir_instr_as_load_const(src.ssa->parent_instr); \
assert(comp < load->def.num_components); \
return nir_const_value_as_##suffix(load->value[comp], \
load->def.bit_size); \
} \
\
static inline type \
nir_src_as_##suffix(nir_src src) \
{ \
assert(nir_src_num_components(src) == 1); \
return nir_src_comp_as_##suffix(src, 0); \
}
NIR_DEFINE_SRC_AS_CONST(int64_t, int)
NIR_DEFINE_SRC_AS_CONST(uint64_t, uint)
NIR_DEFINE_SRC_AS_CONST(bool, bool)
NIR_DEFINE_SRC_AS_CONST(double, float)
#undef NIR_DEFINE_SRC_AS_CONST
typedef struct {
nir_ssa_def *def;
unsigned comp;
} nir_ssa_scalar;
static inline bool
nir_ssa_scalar_is_const(nir_ssa_scalar s)
{
return s.def->parent_instr->type == nir_instr_type_load_const;
}
static inline nir_const_value
nir_ssa_scalar_as_const_value(nir_ssa_scalar s)
{
assert(s.comp < s.def->num_components);
nir_load_const_instr *load = nir_instr_as_load_const(s.def->parent_instr);
return load->value[s.comp];
}
#define NIR_DEFINE_SCALAR_AS_CONST(type, suffix) \
static inline type \
nir_ssa_scalar_as_##suffix(nir_ssa_scalar s) \
{ \
return nir_const_value_as_##suffix( \
nir_ssa_scalar_as_const_value(s), s.def->bit_size); \
}
NIR_DEFINE_SCALAR_AS_CONST(int64_t, int)
NIR_DEFINE_SCALAR_AS_CONST(uint64_t, uint)
NIR_DEFINE_SCALAR_AS_CONST(bool, bool)
NIR_DEFINE_SCALAR_AS_CONST(double, float)
#undef NIR_DEFINE_SCALAR_AS_CONST
static inline bool
nir_ssa_scalar_is_alu(nir_ssa_scalar s)
{
return s.def->parent_instr->type == nir_instr_type_alu;
}
static inline nir_op
nir_ssa_scalar_alu_op(nir_ssa_scalar s)
{
return nir_instr_as_alu(s.def->parent_instr)->op;
}
static inline nir_ssa_scalar
nir_ssa_scalar_chase_alu_src(nir_ssa_scalar s, unsigned alu_src_idx)
{
nir_ssa_scalar out = { NULL, 0 };
nir_alu_instr *alu = nir_instr_as_alu(s.def->parent_instr);
assert(alu_src_idx < nir_op_infos[alu->op].num_inputs);
assert(s.comp < s.def->num_components);
assert(alu->dest.write_mask & (1u << s.comp));
assert(alu->src[alu_src_idx].src.is_ssa);
out.def = alu->src[alu_src_idx].src.ssa;
if (nir_op_infos[alu->op].input_sizes[alu_src_idx] == 0) {
out.comp = alu->src[alu_src_idx].swizzle[s.comp];
} else {
assert(nir_op_infos[alu->op].input_sizes[alu_src_idx] == 1);
out.comp = alu->src[alu_src_idx].swizzle[0];
}
assert(out.comp < out.def->num_components);
return out;
}
nir_ssa_scalar nir_ssa_scalar_chase_movs(nir_ssa_scalar s);
static inline nir_ssa_scalar
nir_ssa_scalar_resolved(nir_ssa_def *def, unsigned channel)
{
nir_ssa_scalar s = { def, channel };
return nir_ssa_scalar_chase_movs(s);
}
typedef struct {
bool success;
nir_variable *var;
unsigned desc_set;
unsigned binding;
unsigned num_indices;
nir_src indices[4];
bool read_first_invocation;
} nir_binding;
nir_binding nir_chase_binding(nir_src rsrc);
nir_variable *nir_get_binding_variable(struct nir_shader *shader, nir_binding binding);
typedef enum {
nir_cf_node_block,
nir_cf_node_if,
nir_cf_node_loop,
nir_cf_node_function
} nir_cf_node_type;
typedef struct nir_cf_node {
struct exec_node node;
nir_cf_node_type type;
struct nir_cf_node *parent;
} nir_cf_node;
typedef struct nir_block {
nir_cf_node cf_node;
struct exec_list instr_list;
unsigned index;
struct nir_block *successors[2];
struct set *predecessors;
struct nir_block *imm_dom;
unsigned num_dom_children;
struct nir_block **dom_children;
struct set *dom_frontier;
uint32_t dom_pre_index, dom_post_index;
uint32_t start_ip;
uint32_t end_ip;
BITSET_WORD *live_in;
BITSET_WORD *live_out;
} nir_block;
static inline bool
nir_block_is_reachable(nir_block *b)
{
return b->dom_post_index != 0;
}
static inline nir_instr *
nir_block_first_instr(nir_block *block)
{
struct exec_node *head = exec_list_get_head(&block->instr_list);
return exec_node_data(nir_instr, head, node);
}
static inline nir_instr *
nir_block_last_instr(nir_block *block)
{
struct exec_node *tail = exec_list_get_tail(&block->instr_list);
return exec_node_data(nir_instr, tail, node);
}
static inline bool
nir_block_ends_in_jump(nir_block *block)
{
return !exec_list_is_empty(&block->instr_list) &&
nir_block_last_instr(block)->type == nir_instr_type_jump;
}
#define nir_foreach_instr(instr, block) \
foreach_list_typed(nir_instr, instr, node, &(block)->instr_list)
#define nir_foreach_instr_reverse(instr, block) \
foreach_list_typed_reverse(nir_instr, instr, node, &(block)->instr_list)
#define nir_foreach_instr_safe(instr, block) \
foreach_list_typed_safe(nir_instr, instr, node, &(block)->instr_list)
#define nir_foreach_instr_reverse_safe(instr, block) \
foreach_list_typed_reverse_safe(nir_instr, instr, node, &(block)->instr_list)
static inline nir_phi_instr *
nir_block_last_phi_instr(nir_block *block)
{
nir_phi_instr *last_phi = NULL;
nir_foreach_instr(instr, block) {
if (instr->type == nir_instr_type_phi)
last_phi = nir_instr_as_phi(instr);
else
return last_phi;
}
return last_phi;
}
typedef enum {
nir_selection_control_none = 0x0,
nir_selection_control_flatten = 0x1,
nir_selection_control_dont_flatten = 0x2,
} nir_selection_control;
typedef struct nir_if {
nir_cf_node cf_node;
nir_src condition;
nir_selection_control control;
struct exec_list then_list;
struct exec_list else_list;
} nir_if;
typedef struct {
nir_if *nif;
nir_instr *conditional_instr;
nir_block *break_block;
nir_block *continue_from_block;
bool continue_from_then;
bool induction_rhs;
bool exact_trip_count_unknown;
struct list_head loop_terminator_link;
} nir_loop_terminator;
typedef struct {
unsigned instr_cost;
unsigned guessed_trip_count;
unsigned max_trip_count;
bool exact_trip_count_known;
bool force_unroll;
bool complex_loop;
nir_loop_terminator *limiting_terminator;
struct list_head loop_terminator_list;
} nir_loop_info;
typedef enum {
nir_loop_control_none = 0x0,
nir_loop_control_unroll = 0x1,
nir_loop_control_dont_unroll = 0x2,
} nir_loop_control;
typedef struct {
nir_cf_node cf_node;
struct exec_list body;
nir_loop_info *info;
nir_loop_control control;
bool partially_unrolled;
bool divergent;
} nir_loop;
typedef enum {
nir_metadata_none = 0x0,
nir_metadata_block_index = 0x1,
nir_metadata_dominance = 0x2,
nir_metadata_live_ssa_defs = 0x4,
nir_metadata_not_properly_reset = 0x8,
nir_metadata_loop_analysis = 0x10,
nir_metadata_instr_index = 0x20,
nir_metadata_all = ~nir_metadata_not_properly_reset,
} nir_metadata;
MESA_DEFINE_CPP_ENUM_BITFIELD_OPERATORS(nir_metadata)
typedef struct {
nir_cf_node cf_node;
struct nir_function *function;
struct exec_list body;
nir_block *end_block;
struct exec_list locals;
struct exec_list registers;
unsigned reg_alloc;
unsigned ssa_alloc;
unsigned num_blocks;
bool structured;
nir_metadata valid_metadata;
} nir_function_impl;
#define nir_foreach_function_temp_variable(var, impl) \
foreach_list_typed(nir_variable, var, node, &(impl)->locals)
#define nir_foreach_function_temp_variable_safe(var, impl) \
foreach_list_typed_safe(nir_variable, var, node, &(impl)->locals)
ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
nir_start_block(nir_function_impl *impl)
{
return (nir_block *) impl->body.head_sentinel.next;
}
ATTRIBUTE_RETURNS_NONNULL static inline nir_block *
nir_impl_last_block(nir_function_impl *impl)
{
return (nir_block *) impl->body.tail_sentinel.prev;
}
static inline nir_cf_node *
nir_cf_node_next(nir_cf_node *node)
{
struct exec_node *next = exec_node_get_next(&node->node);
if (exec_node_is_tail_sentinel(next))
return NULL;
else
return exec_node_data(nir_cf_node, next, node);
}
static inline nir_cf_node *
nir_cf_node_prev(nir_cf_node *node)
{
struct exec_node *prev = exec_node_get_prev(&node->node);
if (exec_node_is_head_sentinel(prev))
return NULL;
else
return exec_node_data(nir_cf_node, prev, node);
}
static inline bool
nir_cf_node_is_first(const nir_cf_node *node)
{
return exec_node_is_head_sentinel(node->node.prev);
}
static inline bool
nir_cf_node_is_last(const nir_cf_node *node)
{
return exec_node_is_tail_sentinel(node->node.next);
}
NIR_DEFINE_CAST(nir_cf_node_as_block, nir_cf_node, nir_block, cf_node,
type, nir_cf_node_block)
NIR_DEFINE_CAST(nir_cf_node_as_if, nir_cf_node, nir_if, cf_node,
type, nir_cf_node_if)
NIR_DEFINE_CAST(nir_cf_node_as_loop, nir_cf_node, nir_loop, cf_node,
type, nir_cf_node_loop)
NIR_DEFINE_CAST(nir_cf_node_as_function, nir_cf_node,
nir_function_impl, cf_node, type, nir_cf_node_function)
static inline nir_block *
nir_if_first_then_block(nir_if *if_stmt)
{
struct exec_node *head = exec_list_get_head(&if_stmt->then_list);
return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
}
static inline nir_block *
nir_if_last_then_block(nir_if *if_stmt)
{
struct exec_node *tail = exec_list_get_tail(&if_stmt->then_list);
return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
}
static inline nir_block *
nir_if_first_else_block(nir_if *if_stmt)
{
struct exec_node *head = exec_list_get_head(&if_stmt->else_list);
return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
}
static inline nir_block *
nir_if_last_else_block(nir_if *if_stmt)
{
struct exec_node *tail = exec_list_get_tail(&if_stmt->else_list);
return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
}
static inline nir_block *
nir_loop_first_block(nir_loop *loop)
{
struct exec_node *head = exec_list_get_head(&loop->body);
return nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
}
static inline nir_block *
nir_loop_last_block(nir_loop *loop)
{
struct exec_node *tail = exec_list_get_tail(&loop->body);
return nir_cf_node_as_block(exec_node_data(nir_cf_node, tail, node));
}
static inline bool
nir_cf_list_is_empty_block(struct exec_list *cf_list)
{
if (exec_list_is_singular(cf_list)) {
struct exec_node *head = exec_list_get_head(cf_list);
nir_block *block =
nir_cf_node_as_block(exec_node_data(nir_cf_node, head, node));
return exec_list_is_empty(&block->instr_list);
}
return false;
}
typedef struct {
uint8_t num_components;
uint8_t bit_size;
} nir_parameter;
typedef struct nir_printf_info {
unsigned num_args;
unsigned *arg_sizes;
unsigned string_size;
char *strings;
} nir_printf_info;
typedef struct nir_function {
struct exec_node node;
const char *name;
struct nir_shader *shader;
unsigned num_params;
nir_parameter *params;
nir_function_impl *impl;
bool is_entrypoint;
} nir_function;
typedef enum {
nir_lower_imul64 = (1 << 0),
nir_lower_isign64 = (1 << 1),
nir_lower_divmod64 = (1 << 2),
nir_lower_imul_high64 = (1 << 3),
nir_lower_mov64 = (1 << 4),
nir_lower_icmp64 = (1 << 5),
nir_lower_iadd64 = (1 << 6),
nir_lower_iabs64 = (1 << 7),
nir_lower_ineg64 = (1 << 8),
nir_lower_logic64 = (1 << 9),
nir_lower_minmax64 = (1 << 10),
nir_lower_shift64 = (1 << 11),
nir_lower_imul_2x32_64 = (1 << 12),
nir_lower_extract64 = (1 << 13),
nir_lower_ufind_msb64 = (1 << 14),
nir_lower_bit_count64 = (1 << 15),
nir_lower_subgroup_shuffle64 = (1 << 16),
nir_lower_scan_reduce_bitwise64 = (1 << 17),
nir_lower_scan_reduce_iadd64 = (1 << 18),
nir_lower_vote_ieq64 = (1 << 19),
} nir_lower_int64_options;
typedef enum {
nir_lower_drcp = (1 << 0),
nir_lower_dsqrt = (1 << 1),
nir_lower_drsq = (1 << 2),
nir_lower_dtrunc = (1 << 3),
nir_lower_dfloor = (1 << 4),
nir_lower_dceil = (1 << 5),
nir_lower_dfract = (1 << 6),
nir_lower_dround_even = (1 << 7),
nir_lower_dmod = (1 << 8),
nir_lower_dsub = (1 << 9),
nir_lower_ddiv = (1 << 10),
nir_lower_fp64_full_software = (1 << 11),
} nir_lower_doubles_options;
typedef enum {
nir_divergence_single_prim_per_subgroup = (1 << 0),
nir_divergence_single_patch_per_tcs_subgroup = (1 << 1),
nir_divergence_single_patch_per_tes_subgroup = (1 << 2),
nir_divergence_view_index_uniform = (1 << 3),
nir_divergence_single_frag_shading_rate_per_subgroup = (1 << 4),
nir_divergence_multiple_workgroup_per_compute_subgroup = (1 << 5),
} nir_divergence_options;
typedef bool (*nir_instr_filter_cb)(const nir_instr *, const void *);
typedef struct nir_shader_compiler_options {
bool lower_fdiv;
bool lower_ffma16;
bool lower_ffma32;
bool lower_ffma64;
bool fuse_ffma16;
bool fuse_ffma32;
bool fuse_ffma64;
bool lower_flrp16;
bool lower_flrp32;
bool lower_flrp64;
bool lower_fpow;
bool lower_fsat;
bool lower_fsqrt;
bool lower_sincos;
bool lower_fmod;
bool lower_bitfield_extract;
bool lower_bitfield_extract_to_shifts;
bool lower_bitfield_insert;
bool lower_bitfield_insert_to_shifts;
bool lower_bitfield_insert_to_bitfield_select;
bool lower_bitfield_reverse;
bool lower_bit_count;
bool lower_ifind_msb;
bool lower_find_msb_to_reverse;
bool lower_find_lsb;
bool lower_uadd_carry;
bool lower_usub_borrow;
bool lower_mul_high;
bool lower_fneg;
bool lower_ineg;
bool lower_scmp;
bool lower_vector_cmp;
bool lower_bitops;
bool lower_isign;
bool lower_fsign;
bool lower_iabs;
bool lower_umax;
bool lower_umin;
bool lower_fdph;
bool lower_fdot;
bool fdot_replicates;
bool lower_ffloor;
bool lower_ffract;
bool lower_fceil;
bool lower_ftrunc;
bool lower_ldexp;
bool lower_pack_half_2x16;
bool lower_pack_unorm_2x16;
bool lower_pack_snorm_2x16;
bool lower_pack_unorm_4x8;
bool lower_pack_snorm_4x8;
bool lower_pack_64_2x32;
bool lower_pack_64_4x16;
bool lower_pack_32_2x16;
bool lower_pack_64_2x32_split;
bool lower_pack_32_2x16_split;
bool lower_unpack_half_2x16;
bool lower_unpack_unorm_2x16;
bool lower_unpack_snorm_2x16;
bool lower_unpack_unorm_4x8;
bool lower_unpack_snorm_4x8;
bool lower_unpack_64_2x32_split;
bool lower_unpack_32_2x16_split;
bool lower_pack_split;
bool lower_extract_byte;
bool lower_extract_word;
bool lower_insert_byte;
bool lower_insert_word;
bool lower_all_io_to_temps;
bool lower_all_io_to_elements;
bool vertex_id_zero_based;
bool lower_base_vertex;
bool lower_helper_invocation;
bool optimize_sample_mask_in;
bool lower_cs_local_index_from_id;
bool lower_cs_local_id_from_index;
bool has_cs_global_id;
bool lower_device_index_to_zero;
bool lower_wpos_pntc;
bool lower_hadd;
bool lower_hadd64;
bool lower_add_sat;
bool lower_usub_sat64;
bool vectorize_io;
bool lower_to_scalar;
nir_instr_filter_cb lower_to_scalar_filter;
bool vectorize_vec2_16bit;
bool unify_interfaces;
bool use_interpolated_input_intrinsics;
bool lower_interpolate_at;
bool lower_mul_2x32_64;
bool lower_rotate;
bool has_imul24;
bool has_umul24;
bool has_umad24;
bool has_fused_comp_and_csel;
bool has_fsub;
bool has_isub;
bool has_txs;
bool use_scoped_barrier;
bool intel_vec4;
bool lower_bfe_with_two_constants;
bool support_8bit_alu;
bool support_16bit_alu;
unsigned max_unroll_iterations;
unsigned max_unroll_iterations_aggressive;
bool lower_uniforms_to_ubo;
bool linker_ignore_precision;
nir_lower_int64_options lower_int64_options;
nir_lower_doubles_options lower_doubles_options;
nir_divergence_options divergence_analysis_options;
} nir_shader_compiler_options;
typedef struct nir_shader {
struct exec_list variables;
const struct nir_shader_compiler_options *options;
struct shader_info info;
struct exec_list functions;
unsigned num_inputs, num_uniforms, num_outputs;
unsigned scratch_size;
void *constant_data;
unsigned constant_data_size;
unsigned printf_info_count;
nir_printf_info *printf_info;
} nir_shader;
#define nir_foreach_function(func, shader) \
foreach_list_typed(nir_function, func, node, &(shader)->functions)
static inline nir_function_impl *
nir_shader_get_entrypoint(nir_shader *shader)
{
nir_function *func = NULL;
nir_foreach_function(function, shader) {
assert(func == NULL);
if (function->is_entrypoint) {
func = function;
#ifndef NDEBUG
break;
#endif
}
}
if (!func)
return NULL;
assert(func->num_params == 0);
assert(func->impl);
return func->impl;
}
typedef struct nir_liveness_bounds {
uint32_t start;
uint32_t end;
} nir_liveness_bounds;
typedef struct nir_instr_liveness {
struct nir_liveness_bounds *defs;
} nir_instr_liveness;
nir_instr_liveness *
nir_live_ssa_defs_per_instr(nir_function_impl *impl);
nir_shader *nir_shader_create(void *mem_ctx,
gl_shader_stage stage,
const nir_shader_compiler_options *options,
shader_info *si);
nir_register *nir_local_reg_create(nir_function_impl *impl);
void nir_reg_remove(nir_register *reg);
void nir_shader_add_variable(nir_shader *shader, nir_variable *var);
static inline void
nir_function_impl_add_variable(nir_function_impl *impl, nir_variable *var)
{
assert(var->data.mode == nir_var_function_temp);
exec_list_push_tail(&impl->locals, &var->node);
}
nir_variable *nir_variable_create(nir_shader *shader,
nir_variable_mode mode,
const struct glsl_type *type,
const char *name);
nir_variable *nir_local_variable_create(nir_function_impl *impl,
const struct glsl_type *type,
const char *name);
nir_variable *nir_find_variable_with_location(nir_shader *shader,
nir_variable_mode mode,
unsigned location);
nir_variable *nir_find_variable_with_driver_location(nir_shader *shader,
nir_variable_mode mode,
unsigned location);
void nir_sort_variables_with_modes(nir_shader *shader,
int (*compar)(const nir_variable *,
const nir_variable *),
nir_variable_mode modes);
nir_function *nir_function_create(nir_shader *shader, const char *name);
nir_function_impl *nir_function_impl_create(nir_function *func);
nir_function_impl *nir_function_impl_create_bare(nir_shader *shader);
nir_block *nir_block_create(nir_shader *shader);
nir_if *nir_if_create(nir_shader *shader);
nir_loop *nir_loop_create(nir_shader *shader);
nir_function_impl *nir_cf_node_get_function(nir_cf_node *node);
void nir_metadata_require(nir_function_impl *impl, nir_metadata required, ...);
void nir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved);
void nir_shader_preserve_all_metadata(nir_shader *shader);
nir_alu_instr *nir_alu_instr_create(nir_shader *shader, nir_op op);
nir_deref_instr *nir_deref_instr_create(nir_shader *shader,
nir_deref_type deref_type);
nir_jump_instr *nir_jump_instr_create(nir_shader *shader, nir_jump_type type);
nir_load_const_instr *nir_load_const_instr_create(nir_shader *shader,
unsigned num_components,
unsigned bit_size);
nir_intrinsic_instr *nir_intrinsic_instr_create(nir_shader *shader,
nir_intrinsic_op op);
nir_call_instr *nir_call_instr_create(nir_shader *shader,
nir_function *callee);
nir_tex_instr *nir_tex_instr_create(nir_shader *shader, unsigned num_srcs);
nir_phi_instr *nir_phi_instr_create(nir_shader *shader);
nir_parallel_copy_instr *nir_parallel_copy_instr_create(nir_shader *shader);
nir_ssa_undef_instr *nir_ssa_undef_instr_create(nir_shader *shader,
unsigned num_components,
unsigned bit_size);
nir_const_value nir_alu_binop_identity(nir_op binop, unsigned bit_size);
typedef enum {
nir_cursor_before_block,
nir_cursor_after_block,
nir_cursor_before_instr,
nir_cursor_after_instr,
} nir_cursor_option;
typedef struct {
nir_cursor_option option;
union {
nir_block *block;
nir_instr *instr;
};
} nir_cursor;
static inline nir_block *
nir_cursor_current_block(nir_cursor cursor)
{
if (cursor.option == nir_cursor_before_instr ||
cursor.option == nir_cursor_after_instr) {
return cursor.instr->block;
} else {
return cursor.block;
}
}
bool nir_cursors_equal(nir_cursor a, nir_cursor b);
static inline nir_cursor
nir_before_block(nir_block *block)
{
nir_cursor cursor;
cursor.option = nir_cursor_before_block;
cursor.block = block;
return cursor;
}
static inline nir_cursor
nir_after_block(nir_block *block)
{
nir_cursor cursor;
cursor.option = nir_cursor_after_block;
cursor.block = block;
return cursor;
}
static inline nir_cursor
nir_before_instr(nir_instr *instr)
{
nir_cursor cursor;
cursor.option = nir_cursor_before_instr;
cursor.instr = instr;
return cursor;
}
static inline nir_cursor
nir_after_instr(nir_instr *instr)
{
nir_cursor cursor;
cursor.option = nir_cursor_after_instr;
cursor.instr = instr;
return cursor;
}
static inline nir_cursor
nir_before_block_after_phis(nir_block *block)
{
nir_phi_instr *last_phi = nir_block_last_phi_instr(block);
if (last_phi)
return nir_after_instr(&last_phi->instr);
else
return nir_before_block(block);
}
static inline nir_cursor
nir_after_block_before_jump(nir_block *block)
{
nir_instr *last_instr = nir_block_last_instr(block);
if (last_instr && last_instr->type == nir_instr_type_jump) {
return nir_before_instr(last_instr);
} else {
return nir_after_block(block);
}
}
static inline nir_cursor
nir_before_src(nir_src *src, bool is_if_condition)
{
if (is_if_condition) {
nir_block *prev_block =
nir_cf_node_as_block(nir_cf_node_prev(&src->parent_if->cf_node));
assert(!nir_block_ends_in_jump(prev_block));
return nir_after_block(prev_block);
} else if (src->parent_instr->type == nir_instr_type_phi) {
#ifndef NDEBUG
nir_phi_instr *cond_phi = nir_instr_as_phi(src->parent_instr);
bool found = false;
nir_foreach_phi_src(phi_src, cond_phi) {
if (phi_src->src.ssa == src->ssa) {
found = true;
break;
}
}
assert(found);
#endif
nir_phi_src *phi_src = LIST_ENTRY(nir_phi_src, src, src);
return nir_after_block_before_jump(phi_src->pred);
} else {
return nir_before_instr(src->parent_instr);
}
}
static inline nir_cursor
nir_before_cf_node(nir_cf_node *node)
{
if (node->type == nir_cf_node_block)
return nir_before_block(nir_cf_node_as_block(node));
return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node)));
}
static inline nir_cursor
nir_after_cf_node(nir_cf_node *node)
{
if (node->type == nir_cf_node_block)
return nir_after_block(nir_cf_node_as_block(node));
return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node)));
}
static inline nir_cursor
nir_after_phis(nir_block *block)
{
nir_foreach_instr(instr, block) {
if (instr->type != nir_instr_type_phi)
return nir_before_instr(instr);
}
return nir_after_block(block);
}
static inline nir_cursor
nir_after_instr_and_phis(nir_instr *instr)
{
if (instr->type == nir_instr_type_phi)
return nir_after_phis(instr->block);
else
return nir_after_instr(instr);
}
static inline nir_cursor
nir_after_cf_node_and_phis(nir_cf_node *node)
{
if (node->type == nir_cf_node_block)
return nir_after_block(nir_cf_node_as_block(node));
nir_block *block = nir_cf_node_as_block(nir_cf_node_next(node));
return nir_after_phis(block);
}
static inline nir_cursor
nir_before_cf_list(struct exec_list *cf_list)
{
nir_cf_node *first_node = exec_node_data(nir_cf_node,
exec_list_get_head(cf_list), node);
return nir_before_cf_node(first_node);
}
static inline nir_cursor
nir_after_cf_list(struct exec_list *cf_list)
{
nir_cf_node *last_node = exec_node_data(nir_cf_node,
exec_list_get_tail(cf_list), node);
return nir_after_cf_node(last_node);
}
void nir_instr_insert(nir_cursor cursor, nir_instr *instr);
bool nir_instr_move(nir_cursor cursor, nir_instr *instr);
static inline void
nir_instr_insert_before(nir_instr *instr, nir_instr *before)
{
nir_instr_insert(nir_before_instr(instr), before);
}
static inline void
nir_instr_insert_after(nir_instr *instr, nir_instr *after)
{
nir_instr_insert(nir_after_instr(instr), after);
}
static inline void
nir_instr_insert_before_block(nir_block *block, nir_instr *before)
{
nir_instr_insert(nir_before_block(block), before);
}
static inline void
nir_instr_insert_after_block(nir_block *block, nir_instr *after)
{
nir_instr_insert(nir_after_block(block), after);
}
static inline void
nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
{
nir_instr_insert(nir_before_cf_node(node), before);
}
static inline void
nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
{
nir_instr_insert(nir_after_cf_node(node), after);
}
static inline void
nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
{
nir_instr_insert(nir_before_cf_list(list), before);
}
static inline void
nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
{
nir_instr_insert(nir_after_cf_list(list), after);
}
void nir_instr_remove_v(nir_instr *instr);
static inline nir_cursor
nir_instr_remove(nir_instr *instr)
{
nir_cursor cursor;
nir_instr *prev = nir_instr_prev(instr);
if (prev) {
cursor = nir_after_instr(prev);
} else {
cursor = nir_before_block(instr->block);
}
nir_instr_remove_v(instr);
return cursor;
}
nir_cursor nir_instr_free_and_dce(nir_instr *instr);
nir_ssa_def *nir_instr_ssa_def(nir_instr *instr);
typedef bool (*nir_foreach_ssa_def_cb)(nir_ssa_def *def, void *state);
typedef bool (*nir_foreach_dest_cb)(nir_dest *dest, void *state);
typedef bool (*nir_foreach_src_cb)(nir_src *src, void *state);
bool nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb,
void *state);
static inline bool nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state);
static inline bool nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state);
bool nir_foreach_phi_src_leaving_block(nir_block *instr,
nir_foreach_src_cb cb,
void *state);
nir_const_value *nir_src_as_const_value(nir_src src);
#define NIR_SRC_AS_(name, c_type, type_enum, cast_macro) \
static inline c_type * \
nir_src_as_ ## name (nir_src src) \
{ \
return src.is_ssa && src.ssa->parent_instr->type == type_enum \
? cast_macro(src.ssa->parent_instr) : NULL; \
}
NIR_SRC_AS_(alu_instr, nir_alu_instr, nir_instr_type_alu, nir_instr_as_alu)
NIR_SRC_AS_(intrinsic, nir_intrinsic_instr,
nir_instr_type_intrinsic, nir_instr_as_intrinsic)
NIR_SRC_AS_(deref, nir_deref_instr, nir_instr_type_deref, nir_instr_as_deref)
bool nir_src_is_dynamically_uniform(nir_src src);
bool nir_srcs_equal(nir_src src1, nir_src src2);
bool nir_instrs_equal(const nir_instr *instr1, const nir_instr *instr2);
static inline void
nir_instr_rewrite_src_ssa(ASSERTED nir_instr *instr,
nir_src *src, nir_ssa_def *new_ssa)
{
assert(src->parent_instr == instr);
assert(src->is_ssa && src->ssa);
list_del(&src->use_link);
src->ssa = new_ssa;
list_addtail(&src->use_link, &new_ssa->uses);
}
void nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src);
void nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src);
static inline void
nir_if_rewrite_condition_ssa(ASSERTED nir_if *if_stmt,
nir_src *src, nir_ssa_def *new_ssa)
{
assert(src->parent_if == if_stmt);
assert(src->is_ssa && src->ssa);
list_del(&src->use_link);
src->ssa = new_ssa;
list_addtail(&src->use_link, &new_ssa->if_uses);
}
void nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src);
void nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest,
nir_dest new_dest);
void nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
unsigned num_components, unsigned bit_size,
const char *name);
void nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
unsigned num_components, unsigned bit_size);
static inline void
nir_ssa_dest_init_for_type(nir_instr *instr, nir_dest *dest,
const struct glsl_type *type,
const char *name)
{
assert(glsl_type_is_vector_or_scalar(type));
nir_ssa_dest_init(instr, dest, glsl_get_components(type),
glsl_get_bit_size(type), name);
}
void nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_ssa_def *new_ssa);
void nir_ssa_def_rewrite_uses_src(nir_ssa_def *def, nir_src new_src);
void nir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_ssa_def *new_ssa,
nir_instr *after_me);
nir_component_mask_t nir_ssa_def_components_read(const nir_ssa_def *def);
static inline bool
nir_ssa_def_is_unused(nir_ssa_def *ssa)
{
return list_is_empty(&ssa->uses) && list_is_empty(&ssa->if_uses);
}
nir_block *nir_block_unstructured_next(nir_block *block);
nir_block *nir_unstructured_start_block(nir_function_impl *impl);
#define nir_foreach_block_unstructured(block, impl) \
for (nir_block *block = nir_unstructured_start_block(impl); block != NULL; \
block = nir_block_unstructured_next(block))
#define nir_foreach_block_unstructured_safe(block, impl) \
for (nir_block *block = nir_unstructured_start_block(impl), \
*next = nir_block_unstructured_next(block); \
block != NULL; \
block = next, next = nir_block_unstructured_next(block))
nir_block *nir_block_cf_tree_next(nir_block *block);
nir_block *nir_block_cf_tree_prev(nir_block *block);
nir_block *nir_cf_node_cf_tree_first(nir_cf_node *node);
nir_block *nir_cf_node_cf_tree_last(nir_cf_node *node);
nir_block *nir_cf_node_cf_tree_next(nir_cf_node *node);
#define nir_foreach_block(block, impl) \
for (nir_block *block = nir_start_block(impl); block != NULL; \
block = nir_block_cf_tree_next(block))
#define nir_foreach_block_safe(block, impl) \
for (nir_block *block = nir_start_block(impl), \
*next = nir_block_cf_tree_next(block); \
block != NULL; \
block = next, next = nir_block_cf_tree_next(block))
#define nir_foreach_block_reverse(block, impl) \
for (nir_block *block = nir_impl_last_block(impl); block != NULL; \
block = nir_block_cf_tree_prev(block))
#define nir_foreach_block_reverse_safe(block, impl) \
for (nir_block *block = nir_impl_last_block(impl), \
*prev = nir_block_cf_tree_prev(block); \
block != NULL; \
block = prev, prev = nir_block_cf_tree_prev(block))
#define nir_foreach_block_in_cf_node(block, node) \
for (nir_block *block = nir_cf_node_cf_tree_first(node); \
block != nir_cf_node_cf_tree_next(node); \
block = nir_block_cf_tree_next(block))
nir_if *nir_block_get_following_if(nir_block *block);
nir_loop *nir_block_get_following_loop(nir_block *block);
nir_block **nir_block_get_predecessors_sorted(const nir_block *block, void *mem_ctx);
void nir_index_local_regs(nir_function_impl *impl);
void nir_index_ssa_defs(nir_function_impl *impl);
unsigned nir_index_instrs(nir_function_impl *impl);
void nir_index_blocks(nir_function_impl *impl);
unsigned nir_shader_index_vars(nir_shader *shader, nir_variable_mode modes);
unsigned nir_function_impl_index_vars(nir_function_impl *impl);
void nir_print_shader(nir_shader *shader, FILE *fp);
void nir_print_shader_annotated(nir_shader *shader, FILE *fp, struct hash_table *errors);
void nir_print_instr(const nir_instr *instr, FILE *fp);
void nir_print_deref(const nir_deref_instr *deref, FILE *fp);
void nir_log_shader_annotated_tagged(enum mesa_log_level level, const char *tag, nir_shader *shader, struct hash_table *annotations);
#define nir_log_shadere(s) nir_log_shader_annotated_tagged(MESA_LOG_ERROR, (MESA_LOG_TAG), (s), NULL)
#define nir_log_shaderw(s) nir_log_shader_annotated_tagged(MESA_LOG_WARN, (MESA_LOG_TAG), (s), NULL)
#define nir_log_shaderi(s) nir_log_shader_annotated_tagged(MESA_LOG_INFO, (MESA_LOG_TAG), (s), NULL)
#define nir_log_shader_annotated(s, annotations) nir_log_shader_annotated_tagged(MESA_LOG_ERROR, (MESA_LOG_TAG), (s), annotations)
char *nir_shader_as_str(nir_shader *nir, void *mem_ctx);
char *nir_shader_as_str_annotated(nir_shader *nir, struct hash_table *annotations, void *mem_ctx);
nir_instr *nir_instr_clone(nir_shader *s, const nir_instr *orig);
nir_alu_instr *nir_alu_instr_clone(nir_shader *s, const nir_alu_instr *orig);
nir_shader *nir_shader_clone(void *mem_ctx, const nir_shader *s);
nir_function_impl *nir_function_impl_clone(nir_shader *shader,
const nir_function_impl *fi);
nir_constant *nir_constant_clone(const nir_constant *c, nir_variable *var);
nir_variable *nir_variable_clone(const nir_variable *c, nir_shader *shader);
void nir_shader_replace(nir_shader *dest, nir_shader *src);
void nir_shader_serialize_deserialize(nir_shader *s);
#ifndef NDEBUG
void nir_validate_shader(nir_shader *shader, const char *when);
void nir_validate_ssa_dominance(nir_shader *shader, const char *when);
void nir_metadata_set_validation_flag(nir_shader *shader);
void nir_metadata_check_validation_flag(nir_shader *shader);
static inline bool
should_skip_nir(const char *name)
{
static const char *list = NULL;
if (!list) {
list = getenv("NIR_SKIP");
if (!list)
list = "";
}
if (!list[0])
return false;
return comma_separated_list_contains(list, name);
}
static inline bool
should_clone_nir(void)
{
static int should_clone = -1;
if (should_clone < 0)
should_clone = env_var_as_boolean("NIR_TEST_CLONE", false);
return should_clone;
}
static inline bool
should_serialize_deserialize_nir(void)
{
static int test_serialize = -1;
if (test_serialize < 0)
test_serialize = env_var_as_boolean("NIR_TEST_SERIALIZE", false);
return test_serialize;
}
static inline bool
should_print_nir(nir_shader *shader)
{
static int should_print = -1;
if (should_print < 0)
should_print = env_var_as_unsigned("NIR_PRINT", 0);
if (should_print == 1)
return !shader->info.internal;
return should_print;
}
#else
static inline void nir_validate_shader(nir_shader *shader, const char *when) { (void) shader; (void)when; }
static inline void nir_validate_ssa_dominance(nir_shader *shader, const char *when) { (void) shader; (void)when; }
static inline void nir_metadata_set_validation_flag(nir_shader *shader) { (void) shader; }
static inline void nir_metadata_check_validation_flag(nir_shader *shader) { (void) shader; }
static inline bool should_skip_nir(UNUSED const char *pass_name) { return false; }
static inline bool should_clone_nir(void) { return false; }
static inline bool should_serialize_deserialize_nir(void) { return false; }
static inline bool should_print_nir(nir_shader *shader) { return false; }
#endif
#define _PASS(pass, nir, do_pass) do { \
if (should_skip_nir(#pass)) { \
printf("skipping %s\n", #pass); \
break; \
} \
do_pass \
if (should_clone_nir()) { \
nir_shader *clone = nir_shader_clone(ralloc_parent(nir), nir); \
nir_shader_replace(nir, clone); \
} \
if (should_serialize_deserialize_nir()) { \
nir_shader_serialize_deserialize(nir); \
} \
} while (0)
#define NIR_PASS(progress, nir, pass, ...) _PASS(pass, nir, \
nir_metadata_set_validation_flag(nir); \
if (should_print_nir(nir)) \
printf("%s\n", #pass); \
if (pass(nir, ##__VA_ARGS__)) { \
nir_validate_shader(nir, "after " #pass); \
progress = true; \
if (should_print_nir(nir)) \
nir_print_shader(nir, stdout); \
nir_metadata_check_validation_flag(nir); \
} \
)
#define NIR_PASS_V(nir, pass, ...) _PASS(pass, nir, \
if (should_print_nir(nir)) \
printf("%s\n", #pass); \
pass(nir, ##__VA_ARGS__); \
nir_validate_shader(nir, "after " #pass); \
if (should_print_nir(nir)) \
nir_print_shader(nir, stdout); \
)
#define NIR_SKIP(name) should_skip_nir(#name)
typedef bool (*nir_instr_writemask_filter_cb)(const nir_instr *,
unsigned writemask, const void *);
typedef nir_ssa_def *(*nir_lower_instr_cb)(struct nir_builder *,
nir_instr *, void *);
#define NIR_LOWER_INSTR_PROGRESS ((nir_ssa_def *)(uintptr_t)1)
#define NIR_LOWER_INSTR_PROGRESS_REPLACE ((nir_ssa_def *)(uintptr_t)2)
bool nir_function_impl_lower_instructions(nir_function_impl *impl,
nir_instr_filter_cb filter,
nir_lower_instr_cb lower,
void *cb_data);
bool nir_shader_lower_instructions(nir_shader *shader,
nir_instr_filter_cb filter,
nir_lower_instr_cb lower,
void *cb_data);
void nir_calc_dominance_impl(nir_function_impl *impl);
void nir_calc_dominance(nir_shader *shader);
nir_block *nir_dominance_lca(nir_block *b1, nir_block *b2);
bool nir_block_dominates(nir_block *parent, nir_block *child);
bool nir_block_is_unreachable(nir_block *block);
void nir_dump_dom_tree_impl(nir_function_impl *impl, FILE *fp);
void nir_dump_dom_tree(nir_shader *shader, FILE *fp);
void nir_dump_dom_frontier_impl(nir_function_impl *impl, FILE *fp);
void nir_dump_dom_frontier(nir_shader *shader, FILE *fp);
void nir_dump_cfg_impl(nir_function_impl *impl, FILE *fp);
void nir_dump_cfg(nir_shader *shader, FILE *fp);
void nir_gs_count_vertices_and_primitives(const nir_shader *shader,
int *out_vtxcnt,
int *out_prmcnt,
unsigned num_streams);
bool nir_shrink_vec_array_vars(nir_shader *shader, nir_variable_mode modes);
bool nir_split_array_vars(nir_shader *shader, nir_variable_mode modes);
bool nir_split_var_copies(nir_shader *shader);
bool nir_split_per_member_structs(nir_shader *shader);
bool nir_split_struct_vars(nir_shader *shader, nir_variable_mode modes);
bool nir_lower_returns_impl(nir_function_impl *impl);
bool nir_lower_returns(nir_shader *shader);
void nir_inline_function_impl(struct nir_builder *b,
const nir_function_impl *impl,
nir_ssa_def **params,
struct hash_table *shader_var_remap);
bool nir_inline_functions(nir_shader *shader);
void nir_find_inlinable_uniforms(nir_shader *shader);
void nir_inline_uniforms(nir_shader *shader, unsigned num_uniforms,
const uint32_t *uniform_values,
const uint16_t *uniform_dw_offsets);
bool nir_propagate_invariant(nir_shader *shader, bool invariant_prim);
void nir_lower_var_copy_instr(nir_intrinsic_instr *copy, nir_shader *shader);
void nir_lower_deref_copy_instr(struct nir_builder *b,
nir_intrinsic_instr *copy);
bool nir_lower_var_copies(nir_shader *shader);
bool nir_opt_memcpy(nir_shader *shader);
bool nir_lower_memcpy(nir_shader *shader);
void nir_fixup_deref_modes(nir_shader *shader);
bool nir_lower_global_vars_to_local(nir_shader *shader);
typedef enum {
nir_lower_direct_array_deref_of_vec_load = (1 << 0),
nir_lower_indirect_array_deref_of_vec_load = (1 << 1),
nir_lower_direct_array_deref_of_vec_store = (1 << 2),
nir_lower_indirect_array_deref_of_vec_store = (1 << 3),
} nir_lower_array_deref_of_vec_options;
bool nir_lower_array_deref_of_vec(nir_shader *shader, nir_variable_mode modes,
nir_lower_array_deref_of_vec_options options);
bool nir_lower_indirect_derefs(nir_shader *shader, nir_variable_mode modes,
uint32_t max_lower_array_len);
bool nir_lower_indirect_builtin_uniform_derefs(nir_shader *shader);
bool nir_lower_locals_to_regs(nir_shader *shader);
void nir_lower_io_to_temporaries(nir_shader *shader,
nir_function_impl *entrypoint,
bool outputs, bool inputs);
bool nir_lower_vars_to_scratch(nir_shader *shader,
nir_variable_mode modes,
int size_threshold,
glsl_type_size_align_func size_align);
void nir_lower_clip_halfz(nir_shader *shader);
void nir_shader_gather_info(nir_shader *shader, nir_function_impl *entrypoint);
void nir_gather_ssa_types(nir_function_impl *impl,
BITSET_WORD *float_types,
BITSET_WORD *int_types);
void nir_assign_var_locations(nir_shader *shader, nir_variable_mode mode,
unsigned *size,
int (*type_size)(const struct glsl_type *, bool));
bool nir_remove_unused_varyings(nir_shader *producer, nir_shader *consumer);
bool nir_remove_unused_io_vars(nir_shader *shader, nir_variable_mode mode,
uint64_t *used_by_other_stage,
uint64_t *used_by_other_stage_patches);
void nir_compact_varyings(nir_shader *producer, nir_shader *consumer,
bool default_to_smooth_interp);
void nir_link_xfb_varyings(nir_shader *producer, nir_shader *consumer);
bool nir_link_opt_varyings(nir_shader *producer, nir_shader *consumer);
void nir_link_varying_precision(nir_shader *producer, nir_shader *consumer);
bool nir_lower_amul(nir_shader *shader,
int (*type_size)(const struct glsl_type *, bool));
bool nir_lower_ubo_vec4(nir_shader *shader);
void nir_assign_io_var_locations(nir_shader *shader,
nir_variable_mode mode,
unsigned *size,
gl_shader_stage stage);
typedef struct {
uint8_t num_linked_io_vars;
uint8_t num_linked_patch_io_vars;
} nir_linked_io_var_info;
nir_linked_io_var_info
nir_assign_linked_io_var_locations(nir_shader *producer,
nir_shader *consumer);
typedef enum {
nir_lower_io_lower_64bit_to_32 = (1 << 0),
nir_lower_io_force_sample_interpolation = (1 << 1),
} nir_lower_io_options;
bool nir_lower_io(nir_shader *shader,
nir_variable_mode modes,
int (*type_size)(const struct glsl_type *, bool),
nir_lower_io_options);
bool nir_io_add_const_offset_to_base(nir_shader *nir, nir_variable_mode modes);
bool
nir_lower_vars_to_explicit_types(nir_shader *shader,
nir_variable_mode modes,
glsl_type_size_align_func type_info);
void
nir_gather_explicit_io_initializers(nir_shader *shader,
void *dst, size_t dst_size,
nir_variable_mode mode);
bool nir_lower_vec3_to_vec4(nir_shader *shader, nir_variable_mode modes);
typedef enum {
nir_address_format_32bit_global,
nir_address_format_64bit_global,
nir_address_format_64bit_global_32bit_offset,
nir_address_format_64bit_bounded_global,
nir_address_format_32bit_index_offset,
nir_address_format_32bit_index_offset_pack64,
nir_address_format_vec2_index_32bit_offset,
nir_address_format_62bit_generic,
nir_address_format_32bit_offset,
nir_address_format_32bit_offset_as_64bit,
nir_address_format_logical,
} nir_address_format;
static inline unsigned
nir_address_format_bit_size(nir_address_format addr_format)
{
switch (addr_format) {
case nir_address_format_32bit_global: return 32;
case nir_address_format_64bit_global: return 64;
case nir_address_format_64bit_global_32bit_offset: return 32;
case nir_address_format_64bit_bounded_global: return 32;
case nir_address_format_32bit_index_offset: return 32;
case nir_address_format_32bit_index_offset_pack64: return 64;
case nir_address_format_vec2_index_32bit_offset: return 32;
case nir_address_format_62bit_generic: return 64;
case nir_address_format_32bit_offset: return 32;
case nir_address_format_32bit_offset_as_64bit: return 64;
case nir_address_format_logical: return 32;
}
unreachable("Invalid address format");
}
static inline unsigned
nir_address_format_num_components(nir_address_format addr_format)
{
switch (addr_format) {
case nir_address_format_32bit_global: return 1;
case nir_address_format_64bit_global: return 1;
case nir_address_format_64bit_global_32bit_offset: return 4;
case nir_address_format_64bit_bounded_global: return 4;
case nir_address_format_32bit_index_offset: return 2;
case nir_address_format_32bit_index_offset_pack64: return 1;
case nir_address_format_vec2_index_32bit_offset: return 3;
case nir_address_format_62bit_generic: return 1;
case nir_address_format_32bit_offset: return 1;
case nir_address_format_32bit_offset_as_64bit: return 1;
case nir_address_format_logical: return 1;
}
unreachable("Invalid address format");
}
static inline const struct glsl_type *
nir_address_format_to_glsl_type(nir_address_format addr_format)
{
unsigned bit_size = nir_address_format_bit_size(addr_format);
assert(bit_size == 32 || bit_size == 64);
return glsl_vector_type(bit_size == 32 ? GLSL_TYPE_UINT : GLSL_TYPE_UINT64,
nir_address_format_num_components(addr_format));
}
const nir_const_value *nir_address_format_null_value(nir_address_format addr_format);
nir_ssa_def *nir_build_addr_ieq(struct nir_builder *b, nir_ssa_def *addr0, nir_ssa_def *addr1,
nir_address_format addr_format);
nir_ssa_def *nir_build_addr_isub(struct nir_builder *b, nir_ssa_def *addr0, nir_ssa_def *addr1,
nir_address_format addr_format);
nir_ssa_def * nir_explicit_io_address_from_deref(struct nir_builder *b,
nir_deref_instr *deref,
nir_ssa_def *base_addr,
nir_address_format addr_format);
bool nir_get_explicit_deref_align(nir_deref_instr *deref,
bool default_to_type_align,
uint32_t *align_mul,
uint32_t *align_offset);
void nir_lower_explicit_io_instr(struct nir_builder *b,
nir_intrinsic_instr *io_instr,
nir_ssa_def *addr,
nir_address_format addr_format);
bool nir_lower_explicit_io(nir_shader *shader,
nir_variable_mode modes,
nir_address_format);
bool
nir_lower_shader_calls(nir_shader *shader,
nir_address_format address_format,
unsigned stack_alignment,
nir_shader ***resume_shaders_out,
uint32_t *num_resume_shaders_out,
void *mem_ctx);
nir_src *nir_get_io_offset_src(nir_intrinsic_instr *instr);
nir_src *nir_get_io_vertex_index_src(nir_intrinsic_instr *instr);
nir_src *nir_get_shader_call_payload_src(nir_intrinsic_instr *call);
bool nir_is_arrayed_io(const nir_variable *var, gl_shader_stage stage);
bool nir_lower_regs_to_ssa_impl(nir_function_impl *impl);
bool nir_lower_regs_to_ssa(nir_shader *shader);
bool nir_lower_vars_to_ssa(nir_shader *shader);
bool nir_remove_dead_derefs(nir_shader *shader);
bool nir_remove_dead_derefs_impl(nir_function_impl *impl);
typedef struct nir_remove_dead_variables_options {
bool (*can_remove_var)(nir_variable *var, void *data);
void *can_remove_var_data;
} nir_remove_dead_variables_options;
bool nir_remove_dead_variables(nir_shader *shader, nir_variable_mode modes,
const nir_remove_dead_variables_options *options);
bool nir_lower_variable_initializers(nir_shader *shader,
nir_variable_mode modes);
bool nir_zero_initialize_shared_memory(nir_shader *shader,
const unsigned shared_size,
const unsigned chunk_size);
bool nir_move_vec_src_uses_to_dest(nir_shader *shader);
bool nir_lower_vec_to_movs(nir_shader *shader, nir_instr_writemask_filter_cb cb,
const void *_data);
void nir_lower_alpha_test(nir_shader *shader, enum compare_func func,
bool alpha_to_one,
const gl_state_index16 *alpha_ref_state_tokens);
bool nir_lower_alu(nir_shader *shader);
bool nir_lower_flrp(nir_shader *shader, unsigned lowering_mask,
bool always_precise);
bool nir_lower_alu_to_scalar(nir_shader *shader, nir_instr_filter_cb cb, const void *data);
bool nir_lower_bool_to_bitsize(nir_shader *shader);
bool nir_lower_bool_to_float(nir_shader *shader);
bool nir_lower_bool_to_int32(nir_shader *shader);
bool nir_opt_simplify_convert_alu_types(nir_shader *shader);
bool nir_lower_convert_alu_types(nir_shader *shader,
bool (*should_lower)(nir_intrinsic_instr *));
bool nir_lower_constant_convert_alu_types(nir_shader *shader);
bool nir_lower_alu_conversion_to_intrinsic(nir_shader *shader);
bool nir_lower_int_to_float(nir_shader *shader);
bool nir_lower_load_const_to_scalar(nir_shader *shader);
bool nir_lower_read_invocation_to_scalar(nir_shader *shader);
bool nir_lower_phis_to_scalar(nir_shader *shader, bool lower_all);
void nir_lower_io_arrays_to_elements(nir_shader *producer, nir_shader *consumer);
void nir_lower_io_arrays_to_elements_no_indirects(nir_shader *shader,
bool outputs_only);
void nir_lower_io_to_scalar(nir_shader *shader, nir_variable_mode mask);
bool nir_lower_io_to_scalar_early(nir_shader *shader, nir_variable_mode mask);
bool nir_lower_io_to_vector(nir_shader *shader, nir_variable_mode mask);
bool nir_vectorize_tess_levels(nir_shader *shader);
bool nir_lower_fragcolor(nir_shader *shader, unsigned max_cbufs);
bool nir_lower_fragcoord_wtrans(nir_shader *shader);
void nir_lower_viewport_transform(nir_shader *shader);
bool nir_lower_uniforms_to_ubo(nir_shader *shader, bool dword_packed, bool load_vec4);
bool nir_lower_is_helper_invocation(nir_shader *shader);
typedef struct nir_lower_subgroups_options {
uint8_t subgroup_size;
uint8_t ballot_bit_size;
uint8_t ballot_components;
bool lower_to_scalar:1;
bool lower_vote_trivial:1;
bool lower_vote_eq:1;
bool lower_subgroup_masks:1;
bool lower_shuffle:1;
bool lower_shuffle_to_32bit:1;
bool lower_shuffle_to_swizzle_amd:1;
bool lower_quad:1;
bool lower_quad_broadcast_dynamic:1;
bool lower_quad_broadcast_dynamic_to_const:1;
bool lower_elect:1;
bool lower_read_invocation_to_cond:1;
} nir_lower_subgroups_options;
bool nir_lower_subgroups(nir_shader *shader,
const nir_lower_subgroups_options *options);
bool nir_lower_system_values(nir_shader *shader);
typedef struct nir_lower_compute_system_values_options {
bool has_base_global_invocation_id:1;
bool has_base_workgroup_id:1;
bool shuffle_local_ids_for_quad_derivatives:1;
bool lower_local_invocation_index:1;
} nir_lower_compute_system_values_options;
bool nir_lower_compute_system_values(nir_shader *shader,
const nir_lower_compute_system_values_options *options);
enum PACKED nir_lower_tex_packing {
nir_lower_tex_packing_none = 0,
nir_lower_tex_packing_16,
nir_lower_tex_packing_8,
};
typedef struct nir_lower_tex_options {
unsigned lower_txp;
bool lower_txf_offset;
bool lower_rect_offset;
bool lower_rect;
unsigned lower_y_uv_external;
unsigned lower_y_u_v_external;
unsigned lower_yx_xuxv_external;
unsigned lower_xy_uxvx_external;
unsigned lower_ayuv_external;
unsigned lower_xyuv_external;
unsigned lower_yuv_external;
unsigned lower_yu_yv_external;
unsigned lower_y41x_external;
unsigned bt709_external;
unsigned bt2020_external;
unsigned saturate_s;
unsigned saturate_t;
unsigned saturate_r;
unsigned swizzle_result;
uint8_t swizzles[32][4];
float scale_factors[32];
unsigned lower_srgb;
bool lower_txd_cube_map;
bool lower_txd_3d;
bool lower_txd_shadow;
bool lower_txd;
bool lower_txb_shadow_clamp;
bool lower_txd_shadow_clamp;
bool lower_txd_offset_clamp;
bool lower_txd_clamp_bindless_sampler;
bool lower_txd_clamp_if_sampler_index_not_lt_16;
bool lower_txs_lod;
bool lower_tg4_broadcom_swizzle;
bool lower_tg4_offsets;
enum nir_lower_tex_packing lower_tex_packing[32];
} nir_lower_tex_options;
bool nir_lower_tex(nir_shader *shader,
const nir_lower_tex_options *options);
bool nir_lower_readonly_images_to_tex(nir_shader *shader, bool per_variable);
enum nir_lower_non_uniform_access_type {
nir_lower_non_uniform_ubo_access = (1 << 0),
nir_lower_non_uniform_ssbo_access = (1 << 1),
nir_lower_non_uniform_texture_access = (1 << 2),
nir_lower_non_uniform_image_access = (1 << 3),
};
typedef nir_component_mask_t (*nir_lower_non_uniform_access_callback)(const nir_src *, void *);
typedef struct nir_lower_non_uniform_access_options {
enum nir_lower_non_uniform_access_type types;
nir_lower_non_uniform_access_callback callback;
void *callback_data;
} nir_lower_non_uniform_access_options;
bool nir_lower_non_uniform_access(nir_shader *shader,
const nir_lower_non_uniform_access_options *options);
typedef struct {
bool imprecise_32bit_lowering;
bool allow_fp16;
} nir_lower_idiv_options;
bool nir_lower_idiv(nir_shader *shader, const nir_lower_idiv_options *options);
typedef struct nir_input_attachment_options {
bool use_fragcoord_sysval;
bool use_layer_id_sysval;
bool use_view_id_for_layer;
} nir_input_attachment_options;
bool nir_lower_input_attachments(nir_shader *shader,
const nir_input_attachment_options *options);
bool nir_lower_clip_vs(nir_shader *shader, unsigned ucp_enables,
bool use_vars,
bool use_clipdist_array,
const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH]);
bool nir_lower_clip_gs(nir_shader *shader, unsigned ucp_enables,
bool use_clipdist_array,
const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH]);
bool nir_lower_clip_fs(nir_shader *shader, unsigned ucp_enables,
bool use_clipdist_array);
bool nir_lower_clip_cull_distance_arrays(nir_shader *nir);
bool nir_lower_clip_disable(nir_shader *shader, unsigned clip_plane_enable);
void nir_lower_point_size_mov(nir_shader *shader,
const gl_state_index16 *pointsize_state_tokens);
bool nir_lower_frexp(nir_shader *nir);
void nir_lower_two_sided_color(nir_shader *shader, bool face_sysval);
bool nir_lower_clamp_color_outputs(nir_shader *shader);
bool nir_lower_flatshade(nir_shader *shader);
void nir_lower_passthrough_edgeflags(nir_shader *shader);
bool nir_lower_patch_vertices(nir_shader *nir, unsigned static_count,
const gl_state_index16 *uniform_state_tokens);
typedef struct nir_lower_wpos_ytransform_options {
gl_state_index16 state_tokens[STATE_LENGTH];
bool fs_coord_origin_upper_left :1;
bool fs_coord_origin_lower_left :1;
bool fs_coord_pixel_center_integer :1;
bool fs_coord_pixel_center_half_integer :1;
} nir_lower_wpos_ytransform_options;
bool nir_lower_wpos_ytransform(nir_shader *shader,
const nir_lower_wpos_ytransform_options *options);
bool nir_lower_wpos_center(nir_shader *shader, const bool for_sample_shading);
bool nir_lower_pntc_ytransform(nir_shader *shader,
const gl_state_index16 clipplane_state_tokens[][STATE_LENGTH]);
bool nir_lower_wrmasks(nir_shader *shader, nir_instr_filter_cb cb, const void *data);
bool nir_lower_fb_read(nir_shader *shader);
typedef struct nir_lower_drawpixels_options {
gl_state_index16 texcoord_state_tokens[STATE_LENGTH];
gl_state_index16 scale_state_tokens[STATE_LENGTH];
gl_state_index16 bias_state_tokens[STATE_LENGTH];
unsigned drawpix_sampler;
unsigned pixelmap_sampler;
bool pixel_maps :1;
bool scale_and_bias :1;
} nir_lower_drawpixels_options;
void nir_lower_drawpixels(nir_shader *shader,
const nir_lower_drawpixels_options *options);
typedef struct nir_lower_bitmap_options {
unsigned sampler;
bool swizzle_xxxx;
} nir_lower_bitmap_options;
void nir_lower_bitmap(nir_shader *shader, const nir_lower_bitmap_options *options);
bool nir_lower_atomics_to_ssbo(nir_shader *shader);
typedef enum {
nir_lower_int_source_mods = 1 << 0,
nir_lower_float_source_mods = 1 << 1,
nir_lower_64bit_source_mods = 1 << 2,
nir_lower_triop_abs = 1 << 3,
nir_lower_all_source_mods = (1 << 4) - 1
} nir_lower_to_source_mods_flags;
bool nir_lower_to_source_mods(nir_shader *shader, nir_lower_to_source_mods_flags options);
typedef enum {
nir_lower_gs_intrinsics_per_stream = 1 << 0,
nir_lower_gs_intrinsics_count_primitives = 1 << 1,
nir_lower_gs_intrinsics_count_vertices_per_primitive = 1 << 2,
nir_lower_gs_intrinsics_overwrite_incomplete = 1 << 3,
} nir_lower_gs_intrinsics_flags;
bool nir_lower_gs_intrinsics(nir_shader *shader, nir_lower_gs_intrinsics_flags options);
typedef unsigned (*nir_lower_bit_size_callback)(const nir_instr *, void *);
bool nir_lower_bit_size(nir_shader *shader,
nir_lower_bit_size_callback callback,
void *callback_data);
bool nir_lower_64bit_phis(nir_shader *shader);
nir_lower_int64_options nir_lower_int64_op_to_options_mask(nir_op opcode);
bool nir_lower_int64(nir_shader *shader);
nir_lower_doubles_options nir_lower_doubles_op_to_options_mask(nir_op opcode);
bool nir_lower_doubles(nir_shader *shader, const nir_shader *softfp64,
nir_lower_doubles_options options);
bool nir_lower_pack(nir_shader *shader);
bool nir_recompute_io_bases(nir_function_impl *impl, nir_variable_mode modes);
bool nir_lower_mediump_io(nir_shader *nir, nir_variable_mode modes,
uint64_t varying_mask, bool use_16bit_slots);
bool nir_force_mediump_io(nir_shader *nir, nir_variable_mode modes,
nir_alu_type types);
bool nir_unpack_16bit_varying_slots(nir_shader *nir, nir_variable_mode modes);
bool nir_fold_16bit_sampler_conversions(nir_shader *nir,
unsigned tex_src_types);
typedef struct {
bool legalize_type;
uint8_t bit_size;
nir_tex_src_type match_src;
} nir_tex_src_type_constraint, nir_tex_src_type_constraints[nir_num_tex_src_types];
bool nir_legalize_16bit_sampler_srcs(nir_shader *nir,
nir_tex_src_type_constraints constraints);
bool nir_lower_point_size(nir_shader *shader, float min, float max);
void nir_lower_texcoord_replace(nir_shader *s, unsigned coord_replace,
bool point_coord_is_sysval, bool yinvert);
typedef enum {
nir_lower_interpolation_at_sample = (1 << 1),
nir_lower_interpolation_at_offset = (1 << 2),
nir_lower_interpolation_centroid = (1 << 3),
nir_lower_interpolation_pixel = (1 << 4),
nir_lower_interpolation_sample = (1 << 5),
} nir_lower_interpolation_options;
bool nir_lower_interpolation(nir_shader *shader,
nir_lower_interpolation_options options);
bool nir_lower_discard_or_demote(nir_shader *shader,
bool force_correct_quad_ops_after_discard);
bool nir_lower_memory_model(nir_shader *shader);
bool nir_lower_goto_ifs(nir_shader *shader);
bool nir_shader_uses_view_index(nir_shader *shader);
bool nir_can_lower_multiview(nir_shader *shader);
bool nir_lower_multiview(nir_shader *shader, uint32_t view_mask);
bool nir_lower_fp16_casts(nir_shader *shader);
bool nir_normalize_cubemap_coords(nir_shader *shader);
void nir_live_ssa_defs_impl(nir_function_impl *impl);
const BITSET_WORD *nir_get_live_ssa_defs(nir_cursor cursor, void *mem_ctx);
void nir_loop_analyze_impl(nir_function_impl *impl,
nir_variable_mode indirect_mask);
bool nir_ssa_defs_interfere(nir_ssa_def *a, nir_ssa_def *b);
bool nir_repair_ssa_impl(nir_function_impl *impl);
bool nir_repair_ssa(nir_shader *shader);
void nir_convert_loop_to_lcssa(nir_loop *loop);
bool nir_convert_to_lcssa(nir_shader *shader, bool skip_invariants, bool skip_bool_invariants);
void nir_divergence_analysis(nir_shader *shader);
bool nir_update_instr_divergence(nir_shader *shader, nir_instr *instr);
bool nir_convert_from_ssa(nir_shader *shader, bool phi_webs_only);
bool nir_lower_phis_to_regs_block(nir_block *block);
bool nir_lower_ssa_defs_to_regs_block(nir_block *block);
bool nir_rematerialize_derefs_in_use_blocks_impl(nir_function_impl *impl);
bool nir_lower_samplers(nir_shader *shader);
bool nir_lower_ssbo(nir_shader *shader);
typedef struct nir_lower_printf_options {
bool treat_doubles_as_floats : 1;
unsigned max_buffer_size;
} nir_lower_printf_options;
bool nir_lower_printf(nir_shader *nir, const nir_lower_printf_options *options);
bool nir_opt_comparison_pre_impl(nir_function_impl *impl);
bool nir_opt_comparison_pre(nir_shader *shader);
typedef struct nir_opt_access_options {
bool is_vulkan;
bool infer_non_readable;
} nir_opt_access_options;
bool nir_opt_access(nir_shader *shader, const nir_opt_access_options *options);
bool nir_opt_algebraic(nir_shader *shader);
bool nir_opt_algebraic_before_ffma(nir_shader *shader);
bool nir_opt_algebraic_late(nir_shader *shader);
bool nir_opt_algebraic_distribute_src_mods(nir_shader *shader);
bool nir_opt_constant_folding(nir_shader *shader);
typedef bool (*nir_combine_memory_barrier_cb)(
nir_intrinsic_instr *a, nir_intrinsic_instr *b, void *data);
bool nir_opt_combine_memory_barriers(nir_shader *shader,
nir_combine_memory_barrier_cb combine_cb,
void *data);
bool nir_opt_combine_stores(nir_shader *shader, nir_variable_mode modes);
bool nir_copy_prop(nir_shader *shader);
bool nir_opt_copy_prop_vars(nir_shader *shader);
bool nir_opt_cse(nir_shader *shader);
bool nir_opt_dce(nir_shader *shader);
bool nir_opt_dead_cf(nir_shader *shader);
bool nir_opt_dead_write_vars(nir_shader *shader);
bool nir_opt_deref_impl(nir_function_impl *impl);
bool nir_opt_deref(nir_shader *shader);
bool nir_opt_find_array_copies(nir_shader *shader);
bool nir_opt_gcm(nir_shader *shader, bool value_number);
bool nir_opt_idiv_const(nir_shader *shader, unsigned min_bit_size);
bool nir_opt_if(nir_shader *shader, bool aggressive_last_continue);
bool nir_opt_intrinsics(nir_shader *shader);
bool nir_opt_large_constants(nir_shader *shader,
glsl_type_size_align_func size_align,
unsigned threshold);
bool nir_opt_loop_unroll(nir_shader *shader, nir_variable_mode indirect_mask);
typedef enum {
nir_move_const_undef = (1 << 0),
nir_move_load_ubo = (1 << 1),
nir_move_load_input = (1 << 2),
nir_move_comparisons = (1 << 3),
nir_move_copies = (1 << 4),
nir_move_load_ssbo = (1 << 5),
} nir_move_options;
bool nir_can_move_instr(nir_instr *instr, nir_move_options options);
bool nir_opt_sink(nir_shader *shader, nir_move_options options);
bool nir_opt_move(nir_shader *shader, nir_move_options options);
bool nir_opt_offsets(nir_shader *shader);
bool nir_opt_peephole_select(nir_shader *shader, unsigned limit,
bool indirect_load_ok, bool expensive_alu_ok);
bool nir_opt_rematerialize_compares(nir_shader *shader);
bool nir_opt_remove_phis(nir_shader *shader);
bool nir_opt_remove_phis_block(nir_block *block);
bool nir_opt_phi_precision(nir_shader *shader);
bool nir_opt_shrink_vectors(nir_shader *shader, bool shrink_image_store);
bool nir_opt_trivial_continues(nir_shader *shader);
bool nir_opt_undef(nir_shader *shader);
bool nir_lower_undef_to_zero(nir_shader *shader);
bool nir_opt_uniform_atomics(nir_shader *shader);
typedef bool (*nir_opt_vectorize_cb)(const nir_instr *instr, void *data);
bool nir_opt_vectorize(nir_shader *shader, nir_opt_vectorize_cb filter,
void *data);
bool nir_opt_conditional_discard(nir_shader *shader);
bool nir_opt_move_discards_to_top(nir_shader *shader);
typedef bool (*nir_should_vectorize_mem_func)(unsigned align_mul,
unsigned align_offset,
unsigned bit_size,
unsigned num_components,
nir_intrinsic_instr *low, nir_intrinsic_instr *high,
void *data);
typedef struct {
nir_should_vectorize_mem_func callback;
nir_variable_mode modes;
nir_variable_mode robust_modes;
void *cb_data;
} nir_load_store_vectorize_options;
bool nir_opt_load_store_vectorize(nir_shader *shader, const nir_load_store_vectorize_options *options);
void nir_sweep(nir_shader *shader);
void nir_remap_dual_slot_attributes(nir_shader *shader,
uint64_t *dual_slot_inputs);
uint64_t nir_get_single_slot_attribs_mask(uint64_t attribs, uint64_t dual_slot);
nir_intrinsic_op nir_intrinsic_from_system_value(gl_system_value val);
gl_system_value nir_system_value_from_intrinsic(nir_intrinsic_op intrin);
static inline bool
nir_variable_is_in_ubo(const nir_variable *var)
{
return (var->data.mode == nir_var_mem_ubo &&
var->interface_type != NULL);
}
static inline bool
nir_variable_is_in_ssbo(const nir_variable *var)
{
return (var->data.mode == nir_var_mem_ssbo &&
var->interface_type != NULL);
}
static inline bool
nir_variable_is_in_block(const nir_variable *var)
{
return nir_variable_is_in_ubo(var) || nir_variable_is_in_ssbo(var);
}
typedef struct nir_unsigned_upper_bound_config {
unsigned min_subgroup_size;
unsigned max_subgroup_size;
unsigned max_workgroup_invocations;
unsigned max_workgroup_count[3];
unsigned max_workgroup_size[3];
uint32_t vertex_attrib_max[32];
} nir_unsigned_upper_bound_config;
uint32_t
nir_unsigned_upper_bound(nir_shader *shader, struct hash_table *range_ht,
nir_ssa_scalar scalar,
const nir_unsigned_upper_bound_config *config);
bool
nir_addition_might_overflow(nir_shader *shader, struct hash_table *range_ht,
nir_ssa_scalar ssa, unsigned const_val,
const nir_unsigned_upper_bound_config *config);
#include "nir_inline_helpers.h"
#ifdef __cplusplus
}
#endif
#endif