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

[ImportVerilog]Support real math functions. #8192

Merged
merged 1 commit into from
Feb 7, 2025

Conversation

hailongSun2000
Copy link
Member

No description provided.

@hailongSun2000 hailongSun2000 marked this pull request as draft February 5, 2025 07:14
Copy link
Contributor

@fabianschuiki fabianschuiki left a 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!

include/circt/Dialect/Moore/MooreOps.td Show resolved Hide resolved
lib/Conversion/ImportVerilog/Expressions.cpp Outdated Show resolved Hide resolved
Comment on lines 719 to 721
auto value = context.convertRvalueExpression(*args[0]);
if (!value)
return {};
Copy link
Contributor

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.

test/Conversion/ImportVerilog/builtins.sv Show resolved Hide resolved
@hailongSun2000 hailongSun2000 force-pushed the hailong/real-match-func branch from 3f9ed3c to 92bdfaa Compare February 6, 2025 06:50
@hailongSun2000 hailongSun2000 marked this pull request as ready for review February 6, 2025 06:50
@hailongSun2000 hailongSun2000 force-pushed the hailong/real-match-func branch from 92bdfaa to 64a42ac Compare February 6, 2025 06:52
Comment on lines 811 to 813
// Invoke convertSystemCallArity1() to select which builtin is.
if (auto result = convertSystemCallArity1(value); args.size() == 1)
return result;
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 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 🤔?

Copy link
Contributor

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?

Copy link
Contributor

@fabianschuiki fabianschuiki Feb 6, 2025

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:

  1. The conversion was successful and you return the result.
  2. The conversion failed due to an error, e.g. that convertToSimpleBitVector fails.
  3. The system call is unknown.

I'd change the return type to FailureOr<Value>, and then doing the following:

  1. Return result of the created op.
  2. Return failure() due to an error.
  3. Return Value{} for unsupported system calls.

Copy link
Member Author

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.

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'd change the return type to FailureOr, and then doing the following:

  1. Return result of the created op.
  2. Return failure() due to an error.
  3. 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.

Copy link
Contributor

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:

  1. successful conversion (a non-null Value is returned)
  2. failed conversion (a null Value is returned)

However, convertSystemCallArity1 needs to distinguish three cases:

  1. successful conversion (a non-null Value is returned)
  2. function name recognized, but conversion failed (failure() is returned)
  3. 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.

Copy link
Member Author

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!

@hailongSun2000 hailongSun2000 force-pushed the hailong/real-match-func branch 2 times, most recently from e19cca3 to 8e68c2a Compare February 7, 2025 02:27
@hailongSun2000 hailongSun2000 force-pushed the hailong/real-match-func branch from 8e68c2a to 5628a3b Compare February 7, 2025 05:29
Comment on lines +1330 to +1336
.Case("$clog2",
[&]() -> FailureOr<Value> {
value = convertToSimpleBitVector(value);
if (!value)
return failure();
return (Value)builder.create<moore::Clog2BIOp>(loc, value);
})
Copy link
Member Author

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 😂.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! 😃

Copy link
Member Author

Choose a reason for hiding this comment

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

Greatly appreciate your guidance!

Comment on lines +511 to +527
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)";
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Borrow from #8020 .

@hailongSun2000 hailongSun2000 merged commit 2b25325 into llvm:main Feb 7, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants