ir_visitor_status ir_explog_to_explog2_visitor::visit_leave(ir_expression *ir) { if (ir->operation == ir_unop_exp) { void *mem_ctx = ralloc_parent(ir); ir_constant *log2_e = new(mem_ctx) ir_constant(log2f(M_E)); ir->operation = ir_unop_exp2; ir->operands[0] = new(mem_ctx) ir_expression(ir_binop_mul, ir->operands[0]->type, ir->operands[0], log2_e); this->progress = true; } if (ir->operation == ir_unop_log) { void *mem_ctx = ralloc_parent(ir); ir->operation = ir_binop_mul; ir->operands[0] = new(mem_ctx) ir_expression(ir_unop_log2, ir->operands[0]->type, ir->operands[0], NULL); ir->operands[1] = new(mem_ctx) ir_constant(1.0f / log2f(M_E)); this->progress = true; } return visit_continue; }
/** * If the given ir satisfies is_clip_distance_vec8(), return new ir * representing its lowered equivalent. That is, map: * * - gl_ClipDistance => gl_ClipDistanceMESA (if gl_ClipDistance is 1D) * - gl_ClipDistance[i] => gl_ClipDistanceMESA[i] (if gl_ClipDistance is 2D) * * Otherwise return NULL. */ ir_rvalue * lower_clip_distance_visitor::lower_clip_distance_vec8(ir_rvalue *ir) { if (!ir->type->is_array()) return NULL; if (ir->type->fields.array != glsl_type::float_type) return NULL; ir_variable **new_var = NULL; if (this->old_clip_distance_out_var) { if (ir->variable_referenced() == this->old_clip_distance_out_var) new_var = &this->new_clip_distance_out_var; } if (this->old_clip_distance_in_var) { if (ir->variable_referenced() == this->old_clip_distance_in_var) new_var = &this->new_clip_distance_in_var; } if (new_var == NULL) return NULL; if (ir->as_dereference_variable()) { return new(ralloc_parent(ir)) ir_dereference_variable(*new_var); } else { ir_dereference_array *array_ref = ir->as_dereference_array(); assert(array_ref); assert(array_ref->array->as_dereference_variable()); return new(ralloc_parent(ir)) ir_dereference_array(*new_var, array_ref->array_index); } }
/** * If the given ir satisfies is_clip_distance_vec8(), return new ir * representing its lowered equivalent. That is, map: * * - gl_ClipDistance => gl_ClipDistanceMESA (if gl_ClipDistance is 1D) * - gl_ClipDistance[i] => gl_ClipDistanceMESA[i] (if gl_ClipDistance is 2D) * * Otherwise return NULL. */ ir_rvalue * lower_clip_distance_visitor::lower_clip_distance_vec8(ir_rvalue *ir) { if (this->old_clip_distance_1d_var) { ir_dereference_variable *var_ref = ir->as_dereference_variable(); if (var_ref && var_ref->var == this->old_clip_distance_1d_var) { return new(ralloc_parent(ir)) ir_dereference_variable(this->new_clip_distance_1d_var); } } if (this->old_clip_distance_2d_var) { /* 2D clip distance is only possible as a geometry input */ assert(this->shader_stage == MESA_SHADER_GEOMETRY); ir_dereference_array *array_ref = ir->as_dereference_array(); if (array_ref) { ir_dereference_variable *var_ref = array_ref->array->as_dereference_variable(); if (var_ref && var_ref->var == this->old_clip_distance_2d_var) { return new(ralloc_parent(ir)) ir_dereference_array(this->new_clip_distance_2d_var, array_ref->array_index); } } } return NULL; }
void ir_coalesce_floats_replacing_visitor::handle_rvalue(ir_rvalue **rvalue) { ir_rvalue *ir = *rvalue; if(!ir) return; ir_dereference *dv; ir_swizzle *swiz; ir_swizzle *outerswiz = ir->as_swizzle(); if( outerswiz ) { dv = outerswiz->val->as_dereference(); if(dv) { //fprintf(stderr, "var1: %s\n", dv->variable_referenced()->name); swiz = (ir_swizzle*)hash_table_find(promotions, dv->variable_referenced()); if(swiz) { //fprintf(stderr, "// replacing[1] %p %p\n", ir, swiz); *rvalue = swiz->clone(ralloc_parent(swiz), NULL); return; } } } dv = ir->as_dereference(); if(!dv) return; swiz = (ir_swizzle*)hash_table_find(promotions, dv->variable_referenced()); //fprintf(stderr, "var2: %s %p\n", dv->variable_referenced()->name, dv->variable_referenced()); if (!swiz) return; //fprintf(stderr, "// replacing[2] %p %p\n", ir, swiz); *rvalue = swiz->clone(ralloc_parent(swiz), NULL); }
/** * Replace any declaration of gl_TessLevel* as an array of floats with a * declaration of gl_TessLevel*MESA as a vec4. */ ir_visitor_status lower_tess_level_visitor::visit(ir_variable *ir) { if ((!ir->name) || ((strcmp(ir->name, "gl_TessLevelInner") != 0) && (strcmp(ir->name, "gl_TessLevelOuter") != 0))) return visit_continue; assert (ir->type->is_array()); if (strcmp(ir->name, "gl_TessLevelOuter") == 0) { if (this->old_tess_level_outer_var) return visit_continue; old_tess_level_outer_var = ir; assert(ir->type->fields.array == glsl_type::float_type); /* Clone the old var so that we inherit all of its properties */ new_tess_level_outer_var = ir->clone(ralloc_parent(ir), NULL); /* And change the properties that we need to change */ new_tess_level_outer_var->name = ralloc_strdup(new_tess_level_outer_var, "gl_TessLevelOuterMESA"); new_tess_level_outer_var->type = glsl_type::vec4_type; new_tess_level_outer_var->data.max_array_access = 0; ir->replace_with(new_tess_level_outer_var); } else if (strcmp(ir->name, "gl_TessLevelInner") == 0) { if (this->old_tess_level_inner_var) return visit_continue; old_tess_level_inner_var = ir; assert(ir->type->fields.array == glsl_type::float_type); /* Clone the old var so that we inherit all of its properties */ new_tess_level_inner_var = ir->clone(ralloc_parent(ir), NULL); /* And change the properties that we need to change */ new_tess_level_inner_var->name = ralloc_strdup(new_tess_level_inner_var, "gl_TessLevelInnerMESA"); new_tess_level_inner_var->type = glsl_type::vec2_type; new_tess_level_inner_var->data.max_array_access = 0; ir->replace_with(new_tess_level_inner_var); } else { assert(0); } this->progress = true; return visit_continue; }
/** * Replace any declaration of gl_ClipDistance as an array of floats with a * declaration of gl_ClipDistanceMESA as an array of vec4's. */ ir_visitor_status lower_clip_distance_visitor::visit(ir_variable *ir) { /* No point in looking for the declaration of gl_ClipDistance if * we've already found it. */ if (this->old_clip_distance_var) return visit_continue; if (ir->name && strcmp(ir->name, "gl_ClipDistance") == 0) { this->progress = true; this->old_clip_distance_var = ir; assert (ir->type->is_array()); assert (ir->type->element_type() == glsl_type::float_type); unsigned new_size = (ir->type->array_size() + 3) / 4; /* Clone the old var so that we inherit all of its properties */ this->new_clip_distance_var = ir->clone(ralloc_parent(ir), NULL); /* And change the properties that we need to change */ this->new_clip_distance_var->name = ralloc_strdup(this->new_clip_distance_var, "gl_ClipDistanceMESA"); this->new_clip_distance_var->type = glsl_type::get_array_instance(glsl_type::vec4_type, new_size); this->new_clip_distance_var->max_array_access = ir->max_array_access / 4; ir->replace_with(this->new_clip_distance_var); } return visit_continue; }
ir_visitor_status output_read_remover::visit(ir_dereference_variable *ir) { if (ir->var->data.mode != ir_var_shader_out) return visit_continue; if (stage == MESA_SHADER_TESS_CTRL) return visit_continue; hash_entry *entry = _mesa_hash_table_search(replacements, ir->var); ir_variable *temp = entry ? (ir_variable *) entry->data : NULL; /* If we don't have an existing temporary, create one. */ if (temp == NULL) { void *var_ctx = ralloc_parent(ir->var); temp = new(var_ctx) ir_variable(ir->var->type, ir->var->name, ir_var_temporary); _mesa_hash_table_insert(replacements, ir->var, temp); ir->var->insert_after(temp); } /* Update the dereference to use the temporary */ ir->var = temp; return visit_continue; }
static bool opt_undef_alu(nir_alu_instr *instr) { if (instr->op != nir_op_bcsel && instr->op != nir_op_fcsel) return false; assert(instr->dest.dest.is_ssa); for (int i = 1; i <= 2; i++) { if (!instr->src[i].src.is_ssa) continue; nir_instr *parent = instr->src[i].src.ssa->parent_instr; if (parent->type != nir_instr_type_ssa_undef) continue; /* We can't just use nir_alu_src_copy, because we need the def/use * updated. */ nir_instr_rewrite_src(&instr->instr, &instr->src[0].src, instr->src[i == 1 ? 2 : 1].src); nir_alu_src_copy(&instr->src[0], &instr->src[i == 1 ? 2 : 1], ralloc_parent(instr)); nir_src empty_src; memset(&empty_src, 0, sizeof(empty_src)); nir_instr_rewrite_src(&instr->instr, &instr->src[1].src, empty_src); nir_instr_rewrite_src(&instr->instr, &instr->src[2].src, empty_src); instr->op = nir_op_imov; return true; } return false; }
ir_rvalue * ir_vec_index_to_swizzle_visitor::convert_vector_extract_to_swizzle(ir_rvalue *ir) { ir_expression *const expr = ir->as_expression(); if (expr == NULL || expr->operation != ir_binop_vector_extract) return ir; ir_constant *const idx = expr->operands[1]->constant_expression_value(); if (idx == NULL) return ir; void *ctx = ralloc_parent(ir); this->progress = true; /* Page 40 of the GLSL 1.20 spec says: * * "When indexing with non-constant expressions, behavior is undefined * if the index is negative, or greater than or equal to the size of * the vector." * * The quoted spec text mentions non-constant expressions, but this code * operates on constants. These constants are the result of non-constant * expressions that have been optimized to constants. The common case here * is a loop counter from an unrolled loop that is used to index a vector. * * The ir_swizzle constructor gets angry if the index is negative or too * large. For simplicity sake, just clamp the index to [0, size-1]. */ const int i = CLAMP(idx->value.i[0], 0, (int) expr->operands[0]->type->vector_elements - 1); return new(ctx) ir_swizzle(expr->operands[0], i, 0, 0, 0, 1); }
void generate(unsigned i, ir_rvalue* condition, exec_list *list) const { /* Just clone the rest of the deref chain when trying to get at the * underlying variable. */ void *mem_ctx = ralloc_parent(base_ir); /* Clone the old r-value in its entirety. Then replace any occurances of * the old variable index with the new constant index. */ ir_dereference *element = this->rvalue->clone(mem_ctx, NULL); ir_constant *const index = new(mem_ctx)ir_constant(i); deref_replacer r(this->old_index, index); element->accept(&r); check(r.progress); /* Generate a conditional assignment to (or from) the constant indexed * array dereference. */ ir_rvalue *variable = new(mem_ctx)ir_dereference_variable(this->var); ir_assignment *const assignment = (is_write) ? new(mem_ctx)ir_assignment(element, variable, condition, write_mask) : new(mem_ctx)ir_assignment(variable, element, condition); list->push_tail(assignment); }
/** * If a 1D gl_ClipDistance variable appears as an argument in an ir_call * expression, replace it with a temporary variable, and make sure the ir_call * is preceded and/or followed by assignments that copy the contents of the * temporary variable to and/or from gl_ClipDistance. Each of these * assignments is then lowered to refer to gl_ClipDistanceMESA. * * We need to do a similar replacement for 2D gl_ClipDistance, however since * it's an input, the only case we need to address is where a 1D slice of it * is passed as an "in" parameter to an ir_call, e.g.: * * foo(gl_in[i].gl_ClipDistance) */ ir_visitor_status lower_clip_distance_visitor::visit_leave(ir_call *ir) { void *ctx = ralloc_parent(ir); const exec_node *formal_param_node = ir->callee->parameters.head; const exec_node *actual_param_node = ir->actual_parameters.head; while (!actual_param_node->is_tail_sentinel()) { ir_variable *formal_param = (ir_variable *) formal_param_node; ir_rvalue *actual_param = (ir_rvalue *) actual_param_node; /* Advance formal_param_node and actual_param_node now so that we can * safely replace actual_param with another node, if necessary, below. */ formal_param_node = formal_param_node->next; actual_param_node = actual_param_node->next; if (this->is_clip_distance_vec8(actual_param)) { /* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D * slice of a 2D gl_ClipDistance array) to a function call. Since we * are reshaping gl_ClipDistance from an array of floats to an array * of vec4's, this isn't going to work anymore, so use a temporary * array instead. */ ir_variable *temp_clip_distance = new(ctx) ir_variable( actual_param->type, "temp_clip_distance", ir_var_temporary, actual_param->get_precision()); this->base_ir->insert_before(temp_clip_distance); actual_param->replace_with( new(ctx) ir_dereference_variable(temp_clip_distance)); if (formal_param->data.mode == ir_var_function_in || formal_param->data.mode == ir_var_function_inout) { /* Copy from gl_ClipDistance to the temporary before the call. * Since we are going to insert this copy before the current * instruction, we need to visit it afterwards to make sure it * gets lowered. */ ir_assignment *new_assignment = new(ctx) ir_assignment( new(ctx) ir_dereference_variable(temp_clip_distance), actual_param->clone(ctx, NULL)); this->base_ir->insert_before(new_assignment); this->visit_new_assignment(new_assignment); } if (formal_param->data.mode == ir_var_function_out || formal_param->data.mode == ir_var_function_inout) { /* Copy from the temporary to gl_ClipDistance after the call. * Since visit_list_elements() has already decided which * instruction it's going to visit next, we need to visit * afterwards to make sure it gets lowered. */ ir_assignment *new_assignment = new(ctx) ir_assignment( actual_param->clone(ctx, NULL), new(ctx) ir_dereference_variable(temp_clip_distance)); this->base_ir->insert_after(new_assignment); this->visit_new_assignment(new_assignment); } } } return rvalue_visit(ir); }
void lower_tess_level_visitor::handle_rvalue(ir_rvalue **rv) { if (*rv == NULL) return; ir_dereference_array *const array_deref = (*rv)->as_dereference_array(); if (array_deref == NULL) return; /* Replace any expression that indexes one of the floats in gl_TessLevel* * with an expression that indexes into one of the vec4's * gl_TessLevel*MESA and accesses the appropriate component. */ ir_rvalue *lowered_vec4 = this->lower_tess_level_array(array_deref->array); if (lowered_vec4 != NULL) { this->progress = true; void *mem_ctx = ralloc_parent(array_deref); ir_expression *const expr = new(mem_ctx) ir_expression(ir_binop_vector_extract, lowered_vec4, array_deref->array_index); *rv = expr; } }
void lower_clip_distance_visitor::handle_rvalue(ir_rvalue **rv) { if (*rv == NULL) return; ir_dereference_array *const array_deref = (*rv)->as_dereference_array(); if (array_deref == NULL) return; /* Replace any expression that indexes one of the floats in gl_ClipDistance * with an expression that indexes into one of the vec4's in * gl_ClipDistanceMESA and accesses the appropriate component. */ ir_rvalue *lowered_vec8 = this->lower_clip_distance_vec8(array_deref->array); if (lowered_vec8 != NULL) { this->progress = true; ir_rvalue *array_index; ir_rvalue *swizzle_index; this->create_indices(array_deref->array_index, array_index, swizzle_index); void *mem_ctx = ralloc_parent(array_deref); ir_dereference_array *const new_array_deref = new(mem_ctx) ir_dereference_array(lowered_vec8, array_index); ir_expression *const expr = new(mem_ctx) ir_expression(ir_binop_vector_extract, new_array_deref, swizzle_index); *rv = expr; } }
ir_expression * expr(ir_expression_operation op, operand a, operand b) { void *mem_ctx = ralloc_parent(a.val); return new(mem_ctx) ir_expression(op, a.val, b.val); }
void lower_clip_distance_visitor::fix_lhs(ir_assignment *ir) { if (ir->lhs->ir_type == ir_type_expression) { void *mem_ctx = ralloc_parent(ir); ir_expression *const expr = (ir_expression *) ir->lhs; /* The expression must be of the form: * * (vector_extract gl_ClipDistanceMESA[i], j). */ assert(expr->operation == ir_binop_vector_extract); assert(expr->operands[0]->ir_type == ir_type_dereference_array); assert(expr->operands[0]->type == glsl_type::vec4_type); ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0]; ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert, glsl_type::vec4_type, new_lhs->clone(mem_ctx, NULL), ir->rhs, expr->operands[1]); ir->set_lhs(new_lhs); ir->write_mask = WRITEMASK_XYZW; } }
/** * If the given ir satisfies is_tess_level_array(), return new ir * representing its lowered equivalent. That is, map: * * - gl_TessLevelOuter => gl_TessLevelOuterMESA * - gl_TessLevelInner => gl_TessLevelInnerMESA * * Otherwise return NULL. */ ir_rvalue * lower_tess_level_visitor::lower_tess_level_array(ir_rvalue *ir) { if (!ir->type->is_array()) return NULL; if (ir->type->fields.array != glsl_type::float_type) return NULL; ir_variable **new_var = NULL; if (this->old_tess_level_outer_var) { if (ir->variable_referenced() == this->old_tess_level_outer_var) new_var = &this->new_tess_level_outer_var; } if (this->old_tess_level_inner_var) { if (ir->variable_referenced() == this->old_tess_level_inner_var) new_var = &this->new_tess_level_inner_var; } if (new_var == NULL) return NULL; assert(ir->as_dereference_variable()); return new(ralloc_parent(ir)) ir_dereference_variable(*new_var); }
static void validate_deref_chain(nir_deref *deref, validate_state *state) { assert(deref->child == NULL || ralloc_parent(deref->child) == deref); nir_deref *parent = NULL; while (deref != NULL) { switch (deref->deref_type) { case nir_deref_type_array: assert(deref->type == glsl_get_array_element(parent->type)); if (nir_deref_as_array(deref)->deref_array_type == nir_deref_array_type_indirect) validate_src(&nir_deref_as_array(deref)->indirect, state); break; case nir_deref_type_struct: assert(deref->type == glsl_get_struct_field(parent->type, nir_deref_as_struct(deref)->index)); break; case nir_deref_type_var: break; default: assert(!"Invalid deref type"); break; } parent = deref; deref = deref->child; } }
ir_expression * bitfield_insert(operand a, operand b, operand c, operand d) { void *mem_ctx = ralloc_parent(a.val); return new(mem_ctx) ir_expression(ir_quadop_bitfield_insert, a.val->type, a.val, b.val, c.val, d.val); }
/** * Replace any assignment having gl_ClipDistance (undereferenced) as its LHS * or RHS with a sequence of assignments, one for each component of the array. * Each of these assignments is lowered to refer to gl_ClipDistanceMESA as * appropriate. */ ir_visitor_status lower_clip_distance_visitor::visit_leave(ir_assignment *ir) { ir_dereference_variable *lhs_var = ir->lhs->as_dereference_variable(); ir_dereference_variable *rhs_var = ir->rhs->as_dereference_variable(); if ((lhs_var && lhs_var->var == this->old_clip_distance_var) || (rhs_var && rhs_var->var == this->old_clip_distance_var)) { /* LHS or RHS of the assignment is the entire gl_ClipDistance array. * Since we are reshaping gl_ClipDistance from an array of floats to an * array of vec4's, this isn't going to work as a bulk assignment * anymore, so unroll it to element-by-element assignments and lower * each of them. * * Note: to unroll into element-by-element assignments, we need to make * clones of the LHS and RHS. This is safe because expressions and * l-values are side-effect free. */ void *ctx = ralloc_parent(ir); int array_size = this->old_clip_distance_var->type->array_size(); for (int i = 0; i < array_size; ++i) { ir_dereference_array *new_lhs = new(ctx) ir_dereference_array( ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i)); ir_dereference_array *new_rhs = new(ctx) ir_dereference_array( ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i)); this->handle_rvalue((ir_rvalue **) &new_rhs); /* Handle the LHS after creating the new assignment. This must * happen in this order because handle_rvalue may replace the old LHS * with an ir_expression of ir_binop_vector_extract. Since this is * not a valide l-value, this will cause an assertion in the * ir_assignment constructor to fail. * * If this occurs, replace the mangled LHS with a dereference of the * vector, and replace the RHS with an ir_triop_vector_insert. */ ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs); this->handle_rvalue((ir_rvalue **) &assign->lhs); this->fix_lhs(assign); this->base_ir->insert_before(assign); } ir->remove(); return visit_continue; } /* Handle the LHS as if it were an r-value. Normally * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower * expressions in the LHS as well. * * This may cause the LHS to get replaced with an ir_expression of * ir_binop_vector_extract. If this occurs, replace it with a dereference * of the vector, and replace the RHS with an ir_triop_vector_insert. */ handle_rvalue((ir_rvalue **)&ir->lhs); this->fix_lhs(ir); return rvalue_visit(ir); }
ir_expression * saturate(operand a) { void *mem_ctx = ralloc_parent(a.val); return expr(ir_binop_max, expr(ir_binop_min, a, new(mem_ctx) ir_constant(1.0f)), new(mem_ctx) ir_constant(0.0f)); }
static bool nir_opt_constant_folding_impl(nir_function_impl *impl) { void *mem_ctx = ralloc_parent(impl); bool progress = false; nir_foreach_block(block, impl) { progress |= constant_fold_block(block, mem_ctx); }
virtual void handle_rvalue(ir_rvalue **rvalue) { ir_dereference_variable *const dv = (*rvalue)->as_dereference_variable(); if ((dv != NULL) && (dv->var == this->variable_to_replace)) { this->progress = true; *rvalue = this->value->clone(ralloc_parent(*rvalue), NULL); } }
ir_assignment * assign(deref lhs, operand rhs, int writemask) { void *mem_ctx = ralloc_parent(lhs.val); ir_assignment *assign = new(mem_ctx) ir_assignment(lhs.val, rhs.val, NULL, writemask); return assign; }
loop_terminator * loop_variable_state::insert(ir_if *if_stmt) { void *mem_ctx = ralloc_parent(this); loop_terminator *t = new(mem_ctx) loop_terminator(); t->ir = if_stmt; this->terminators.push_tail(t); return t; }
static void validate_deref_var(void *parent_mem_ctx, nir_deref_var *deref, validate_state *state) { assert(deref != NULL); assert(ralloc_parent(deref) == parent_mem_ctx); assert(deref->deref.type == deref->var->type); validate_var_use(deref->var, state); validate_deref_chain(&deref->deref, state); }
static void nir_lower_io_impl(nir_function_impl *impl) { struct lower_io_state state; state.mem_ctx = ralloc_parent(impl); nir_foreach_block(impl, nir_lower_io_block, &state); nir_metadata_preserve(impl, nir_metadata_block_index | nir_metadata_dominance); }
static void lower_impl(nir_function_impl *impl, struct gl_shader_program *shader_program, struct gl_program *prog) { lower_state state; state.mem_ctx = ralloc_parent(impl); state.shader_program = shader_program; state.prog = prog; nir_foreach_block(impl, lower_block_cb, &state); }
ir_if* if_tree(operand condition, ir_instruction *then_branch) { assert(then_branch != NULL); void *mem_ctx = ralloc_parent(condition.val); ir_if *result = new(mem_ctx) ir_if(condition.val); result->then_instructions.push_tail(then_branch); return result; }
ir_swizzle * swizzle(operand a, int swizzle, int components) { void *mem_ctx = ralloc_parent(a.val); return new(mem_ctx) ir_swizzle(a.val, GET_SWZ(swizzle, 0), GET_SWZ(swizzle, 1), GET_SWZ(swizzle, 2), GET_SWZ(swizzle, 3), components); }
loop_variable * loop_variable_state::insert(ir_variable *var) { void *mem_ctx = ralloc_parent(this); loop_variable *lv = rzalloc(mem_ctx, loop_variable); lv->var = var; _mesa_hash_table_insert(this->var_hash, lv->var, lv); this->variables.push_tail(lv); return lv; }