Skip to content

Commit

Permalink
Format the generator helper
Browse files Browse the repository at this point in the history
via black, version 20.8b1
  • Loading branch information
mattsta committed Jan 9, 2021
1 parent 8cf69a0 commit 62ae97a
Showing 1 changed file with 69 additions and 41 deletions.
110 changes: 69 additions & 41 deletions generateCPUDetailsToFeatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def enumerateFeatures_(feature):
""" Given a regex, run findall on target and complain if nothing found. """
features = []
found = None
with open(TARGET_FILE, 'r') as t:
with open(TARGET_FILE, "r") as t:
tFull = t.read()
found = re.findall(feature, tFull)
if not found:
Expand All @@ -37,13 +37,14 @@ def enumerateCPUFeatures():
# Line looks like:
# PROC_WITH_FEAT(Nehalem, "nehalem", PROC_64_BIT, FEATURE_SSE4_2)
return enumerateFeatures_(
"\nPROC_WITH_FEAT\((.*), \"(.*)\", PROC_64_BIT, FEATURE_(.*)\)")
'\nPROC_WITH_FEAT\((.*), "(.*)", PROC_64_BIT, FEATURE_(.*)\)'
)


def enumerateCPUAliases():
# Line looks like:
# PROC_ALIAS(Nehalem, "corei7")
return enumerateFeatures_("\nPROC_ALIAS\((.*), \"(.*)\"\)")
return enumerateFeatures_('\nPROC_ALIAS\((.*), "(.*)"\)')


# Now create multiple data structures:
Expand All @@ -65,37 +66,36 @@ def mergeCPUWithAliases(cpus, aliases, featureMap):
cpuFeatureMap = {}
for cpu in cpus:
internalName, marchName, feature = cpu
cpuFeatureMap[marchName] = {
"features": featureMap[feature], "aliasFor": None}
cpuFeatureMap[marchName] = {"features": featureMap[feature], "aliasFor": None}
for alias in aliases:
aliasInternal, aliasMarch = alias
if aliasInternal == internalName:
cpuFeatureMap[aliasMarch] = {
"features": featureMap[feature], "aliasFor": marchName}
"features": featureMap[feature],
"aliasFor": marchName,
}
return cpuFeatureMap


def printCPUFeatureMap(marchFeatureMap, formatter):
for march, value in sorted(marchFeatureMap.items(),
key=lambda x: x[1]["features"]):
alias = value['aliasFor']
features = value['features']
for march, value in sorted(marchFeatureMap.items(), key=lambda x: x[1]["features"]):
alias = value["aliasFor"]
features = value["features"]
formatter(march, alias, features)


def enumerateFeaturesByCPU(marchFeatureMap):
featureCPUMap = collections.defaultdict(list)
for march, value in marchFeatureMap.items():
features = value['features']
features = value["features"]
for feature in features:
featureCPUMap[feature].append(march)

return featureCPUMap


def printFeatureCPUMap(featureMarchMap, formatter):
for feature, marchs in sorted(featureMarchMap.items(),
key=lambda x: x[0]):
for feature, marchs in sorted(featureMarchMap.items(), key=lambda x: x[0]):
formatter(feature, marchs)


Expand All @@ -104,10 +104,8 @@ def printFeatureCPUMap(featureMarchMap, formatter):
# - system feature knowledge (when march=[a known uarch])
def writeCMakeHelper(featureMarchMap):
unsetAll = []
for feature, marchs in sorted(featureMarchMap.items(),
key=lambda x: x[0]):
unsetAll.append(
f" unset(${{RESULT_PREFIX}}_FEATURE_{feature} CACHE)")
for feature, marchs in sorted(featureMarchMap.items(), key=lambda x: x[0]):
unsetAll.append(f" unset(${{RESULT_PREFIX}}_FEATURE_{feature} CACHE)")
unsetAll.append(f" unset(${{RESULT_PREFIX}}_{feature} CACHE)")
unsetAll = "\n".join(unsetAll)

Expand Down Expand Up @@ -171,7 +169,10 @@ def writeCMakeHelper(featureMarchMap):
if (("${{BUILDING_FLAGS}}" MATCHES "march=native") OR
(NOT "${{BUILDING_FLAGS}}" MATCHES "march=") OR
("${{KNOWN_BUILD_TYPE}}" STREQUAL ""))
""".format(unsetAll)) # empty format() because we already doubled all the {{}}
""".format(
unsetAll
)
) # empty format() because we already doubled all the {{}}

