Created
June 30, 2020 16:51
-
-
Save froydnj/1192911af46661fb62e724957099d4e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
import sys | |
# nvptx preprocessor definitions. | |
SM_80 = "sm_80" | |
SM_75 = "sm_75|" + SM_80 | |
SM_72 = "sm_72|" + SM_75 | |
SM_70 = "sm_70|" + SM_72 | |
SM_60 = "sm_60|sm_61|sm_62|" + SM_70 | |
PTX70 = "ptx70" | |
PTX65 = "ptx65|" + PTX70 | |
PTX64 = "ptx64|" + PTX65 | |
PTX63 = "ptx63|" + PTX64 | |
PTX61 = "ptx61|" + PTX63 | |
PTX60 = "ptx60|" + PTX61 | |
preprocessor_features = { | |
"PTX60": PTX60, | |
"SM": None, | |
"60": None, | |
"SM_60": SM_60, | |
"HVXV65": None, | |
"HVXV60": None, | |
"HVXV62": None, | |
"HVXV66": None, | |
"V5": None, | |
"V67": None, | |
"V66": None, | |
"V60": None, | |
"V62": None, | |
"V65": None, | |
"AND\\(SM_70,PTX60\\)": SM_70 + "|" + PTX60, | |
"AND\\(SM_70,PTX61\\)": SM_70 + "|" + PTX61, | |
"AND\\(SM_70,PTX63\\)": SM_70 + "|" + PTX63, | |
"AND\\(SM_72,PTX63\\)": SM_72 + "|" + PTX63, | |
"AND\\(SM_75,PTX63\\)": SM_75 + "|" + PTX63, | |
} | |
name_re = "(?P<name>[^, ]+)" | |
type_re = "(?P<type>\"[^\"]*\")" | |
attr_re = "(?P<attr>\"[^\"]*\")" | |
# The alternatives here are a hack to catch NVPTX and other preprocessor macro users. | |
feat_re = "(?P<feat>\"[^\"]*\"|%s)" % '|'.join(preprocessor_features.keys()) | |
head_re = "(?P<head>\"[^\"]+\")" | |
lang_re = "(?P<lang>[A-Z0-9_]+)" | |
def regex_for(macro, *args): | |
# Anchor so BUILTIN doesn't get confused with TARGET_BUILTIN or similar. | |
return re.compile("^%s\\(%s\\)(?P<trailing>.*)" % (macro, " *, *".join(args))) | |
BUILTIN = regex_for('BUILTIN', name_re, type_re, attr_re) | |
ATOMIC_BUILTIN = regex_for('ATOMIC_BUILTIN', name_re, type_re, attr_re) | |
LIBBUILTIN = regex_for('LIBBUILTIN', name_re, type_re, attr_re, head_re, lang_re) | |
LANGBUILTIN = regex_for('LANGBUILTIN', name_re, type_re, attr_re, lang_re) | |
TARGET_BUILTIN = regex_for('TARGET_BUILTIN', name_re, type_re, attr_re, feat_re) | |
TARGET_HEADER_BUILTIN = regex_for('TARGET_HEADER_BUILTIN', name_re, type_re, attr_re, head_re, lang_re, feat_re) | |
lang_subs = { | |
"GNU_LANG": "GnuLang", | |
"C_LANG": "CLang", | |
"CXX_LANG": "CxxLang", | |
"OBJC_LANG": "ObjcLang", | |
"MS_LANG": "MsLang", | |
"OCLC20_LANG": "OCLC20Lang", | |
"OCLC1X_LANG": "OCLC1xLang", | |
"OMP_LANG": "OMPLang", | |
"ALL_LANGUAGES": "AllLanguages", | |
"ALL_MS_LANGUAGES": "AllMSLanguages", | |
"ALL_GNU_LANGUAGES": "AllGnuLanguages", | |
"ALL_OCLC_LANGUAGES": "AllOCLCLanguages", | |
} | |
Builtin = "def {name} : Builtin<{type}, {attr}>;{trailing}" | |
AtomicBuiltin = "def {name} : Builtin<{type}, {attr}>;{trailing}" | |
LibBuiltin = "def {name} : LibraryBuiltin<{type}, {attr}, {head}, {lang}>;{trailing}" | |
LangBuiltin = "def {name} : LangBuiltin<{type}, {attr}, {lang}>;{trailing}" | |
TargetBuiltin = "def {name} : TargetBuiltin<{type}, {attr}, {feat}>;{trailing}" | |
TargetHeaderBuiltin = "def {name} : TargetHeaderBuiltin<{type}, {attr}, {head}, {lang}, {feat}>;{trailing}" | |
builtins = { | |
TARGET_BUILTIN: TargetBuiltin, | |
TARGET_HEADER_BUILTIN: TargetHeaderBuiltin, | |
BUILTIN: Builtin, | |
LANGBUILTIN: LangBuiltin, | |
LIBBUILTIN: LibBuiltin, | |
ATOMIC_BUILTIN: AtomicBuiltin, | |
} | |
builtins_file = sys.argv[1] | |
# Remove "Builtins" prefix and ".def" suffix. | |
arch = builtins_file[8:-4] | |
builtins_file = open(builtins_file, 'r') | |
# Try to handle converting the generic builtins just like architecture ones. | |
print_target_arch_wrap = arch != "" | |
have_seen_first_def = False | |
processing_atomic = False | |
for line in builtins_file.readlines(): | |
line = line.rstrip() | |
# Skip preprocessor macros. | |
if line.startswith('#'): | |
continue | |
# Two steps to avoid calling re.match twice. | |
matched = [(re.match(regex, line), regex, tablegen) | |
for (regex, tablegen) in builtins.items()] | |
matched = [x for x in matched if x[0]] | |
if not matched: | |
print line | |
continue | |
(m, regex, tablegen) = matched[0] | |
fields = m.groupdict() | |
if regex is ATOMIC_BUILTIN: | |
if not processing_atomic: | |
processing_atomic = True | |
print 'let Atomic = 1 in {' | |
else: | |
if processing_atomic: | |
processing_atomic = False | |
print '}' | |
# Translate language enums to tablegen definitions. | |
if 'lang' in fields: | |
fields['lang'] = lang_subs[fields['lang']] | |
# We should really be rewriting 'feat' according to preprocessor_features, | |
# above, but that's not complete yet -- and we probably really want to | |
# translate the preprocessor macros into tablegen definitions anyway. Just | |
# turn things into strings if they're not already strings. | |
if 'feat' in fields and not fields['feat'].startswith('"'): | |
fields['feat'] = '"%s"' % fields['feat'] | |
def_line = tablegen.format(**fields) | |
if processing_atomic: | |
def_line = ' ' + def_line | |
if not have_seen_first_def: | |
print 'include "BuiltinTableGenDefs.inc"' | |
print '' | |
if print_target_arch_wrap: | |
print 'let TargetArch = "%s" in {' % arch | |
have_seen_first_def = True | |
print def_line | |
if have_seen_first_def: | |
if print_target_arch_wrap: | |
print '}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment