1b8e80941Smrg/* 2b8e80941Smrg * Copyright © 2015 Intel Corporation 3b8e80941Smrg * 4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5b8e80941Smrg * copy of this software and associated documentation files (the "Software"), 6b8e80941Smrg * to deal in the Software without restriction, including without limitation 7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the 9b8e80941Smrg * Software is furnished to do so, subject to the following conditions: 10b8e80941Smrg * 11b8e80941Smrg * The above copyright notice and this permission notice (including the next 12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the 13b8e80941Smrg * Software. 14b8e80941Smrg * 15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21b8e80941Smrg * IN THE SOFTWARE. 22b8e80941Smrg * 23b8e80941Smrg */ 24b8e80941Smrg 25b8e80941Smrg#include "nir.h" 26b8e80941Smrg#include "nir_builder.h" 27b8e80941Smrg 28b8e80941Smrg/* 29b8e80941Smrg * lowers: 30b8e80941Smrg * 31b8e80941Smrg * packDouble2x32(foo) -> packDouble2x32Split(foo.x, foo.y) 32b8e80941Smrg * unpackDouble2x32(foo) -> vec2(unpackDouble2x32_x(foo), unpackDouble2x32_y(foo)) 33b8e80941Smrg * packInt2x32(foo) -> packInt2x32Split(foo.x, foo.y) 34b8e80941Smrg * unpackInt2x32(foo) -> vec2(unpackInt2x32_x(foo), unpackInt2x32_y(foo)) 35b8e80941Smrg */ 36b8e80941Smrg 37b8e80941Smrgstatic nir_ssa_def * 38b8e80941Smrglower_pack_64_from_32(nir_builder *b, nir_ssa_def *src) 39b8e80941Smrg{ 40b8e80941Smrg return nir_pack_64_2x32_split(b, nir_channel(b, src, 0), 41b8e80941Smrg nir_channel(b, src, 1)); 42b8e80941Smrg} 43b8e80941Smrg 44b8e80941Smrgstatic nir_ssa_def * 45b8e80941Smrglower_unpack_64_to_32(nir_builder *b, nir_ssa_def *src) 46b8e80941Smrg{ 47b8e80941Smrg return nir_vec2(b, nir_unpack_64_2x32_split_x(b, src), 48b8e80941Smrg nir_unpack_64_2x32_split_y(b, src)); 49b8e80941Smrg} 50b8e80941Smrg 51b8e80941Smrgstatic nir_ssa_def * 52b8e80941Smrglower_pack_32_from_16(nir_builder *b, nir_ssa_def *src) 53b8e80941Smrg{ 54b8e80941Smrg return nir_pack_32_2x16_split(b, nir_channel(b, src, 0), 55b8e80941Smrg nir_channel(b, src, 1)); 56b8e80941Smrg} 57b8e80941Smrg 58b8e80941Smrgstatic nir_ssa_def * 59b8e80941Smrglower_unpack_32_to_16(nir_builder *b, nir_ssa_def *src) 60b8e80941Smrg{ 61b8e80941Smrg return nir_vec2(b, nir_unpack_32_2x16_split_x(b, src), 62b8e80941Smrg nir_unpack_32_2x16_split_y(b, src)); 63b8e80941Smrg} 64b8e80941Smrg 65b8e80941Smrgstatic nir_ssa_def * 66b8e80941Smrglower_pack_64_from_16(nir_builder *b, nir_ssa_def *src) 67b8e80941Smrg{ 68b8e80941Smrg nir_ssa_def *xy = nir_pack_32_2x16_split(b, nir_channel(b, src, 0), 69b8e80941Smrg nir_channel(b, src, 1)); 70b8e80941Smrg 71b8e80941Smrg nir_ssa_def *zw = nir_pack_32_2x16_split(b, nir_channel(b, src, 2), 72b8e80941Smrg nir_channel(b, src, 3)); 73b8e80941Smrg 74b8e80941Smrg return nir_pack_64_2x32_split(b, xy, zw); 75b8e80941Smrg} 76b8e80941Smrg 77b8e80941Smrgstatic nir_ssa_def * 78b8e80941Smrglower_unpack_64_to_16(nir_builder *b, nir_ssa_def *src) 79b8e80941Smrg{ 80b8e80941Smrg nir_ssa_def *xy = nir_unpack_64_2x32_split_x(b, src); 81b8e80941Smrg nir_ssa_def *zw = nir_unpack_64_2x32_split_y(b, src); 82b8e80941Smrg 83b8e80941Smrg return nir_vec4(b, nir_unpack_32_2x16_split_x(b, xy), 84b8e80941Smrg nir_unpack_32_2x16_split_y(b, xy), 85b8e80941Smrg nir_unpack_32_2x16_split_x(b, zw), 86b8e80941Smrg nir_unpack_32_2x16_split_y(b, zw)); 87b8e80941Smrg} 88b8e80941Smrg 89b8e80941Smrgstatic bool 90b8e80941Smrglower_pack_impl(nir_function_impl *impl) 91b8e80941Smrg{ 92b8e80941Smrg nir_builder b; 93b8e80941Smrg nir_builder_init(&b, impl); 94b8e80941Smrg bool progress = false; 95b8e80941Smrg 96b8e80941Smrg nir_foreach_block(block, impl) { 97b8e80941Smrg nir_foreach_instr_safe(instr, block) { 98b8e80941Smrg if (instr->type != nir_instr_type_alu) 99b8e80941Smrg continue; 100b8e80941Smrg 101b8e80941Smrg nir_alu_instr *alu_instr = (nir_alu_instr *) instr; 102b8e80941Smrg 103b8e80941Smrg if (alu_instr->op != nir_op_pack_64_2x32 && 104b8e80941Smrg alu_instr->op != nir_op_unpack_64_2x32 && 105b8e80941Smrg alu_instr->op != nir_op_pack_64_4x16 && 106b8e80941Smrg alu_instr->op != nir_op_unpack_64_4x16 && 107b8e80941Smrg alu_instr->op != nir_op_pack_32_2x16 && 108b8e80941Smrg alu_instr->op != nir_op_unpack_32_2x16) 109b8e80941Smrg continue; 110b8e80941Smrg 111b8e80941Smrg b.cursor = nir_before_instr(&alu_instr->instr); 112b8e80941Smrg 113b8e80941Smrg nir_ssa_def *src = nir_ssa_for_alu_src(&b, alu_instr, 0); 114b8e80941Smrg nir_ssa_def *dest; 115b8e80941Smrg 116b8e80941Smrg switch (alu_instr->op) { 117b8e80941Smrg case nir_op_pack_64_2x32: 118b8e80941Smrg dest = lower_pack_64_from_32(&b, src); 119b8e80941Smrg break; 120b8e80941Smrg case nir_op_unpack_64_2x32: 121b8e80941Smrg dest = lower_unpack_64_to_32(&b, src); 122b8e80941Smrg break; 123b8e80941Smrg case nir_op_pack_64_4x16: 124b8e80941Smrg dest = lower_pack_64_from_16(&b, src); 125b8e80941Smrg break; 126b8e80941Smrg case nir_op_unpack_64_4x16: 127b8e80941Smrg dest = lower_unpack_64_to_16(&b, src); 128b8e80941Smrg break; 129b8e80941Smrg case nir_op_pack_32_2x16: 130b8e80941Smrg dest = lower_pack_32_from_16(&b, src); 131b8e80941Smrg break; 132b8e80941Smrg case nir_op_unpack_32_2x16: 133b8e80941Smrg dest = lower_unpack_32_to_16(&b, src); 134b8e80941Smrg break; 135b8e80941Smrg default: 136b8e80941Smrg unreachable("Impossible opcode"); 137b8e80941Smrg } 138b8e80941Smrg 139b8e80941Smrg nir_ssa_def_rewrite_uses(&alu_instr->dest.dest.ssa, nir_src_for_ssa(dest)); 140b8e80941Smrg nir_instr_remove(&alu_instr->instr); 141b8e80941Smrg nir_metadata_preserve(impl, nir_metadata_block_index | 142b8e80941Smrg nir_metadata_dominance); 143b8e80941Smrg progress = true; 144b8e80941Smrg } 145b8e80941Smrg } 146b8e80941Smrg 147b8e80941Smrg return progress; 148b8e80941Smrg} 149b8e80941Smrg 150b8e80941Smrgbool 151b8e80941Smrgnir_lower_pack(nir_shader *shader) 152b8e80941Smrg{ 153b8e80941Smrg bool progress = false; 154b8e80941Smrg 155b8e80941Smrg nir_foreach_function(function, shader) { 156b8e80941Smrg if (function->impl) 157b8e80941Smrg progress |= lower_pack_impl(function->impl); 158b8e80941Smrg } 159b8e80941Smrg 160b8e80941Smrg return false; 161b8e80941Smrg} 162