From 9bf30c4a5cba7e148da7ccb042906eff8a8ad96a Mon Sep 17 00:00:00 2001 From: Rhys Perry Date: Thu, 20 May 2021 10:41:37 +0100 Subject: [PATCH] aco/tests: add tests for form_hard_clauses() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rhys Perry Reviewed-by: Timur Kristóf Part-of: --- src/amd/compiler/tests/README.md | 5 + src/amd/compiler/tests/check_output.py | 15 +- src/amd/compiler/tests/helpers.cpp | 7 + src/amd/compiler/tests/helpers.h | 1 + src/amd/compiler/tests/meson.build | 1 + src/amd/compiler/tests/test_hard_clause.cpp | 392 ++++++++++++++++++++ 6 files changed, 418 insertions(+), 3 deletions(-) create mode 100644 src/amd/compiler/tests/test_hard_clause.cpp diff --git a/src/amd/compiler/tests/README.md b/src/amd/compiler/tests/README.md index ab5186a6eeb..94f95472ab6 100644 --- a/src/amd/compiler/tests/README.md +++ b/src/amd/compiler/tests/README.md @@ -17,3 +17,8 @@ Patterns can define variables which can be accessed in both python code and the - A `$` in the pattern stores the output until the first whitespace character into a variable. - A `%` in the pattern followed by an identifier is the same as a `#` but it expects a `%` before the integer in the output. It basically matches a ACO temporary. - A `@` calls a variable as a function. It can be followed by an argument string wrapped in `(` and `)`. + +# Functions +- `s64`, `s96`, `s128`, `v2`, `v3`, etc, expand to a pattern which matches a disassembled instruction's definition or operand. It later checks that the size and alignment is what's expected. +- `match_func` expands to a sequence of `$` and inserts functions with expand to the extracted output +- `search_re` consumes the rest of the line and fails the test if the pattern is not found diff --git a/src/amd/compiler/tests/check_output.py b/src/amd/compiler/tests/check_output.py index 7d6670c1990..c4a022e7a34 100644 --- a/src/amd/compiler/tests/check_output.py +++ b/src/amd/compiler/tests/check_output.py @@ -37,6 +37,8 @@ else: set_normal = '' initial_code = ''' +import re + def insert_code(code): insert_queue.append(CodeCheck(code)) @@ -70,6 +72,11 @@ def _match_func(names): return ' '.join(f'${name}' for name in names.split(' ')) funcs['match_func'] = _match_func + +def search_re(pattern): + global success + success = re.search(pattern, output.read_line()) != None and success + ''' class Check: @@ -149,10 +156,12 @@ class StringStream: def get_line(self, num): return self.data.split('\n')[num - 1].rstrip() - def skip_line(self): + def read_line(self): + line = '' while self.peek(1) not in ['\n', '']: - self.read(1) + line += self.read(1) self.read(1) + return line def skip_whitespace(self, inc_line): chars = [' ', '\t'] + (['\n'] if inc_line else []) @@ -308,7 +317,7 @@ def do_match(g, pattern, output, skip_lines, in_func=False): g.clear() g.update(old_g) res.success = True - output.skip_line() + output.read_line() pattern.reset() output.skip_whitespace(False) pattern.skip_whitespace(False) diff --git a/src/amd/compiler/tests/helpers.cpp b/src/amd/compiler/tests/helpers.cpp index 7f3202fe3eb..d24e21e2c49 100644 --- a/src/amd/compiler/tests/helpers.cpp +++ b/src/amd/compiler/tests/helpers.cpp @@ -206,6 +206,13 @@ void finish_insert_nops_test() aco_print_program(program.get(), output); } +void finish_form_hard_clause_test() +{ + finish_program(program.get()); + aco::form_hard_clauses(program.get()); + aco_print_program(program.get(), output); +} + void finish_assembler_test() { finish_program(program.get()); diff --git a/src/amd/compiler/tests/helpers.h b/src/amd/compiler/tests/helpers.h index f9b966bd49d..268ea5d3ab7 100644 --- a/src/amd/compiler/tests/helpers.h +++ b/src/amd/compiler/tests/helpers.h @@ -83,6 +83,7 @@ void finish_ra_test(aco::ra_test_policy); void finish_optimizer_postRA_test(); void finish_to_hw_instr_test(); void finish_insert_nops_test(); +void finish_form_hard_clause_test(); void finish_assembler_test(); void writeout(unsigned i, aco::Temp tmp=aco::Temp(0, aco::s1)); diff --git a/src/amd/compiler/tests/meson.build b/src/amd/compiler/tests/meson.build index af2c462863b..f8e5f902a58 100644 --- a/src/amd/compiler/tests/meson.build +++ b/src/amd/compiler/tests/meson.build @@ -24,6 +24,7 @@ aco_tests_files = files( 'main.cpp', 'test_assembler.cpp', 'test_builder.cpp', + 'test_hard_clause.cpp', 'test_insert_nops.cpp', 'test_isel.cpp', 'test_optimizer.cpp', diff --git a/src/amd/compiler/tests/test_hard_clause.cpp b/src/amd/compiler/tests/test_hard_clause.cpp new file mode 100644 index 00000000000..236b5d8b76c --- /dev/null +++ b/src/amd/compiler/tests/test_hard_clause.cpp @@ -0,0 +1,392 @@ +/* + * Copyright © 2020 Valve Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ +#include "helpers.h" +#include "sid.h" + +using namespace aco; + +static void create_mubuf(Temp desc=Temp(0, s8), unsigned vtx_binding=0) +{ + Operand desc_op(desc); + desc_op.setFixed(PhysReg(0)); + bld.mubuf(aco_opcode::buffer_load_dword, Definition(PhysReg(256), v1), + desc_op, Operand(PhysReg(256), v1), + Operand(0u), 0, false).instr->mubuf().vtx_binding = vtx_binding; +} + +static void create_mubuf_store() +{ + bld.mubuf(aco_opcode::buffer_store_dword, Operand(PhysReg(0), s4), + Operand(PhysReg(256), v1), Operand(PhysReg(256), v1), Operand(0u), 0, false); +} + +static void create_mtbuf(Temp desc=Temp(0, s8), unsigned vtx_binding=0) +{ + Operand desc_op(desc); + desc_op.setFixed(PhysReg(0)); + bld.mtbuf(aco_opcode::tbuffer_load_format_x, Definition(PhysReg(256), v1), + desc_op, Operand(PhysReg(256), v1), Operand(0u), + V_008F0C_BUF_DATA_FORMAT_32, V_008F0C_BUF_NUM_FORMAT_FLOAT, 0, false) + .instr->mtbuf().vtx_binding = vtx_binding; +} + +static void create_flat() +{ + bld.flat(aco_opcode::flat_load_dword, Definition(PhysReg(256), v1), + Operand(PhysReg(256), v2), Operand(s2)); +} + +static void create_global() +{ + bld.global(aco_opcode::global_load_dword, Definition(PhysReg(256), v1), + Operand(PhysReg(256), v2), Operand(s2)); +} + +static void create_mimg(bool nsa, Temp desc=Temp(0, s8)) +{ + aco_ptr mimg{create_instruction( + aco_opcode::image_sample, Format::MIMG, 5, 1)}; + mimg->definitions[0] = Definition(PhysReg(256), v1); + mimg->operands[0] = Operand(desc); + mimg->operands[0].setFixed(PhysReg(0)); + mimg->operands[1] = Operand(PhysReg(0), s4); + mimg->operands[2] = Operand(v1); + for (unsigned i = 0; i < 2; i++) + mimg->operands[3 + i] = Operand(PhysReg(256 + (nsa ? i * 2 : i)), v1); + mimg->dmask = 0x1; + mimg->dim = ac_image_2d; + + bld.insert(std::move(mimg)); +} + +static void create_smem() +{ + bld.smem(aco_opcode::s_load_dword, Definition(PhysReg(0), s1), + Operand(PhysReg(0), s2), Operand(0u)); +} + +static void create_smem_buffer(Temp desc=Temp(0, s4)) +{ + Operand desc_op(desc); + desc_op.setFixed(PhysReg(0)); + bld.smem(aco_opcode::s_buffer_load_dword, Definition(PhysReg(0), s1), + desc_op, Operand(0u)); +} + +BEGIN_TEST(form_hard_clauses.type_restrictions) + if (!setup_cs(NULL, GFX10)) + return; + + //>> p_unit_test 0 + //! s_clause imm:1 + //; search_re('image_sample') + //; search_re('image_sample') + bld.pseudo(aco_opcode::p_unit_test, Operand(0u)); + create_mimg(false); + create_mimg(false); + + //>> p_unit_test 1 + //! s_clause imm:1 + //; search_re('buffer_load_dword') + //; search_re('buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(1u)); + create_mubuf(); + create_mubuf(); + + //>> p_unit_test 2 + //! s_clause imm:1 + //; search_re('global_load_dword') + //; search_re('global_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(2u)); + create_global(); + create_global(); + + //>> p_unit_test 3 + //! s_clause imm:1 + //; search_re('flat_load_dword') + //; search_re('flat_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(3u)); + create_flat(); + create_flat(); + + //>> p_unit_test 4 + //! s_clause imm:1 + //; search_re('s_load_dword') + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(4u)); + create_smem(); + create_smem(); + + //>> p_unit_test 5 + //; search_re('buffer_load_dword') + //; search_re('flat_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(5u)); + create_mubuf(); + create_flat(); + + //>> p_unit_test 6 + //; search_re('buffer_load_dword') + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(6u)); + create_mubuf(); + create_smem(); + + //>> p_unit_test 7 + //; search_re('flat_load_dword') + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(7u)); + create_flat(); + create_smem(); + + finish_form_hard_clause_test(); +END_TEST + +BEGIN_TEST(form_hard_clauses.size) + if (!setup_cs(NULL, GFX10)) + return; + + //>> p_unit_test 0 + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(0u)); + create_smem(); + + //>> p_unit_test 1 + //! s_clause imm:63 + //; for i in range(64): + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(1u)); + for (unsigned i = 0; i < 64; i++) + create_smem(); + + //>> p_unit_test 2 + //! s_clause imm:63 + //; for i in range(65): + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(2u)); + for (unsigned i = 0; i < 65; i++) + create_smem(); + + //>> p_unit_test 3 + //! s_clause imm:63 + //; for i in range(64): + //; search_re('s_load_dword') + //! s_clause imm:1 + //; search_re('s_load_dword') + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(3u)); + for (unsigned i = 0; i < 66; i++) + create_smem(); + + finish_form_hard_clause_test(); +END_TEST + +BEGIN_TEST(form_hard_clauses.nsa) + for (unsigned i = GFX10; i <= GFX10_3; i++) { + if (!setup_cs(NULL, (chip_class)i)) + continue; + + //>> p_unit_test 0 + //! s_clause imm:1 + //; search_re('image_sample .* %0:v\[0\], %0:v\[1\]') + //; search_re('image_sample .* %0:v\[0\], %0:v\[1\]') + bld.pseudo(aco_opcode::p_unit_test, Operand(0u)); + create_mimg(false); + create_mimg(false); + + //>> p_unit_test 1 + //~gfx10_3! s_clause imm:1 + //; search_re('image_sample .* %0:v\[0\], %0:v\[1\]') + //; search_re('image_sample .* %0:v\[0\], %0:v\[2\]') + bld.pseudo(aco_opcode::p_unit_test, Operand(1u)); + create_mimg(false); + create_mimg(true); + + //>> p_unit_test 2 + //~gfx10_3! s_clause imm:1 + //; search_re('image_sample .* %0:v\[0\], %0:v\[2\]') + //; search_re('image_sample .* %0:v\[0\], %0:v\[2\]') + bld.pseudo(aco_opcode::p_unit_test, Operand(2u)); + create_mimg(true); + create_mimg(true); + + finish_form_hard_clause_test(); + } +END_TEST + +BEGIN_TEST(form_hard_clauses.heuristic) + if (!setup_cs(NULL, GFX10)) + return; + + Temp img_desc0 = bld.tmp(s8); + Temp img_desc1 = bld.tmp(s8); + Temp buf_desc0 = bld.tmp(s4); + Temp buf_desc1 = bld.tmp(s4); + + /* Don't form clause with different descriptors */ + //>> p_unit_test 0 + //! s_clause imm:1 + //; search_re('image_sample') + //; search_re('image_sample') + bld.pseudo(aco_opcode::p_unit_test, Operand(0u)); + create_mimg(false, img_desc0); + create_mimg(false, img_desc0); + + //>> p_unit_test 1 + //; search_re('image_sample') + //; search_re('image_sample') + bld.pseudo(aco_opcode::p_unit_test, Operand(1u)); + create_mimg(false, img_desc0); + create_mimg(false, img_desc1); + + //>> p_unit_test 2 + //! s_clause imm:1 + //; search_re('buffer_load_dword') + //; search_re('buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(2u)); + create_mubuf(buf_desc0); + create_mubuf(buf_desc0); + + //>> p_unit_test 3 + //; search_re('buffer_load_dword') + //; search_re('buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(3u)); + create_mubuf(buf_desc0); + create_mubuf(buf_desc1); + + //>> p_unit_test 4 + //! s_clause imm:1 + //; search_re('s_buffer_load_dword') + //; search_re('s_buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(4u)); + create_smem_buffer(buf_desc0); + create_smem_buffer(buf_desc0); + + //>> p_unit_test 5 + //; search_re('s_buffer_load_dword') + //; search_re('s_buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(5u)); + create_smem_buffer(buf_desc0); + create_smem_buffer(buf_desc1); + + //>> p_unit_test 6 + //; search_re('s_buffer_load_dword') + //; search_re('s_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(6u)); + create_smem_buffer(buf_desc0); + create_smem(); + + /* Only form clause between MUBUF and MTBUF if they load from the same binding. Ignore descriptor + * if they're te same binding. + */ + //>> p_unit_test 7 + //; search_re('buffer_load_dword') + //; search_re('tbuffer_load_format_x') + bld.pseudo(aco_opcode::p_unit_test, Operand(7u)); + create_mubuf(buf_desc0); + create_mtbuf(buf_desc0); + + //>> p_unit_test 8 + //! s_clause imm:1 + //; search_re('buffer_load_dword') + //; search_re('tbuffer_load_format_x') + bld.pseudo(aco_opcode::p_unit_test, Operand(8u)); + create_mubuf(buf_desc0, 1); + create_mtbuf(buf_desc0, 1); + + //>> p_unit_test 9 + //! s_clause imm:1 + //; search_re('buffer_load_dword') + //; search_re('tbuffer_load_format_x') + bld.pseudo(aco_opcode::p_unit_test, Operand(9u)); + create_mubuf(buf_desc0, 1); + create_mtbuf(buf_desc1, 1); + + finish_form_hard_clause_test(); +END_TEST + +BEGIN_TEST(form_hard_clauses.stores) + if (!setup_cs(NULL, GFX10)) + return; + + //>> p_unit_test 0 + //; search_re('buffer_store_dword') + //; search_re('buffer_store_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(0u)); + create_mubuf_store(); + create_mubuf_store(); + + //>> p_unit_test 1 + //! s_clause imm:1 + //; search_re('buffer_load_dword') + //; search_re('buffer_load_dword') + //; search_re('buffer_store_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(1u)); + create_mubuf(); + create_mubuf(); + create_mubuf_store(); + + //>> p_unit_test 2 + //; search_re('buffer_store_dword') + //! s_clause imm:1 + //; search_re('buffer_load_dword') + //; search_re('buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(2u)); + create_mubuf_store(); + create_mubuf(); + create_mubuf(); + + /* Unclear whether this is the best behaviour */ + //>> p_unit_test 3 + //; search_re('buffer_load_dword') + //; search_re('buffer_store_dword') + //; search_re('buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(3u)); + create_mubuf(); + create_mubuf_store(); + create_mubuf(); + + /* Unimportant pass limitations */ + //>> p_unit_test 4 + //; search_re('buffer_store_dword') + //! s_clause imm:62 + //; for i in range(63): + //; search_re('buffer_load_dword') + //; search_re('buffer_load_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(4u)); + create_mubuf_store(); + for (unsigned i = 0; i < 64; i++) + create_mubuf(); + + //>> p_unit_test 5 + //! s_clause imm:63 + //; for i in range(64): + //; search_re('buffer_load_dword') + //; search_re('buffer_store_dword') + bld.pseudo(aco_opcode::p_unit_test, Operand(5u)); + for (unsigned i = 0; i < 64; i++) + create_mubuf(); + create_mubuf_store(); + + finish_form_hard_clause_test(); +END_TEST