Mesa (main): glsl: Move exp/log-to-exp2/log2 lowering to glsl-to-NIR.

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Tue Jun 7 03:04:35 UTC 2022


Module: Mesa
Branch: main
Commit: 0fbd1b1f4ce0322ec6af96d17bb599b2a74baac9
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=0fbd1b1f4ce0322ec6af96d17bb599b2a74baac9

Author: Emma Anholt <emma at anholt.net>
Date:   Tue May 31 13:36:15 2022 -0700

glsl: Move exp/log-to-exp2/log2 lowering to glsl-to-NIR.

It's way more concise to write as nir_builder calls.

Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig at collabora.com>
Reviewed-by: Timothy Arceri <tarceri at itsqueeze.com>
Reviewed-by: Erik Faye-Lund <erik.faye-lund at collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16823>

---

 src/compiler/glsl/glsl_to_nir.cpp        |  4 +--
 src/compiler/glsl/ir_optimization.h      |  2 --
 src/compiler/glsl/lower_instructions.cpp | 62 --------------------------------
 src/mesa/state_tracker/st_glsl_to_ir.cpp |  2 --
 4 files changed, 2 insertions(+), 68 deletions(-)

diff --git a/src/compiler/glsl/glsl_to_nir.cpp b/src/compiler/glsl/glsl_to_nir.cpp
index 536bedd316b..165b827110f 100644
--- a/src/compiler/glsl/glsl_to_nir.cpp
+++ b/src/compiler/glsl/glsl_to_nir.cpp
@@ -1973,8 +1973,8 @@ nir_visitor::visit(ir_expression *ir)
    case ir_unop_rcp:  result = nir_frcp(&b, srcs[0]);  break;
    case ir_unop_rsq:  result = nir_frsq(&b, srcs[0]);  break;
    case ir_unop_sqrt: result = nir_fsqrt(&b, srcs[0]); break;
-   case ir_unop_exp:  unreachable("ir_unop_exp should have been lowered");
-   case ir_unop_log:  unreachable("ir_unop_log should have been lowered");
+   case ir_unop_exp:  result = nir_fexp2(&b, nir_fmul_imm(&b, srcs[0], M_LOG2E)); break;
+   case ir_unop_log:  result = nir_fmul_imm(&b, nir_flog2(&b, srcs[0]), 1.0 / M_LOG2E); break;
    case ir_unop_exp2: result = nir_fexp2(&b, srcs[0]); break;
    case ir_unop_log2: result = nir_flog2(&b, srcs[0]); break;
    case ir_unop_i2f:
diff --git a/src/compiler/glsl/ir_optimization.h b/src/compiler/glsl/ir_optimization.h
index 81dc2719dfd..95e462fe37d 100644
--- a/src/compiler/glsl/ir_optimization.h
+++ b/src/compiler/glsl/ir_optimization.h
@@ -36,8 +36,6 @@ struct gl_shader_program;
 /* Operations for lower_instructions() */
 #define SUB_TO_ADD_NEG     0x01
 #define FDIV_TO_MUL_RCP    0x02
-#define EXP_TO_EXP2        0x04
-#define LOG_TO_LOG2        0x10
 #define INT_DIV_TO_MUL_RCP 0x40
 #define LDEXP_TO_ARITH     0x80
 #define CARRY_TO_ARITH     0x100
diff --git a/src/compiler/glsl/lower_instructions.cpp b/src/compiler/glsl/lower_instructions.cpp
index 46f60b92e70..dfae74bc3fc 100644
--- a/src/compiler/glsl/lower_instructions.cpp
+++ b/src/compiler/glsl/lower_instructions.cpp
@@ -33,8 +33,6 @@
  * - SUB_TO_ADD_NEG
  * - DIV_TO_MUL_RCP
  * - INT_DIV_TO_MUL_RCP
- * - EXP_TO_EXP2
- * - LOG_TO_LOG2
  * - LDEXP_TO_ARITH
  * - CARRY_TO_ARITH
  * - BORROW_TO_ARITH
@@ -66,12 +64,6 @@
  * INT_DIV_TO_MUL_RCP handles the integer case, converting to and from floating
  * point so that RCP is possible.
  *
