Skip to content

Make argument order of imap_go's black box consistent with #2543 #2946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 6, 2025

Conversation

martijnbastiaan
Copy link
Member

@martijnbastiaan martijnbastiaan commented Apr 28, 2025

Fixes #2904

Still TODO:

Copy link
Member

@DigitalBrains1 DigitalBrains1 left a comment

Choose a reason for hiding this comment

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

If I'm right about this, it's worrying that the test doesn't catch the mistakes. It is unclear to me, without more investigation, how this black box works. For a proper review, maybe I ought to read up on its workings...

@martijnbastiaan
Copy link
Member Author

martijnbastiaan commented May 6, 2025

I've renamed everything with a dumb Python script now:

#!/usr/bin/env python3
import random
import re
import string
from ruamel.yaml import YAML

TYPE_SIG = "imap_go :: (Index n -> a -> b) -> Vec m a -> Index n -> Vec m b"

PRIM_NAME = "Clash.Sized.Vector.imap_go"

PRIM_FILES = [
    "clash-lib/prims/systemverilog/Clash_Sized_Vector.primitives.yaml",
    "clash-lib/prims/verilog/Clash_Sized_Vector.primitives.yaml",
    "clash-lib/prims/vhdl/Clash_Sized_Vector.primitives.yaml",
]

TAGS = [
    (r"~OUTPUTUSAGE\[{n}\]",  r"~OUTPUTUSAGE[{n}]"),
    (r"~TYP\[{n}\]",          r"~TYP[{n}]"),
    (r"~ARG\[{n}\]",          r"~ARG[{n}]"),
    (r"~INST {n}",            r"~INST {n}"),
    (r"~VAR\[(\w+)\]\[{n}\]", r"~VAR[\1][{n}]"),
]


MAX_INDEX = 100

def rename_index(n):
    if n == 0:
        return 2
    return n - 1

def find(haystack, needle_func):
    for item in haystack:
        if needle_func(item):
            return item

    raise ValueError(f"Could not find needle in {haystack}")

def generate_random_string(length):
    return ''.join(random.choice(string.ascii_letters) for i in range(length))

def replace_tag(template, pattern, replacement):
    random_strings = [generate_random_string(20) for _ in range(MAX_INDEX)]

    for i in range(MAX_INDEX):
        template = re.sub(
            pattern.format(n=i),
            replacement.format(n=random_strings[i]),
            template
        )

    for i in range(MAX_INDEX):
        template = re.sub(
            pattern.format(n=random_strings[i]),
            replacement.format(n=rename_index(i)),
            template
        )

    return template

def replace_tags(template):
    for tag, replacement in TAGS:
        template = replace_tag(template, tag, replacement)
    return template

def replace_in_prim_file(prim_file):
    yaml = YAML()
    with open(prim_file, "r") as f:
        parsed = yaml.load(f)

    prim = find(parsed, lambda x: x["BlackBox"]["name"] == PRIM_NAME)
    prim["BlackBox"]["type"] = TYPE_SIG
    prim["BlackBox"]["template"] = replace_tags(prim["BlackBox"]["template"])

    with open(prim_file, "w") as f:
        yaml.dump(parsed, f)

def main():
    for prim_file in PRIM_FILES:
        replace_in_prim_file(prim_file)

if __name__ == "__main__":
    main()

Copy link
Member

@DigitalBrains1 DigitalBrains1 left a comment

Choose a reason for hiding this comment

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

I've played around with it a bit and it looks fine now!

@martijnbastiaan martijnbastiaan merged commit 998ac6e into master May 6, 2025
14 checks passed
@martijnbastiaan martijnbastiaan deleted the fix2904 branch May 6, 2025 17:43
martijnbastiaan added a commit that referenced this pull request May 6, 2025
Make argument order of `imap_go`'s black box consistent with #2543  (copy #2946)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BlackBox of imap_go is broken
2 participants