From a8b0b6e52b095f03c96a72394d15327c42512815 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Daniel=20Sch=C3=BCrmann?= Date: Thu, 13 Jun 2019 11:34:01 +0200 Subject: [PATCH] nir: introduce lowering of bitfield_insert to bfm and a new opcode bitfield_select. bitfield_select is defined as: bitfield_select(mask, base, insert) = (mask & base) | (~mask & insert) matching the behavior of AMD's BFI instruction. Reviewed-by: Connor Abbott --- src/compiler/nir/nir.h | 2 ++ src/compiler/nir/nir_opcodes.py | 3 +++ src/compiler/nir/nir_opt_algebraic.py | 6 ++++++ 3 files changed, 11 insertions(+) diff --git a/src/compiler/nir/nir.h b/src/compiler/nir/nir.h index 9feed169e47..bc9122d1f25 100644 --- a/src/compiler/nir/nir.h +++ b/src/compiler/nir/nir.h @@ -2288,6 +2288,8 @@ typedef struct nir_shader_compiler_options { bool lower_bitfield_insert; /** Lowers bitfield_insert to compares, and shifts. */ bool lower_bitfield_insert_to_shifts; + /** Lowers bitfield_insert to bfm/bitfield_select. */ + bool lower_bitfield_insert_to_bitfield_select; /** Lowers bitfield_reverse to shifts. */ bool lower_bitfield_reverse; /** Lowers bit_count to shifts. */ diff --git a/src/compiler/nir/nir_opcodes.py b/src/compiler/nir/nir_opcodes.py index 8771c74b033..a12b0269e2e 100644 --- a/src/compiler/nir/nir_opcodes.py +++ b/src/compiler/nir/nir_opcodes.py @@ -871,6 +871,9 @@ if (mask == 0) { } """) + +triop("bitfield_select", tuint, "", "(src0 & src1) | (~src0 & src2)") + # SM5 ubfe/ibfe assembly: only the 5 least significant bits of offset and bits are used. opcode("ubfe", 0, tuint32, [0, 0, 0], [tuint32, tuint32, tuint32], False, "", """ diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 0d21696273a..4147be9c1f7 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -799,6 +799,12 @@ optimizations.extend([ ('iand', ('ishl', 'insert', 'offset'), ('ishl', ('isub', ('ishl', 1, 'bits'), 1), 'offset'))), 'options->lower_bitfield_insert_to_shifts'), + # Alternative lowering that uses bitfield_select. + (('bitfield_insert', 'base', 'insert', 'offset', 'bits'), + ('bcsel', ('ult', 31, 'bits'), 'insert', + ('bitfield_select', ('bfm', 'bits', 'offset'), ('ishl', 'insert', 'offset'), 'base')), + 'options->lower_bitfield_insert_to_bitfield_select'), + (('ibitfield_extract', 'value', 'offset', 'bits'), ('bcsel', ('ult', 31, 'bits'), 'value', ('ibfe', 'value', 'offset', 'bits')), -- 2.30.2