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

[CIR][CIRGen] Use Clang Codegen's skeleton in CIRGenFunction::buildBuiltinExpr #967

Merged
merged 4 commits into from
Oct 18, 2024

Conversation

ghehg
Copy link
Collaborator

@ghehg ghehg commented Oct 13, 2024

This PR helps us to triage unimplemented builtins (that are target independent).
There are unhandled builtins in CIR Codegen [CIRGenFunction::buildBuiltinExpr](https://github.com/llvm/clangir/blob/4c446b3287895879da598e23164d338d04bced3e/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp#L305).
And those builtins have implementation in OG.
Currently, those builtins just are treated as LibraryCall or some other ways which eventually get failure, and failure messages are confusing.
This PR address this problem by refactoring CIRGenFunction::buildBuiltinExpr to keep the same skeleton as OG
counterpart CodeGenFunction::EmitBuiltinExpr, and add builtin name to NYI message

@ghehg ghehg changed the title [CIR][CIRGen]Let should-be-handled unhandled Builtins NYI with their name [CIR][CIRGen] Let should-be-handled unhandled Builtins NYI with their name Oct 13, 2024
@ghehg ghehg marked this pull request as ready for review October 14, 2024 01:32
Copy link
Collaborator

@smeenai smeenai left a comment

Choose a reason for hiding this comment

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

This is a great idea!

/// in OG code `CodeGenFunction::EmitBuiltinExpr`.
/// NOTE: Every time we implement handling of a case in
/// the function `CIRGenFunction::buildBuiltinExpr`, it should be remove here.
static std::string getUnhandledBuiltinName(unsigned builtinID) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should return StringRef.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll use StringRef if we do return name.

switch (builtinID) {
default:
return std::string();
case Builtin::BI__builtin_acos:
Copy link
Collaborator

Choose a reason for hiding this comment

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

These builtins are defined in tablegen, and I usually search in my build directory to find the corresponding .inc file (tools/clang/include/clang/Basic/Builtins.inc in this case). Those files define all the builtins using macros, and you can provide an appropriate macro definition before including the file to autogenerate this switch instead of needing to manually write out every intrinsic.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks for great advice. This situation is a bit different though because these builtin names are a selection of tools/clang/include/clang/Basic/Builtins.inc. They are actually a manual selection of builtins that has implementation in OG but not yet in CIRGen, And the idea was just to capture names and NYI early of those builtins (and them only) as those implemented in CIRGen should never hit the NYI I created here, and those not implemented in OG should go through and never hit NYI I created.
i.e. I used command like

"sed -n '2573,6333p' /data/users/guojinhe/clangir-llvm-ghehg/clang/lib/CodeGen/CGBuiltin.cpp | grep -o 'Builtin::BI__[^:]*' | sed 's/^/case /;s/$/:/' " to get them from oG.
and exclude what's from sed -n '305,1416p' /data/users/guojinhe/clangir-llvm-ghehg/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | grep "case Builtin:"
to pick them.

Yeah, let me know if we have better way to generate them

Copy link
Collaborator

Choose a reason for hiding this comment

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

With the current implementation, I think it'd be okay to have this function handle all built-ins and not just unimplemented ones. Bruno's suggestion changes that, of course.

std::string unhandledName = getUnhandledBuiltinName(BuiltinID);
if (!unhandledName.empty()) {
llvm::errs() << unhandledName << " ";
llvm_unreachable("NYI");
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it'll be easier to parse the error if the unreachable message itself includes the builtin name.

return std::string();
case Builtin::BI__builtin_acos:
return "Builtin::BI__builtin_acos";
case Builtin::BI__builtin_acosf:
Copy link
Member

Choose a reason for hiding this comment

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

When you did similar for Aarch64 my feedback was to make those inline (without a function that returns the name), and you did implement like that. The idea is to get an accurate account of source line of failure, I'm confused on why you took a different approach here.

Copy link
Member

Choose a reason for hiding this comment

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

Example:

llvm_unreachable("NYI");

Copy link
Collaborator Author

@ghehg ghehg Oct 14, 2024

Choose a reason for hiding this comment

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

I see, sounds like better approach!
Let me see if I understand the tradeoff, because if we do have them inline instead of returning the name, not only we can have source line of failures (and name can be known by looking up source code), we would have a skeleton that we can fill when we have implementation ready. Plus, we don't need to keep and maintain this "return name" function as they may not be needed eventually. The only con with this alternative is every time we change the code, failing line number would change, but I think it might be Ok.

I'll change this PR to use this approach.

Copy link
Member

Choose a reason for hiding this comment

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

exactly, good point on the maintain bits too

@bcardosolopes
Copy link
Member

This PR currently has no content, should probably be a draft to prevent generating noise if it's not ready for review

@ghehg ghehg changed the title [CIR][CIRGen] Let should-be-handled unhandled Builtins NYI with their name [CIR][CIRGen] Use Clang Codegen's skeleton in CIRGenFunction::buildBuiltinExpr Oct 18, 2024
@ghehg
Copy link
Collaborator Author

ghehg commented Oct 18, 2024

Skeleton of OG introduced with informative NYIs.

Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

This is awesome, many thanks!

@bcardosolopes bcardosolopes merged commit 25947d0 into llvm:main Oct 18, 2024
6 checks passed
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
…iltinExpr (llvm#967)

This PR helps us to triage unimplemented builtins (that are target
independent).
There are unhandled builtins in CIR Codegen
`[CIRGenFunction::buildBuiltinExpr](https://github.com/llvm/clangir/blob/4c446b3287895879da598e23164d338d04bced3e/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp#L305)`.
And those builtins have implementation in
[OG](https://github.com/llvm/clangir/blob/4c446b3287895879da598e23164d338d04bced3e/clang/lib/CodeGen/CGBuiltin.cpp#L2573).
Currently, those builtins just are treated as LibraryCall or some other
ways which eventually get failure, and failure messages are confusing.
This PR address this problem by refactoring
`CIRGenFunction::buildBuiltinExpr` to keep the same skeleton as OG
counterpart `CodeGenFunction::EmitBuiltinExpr`, and add builtin name to
NYI message
lanza pushed a commit that referenced this pull request Nov 5, 2024
…iltinExpr (#967)

This PR helps us to triage unimplemented builtins (that are target
independent).
There are unhandled builtins in CIR Codegen
`[CIRGenFunction::buildBuiltinExpr](https://github.com/llvm/clangir/blob/4c446b3287895879da598e23164d338d04bced3e/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp#L305)`.
And those builtins have implementation in
[OG](https://github.com/llvm/clangir/blob/4c446b3287895879da598e23164d338d04bced3e/clang/lib/CodeGen/CGBuiltin.cpp#L2573).
Currently, those builtins just are treated as LibraryCall or some other
ways which eventually get failure, and failure messages are confusing.
This PR address this problem by refactoring
`CIRGenFunction::buildBuiltinExpr` to keep the same skeleton as OG
counterpart `CodeGenFunction::EmitBuiltinExpr`, and add builtin name to
NYI message
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