- * EXP_TO_EXP2 and LOG_TO_LOG2:
- * ----------------------------
- * Many GPUs don't have a base e log or exponent instruction, but they
- * do have base 2 versions, so this pass converts exp and log to exp2
- * and log2 operations.
- *
  * LDEXP_TO_ARITH:
  * -------------
  * Converts ir_binop_ldexp to arithmetic and bit operations for float sources.
@@ -122,8 +114,6 @@ private:
    void sub_to_add_neg(ir_expression *);
    void div_to_mul_rcp(ir_expression *);
    void int_div_to_mul_rcp(ir_expression *);
-   void exp_to_exp2(ir_expression *);
-   void log_to_log2(ir_expression *);
    void ldexp_to_arith(ir_expression *);
    void dldexp_to_arith(ir_expression *);
    void dfrexp_sig_to_arith(ir_expression *);
@@ -250,29 +240,6 @@ lower_instructions_visitor::int_div_to_mul_rcp(ir_expression *ir)
    this->progress = true;
 }
 
-void
-lower_instructions_visitor::exp_to_exp2(ir_expression *ir)
-{
-   ir_constant *log2_e = _imm_fp(ir, ir->type, M_LOG2E);
-
-   ir->operation = ir_unop_exp2;
-   ir->init_num_operands();
-   ir->operands[0] = new(ir) ir_expression(ir_binop_mul, ir->operands[0]->type,
-					   ir->operands[0], log2_e);
-   this->progress = true;
-}
-
-void
-lower_instructions_visitor::log_to_log2(ir_expression *ir)
-{
-   ir->operation = ir_binop_mul;
-   ir->init_num_operands();
-   ir->operands[0] = new(ir) ir_expression(ir_unop_log2, ir->operands[0]->type,
-					   ir->operands[0], NULL);
-   ir->operands[1] = _imm_fp(ir, ir->operands[0]->type, 1.0 / M_LOG2E);
-   this->progress = true;
-}
-
 void
 lower_instructions_visitor::ldexp_to_arith(ir_expression *ir)
 {
@@ -1411,25 +1378,6 @@ lower_instructions_visitor::_carry(operand a, operand b)
       return carry(a, b);
 }
 
-ir_constant *
-lower_instructions_visitor::_imm_fp(void *mem_ctx,
-                                    const glsl_type *type,
-                                    double f,
-                                    unsigned vector_elements)
-{
-   switch (type->base_type) {
-   case GLSL_TYPE_FLOAT:
-      return new(mem_ctx) ir_constant((float) f, vector_elements);
-   case GLSL_TYPE_DOUBLE:
-      return new(mem_ctx) ir_constant((double) f, vector_elements);
-   case GLSL_TYPE_FLOAT16:
-      return new(mem_ctx) ir_constant(float16_t(f), vector_elements);
-   default:
-      assert(!"unknown float type for immediate");
-      return NULL;
-   }
-}
-
 void
 lower_instructions_visitor::imul_high_to_mul(ir_expression *ir)
 {
@@ -1607,16 +1555,6 @@ lower_instructions_visitor::visit_leave(ir_expression *ir)
 	 div_to_mul_rcp(ir);
       break;
 
-   case ir_unop_exp:
-      if (lowering(EXP_TO_EXP2))
-	 exp_to_exp2(ir);
-      break;
-
-   case ir_unop_log:
-      if (lowering(LOG_TO_LOG2))
-	 log_to_log2(ir);
-      break;
-
    case ir_binop_ldexp:
       if (lowering(LDEXP_TO_ARITH) && ir->type->is_float())
          ldexp_to_arith(ir);
diff --git a/src/mesa/state_tracker/st_glsl_to_ir.cpp b/src/mesa/state_tracker/st_glsl_to_ir.cpp
index d8242114c9b..83e53226aa6 100644
--- a/src/mesa/state_tracker/st_glsl_to_ir.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_ir.cpp
@@ -104,8 +104,6 @@ link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
 
       lower_instructions(ir,
                          FDIV_TO_MUL_RCP |
-                         EXP_TO_EXP2 |
-                         LOG_TO_LOG2 |
                          (have_ldexp ? 0 : LDEXP_TO_ARITH) |
                          (have_dfrexp ? 0 : DFREXP_DLDEXP_TO_ARITH) |
                          CARRY_TO_ARITH |



More information about the mesa-commit mailing list