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
dialects: Add print_int, print_char ops. #1311
Conversation
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #1311 +/- ##
==========================================
+ Coverage 90.03% 90.06% +0.03%
==========================================
Files 211 215 +4
Lines 26266 26815 +549
Branches 3902 3986 +84
==========================================
+ Hits 23649 24152 +503
- Misses 2004 2025 +21
- Partials 613 638 +25
☔ View full report in Codecov by Sentry. |
xdsl/dialects/printf.py
Outdated
[ | ||
PrintFormatOp, | ||
], | ||
[PrintFormatOp, PrintCharOp, PrintIntOp], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[PrintFormatOp, PrintCharOp, PrintIntOp], | |
[PrintFormatOp, PrintCharOp, PrintIntOp,], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a useful change to minimise spurious git diffs in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but what do you mean with
to minimise spurious git diffs in the future
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for providing such detailed info for this PR, it made the review easier.
I mainly focused on itoa
.
As for signless integers, my understanding is that treating them as signed is compatible with their semantics and it's up to the corresponding operation to interpret them as required.
Keeping this working just for 32-bit int sounds good to me for now.
@@ -0,0 +1,15 @@ | |||
// RUN: xdsl-opt -p lower-printchar-to-putchar %s | xdsl-opt | filecheck %s | |||
|
|||
builtin.module { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can omit the builtin.module
now and save some indentation space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with minor corrections!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
36e526c
to
d00e1f0
Compare
I am happy that def reverse(n: int) -> int: # n is absolute value
m = 0
while n:
m = 10 * m + n % 10
n //= 10
return m
def print_integer(n: int) -> None:
m = reverse(n)
while m:
print(m % 10, end="") # print single digit as char
m //= 10
print_integer(123456) Just some ideas. |
Thanks for the idea @kingiler, i'm not entirely sure if what you are proposing is strictly necessary though, since the |
xdsl/transforms/printf_to_putchar.py
Outdated
) | ||
print_char_walker.rewrite_module(op) | ||
# Add external putchar reference | ||
# TODO Don't insert this if the operation is not there! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to add this functionality as part of this PR. it should be pretty easy with the symbol table functionality, or just a manual scan of the module op body.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you give a reference/example to the symbol table functionality? I don't know it. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might not work here, but in theory can be something like in the get_op_for_symbol
function in interpreter.py
def get_op_for_symbol(self, symbol: str) -> Operation:
if symbol in self.symbol_table:
return self.symbol_table[symbol]
else:
raise InterpretationError(f'Could not find symbol "{symbol}"')
@property
def symbol_table(self) -> dict[str, Operation]:
if self._symbol_table is None:
self._symbol_table = {}
for op in self.module.walk():
if (symbol_interface := op.get_trait(SymbolOpInterface)) is not None:
symbol = symbol_interface.get_sym_attr_name(op)
if symbol:
self._symbol_table[symbol.data] = op
return self._symbol_table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm actually maybe just this will work: SymbolTable.lookup_symbol(op, "foo")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it doesn't work I'd be interested :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you doublecheck now
As per @AntonLydike's suggestion I've removed the |
var_operand_def, | ||
) | ||
from xdsl.parser import Parser | ||
from xdsl.printer import Printer | ||
|
||
i8 = builtin.IntegerType(8) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure but i8
seems useful to be put in xdsl/dialect/builtin.py
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also not sure, I don't think it matters that much though, it would save about one line in code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, some minor changes would be nice to make or discuss
xdsl/dialects/printf.py
Outdated
[ | ||
PrintFormatOp, | ||
], | ||
[PrintFormatOp, PrintCharOp, PrintIntOp], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking forward to using this :)
Thank you all for the reviews! I learned a lot while working on this! |
Hi, this is an initial PR for adding more self-contained printing capabilities to the
printf
dialect.I'm still unsure about a lot of stuff, and it is not finished yet, some initial help is appreciated!
This PR adds:
Two new ops:
One pass
lower-printchar-to-putchar
(naming suggestions welcome!) that implements:print_int
to afunc.func inline_itoa
calls. The implementation ofinline_itoa
is inserted in the pass.Inline_itoa
was written in MLIR assembly directly.print_char
s to C styleput_chars
.Rationale
Some platforms don't ship with printf implementations or are very bad at printing. This implementation allows for printing integers:
put_char
)I think we can (if necessary) even write a pass that composes the current
printf.format()
intoprint_char
s andprint_int
s if necessary.Why a
print_char
op?Why a
print_int
op?Still missing/Questions/Concerns