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

[IR] Add Support for Struct Types #265

Merged
merged 9 commits into from
Nov 26, 2024
Merged

Conversation

zzzDavid
Copy link
Contributor

@zzzDavid zzzDavid commented Nov 25, 2024

Description

This PR introduces support for struct types in the Allo. It adds the ability to define, manipulate, and resolve struct types in the IR, enabling more expressive representations and operations in programs. The implementation resolves dictionary literals to structs and provides infrastructure for struct construction, field access, and type inference.

Problems

  • Lack of support for struct types in the Allo frontend.

Proposed Solutions

  • Added struct type handling across the compiler pipeline:
    • IR/Builder: Added methods for struct construction (StructConstructOp) and field access (StructGetOp).
    • Type Inference: Enhanced type inference for struct types and dictionary literals, mapping keys to their respective types.
    • Symbol Resolver: Resolved dictionary literals into struct types during AST traversal.
    • IR Transformations: Implemented lowering for composite (struct) types.
  • Introduced corresponding unit tests to validate the struct implementation.
  • Enhanced test_types.py to include a comprehensive test case for a simple struct-based kernel.

Examples

Input Program

struct_type = T.Struct({"x": int32, "y": float32})

def kernel(x: int32[16], y: float32[16]) -> int32:
    sum_val: int32 = 0
    for i in range(16):
        point: struct_type = {"x": x[i], "y": y[i]}
        sum_val += point["x"]
        sum_val += int(point["y"])
    return sum_val

Expected Behavior

  • Generated IR:
    • Struct construction (StructConstructOp) for creating the point variable.
    • Field access (StructGetOp) for extracting "x" and "y" fields from point.
  • Execution: Outputs correct sum of integer and float elements.

Checklist

  • PR's title starts with a category (e.g. [IR], [Struct], [TypeSystem]).
  • Changes are complete (i.e., coding and testing for this PR are finished).
  • All changes have test coverage (unit tests included for struct handling and edge cases).
  • Code is well-documented.

Copy link
Member

@chhzh123 chhzh123 left a comment

Choose a reason for hiding this comment

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

Mostly LGTM

allo/ir/infer.py Outdated Show resolved Hide resolved
@chhzh123
Copy link
Member

Please let me know if you are ready to merge @zzzDavid

@zzzDavid
Copy link
Contributor Author

Yes, it's ready

Copy link
Member

@chhzh123 chhzh123 left a comment

Choose a reason for hiding this comment

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

Thanks!

@chhzh123 chhzh123 merged commit e9275ef into cornell-zhang:main Nov 26, 2024
1 check passed
@chhzh123 chhzh123 mentioned this pull request Nov 26, 2024
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.

2 participants