| 1 |
# Copyright (C) 2020 Collabora, Ltd. |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
# Useful for autogeneration |
| 5 |
COPYRIGHT = """/* |
| 6 |
* Copyright (C) 2020 Collabora, Ltd. |
| 7 |
* SPDX-License-Identifier: MIT |
| 8 |
*/ |
| 9 |
|
| 10 |
/* Autogenerated file, do not edit */ |
| 11 |
|
| 12 |
""" |
| 13 |
|
| 14 |
# Parse instruction set XML into a normalized form for processing |
| 15 |
|
| 16 |
import xml.etree.ElementTree as ET |
| 17 |
import copy |
| 18 |
import itertools |
| 19 |
from collections import OrderedDict |
| 20 |
|
| 21 |
def parse_cond(cond, aliased = False): |
| 22 |
if cond.tag == 'reserved': |
| 23 |
return None |
| 24 |
|
| 25 |
if cond.attrib.get('alias', False) and not aliased: |
| 26 |
return ['alias', parse_cond(cond, True)] |
| 27 |
|
| 28 |
if 'left' in cond.attrib: |
| 29 |
return [cond.tag, cond.attrib['left'], cond.attrib['right']] |
| 30 |
else: |
| 31 |
return [cond.tag] + [parse_cond(x) for x in cond.findall('*')] |
| 32 |
|
| 33 |
def parse_exact(obj): |
| 34 |
return [int(obj.attrib['mask'], 0), int(obj.attrib['exact'], 0)] |
| 35 |
|
| 36 |
def parse_derived(obj): |
| 37 |
out = [] |
| 38 |
|
| 39 |
for deriv in obj.findall('derived'): |
| 40 |
loc = [int(deriv.attrib['start']), int(deriv.attrib['size'])] |
| 41 |
count = 1 << loc[1] |
| 42 |
|
| 43 |
opts = [parse_cond(d) for d in deriv.findall('*')] |
| 44 |
default = [None] * count |
| 45 |
opts_fit = (opts + default)[0:count] |
| 46 |
|
| 47 |
out.append([loc, opts_fit]) |
| 48 |
|
| 49 |
return out |
| 50 |
|
| 51 |
def parse_modifiers(obj, include_pseudo): |
| 52 |
out = [] |
| 53 |
|
| 54 |
for mod in obj.findall('mod'): |
| 55 |
if mod.attrib.get('pseudo', False) and not include_pseudo: |
| 56 |
continue |
| 57 |
|
| 58 |
name = mod.attrib['name'] |
| 59 |
start = mod.attrib.get('start', None) |
| 60 |
size = int(mod.attrib['size']) |
| 61 |
|
| 62 |
if start is not None: |
| 63 |
start = int(start) |
| 64 |
|
| 65 |
opts = [x.text if x.tag == 'opt' else x.tag for x in mod.findall('*')] |
| 66 |
|
| 67 |
if len(opts) == 0: |
| 68 |
if 'opt' in mod.attrib: |
| 69 |
opts = ['none', mod.attrib['opt']] |
| 70 |
|
| 71 |
# Find suitable default |
| 72 |
default = mod.attrib.get('default', 'none' if 'none' in opts else None) |
| 73 |
|
| 74 |
# Pad out as reserved |
| 75 |
count = (1 << size) |
| 76 |
opts = (opts + (['reserved'] * count))[0:count] |
| 77 |
out.append([[name, start, size], default, opts]) |
| 78 |
|
| 79 |
return out |
| 80 |
|
| 81 |
def parse_copy(enc, existing): |
| 82 |
for node in enc.findall('copy'): |
| 83 |
name = node.get('name') |
| 84 |
for ex in existing: |
| 85 |
if ex[0][0] == name: |
| 86 |
ex[0][1] = node.get('start') |
| 87 |
|
| 88 |
mod_names = { |
| 89 |
'cmp' : [['cmpf', None, None], None, ['eq', 'gt', 'ge', 'ne', 'lt', 'le', 'gtlt', 'total']], |
| 90 |
# FIXME: Valhall can accept any integer comparision, but the old IR generator only generated |
| 91 |
# gt and ge, and left out lt and le. For now follow the old way so we can compare generated files, |
| 92 |
# but we should switch over to the proper way once everything is working |
| 93 |
# 'cmpfi' : [['cmpf', None, None], None, ['eq', 'ne', 'gt', 'ge', 'lt', 'le']], |
| 94 |
'cmpfi' : [['cmpf', None, None], None, ['eq', 'ne', 'gt', 'ge']], |
| 95 |
'eq' : [['cmpf', None, None], 'ne', ['eq', 'ne']], |
| 96 |
'dimension' : [['dimension', None, None], None, ['1d', '2d', '3d', 'cube']], |
| 97 |
'fetch_component' : [['fetch_component', None, None], None, ['gather4_r', 'gather4_g', 'gather4_b', 'gather4_a']], |
| 98 |
'lod_mode': [['va_lod_mode', None, None], 'zero_lod', ['zero_lod', 'computed_lod', 'explicit', 'computed_bias', 'grdesc']], |
| 99 |
'regfmt' : [['register_format', None, None], None, ['f16', 'f32', 's32', 'u32', 's16', 'u16', 'f64', 'i64', 'auto']], |
| 100 |
'result_type' : [['result_type', None, None], None, ['i1', 'f1', 'm1']], |
| 101 |
'sample' : [['sample', None, None], 'none', ['center', 'centroid', 'sample', 'explicit', 'none']], |
| 102 |
'update' : [['update', None, None], None, ['store', 'retrieve', 'conditional', 'clobber']], |
| 103 |
'vecsize' : [['vecsize', None, None], 'none', ['none', 'v2', 'v3', 'v4']], |
| 104 |
'source_format' : [['source_format', None, None], None, ['flat32', 'flat16', 'f32', 'f16']], |
| 105 |
|
| 106 |
'array_enable': [['array_enable', None, None], 'none', ['none', 'array_enable']], |
| 107 |
'integer_coordinates': [['integer_coordinates', None, None], 'none', ['none', 'integer_coordinates']], |
| 108 |
'shadow' : [['shadow', None, None], 'none', ['none', 'shadow']], |
| 109 |
'skip' : [['skip', None, None], 'none', ['none', 'skip']], |
| 110 |
'texel_offset': [['texel_offset', None, None], 'none', ['none', 'texel_offset']], |
| 111 |
'wide_indices': [['wide_indices', None, None], 'none', ['none', 'wide_indices']], |
| 112 |
'write_mask' : [['write_mask', None, None], 'none', ['none', 'r', 'g', 'rg', 'b', 'rb', 'gb', 'rgb', 'a', 'ra', 'ga', 'rga', 'ba', 'rba', 'gba', 'rgba']] |
| 113 |
} |
| 114 |
|
| 115 |
def parse_instruction(ins, include_pseudo): |
| 116 |
common = { |
| 117 |
'srcs': [], |
| 118 |
'modifiers': [], |
| 119 |
'immediates': [], |
| 120 |
'swaps': [], |
| 121 |
'derived': [], |
| 122 |
'staging': ins.attrib.get('staging', '').split('=')[0], |
| 123 |
'staging_count': ins.attrib.get('staging', '=0').split('=')[1], |
| 124 |
'dests': int(ins.attrib.get('dests', '1')), |
| 125 |
'variable_dests': ins.attrib.get('variable_dests', False), |
| 126 |
'variable_srcs': ins.attrib.get('variable_srcs', False), |
| 127 |
'unused': ins.attrib.get('unused', False), |
| 128 |
'pseudo': ins.attrib.get('pseudo', False), |
| 129 |
'message': ins.attrib.get('message', 'none'), |
| 130 |
'last': ins.attrib.get('last', False), |
| 131 |
'table': ins.attrib.get('table', False), |
| 132 |
} |
| 133 |
|
| 134 |
if 'exact' in ins.attrib: |
| 135 |
common['exact'] = parse_exact(ins) |
| 136 |
|
| 137 |
extra_modifiers=[] |
| 138 |
src_num = 0 |
| 139 |
for src in ins.findall('src'): |
| 140 |
if src.attrib.get('pseudo', False) and not include_pseudo: |
| 141 |
continue |
| 142 |
|
| 143 |
mask = int(src.attrib['mask'], 0) if ('mask' in src.attrib) else 0xFF |
| 144 |
if src.attrib.get('start') is not None: |
| 145 |
common['srcs'].append([int(src.attrib['start'], 0), mask]) |
| 146 |
else: |
| 147 |
common['srcs'].append([src_num*3, mask]) |
| 148 |
if src.attrib.get('absneg', False): |
| 149 |
extra_modifiers.append([['neg'+str(src_num), '0', '1'], 'neg',['none', 'neg']]) |
| 150 |
extra_modifiers.append([['abs'+str(src_num), '0', '1'], 'abs',['none', 'abs']]) |
| 151 |
src_num += 1 |
| 152 |
|
| 153 |
for imm in ins.findall('immediate'): |
| 154 |
if imm.attrib.get('pseudo', False) and not include_pseudo: |
| 155 |
continue |
| 156 |
|
| 157 |
start = int(imm.attrib['start']) if 'start' in imm.attrib else None |
| 158 |
common['immediates'].append([imm.attrib['name'], start, int(imm.attrib['size'])]) |
| 159 |
|
| 160 |
# FIXME valhall ISA.xml uses <imm/> instead of <immediate/> |
| 161 |
for imm in ins.findall('imm'): |
| 162 |
if imm.attrib.get('pseudo', False) and not include_pseudo: |
| 163 |
continue |
| 164 |
|
| 165 |
base_name = imm.attrib['name'] |
| 166 |
name = imm.attrib.get('ir_name', base_name) |
| 167 |
if not name: |
| 168 |
continue |
| 169 |
|
| 170 |
start = int(imm.attrib['start']) if 'start' in imm.attrib else None |
| 171 |
common['immediates'].append([name, start, int(imm.attrib['size'])]) |
| 172 |
|
| 173 |
# FIXME valhall ISA.xml uses <saturate/> instead of <mod name="saturate"/> |
| 174 |
for sat in ins.findall('saturate'): |
| 175 |
extra_modifiers.append([['saturate', None, 1], 'none', ['none', 'sat']]) |
| 176 |
|
| 177 |
staging_read = False |
| 178 |
staging_write = False |
| 179 |
for sr in ins.findall('sr'): |
| 180 |
if sr.attrib.get('read', False): |
| 181 |
staging_read = True |
| 182 |
if sr.attrib.get('write', False): |
| 183 |
staging_write = True |
| 184 |
|
| 185 |
if staging_read: |
| 186 |
common['staging'] = 'r' |
| 187 |
if staging_write: |
| 188 |
common['staging'] += 'w' |
| 189 |
for sr in ins.findall('sr_count'): |
| 190 |
size = sr.attrib.get('count', 'sr_count') |
| 191 |
common['staging_count'] = size |
| 192 |
|
| 193 |
for m in ins.findall('*'): |
| 194 |
name = m.tag |
| 195 |
if name == 'cmp': |
| 196 |
if m.attrib.get('int_only', False): |
| 197 |
name = 'cmpfi' |
| 198 |
elif m.attrib.get('eqne_only', False): |
| 199 |
name = 'cmpfeq' |
| 200 |
if name == 'va_mod': |
| 201 |
name = m.attrib.get('name', '') |
| 202 |
if name in mod_names: |
| 203 |
extra_modifiers.append(mod_names[name]) |
| 204 |
|
| 205 |
common['derived'] = parse_derived(ins) |
| 206 |
common['modifiers'] = parse_modifiers(ins, include_pseudo) + extra_modifiers |
| 207 |
|
| 208 |
for swap in ins.findall('swap'): |
| 209 |
lr = [int(swap.get('left')), int(swap.get('right'))] |
| 210 |
cond = parse_cond(swap.findall('*')[0]) |
| 211 |
rewrites = {} |
| 212 |
|
| 213 |
for rw in swap.findall('rewrite'): |
| 214 |
mp = {} |
| 215 |
|
| 216 |
for m in rw.findall('map'): |
| 217 |
mp[m.attrib['from']] = m.attrib['to'] |
| 218 |
|
| 219 |
rewrites[rw.attrib['name']] = mp |
| 220 |
|
| 221 |
common['swaps'].append([lr, cond, rewrites]) |
| 222 |
|
| 223 |
encodings = ins.findall('encoding') |
| 224 |
variants = [] |
| 225 |
|
| 226 |
if len(encodings) == 0: |
| 227 |
variants = [[None, common]] |
| 228 |
else: |
| 229 |
for enc in encodings: |
| 230 |
variant = copy.deepcopy(common) |
| 231 |
assert(len(variant['derived']) == 0) |
| 232 |
|
| 233 |
variant['exact'] = parse_exact(enc) |
| 234 |
variant['derived'] = parse_derived(enc) |
| 235 |
parse_copy(enc, variant['modifiers']) |
| 236 |
|
| 237 |
cond = parse_cond(enc.findall('*')[0]) |
| 238 |
variants.append([cond, variant]) |
| 239 |
|
| 240 |
return variants |
| 241 |
|
| 242 |
def ins_name(ins, group = False): |
| 243 |
# a historical artifact: the first character of the name should contain |
| 244 |
# a single character tag to indicate the unit: '+' for add, '*' for fma |
| 245 |
# bifrost has only those two units, valhall has others but for those |
| 246 |
# it doesn't matter (pretend they are '+') |
| 247 |
if not group: |
| 248 |
group = ins |
| 249 |
|
| 250 |
base_name = ins.attrib['name'] |
| 251 |
if group.attrib['unit'] == 'fma': |
| 252 |
tagged_name = '*' + base_name |
| 253 |
else: |
| 254 |
tagged_name = '+' + base_name |
| 255 |
return tagged_name |
| 256 |
|
| 257 |
def parse_instructions(xml, include_unused = False, include_pseudo = False): |
| 258 |
final = {} |
| 259 |
|
| 260 |
# look at groups of instructions |
| 261 |
groups = ET.parse(xml).getroot().findall('group') |
| 262 |
for gr in groups: |
| 263 |
if gr.attrib.get('unused', False) and not include_unused: |
| 264 |
continue |
| 265 |
if gr.attrib.get('pseudo', False) and not include_pseudo: |
| 266 |
continue |
| 267 |
group_base = parse_instruction(gr, include_pseudo) |
| 268 |
for ins in gr.findall('ins'): |
| 269 |
parsed = copy.deepcopy(group_base) |
| 270 |
tagged_name = ins_name(ins, gr) |
| 271 |
final[tagged_name] = parsed |
| 272 |
|
| 273 |
# now look at individual instructions |
| 274 |
instructions = ET.parse(xml).getroot().findall('ins') |
| 275 |
|
| 276 |
for ins in instructions: |
| 277 |
parsed = parse_instruction(ins, include_pseudo) |
| 278 |
|
| 279 |
# Some instructions are for useful disassembly only and can be stripped |
| 280 |
# out of the compiler, particularly useful for release builds |
| 281 |
if parsed[0][1]["unused"] and not include_unused: |
| 282 |
continue |
| 283 |
|
| 284 |
# On the other hand, some instructions are only for the IR, not disassembly |
| 285 |
if parsed[0][1]["pseudo"] and not include_pseudo: |
| 286 |
continue |
| 287 |
|
| 288 |
tagged_name = ins_name(ins) |
| 289 |
final[tagged_name] = parsed |
| 290 |
|
| 291 |
return final |
| 292 |
|
| 293 |
# Expand out an opcode name to something C-escaped |
| 294 |
|
| 295 |
def opname_to_c(name): |
| 296 |
return name.lower().replace('*', 'fma_').replace('+', 'add_').replace('.', '_') |
| 297 |
|
| 298 |
# Expand out distinct states to distrinct instructions, with a placeholder |
| 299 |
# condition for instructions with a single state |
| 300 |
|
| 301 |
def expand_states(instructions): |
| 302 |
out = {} |
| 303 |
|
| 304 |
for ins in instructions: |
| 305 |
c = instructions[ins] |
| 306 |
|
| 307 |
for ((test, desc), i) in zip(c, range(len(c))): |
| 308 |
# Construct a name for the state |
| 309 |
name = ins + (('.' + str(i)) if len(c) > 1 else '') |
| 310 |
|
| 311 |
out[name] = (ins, test if test is not None else [], desc) |
| 312 |
|
| 313 |
return out |
| 314 |
|
| 315 |
# Drop keys used for packing to simplify IR representation, so we can check for |
| 316 |
# equivalence easier |
| 317 |
|
| 318 |
def simplify_to_ir(ins): |
| 319 |
return { |
| 320 |
'staging': ins['staging'], |
| 321 |
'srcs': len(ins['srcs']), |
| 322 |
'dests': ins['dests'], |
| 323 |
'variable_dests': ins['variable_dests'], |
| 324 |
'variable_srcs': ins['variable_srcs'], |
| 325 |
'modifiers': [[m[0][0], m[2]] for m in ins['modifiers']], |
| 326 |
'immediates': [m[0] for m in ins['immediates']] |
| 327 |
} |
| 328 |
|
| 329 |
# Converstions to integers default to rounding-to-zero |
| 330 |
# All other opcodes default to rounding to nearest even |
| 331 |
def default_round_to_zero(name): |
| 332 |
# 8-bit int to float is exact |
| 333 |
subs = ['_TO_U', '_TO_S', '_TO_V2U', '_TO_V2S', '_TO_V4U', '_TO_V4S'] |
| 334 |
return any([x in name for x in subs]) |
| 335 |
|
| 336 |
def combine_ir_variants(instructions, key): |
| 337 |
seen = [op for op in instructions.keys() if op[1:] == key] |
| 338 |
variant_objs = [[simplify_to_ir(Q[1]) for Q in instructions[x]] for x in seen] |
| 339 |
variants = sum(variant_objs, []) |
| 340 |
|
| 341 |
# Accumulate modifiers across variants |
| 342 |
modifiers = {} |
| 343 |
|
| 344 |
for s in variants[0:]: |
| 345 |
# Check consistency |
| 346 |
assert(s['srcs'] == variants[0]['srcs']) |
| 347 |
assert(s['dests'] == variants[0]['dests']) |
| 348 |
assert(s['immediates'] == variants[0]['immediates']) |
| 349 |
assert(s['staging'] == variants[0]['staging']) |
| 350 |
|
| 351 |
for name, opts in s['modifiers']: |
| 352 |
if name not in modifiers: |
| 353 |
modifiers[name] = copy.deepcopy(opts) |
| 354 |
else: |
| 355 |
modifiers[name] += opts |
| 356 |
|
| 357 |
# Great, we've checked srcs/immediates are consistent and we've summed over |
| 358 |
# modifiers |
| 359 |
return { |
| 360 |
'key': key, |
| 361 |
'srcs': variants[0]['srcs'], |
| 362 |
'dests': variants[0]['dests'], |
| 363 |
'variable_dests': variants[0]['variable_dests'], |
| 364 |
'variable_srcs': variants[0]['variable_srcs'], |
| 365 |
'staging': variants[0]['staging'], |
| 366 |
'immediates': sorted(variants[0]['immediates']), |
| 367 |
'modifiers': modifiers, |
| 368 |
'v': len(variants), |
| 369 |
'ir': variants, |
| 370 |
'rtz': default_round_to_zero(key) |
| 371 |
} |
| 372 |
|
| 373 |
# Partition instructions to mnemonics, considering units and variants |
| 374 |
# equivalent. |
| 375 |
|
| 376 |
def partition_mnemonics(instructions): |
| 377 |
key_func = lambda x: x[1:] |
| 378 |
sorted_instrs = sorted(instructions.keys(), key = key_func) |
| 379 |
partitions = itertools.groupby(sorted_instrs, key_func) |
| 380 |
return { k: combine_ir_variants(instructions, k) for k, v in partitions } |
| 381 |
|
| 382 |
# Generate modifier lists, by accumulating all the possible modifiers, and |
| 383 |
# deduplicating thus assigning canonical enum values. We don't try _too_ hard |
| 384 |
# to be clever, but by preserving as much of the original orderings as |
| 385 |
# possible, later instruction encoding is simplified a bit. Probably a micro |
| 386 |
# optimization but we have to pick _some_ ordering, might as well choose the |
| 387 |
# most convenient. |
| 388 |
# |
| 389 |
# THIS MUST BE DETERMINISTIC |
| 390 |
|
| 391 |
def order_modifiers(ir_instructions): |
| 392 |
out = {} |
| 393 |
|
| 394 |
# modifier name -> (list of option strings) |
| 395 |
modifier_lists = {} |
| 396 |
|
| 397 |
for ins in sorted(ir_instructions): |
| 398 |
modifiers = ir_instructions[ins]["modifiers"] |
| 399 |
|
| 400 |
for name in modifiers: |
| 401 |
name_ = name[0:-1] if name[-1] in "0123" else name |
| 402 |
|
| 403 |
if name_ not in modifier_lists: |
| 404 |
modifier_lists[name_] = copy.deepcopy(modifiers[name]) |
| 405 |
else: |
| 406 |
modifier_lists[name_] += modifiers[name] |
| 407 |
|
| 408 |
for mod in modifier_lists: |
| 409 |
lst = list(OrderedDict.fromkeys(modifier_lists[mod])) |
| 410 |
|
| 411 |
# Ensure none is false for booleans so the builder makes sense |
| 412 |
if len(lst) == 2 and lst[1] == "none": |
| 413 |
lst.reverse() |
| 414 |
elif mod == "table": |
| 415 |
# We really need a zero sentinel to materialize DTSEL |
| 416 |
assert(lst[2] == "none") |
| 417 |
lst[2] = lst[0] |
| 418 |
lst[0] = "none" |
| 419 |
|
| 420 |
out[mod] = lst |
| 421 |
|
| 422 |
return out |
| 423 |
|
| 424 |
# Count sources for a simplified (IR) instruction, including a source for a |
| 425 |
# staging register if necessary |
| 426 |
def src_count(op): |
| 427 |
staging = 1 if (op["staging"] in ["r", "rw"]) else 0 |
| 428 |
return op["srcs"] + staging |
| 429 |
|
| 430 |
# Parses out the size part of an opocde name |
| 431 |
def typesize(opcode): |
| 432 |
if opcode[-3:] == '128': |
| 433 |
return 128 |
| 434 |
if opcode[-2:] == '48': |
| 435 |
return 48 |
| 436 |
elif opcode[-1] == '8': |
| 437 |
return 8 |
| 438 |
else: |
| 439 |
try: |
| 440 |
return int(opcode[-2:]) |
| 441 |
except: |
| 442 |
return 32 |