(Translated by https://www.hiragana.jp/)
[CIR][CIRGen] Add CIRGen support for pointer-to-member-functions by Lancern · Pull Request #722 · llvm/clangir · GitHub
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] Add CIRGen support for pointer-to-member-functions #722

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lancern
Copy link
Collaborator

@Lancern Lancern commented Jul 6, 2024

This PR adds the initial CIRGen support for pointer-to-member-functions. It contains the following new types, attributes, and operations:

  • !cir.method, which represents the pointer-to-member-function type.
  • #cir.method, which represents a literal pointer-to-member-function value that points to non-virtual member functions.
  • #cir.virtual_method, which represents a literal pointer-to-member-function value that points to virtual member functions.
  • cir.get_method_callee, which resolves a pointer-to-member-function to a function pointer as the callee.

See the new test at clang/test/CIR/CIRGen/pointer-to-member-func.cpp for how these new CIR stuff works to support pointer-to-member-functions.

@bcardosolopes
Copy link
Member

Sorry for the delay here, taking a look next week!

This patch adds the initial CIRGen support for pointer-to-member-functions. It
contains the following new types, attributes, and operations:

  - `!cir.method`, which represents the pointer-to-member-function type.
  - `#cir.method`, which represents a literal pointer-to-member-function value
    that points to non-virtual member functions.
  - `#cir.virtual_method`, which represents a literal pointer-to-member-function
    value that points to virtual member functions.
  - `cir.get_method_callee`, which resolves a pointer-to-member-function to a
    function pointer as the callee.

See the new test at `clang/test/CIR/CIRGen/pointer-to-member-func.cpp` for how
these new CIR stuff works to support pointer-to-member-functions.
@Lancern
Copy link
Collaborator Author

Lancern commented Jul 28, 2024

Rebased onto the latest main.

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 cool, a nice missing feature!

Some comments inline. Should I assume lowering prepare + LLVM comes next?

// %arg = ...
// %method = ...
%callee, %this_adj = cir.get_method_callee %method, %object
%this = cir.ptr_stride %object, %this_adj
Copy link
Member

Choose a reason for hiding this comment

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

Rename this to cir.get_method. Is there any purpose in keeping the %this_adj around? I wonder if we should hide the ptr_stride and return the adjusted this point instead.


// Assume:
// - objectTy is !cir.ptr<!T>
// - methodFuncTy is !cir.func<!Ret (!Args)>
Copy link
Member

Choose a reason for hiding this comment

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

The 3 first lines of this should be above mlir::casts.

//
// We verify at here that:
// - calleeTy is !cir.func<!Ret (!cir.ptr<!T>, !Args)>
if (methodFuncTy.getReturnType() != calleeTy.getReturnType()) {
Copy link
Member

Choose a reason for hiding this comment

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

Is this possible to constrain this in tablegen?

if (calleeArgsTy.slice(1) != methodFuncArgsTy) {
emitError() << "callee parameters and method parameters do not match";
return mlir::failure();
}
Copy link
Member

Choose a reason for hiding this comment

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

These constraints should be mentioned in the documentation.

methodFuncInputTypes.end());
auto calleeFuncTy =
methodFuncTy.clone(calleeFuncInputTypes, methodFuncTy.getReturnType());
// TODO(cir): consider the address space of the callee.
Copy link
Member

Choose a reason for hiding this comment

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

A missing feature here.

}];

let parameters = (ins AttributeSelfTypeParameter<
"", "mlir::cir::MethodType">:$type,
Copy link
Member

Choose a reason for hiding this comment

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

Can we encode if it's virtual in the MethodType instead? One MethodAttr seems enough. It should then have two optionals and verifier ensure some of the semantics.

llvm::TypeSize
MethodType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
mlir::DataLayoutEntryListRef params) const {
// TODO: consider size differences under different ABIs
Copy link
Member

Choose a reason for hiding this comment

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

We already have a better ABI infra, you should implement what's necessary here, at least for one target.

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.

None yet

2 participants