Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ jobs:

diff cpu/microarchitectures.json <(jq . cpu/microarchitectures.json)
diff cpu/cpuid.json <(jq . cpu/cpuid.json)
- name: Check DAG consistency with feature sets
run: |
python3 tests/check_dag_consistency.py
5 changes: 3 additions & 2 deletions cpu/microarchitectures.json
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,8 @@
},
"cannonlake": {
"from": [
"skylake"
"skylake",
"x86_64_v4"
],
"vendor": "GenuineIntel",
"features": [
Expand Down Expand Up @@ -3884,7 +3885,7 @@
},
"ampere1a": {
"from": [
"neoverse_n1",
"ampere1",
"armv8.6a"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"armv8.6a"

],
"vendor": "Ampere",
Expand Down
96 changes: 96 additions & 0 deletions tests/check_dag_consistency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env python3
# Copyright 2019-2020 Lawrence Livermore National Security, LLC and other

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2019-2020 ?

# Archspec Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
"""Check that the explicit ``from`` DAG is consistent with feature set inclusion.

Every microarchitecture names a set of features, so feature inclusion defines a
partial order of its own. The ``from`` DAG must agree with it in two directions:

* A descendant must list every feature of its ancestors, up to ``feature_aliases``.
* Within a family, a microarchitecture whose feature set strictly contains
another's must descend from it when both share a vendor, or when the smaller
one is a generic level. Cross-vendor lineage is intentionally not required:
portability between vendors is expressed through the generic levels only.
"""
import itertools
import json
import pathlib
import sys

JSON_DIR = pathlib.Path(__file__).parent.parent / "cpu"


def main() -> int:
with open(JSON_DIR / "microarchitectures.json") as f:
data = json.load(f)

uarchs = data["microarchitectures"]
aliases = data.get("feature_aliases", {})

ancestors = {}

def transitive_ancestors(name):
if name not in ancestors:
result = set()
for parent in uarchs[name]["from"]:
result.add(parent)
result |= transitive_ancestors(parent)
ancestors[name] = result
return ancestors[name]

def family(name):
roots = [a for a in transitive_ancestors(name) | {name} if not uarchs[a]["from"]]
assert len(roots) == 1, f"{name} has multiple family roots: {roots}"
return roots[0]

def closed_features(name):
"""The declared features, closed under the alias rules."""
result = set(uarchs[name]["features"])
for feature, rule in aliases.items():
if "any_of" in rule and result & set(rule["any_of"]):
result.add(feature)
if "families" in rule and family(name) in rule["families"]:
result.add(feature)
return result

features = {name: closed_features(name) for name in uarchs}
violations = []

for ancestor in uarchs:
for descendant in uarchs:
if ancestor not in transitive_ancestors(descendant):
continue
missing = features[ancestor] - features[descendant]
if missing:
violations.append(
f"{descendant} descends from {ancestor} but lacks its features: "
f"{', '.join(sorted(missing))}"
)

for smaller, larger in itertools.permutations(uarchs, 2):
if family(smaller) != family(larger):
continue
# an entry with no declared features asserts nothing, and alias closure may still
# give it synthetic ones, so it cannot imply an edge
if not uarchs[smaller]["features"]:
continue
if not features[smaller] < features[larger]:
continue
same_vendor = uarchs[smaller]["vendor"] == uarchs[larger]["vendor"]
if not same_vendor and uarchs[smaller]["vendor"] != "generic":
continue
if smaller not in transitive_ancestors(larger):
violations.append(
f"{larger} has every feature of {smaller} and more, but does not descend "
f"from it"
)

for violation in violations:
print(f"ERROR: {violation}")
return 1 if violations else 0


if __name__ == "__main__":
sys.exit(main())
Loading