+_TYPE_SPLIT_RE = re.compile(r'(?P<type>int|uint|float|bool)(?P<bits>\d+)?')
+
+def type_has_size(type_):
+ m = _TYPE_SPLIT_RE.match(type_)
+ assert m is not None, 'Invalid NIR type string: "{}"'.format(type_)
+ return m.group('bits') is not None
+
+def type_size(type_):
+ m = _TYPE_SPLIT_RE.match(type_)
+ assert m is not None, 'Invalid NIR type string: "{}"'.format(type_)
+ assert m.group('bits') is not None, \
+ 'NIR type string has no bit size: "{}"'.format(type_)
+ return int(m.group('bits'))
+
+def type_sizes(type_):
+ if type_has_size(type_):
+ return [type_size(type_)]
+ elif type_ == 'float':
+ return [16, 32, 64]
+ else:
+ return [8, 16, 32, 64]
+
+def type_base_type(type_):
+ m = _TYPE_SPLIT_RE.match(type_)
+ assert m is not None, 'Invalid NIR type string: "{}"'.format(type_)
+ return m.group('type')
+