pan/bi: Add constant folding unit test

I just played with the implementation, let's ensure I didn't break it.

Signed-off-by: Alyssa Rosenzweig <alyssa@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12026>
This commit is contained in:
Alyssa Rosenzweig 2021-07-26 12:15:15 -04:00 committed by Marge Bot
parent 87ebad74e5
commit 16579ca4b7
2 changed files with 111 additions and 0 deletions

View file

@ -140,3 +140,19 @@ libpanfrost_bifrost = static_library(
gnu_symbol_visibility : 'hidden',
build_by_default : false,
)
if with_tests
test(
'bifrost_constant_fold',
executable(
'bifrost_constant_fold_test',
files('test/test-constant-fold.c'),
c_args : [c_msvc_compat_args, no_override_init_args],
gnu_symbol_visibility : 'hidden',
include_directories : [inc_include, inc_src, inc_mesa],
dependencies: [idep_nir, idep_bi_opcodes_h, idep_bi_builder_h],
link_with : [libpanfrost_bifrost],
),
suite : ['panfrost'],
)
endif

View file

@ -0,0 +1,95 @@
/*
* Copyright (C) 2021 Collabora, Ltd.
*
* 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 "compiler.h"
#include "bi_test.h"
#include "bi_builder.h"
#define CASE(instr, expected) do { \
bool _unsupported = false; \
uint32_t _value = bi_fold_constant(instr, &_unsupported); \
if (_unsupported) { \
fprintf(stderr, "Constant folding failed:\n"); \
bi_print_instr(instr, stderr); \
fprintf(stderr, "\n"); \
} else if (_value == expected) { \
nr_pass++; \
} else { \
fprintf(stderr, "Got %" PRIx32 ", expected %" PRIx32 "\n", _value, expected); \
bi_print_instr(instr, stderr); \
fprintf(stderr, "\n"); \
nr_fail++; \
} \
} while(0)
#define NEGCASE(instr) do { \
bool _unsupported = false; \
bi_fold_constant(instr, &_unsupported); \
if (_unsupported) { \
nr_pass++; \
} else { \
fprintf(stderr, "Should not have constant folded:\n"); \
bi_print_instr(instr, stderr); \
fprintf(stderr, "\n"); \
nr_fail++; \
} \
} while(0)
int
main(int argc, const char **argv)
{
unsigned nr_fail = 0, nr_pass = 0;
void *ralloc_ctx = ralloc_context(NULL);
bi_builder *b = bit_builder(ralloc_ctx);
bi_index zero = bi_fau(BIR_FAU_IMMEDIATE | 0, false);
bi_index reg = bi_register(0);
/* Swizzles should be constant folded */
CASE(bi_swz_v2i16_to(b, reg, bi_imm_u32(0xCAFEBABE)), 0xCAFEBABE);
CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), false, false)),
0xBABEBABE);
CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, false)),
0xBABECAFE);
CASE(bi_swz_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true)),
0xCAFECAFE);
/* Vector constructions should be constant folded */
CASE(bi_mkvec_v2i16_to(b, reg, bi_imm_u16(0xCAFE), bi_imm_u16(0xBABE)), 0xBABECAFE);
CASE(bi_mkvec_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true),
bi_imm_u16(0xBABE)), 0xBABECAFE);
CASE(bi_mkvec_v2i16_to(b, reg, bi_swz_16(bi_imm_u32(0xCAFEBABE), true, true),
bi_swz_16(bi_imm_u32(0xCAFEBABE), false, false)), 0xBABECAFE);
/* Instructions with non-constant sources cannot be constant folded */
NEGCASE(bi_swz_v2i16_to(b, reg, bi_temp(b->shader)));
NEGCASE(bi_mkvec_v2i16_to(b, reg, bi_temp(b->shader), bi_temp(b->shader)));
NEGCASE(bi_mkvec_v2i16_to(b, reg, bi_temp(b->shader), bi_imm_u32(0xDEADBEEF)));
NEGCASE(bi_mkvec_v2i16_to(b, reg, bi_imm_u32(0xDEADBEEF), bi_temp(b->shader)));
/* Other operations should not be constant folded */
NEGCASE(bi_fma_f32_to(b, reg, zero, zero, zero, BI_ROUND_NONE));
NEGCASE(bi_fadd_f32_to(b, reg, zero, zero, BI_ROUND_NONE));
ralloc_free(ralloc_ctx);
TEST_END(nr_pass, nr_fail);
}