Skip to content
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

Externalize builtins and fix bugs #637

Merged
merged 10 commits into from
Oct 13, 2024
Merged

Externalize builtins and fix bugs #637

merged 10 commits into from
Oct 13, 2024

Conversation

yamaguchi1024
Copy link
Member

@yamaguchi1024 yamaguchi1024 commented Apr 30, 2024

  • Rename builtins to externs
  • Externalize externs
  • Fix bug 1: extern did not work on read expressions with index accesses
  • Fix bug 2: extern fnarg codegen should be comp_e not comp_fnarg
  • Fix bug 3: extern did not work on constants
  • Change pattern_match and parse_fragment to take local and global variables in scope.

Fixes #505

@codecov-commenter
Copy link

codecov-commenter commented Apr 30, 2024

Codecov Report

Attention: Patch coverage is 93.47181% with 22 lines in your changes missing coverage. Please review.

Project coverage is 87.94%. Comparing base (b8eba08) to head (4040e7a).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/exo/extern.py 80.00% 4 Missing ⚠️
src/exo/parse_fragment.py 60.00% 4 Missing ⚠️
src/exo/pyparser.py 86.36% 3 Missing ⚠️
src/exo/API_cursors.py 75.00% 2 Missing ⚠️
src/exo/LoopIR_unification.py 33.33% 2 Missing ⚠️
src/exo/prec_analysis.py 91.30% 2 Missing ⚠️
src/exo/stdlib/inspection.py 33.33% 2 Missing ⚠️
src/exo/API.py 80.00% 1 Missing ⚠️
src/exo/LoopIR_compiler.py 95.45% 1 Missing ⚠️
src/exo/LoopIR_pprint.py 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #637      +/-   ##
==========================================
+ Coverage   87.78%   87.94%   +0.15%     
==========================================
  Files          84       86       +2     
  Lines       20721    20903     +182     
==========================================
+ Hits        18190    18383     +193     
+ Misses       2531     2520      -11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yamaguchi1024 yamaguchi1024 changed the title builtins Externalize builtins and fix some bugs May 1, 2024
@yamaguchi1024 yamaguchi1024 marked this pull request as ready for review May 1, 2024 00:52
@yamaguchi1024 yamaguchi1024 requested a review from kehemo May 1, 2024 00:52
@yamaguchi1024 yamaguchi1024 changed the title Externalize builtins and fix some bugs Externalize builtins and fix bugs May 1, 2024
@@ -434,17 +434,17 @@ def from_lines(x):
// Compiler feature macros adapted from Hedley (public domain)
// https://github.com/nemequ/hedley

#if defined(__has_builtin)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am confused. Why were these changed? These are referring to C builtins, not Exo.

Copy link
Member Author

Choose a reason for hiding this comment

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

omg good catch

private_fwd_decls = []
proc_bodies = []
instrs_global = []
new_proc_list = []
Copy link
Collaborator

Choose a reason for hiding this comment

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

I found this name confusing. Maybe you can call it non_instr_procs. Or at least add a comment after it.

Copy link
Member Author

Choose a reason for hiding this comment

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

analyzed_proc_list ?

@SamirDroubi
Copy link
Collaborator

I think a more ergonomic way of implementing this feature would be something like the following:

@extern("{a_data} < {b_data} ? {c_data} : {d_data}")
def select(a: f32, b: f32, c: f32, d: f32) -> f32:
      return c if a < b else d

This would effectively encode all the information you are encoding in the class and it is more akin to how we externalized instructions definitions. There is the issue however of how to make sure these externs' arguments are dynamically typed which was the case before. We might want to allow these externs to be overloaded to avoid having to write on per-type or maybe this is not an issue because it is the same problem for instruction. We might though need to allow them to be rewritten modulo precisions. e.g. swap_builtin(proc, proc.find("select_R(_)"), select_f32) or something like that.

@yamaguchi1024
Copy link
Member Author

