| 1 |
# Copyright © 2025 Imagination Technologies Ltd. |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
import argparse |
| 5 |
import sys |
| 6 |
import math |
| 7 |
|
| 8 |
a = 'a' |
| 9 |
b = 'b' |
| 10 |
|
| 11 |
lower_algebraic = [] |
| 12 |
lower_algebraic_late = [] |
| 13 |
|
| 14 |
def lowered_fround_even(src): |
| 15 |
abs_src = ('fabs', src) |
| 16 |
ffloor_temp = ('ffloor', abs_src) |
| 17 |
ffract_temp = ('ffract', abs_src) |
| 18 |
|
| 19 |
ceil_temp = ('fadd', ffloor_temp, 1.0) |
| 20 |
even_temp = ('fmul', ffloor_temp, 0.5) |
| 21 |
|
| 22 |
even_ffract_temp = ('ffract', even_temp) |
| 23 |
|
| 24 |
ishalf_temp = ('feq', ffract_temp, 0.5) |
| 25 |
ffract_temp = ('bcsel', ishalf_temp, even_ffract_temp, ffract_temp) |
| 26 |
|
| 27 |
lesshalf_temp = ('flt', ffract_temp, 0.5) |
| 28 |
result_temp = ('bcsel', lesshalf_temp, ffloor_temp, ceil_temp) |
| 29 |
|
| 30 |
res = ('fcopysign_pco', result_temp, src) |
| 31 |
return res |
| 32 |
|
| 33 |
lower_algebraic.append((('fround_even', a), lowered_fround_even(a))) |
| 34 |
|
| 35 |
lower_insert_extract = [ |
| 36 |
(('insert_u8', 'a@32', b), ('bitfield_insert', 0, a, ('imul', b, 8), 8)), |
| 37 |
(('insert_u16', 'a@32', b), ('bitfield_insert', 0, a, ('imul', b, 16), 16)), |
| 38 |
|
| 39 |
(('extract_u8', 'a@32', b), ('ubitfield_extract', a, ('imul', b, 8), 8)), |
| 40 |
(('extract_i8', 'a@32', b), ('ibitfield_extract', a, ('imul', b, 8), 8)), |
| 41 |
|
| 42 |
(('extract_u16', 'a@32', b), ('ubitfield_extract', a, ('imul', b, 16), 16)), |
| 43 |
(('extract_i16', 'a@32', b), ('ibitfield_extract', a, ('imul', b, 16), 16)), |
| 44 |
] |
| 45 |
|
| 46 |
lower_algebraic_late.extend(lower_insert_extract) |
| 47 |
|
| 48 |
lower_b2b = [ |
| 49 |
(('b2b32', a), ('ineg', ('b2i32', a))), |
| 50 |
(('b2b1', a), ('ine', a, 0)), |
| 51 |
] |
| 52 |
|
| 53 |
lower_algebraic.extend(lower_b2b) |
| 54 |
|
| 55 |
lower_scmp = [ |
| 56 |
# Float comparisons + bool conversions. |
| 57 |
(('b2f32', ('flt', a, b)), ('slt', a, 'b@32'), '!options->lower_scmp'), |
| 58 |
(('b2f32', ('fge', a, b)), ('sge', a, 'b@32'), '!options->lower_scmp'), |
| 59 |
(('b2f32', ('feq', a, b)), ('seq', a, 'b@32'), '!options->lower_scmp'), |
| 60 |
(('b2f32', ('fneu', a, b)), ('sne', a, 'b@32'), '!options->lower_scmp'), |
| 61 |
|
| 62 |
# Float comparisons + bool conversions via bcsel. |
| 63 |
(('bcsel@32', ('flt', a, b), 1.0, 0.0), ('slt', a, 'b@32'), '!options->lower_scmp'), |
| 64 |
(('bcsel@32', ('fge', a, b), 1.0, 0.0), ('sge', a, 'b@32'), '!options->lower_scmp'), |
| 65 |
(('bcsel@32', ('feq', a, b), 1.0, 0.0), ('seq', a, 'b@32'), '!options->lower_scmp'), |
| 66 |
(('bcsel@32', ('fneu', a, b), 1.0, 0.0), ('sne', a, 'b@32'), '!options->lower_scmp'), |
| 67 |
] |
| 68 |
|
| 69 |
lower_algebraic_late.extend(lower_scmp) |
| 70 |
|
| 71 |
# TODO: core-specific info. |
| 72 |
params=[] |
| 73 |
|
| 74 |
def run(): |
| 75 |
import nir_algebraic # pylint: disable=import-error |
| 76 |
print('#include "pco_internal.h"') |
| 77 |
print(nir_algebraic.AlgebraicPass('pco_nir_lower_algebraic', lower_algebraic, params=params).render()) |
| 78 |
print(nir_algebraic.AlgebraicPass('pco_nir_lower_algebraic_late', lower_algebraic_late, params=params).render()) |
| 79 |
|
| 80 |
def main(): |
| 81 |
parser = argparse.ArgumentParser() |
| 82 |
parser.add_argument('-p', '--import-path', required=True) |
| 83 |
args = parser.parse_args() |
| 84 |
sys.path.insert(0, args.import_path) |
| 85 |
run() |
| 86 |
|
| 87 |
if __name__ == '__main__': |
| 88 |
main() |