| 1 |
# Copyright (C) 2020 Collabora, Ltd. |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
SKIP = set(["lane", "lane_dest", "lanes", "lanes", "replicate", "swz", "widen", |
| 5 |
"swap", "neg", "abs", "not", "sign", "extend", "divzero", "clamp", "sem", |
| 6 |
"not_result", "skip", "round", "ftz", "z_stencil"]) |
| 7 |
|
| 8 |
TEMPLATE = """ |
| 9 |
#ifndef _BI_BUILDER_H_ |
| 10 |
#define _BI_BUILDER_H_ |
| 11 |
|
| 12 |
#include "compiler.h" |
| 13 |
|
| 14 |
<% |
| 15 |
# For <32-bit loads/stores, the default extend `none` with a natural sized |
| 16 |
# input is not encodeable! To avoid a footgun, swap the default to `zext` which |
| 17 |
# will work as expected |
| 18 |
ZEXT_DEFAULT = set(["LOAD.i8", "LOAD.i16", "LOAD.i24", "STORE.i8", "STORE.i16", "STORE.i24"]) |
| 19 |
|
| 20 |
def nirtypes(opcode): |
| 21 |
split = opcode.split('.', 1) |
| 22 |
if len(split) < 2: |
| 23 |
split = opcode.split('_') |
| 24 |
|
| 25 |
if len(split) <= 1: |
| 26 |
return None |
| 27 |
|
| 28 |
assert len(split) > 1 |
| 29 |
|
| 30 |
type = split[1] |
| 31 |
if type[0] == 'v': |
| 32 |
type = type[2:] |
| 33 |
|
| 34 |
if type[0] == 'f': |
| 35 |
return ['nir_type_float'] |
| 36 |
elif type[0] == 's': |
| 37 |
return ['nir_type_int'] |
| 38 |
elif type[0] == 'u': |
| 39 |
return ['nir_type_uint'] |
| 40 |
elif type[0] == 'i': |
| 41 |
return ['nir_type_uint', 'nir_type_int'] |
| 42 |
else: |
| 43 |
return None |
| 44 |
|
| 45 |
def condition(opcode, typecheck, sizecheck): |
| 46 |
cond = '' |
| 47 |
|
| 48 |
if sizecheck == True: |
| 49 |
cond += "bitsize == {}".format(typesize(opcode)) |
| 50 |
|
| 51 |
cmpf_mods = ops[opcode]["modifiers"]["cmpf"] if "cmpf" in ops[opcode]["modifiers"] else None |
| 52 |
if "cmpf" in ops[opcode]["modifiers"]: |
| 53 |
cond += "{}(".format(' && ' if cond != '' else '') |
| 54 |
for cmpf in ops[opcode]["modifiers"]["cmpf"]: |
| 55 |
if cmpf != 'reserved': |
| 56 |
cond += "{}cmpf == BI_CMPF_{}".format(' || ' if cond[-1] != '(' else '', cmpf.upper()) |
| 57 |
cond += ')' |
| 58 |
|
| 59 |
if typecheck == True: |
| 60 |
types = nirtypes(opcode) |
| 61 |
assert types != None |
| 62 |
typecheck_cond = ' || '.join(map(lambda T: "type == {}".format(T), types)) |
| 63 |
if cond != '': |
| 64 |
typecheck_cond = '(' + typecheck_cond + ') && ' |
| 65 |
cond = typecheck_cond + cond |
| 66 |
|
| 67 |
return 'true' if cond == '' else cond |
| 68 |
|
| 69 |
def to_suffix(op): |
| 70 |
return "_to" if op["dests"] > 0 else "" |
| 71 |
|
| 72 |
%> |
| 73 |
|
| 74 |
% for opcode in ops: |
| 75 |
static inline |
| 76 |
bi_instr * bi_${opcode.replace('.', '_').lower()}${to_suffix(ops[opcode])}(${signature(ops[opcode], modifiers)}) |
| 77 |
{ |
| 78 |
<% |
| 79 |
op = ops[opcode] |
| 80 |
nr_dests = "nr_dests" if op["variable_dests"] else op["dests"] |
| 81 |
nr_srcs = "nr_srcs" if op["variable_srcs"] else src_count(op) |
| 82 |
%> |
| 83 |
size_t size = sizeof(bi_instr) + sizeof(bi_index) * (${nr_dests} + ${nr_srcs}); |
| 84 |
bi_instr *I = (bi_instr *) rzalloc_size(b->shader, size); |
| 85 |
|
| 86 |
I->op = BI_OPCODE_${opcode.replace('.', '_').upper()}; |
| 87 |
I->nr_dests = ${nr_dests}; |
| 88 |
I->nr_srcs = ${nr_srcs}; |
| 89 |
I->dest = (bi_index *) (&I[1]); |
| 90 |
I->src = I->dest + ${nr_dests}; |
| 91 |
I->debug_info = b->debug_info; |
| 92 |
|
| 93 |
% if not op["variable_dests"]: |
| 94 |
% for dest in range(op["dests"]): |
| 95 |
I->dest[${dest}] = dest${dest}; |
| 96 |
% endfor |
| 97 |
%endif |
| 98 |
|
| 99 |
% if not op["variable_srcs"]: |
| 100 |
% for src in range(src_count(op)): |
| 101 |
I->src[${src}] = src${src}; |
| 102 |
% endfor |
| 103 |
% endif |
| 104 |
|
| 105 |
% for mod in ops[opcode]["modifiers"]: |
| 106 |
% if not should_skip(mod, opcode): |
| 107 |
I->${mod} = ${mod}; |
| 108 |
% endif |
| 109 |
% endfor |
| 110 |
% if ops[opcode]["rtz"]: |
| 111 |
I->round = BI_ROUND_RTZ; |
| 112 |
% elif "round" in ops[opcode]["modifiers"] and use_default_round_mode(opcode): |
| 113 |
I->round = bi_round_mode(b->shader, ${typesize(opcode)}); |
| 114 |
% endif |
| 115 |
% for imm in ops[opcode]["immediates"]: |
| 116 |
I->${imm} = ${imm}; |
| 117 |
% endfor |
| 118 |
% if opcode in ZEXT_DEFAULT: |
| 119 |
I->extend = BI_EXTEND_ZEXT; |
| 120 |
% endif |
| 121 |
bi_builder_insert(&b->cursor, I); |
| 122 |
return I; |
| 123 |
} |
| 124 |
|
| 125 |
% if ops[opcode]["dests"] == 1 and not ops[opcode]["variable_dests"]: |
| 126 |
static inline |
| 127 |
bi_index bi_${opcode.replace('.', '_').lower()}(${signature(ops[opcode], modifiers, no_dests=True)}) |
| 128 |
{ |
| 129 |
return (bi_${opcode.replace('.', '_').lower()}_to(${arguments(ops[opcode])}))->dest[0]; |
| 130 |
} |
| 131 |
|
| 132 |
%endif |
| 133 |
<% |
| 134 |
common_op = opcode.split('.')[0] |
| 135 |
variants = [a for a in ops.keys() if a.split('.')[0] == common_op] |
| 136 |
signatures = [signature(ops[op], modifiers, no_dests=True) for op in variants] |
| 137 |
homogenous = all([sig == signatures[0] for sig in signatures]) |
| 138 |
types = [nirtypes(x) for x in variants] |
| 139 |
typeful = False |
| 140 |
for t in types: |
| 141 |
if t != types[0]: |
| 142 |
typeful = True |
| 143 |
|
| 144 |
sizes = [typesize(x) for x in variants] |
| 145 |
sized = False |
| 146 |
for size in sizes: |
| 147 |
if size != sizes[0]: |
| 148 |
sized = True |
| 149 |
|
| 150 |
last = opcode == variants[-1] |
| 151 |
%> |
| 152 |
% if homogenous and len(variants) > 1 and last: |
| 153 |
% for (suffix, temp, dests, ret) in (('_to', False, 1, 'instr *'), ('', True, 0, 'index')): |
| 154 |
% if not temp or ops[opcode]["dests"] > 0: |
| 155 |
static inline |
| 156 |
bi_${ret} bi_${common_op.replace('.', '_').lower()}${suffix if ops[opcode]['dests'] > 0 else ''}(${signature(ops[opcode], modifiers, typeful=typeful, sized=sized, no_dests=not dests)}) |
| 157 |
{ |
| 158 |
% for i, variant in enumerate(variants): |
| 159 |
${"{}if ({})".format("else " if i > 0 else "", condition(variant, typeful, sized))} |
| 160 |
return (bi_${variant.replace('.', '_').lower()}${to_suffix(ops[opcode])}(${arguments(ops[opcode], temp_dest = temp)}))${"->dest[0]" if temp else ""}; |
| 161 |
% endfor |
| 162 |
else |
| 163 |
UNREACHABLE("Invalid parameters for ${common_op}"); |
| 164 |
} |
| 165 |
|
| 166 |
%endif |
| 167 |
%endfor |
| 168 |
%endif |
| 169 |
%endfor |
| 170 |
#endif""" |
| 171 |
|
| 172 |
import sys |
| 173 |
from bifrost_isa import * |
| 174 |
from mako.template import Template |
| 175 |
|
| 176 |
instructions = {} |
| 177 |
for arg in sys.argv[1:]: |
| 178 |
new_instructions = parse_instructions(arg, include_pseudo = True) |
| 179 |
instructions.update(new_instructions) |
| 180 |
|
| 181 |
ir_instructions = partition_mnemonics(instructions) |
| 182 |
modifier_lists = order_modifiers(ir_instructions) |
| 183 |
|
| 184 |
# Generate type signature for a builder routine |
| 185 |
|
| 186 |
def should_skip(mod, op): |
| 187 |
# FROUND and HADD only make sense in context of a round mode, so override |
| 188 |
# the usual skip |
| 189 |
if mod == "round" and ("FROUND" in op or "HADD" in op): |
| 190 |
return False |
| 191 |
|
| 192 |
return mod in SKIP or mod[0:-1] in SKIP |
| 193 |
|
| 194 |
# Returns whether the default round mode from the shader float controls should |
| 195 |
# be set on an op that has a round modifier. |
| 196 |
def use_default_round_mode(op): |
| 197 |
# FMA_RSCALE/FADD_RSCALE round modifiers are not usable in all situations, |
| 198 |
# and so the instructions are not used when implementing ops that require |
| 199 |
# correct rounding. |
| 200 |
return should_skip("round", op) and \ |
| 201 |
not ("FMA_RSCALE" in op or "FADD_RSCALE" in op) |
| 202 |
|
| 203 |
def modifier_signature(op): |
| 204 |
return sorted([m for m in op["modifiers"].keys() if not should_skip(m, op["key"])]) |
| 205 |
|
| 206 |
def signature(op, modifiers, typeful = False, sized = False, no_dests = False): |
| 207 |
return ", ".join( |
| 208 |
["bi_builder *b"] + |
| 209 |
(["nir_alu_type type"] if typeful == True else []) + |
| 210 |
(["unsigned bitsize"] if sized == True else []) + |
| 211 |
(["unsigned nr_dests"] if op["variable_dests"] else |
| 212 |
["bi_index dest{}".format(i) for i in range(0 if no_dests else op["dests"])]) + |
| 213 |
(["unsigned nr_srcs"] if op["variable_srcs"] else |
| 214 |
["bi_index src{}".format(i) for i in range(src_count(op))]) + |
| 215 |
["{} {}".format( |
| 216 |
"bool" if len(modifiers[T[0:-1]] if T[-1] in "0123" else modifiers[T]) == 2 else |
| 217 |
"enum bi_" + T[0:-1] if T[-1] in "0123" else |
| 218 |
"enum bi_" + T, |
| 219 |
T) for T in modifier_signature(op)] + |
| 220 |
["uint32_t {}".format(imm) for imm in op["immediates"]]) |
| 221 |
|
| 222 |
def arguments(op, temp_dest = True): |
| 223 |
dest_pattern = "bi_temp(b->shader)" if temp_dest else 'dest{}' |
| 224 |
dests = [dest_pattern.format(i) for i in range(op["dests"])] |
| 225 |
srcs = ["src{}".format(i) for i in range(src_count(op))] |
| 226 |
|
| 227 |
# Variable source/destinations just pass in the count |
| 228 |
if op["variable_dests"]: |
| 229 |
dests = ["nr_dests"] |
| 230 |
|
| 231 |
if op["variable_srcs"]: |
| 232 |
srcs = ["nr_srcs"] |
| 233 |
|
| 234 |
return ", ".join(["b"] + dests + srcs + modifier_signature(op) + op["immediates"]) |
| 235 |
|
| 236 |
print(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, modifiers = |
| 237 |
modifier_lists, signature = signature, arguments = arguments, src_count = |
| 238 |
src_count, typesize = typesize, should_skip = should_skip, use_default_round_mode = use_default_round_mode)) |