# In cmake, use:
# if (CLANG_FEATURE_AVX2) ...
Expand All @@ -191,7 +192,8 @@ def writeCMakeHelper(featureMarchMap):
# on architecture instead of headers, we're fine using
# any well-known and widely available header here.
def printCMakeDetectionCapability(feature, marchs):
x.write("""
x.write(
"""
check_c_source_compiles("
#ifndef __{}__
#error not supported
Expand All @@ -201,52 +203,76 @@ def printCMakeDetectionCapability(feature, marchs):
return 0;
}}" ${{RESULT_PREFIX}}_FEATURE_{})
set(${{RESULT_PREFIX}}_{} ${{${{RESULT_PREFIX}}_FEATURE_{}}} PARENT_SCOPE)
""".format(feature, feature, feature, feature, feature, feature, feature, feature))
""".format(
feature,
feature,
feature,
feature,
feature,
feature,
feature,
feature,
)
)

# Add a catch-all "AVX512 support" query
if feature == "AVX512F":
x.write(
"""set(${{RESULT_PREFIX}}_AVX512 ${{RESULT_PREFIX}}_FEATURE_{} PARENT_SCOPE)
""".format(feature))
""".format(
feature
)
)

printFeatureCPUMap(featureMarchMap, printCMakeDetectionCapability)

x.write(
"""
else()
# else, use march=string to check feature capability
""")
"""
)

# In cmake, use:
# if (LIST_CLANG_FEATURE_MAP_AVX2 IN_LIST marchvalue)
def printCMakeFeatureLists(feature, marchs):
x.write("""
x.write(
"""
set(CLANG_FEATURE_MAP_{} {})
message(STATUS "Querying for ${{marchName}} {}...")
foreach(marchName IN LISTS CLANG_FEATURE_MAP_{})
if ("${{BUILDING_FLAGS}}" MATCHES "march=${{marchName}}")
message(STATUS "Querying for ${{marchName}} {}... - found")
set(${{RESULT_PREFIX}}_{} YES PARENT_SCOPE)
""".format(feature, " ".join(marchs), feature, feature, feature, feature))
""".format(
feature, " ".join(marchs), feature, feature, feature, feature
)
)

# Add a catch-all "AVX512 support" query
if feature == "AVX512F":
x.write(
""" set(${{RESULT_PREFIX}}_AVX512 YES PARENT_SCOPE)
""".format(feature))
""".format(
feature
)
)

x.write(""" endif()
x.write(
""" endif()
set(CMAKE_REQUIRED_FLAGS)
endforeach(marchName)
""")
"""
)

printFeatureCPUMap(featureMarchMap, printCMakeFeatureLists)

x.write(
"""
endif()
endfunction()
""")
"""
)


if __name__ == "__main__":
Expand All @@ -256,33 +282,35 @@ def printCMakeFeatureLists(feature, marchs):

featureTree = createFeatureTree(featuresByAge)

marchFeatureMap = mergeCPUWithAliases(cpuFeatures, cpuAliases,
featureTree)
marchFeatureMap = mergeCPUWithAliases(cpuFeatures, cpuAliases, featureTree)

featureMarchMap = enumerateFeaturesByCPU(marchFeatureMap)

# print(featuresByAge)
# print(featureTree)
# print(cpuFeatures)
# print(cpuAliases)
# print(marchFeatureMap)
# print(featuresByAge)
# print(featureTree)
# print(cpuFeatures)
# print(cpuAliases)
# print(marchFeatureMap)

# Sort feature map by list of length of features
# from shortest to longest

def debugPrintCFM(march, alias, features):
features.sort()
print("{}{}: ({} features)\n{}\n".
format(march,
" (alias for {})".format(alias) if alias else "",
len(features), features))
print(
"{}{}: ({} features)\n{}\n".format(
march,
" (alias for {})".format(alias) if alias else "",
len(features),
features,
)
)

printCPUFeatureMap(marchFeatureMap, debugPrintCFM)

def debugPrintFCM(feature, marchs):
marchs.sort()
print("{}: ({} marchs)\n{}\n".
format(feature, len(marchs), marchs))
print("{}: ({} marchs)\n{}\n".format(feature, len(marchs), marchs))

printFeatureCPUMap(featureMarchMap, debugPrintFCM)

Expand Down

0 comments on commit 62ae97a

Please sign in to comment.