-
Notifications
You must be signed in to change notification settings - Fork 316
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
[ImportVerilog]Support real math functions. #8192
[ImportVerilog]Support real math functions. #8192
Conversation
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 looks great! Thanks for adding all of this. Just a minor comment about first checking that the syscall has exactly 1 argument. Besides that, this looks good to go!
auto value = context.convertRvalueExpression(*args[0]); | ||
if (!value) | ||
return {}; |
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.
We need to check whether args.size() == 1
first. Maybe you could wrap the convertRvalueExpression
and your entire StringSwitch in an if (args.size() == 1) { ... }
? In the future, when we support different arities of system tasks/calls, we can expand this to call into some special handling functions like convertSystemCall1
, convertSystemCall2
, convertSystemCallVariadic
, and similar. Not needed now though, since all we support are unary/arity-1 system calls that have a single argument.
3f9ed3c
to
92bdfaa
Compare
92bdfaa
to
64a42ac
Compare
// Invoke convertSystemCallArity1() to select which builtin is. | ||
if (auto result = convertSystemCallArity1(value); args.size() == 1) | ||
return result; |
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 changed the original realMathFunc()
into convertSystemCallArity1(value)
and moved it out of the if
block. In the future, we can add convertSystemCallArity2(x, y)
or convertSystemCallArityN()
without args. What do @fabianschuiki think about this tweak 🤔?
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! How about turning this into a proper function instead of a large inline lambda?
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 think you still need to move all of this into a if (args.size() == 1)
. Further up where you call context.convertRvalueExpression(*args[0])
, this call will crash if args.size() == 0
.
I'd suggest moving the convertSystemCallArity1
into a proper function and doing something like this:
if (args.size() == 1) {
auto value = context.convertRvalueExpression(*args[0]);
if (!value)
return {};
auto result = convertSystemCallArity1(value);
if (failed(result))
return {};
if (*result)
return *result;
}
One thing you need to do in convertSystemCallArity1
is you need to make it clear from the return value whether:
- The conversion was successful and you return the result.
- The conversion failed due to an error, e.g. that
convertToSimpleBitVector
fails. - The system call is unknown.
I'd change the return type to FailureOr<Value>
, and then doing the following:
- Return
result
of the created op. - Return
failure()
due to an error. - Return
Value{}
for unsupported system calls.
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.
You're right! Regarding if(args.size() == 1)
, I attempted the $random()
and it will crash.
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'd change the return type to FailureOr, and then doing the following:
- Return result of the created op.
- Return failure() due to an error.
- Return Value{} for unsupported system calls.
I have a question about why we'll change the return type to FailureOr<Value>
. I notice FailureOr<Value> convertFormatString
, but this function is used in Statement.cpp. However, convertToSimpleBitVector
returns Value
, even if failed conversion, it just returns Value{}
with an error message and not LogicalResult
, so if(failed(result))
is never to be triggered.
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.
You're definitely right! The difference is that convertToSimpleBitVector
only needs to distinguish two cases:
- successful conversion (a non-null
Value
is returned) - failed conversion (a null
Value
is returned)
However, convertSystemCallArity1
needs to distinguish three cases:
- successful conversion (a non-null
Value
is returned) - function name recognized, but conversion failed (
failure()
is returned) - function name not recognized (a null
Value
is returned)
The FailureOr<Value>
allows you to distinguish a failure from the case where the function didn't recognize the function name.
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 your detailed explanation!
e19cca3
to
8e68c2a
Compare
8e68c2a
to
5628a3b
Compare
.Case("$clog2", | ||
[&]() -> FailureOr<Value> { | ||
value = convertToSimpleBitVector(value); | ||
if (!value) | ||
return failure(); | ||
return (Value)builder.create<moore::Clog2BIOp>(loc, value); | ||
}) |
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.
Maybe this is reasonable? First time to use std::function<FailureOr<Value>()>
. It just looks correct 😂.
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! 😃
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.
Greatly appreciate your guidance!
def RealLiteralOp : MooreOp<"real_constant", [Pure]> { | ||
let summary = "Produce a constant real value"; | ||
let description = [{ | ||
Produces a constant value of real type. | ||
|
||
Example: | ||
```mlir | ||
%0 = moore.real_constant 1.23 | ||
``` | ||
The real type has fixed-point(1.2) and exponent(2.0e10) formats. | ||
See IEEE 1800-2017 § 5.7.2 "Real literal constants". | ||
}]; | ||
let arguments = (ins F64Attr:$value); | ||
let results = (outs RealType:$result); | ||
let assemblyFormat = "$value attr-dict `:` type($result)"; | ||
} | ||
|
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.
Borrow from #8020 .
No description provided.