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

Extend ASL support: strings, comparisons, etc. #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

alastairreid
Copy link

This PR:

  • fixes minor errors in tests
  • aligns the spelling of primops with their name in ASLi
  • adds some new operations (e.g., comparisons)
  • adds some new external functions (e.g., print functions)
  • adds the type !asl.string and the operation asl.constant_string
  • adds a disgusting hack to let me use scf.if because I need help adding a proper asl.if
    (this hack is in a separate commit to make it easier to revert/fix)

I have not added a test for each of the new operations yet.

If used with this branch of ASLi

https://github.com/alastairreid/asl-interpreter/tree/areid-mlir

whose HEAD is currently this commit

commit c679fe6a23d0fbedad72663a7f3311db3e83dc67 
Author: Alastair Reid <[email protected]>
Date:   Thu Jan 9 11:58:12 2025 +0000

MLIR: add lit test support

then running the following commands

export ASL_XDSL_DIR=`pwd`/../asl-xdsl
make test_backend_mlir

will run 119 tests and 17 of them will pass.

This makes it match the name used in ASLi
Also changes the spelling of some primops to match ASLi's spelling
This hack consists of

- a bogus operation 'asl.bool_to_if' that fills the gap between
  how ASL represents bools and how scf represents them
- limited implementations of scf.if and scf.yield that are
  limited to the case that no mutable local variables are
  modified in the if statement

The right way to do this is to add asl.if which would

1) Support a list of elsif blocks
2) Use a region for the guard on each of the elsif blocks
3) Optionally use a region for the guard on the if block
   (not needed but it is more symmetric)

This hack let me run a bunch of tests for operations
that return boolean
Comment on lines +164 to +165
@irdl_attr_definition
class StringAttr(Data[str]):
Copy link
Member

Choose a reason for hiding this comment

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

I would recommend using the StringAttr in the builtin dialect

Comment on lines +426 to +438
@classmethod
def parse(cls, parser: Parser) -> ConstantStringOp:
"""Parse the operation."""
value = parser.parse_str_literal()
attr_dict = parser.parse_optional_attr_dict()
return ConstantStringOp(value, attr_dict)

def print(self, printer: Printer) -> None:
"""Print the operation."""
printer.print(" ", '"' + self.value.data + '"')
if self.attributes:
printer.print(" ")
printer.print_attr_dict(self.attributes)
Copy link
Member

@superlopuh superlopuh Jan 10, 2025

Choose a reason for hiding this comment

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

Would something like this work instead?

assembly_format = "$value attr-dict"

arg = operand_def(BoolType())
res = result_def(builtin.IntegerType(1))

assembly_format = "$arg `:` type($arg) `->` type($res) attr-dict"
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure why, but the attr dict tends to be before the type by convention

Suggested change
assembly_format = "$arg `:` type($arg) `->` type($res) attr-dict"
assembly_format = "$arg attr-dict `:` type($arg) `->` type($res)"

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually, that's because I moved it to the end on the other operations.
Mostly because I hate being it at the beginning, because it is "optional" in a way.

Copy link
Member

Choose a reason for hiding this comment

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

Are there other dialects that do this? Seems to me like we should stick with the convention if not.

results = [] # hack that is maybe good enough for the print_bool function
if cond:
args = interpreter.run_ssacfg_region(
op.true_region, tuple(results), "then_region"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
op.true_region, tuple(results), "then_region"
op.true_region, (), "then_region"

)
else:
args = interpreter.run_ssacfg_region(
op.false_region, tuple(results), "else_region"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
op.false_region, tuple(results), "else_region"
op.false_region, (), "else_region"

@@ -40,6 +41,40 @@ def run_call(
) -> tuple[Any, ...]:
return interpreter.call_op(op.callee.string_value(), args)

@impl(scf.IfOp)
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this is necessary, xDSL already has an implementation for scf.If, I would have expected it to be enough

Copy link
Member

Choose a reason for hiding this comment

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

I just pushed the change that includes the xDSL version of this to main

@@ -1,17 +1,20 @@
// RUN: asl-opt %s | asl-opt %s | filecheck %s

builtin.module {
%true = asl.constant_bool true {attr_dict}
%false = asl.constant_bool false {attr_dict}
%true = asl.constant_bool true {"attr_dict"}
Copy link
Member

Choose a reason for hiding this comment

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

This is my bad, I switched from a commit to just "main" in xDSL, so I have a feeling that there's drift in our versions, we recently merged a change that avoide the quotes when their content is a valid symbol, so the version on the left in this diff is actually the more up to date spelling.

@math-fehr
Copy link
Contributor

Very nice!

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.

3 participants