I like this interface!! It's like we're adding Exo type annotation (e.g., f32) to Python function, but the @extern function body is in Python.

@yamaguchi1024
Copy link
Member Author

@SamirDroubi I think @extern makes sense and we should do it at some point, but I don't have an energy to do it now so I'll just move the typechecking into the compiler and parse the proc's externs instead of using call_depth

@yamaguchi1024
Copy link
Member Author

Flagging here that we don't have documentation for builtins/externs. Maybe worth fixing that in this PR

- Rename builtins to externs
- Externalize externs
- Fix bug 1: extern did not work on read expressions with index accesses
- Fix bug 2: extern fnarg codegen should be comp_e not comp_fnarg
- Fix bug 3: extern did not work on constants
- Change pattern_match and parse_fragment to take local and global variables in scope.
@yamaguchi1024 yamaguchi1024 requested review from akeley98 and removed request for skeqiqevian and SamirDroubi October 11, 2024 14:27
Copy link
Contributor

@akeley98 akeley98 left a comment

Choose a reason for hiding this comment

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

I tried to scan through all the nontrivially changed files and the grammar changes and it looks reasonable so far, except the one performance note I made. I don't understand what call_depth is doing very well ... this is something about being able to look-up Python local variables?

@@ -166,15 +172,18 @@ def __init__(
self.is_fragment = is_fragment

self.push()
special_cases = ["stride"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Creating this special_cases list and then searching it linearly (in the s.value.func.id in special_cases) looks like potentially expensive "accumulated cost" considering how many times this parser is used, e.g., to parse all procs and all patterns in @instr. Can this be an is_special_case lambda that just searches for the func id in the 2 namespaces (+ the special stride case)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Something like

        def is_special_case(func_id):
            return (
                func_id == "stride"
                or isinstance(self.globals.get(func_id), Extern)
                or isinstance(self.locals.get(func_id), Extern)
            )
...
            if len(module_ast) == 1:
                s = module_ast[0]
                if isinstance(s, pyast.Expr) and (
                    not isinstance(s.value, pyast.Call)
                    or is_special_case(s.value.func.id)
                ):
                    is_expr = True

Copy link
Collaborator

Choose a reason for hiding this comment

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

i think special_cases is only used once which is to check patterns that are of the form <extern>(...) so I don't think there is a big cost to doing this

Copy link
Member Author

Choose a reason for hiding this comment

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

It's possible that scanning all the global and local variables (at the beginning of Parser) is expensive, but looking up func_id each time in globals and locals is also expensive, so 🤷‍♀️

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is fine

@kehemo
Copy link
Collaborator

kehemo commented Oct 13, 2024

I tried to scan through all the nontrivially changed files and the grammar changes and it looks reasonable so far, except the one performance note I made. I don't understand what call_depth is doing very well ... this is something about being able to look-up Python local variables?

yeah, when the user calls the api to create a pattern, the pattern might reference externs in the local scope or global scope where the user did the call. the api might make additional calls to other api functions, which then need to figure out the local scope of the function that originally called into the api. so the api needs to keep track of how many calls were made after the user initially called into the api

Copy link
Collaborator

@kehemo kehemo left a comment

Choose a reason for hiding this comment

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

looks good

args = []
for a in e.args:
if a.type == T.R:
args.append(self.coerce_e(a, btyp))
Copy link
Collaborator

Choose a reason for hiding this comment

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

are externs expected to support every available precision on Exo?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes! But this is coercing the argument when it was T.R (which means the precision was not specified during scheduling)

@@ -166,15 +172,18 @@ def __init__(
self.is_fragment = is_fragment

self.push()
special_cases = ["stride"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think special_cases is only used once which is to check patterns that are of the form <extern>(...) so I don't think there is a big cost to doing this

@yamaguchi1024 yamaguchi1024 merged commit cb416d4 into main Oct 13, 2024
9 checks passed
@yamaguchi1024 yamaguchi1024 deleted the yam branch October 13, 2024 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

proc/builtin calls semantics
5 participants