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] Add cir.global_addr attribute #1248

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

Conversation

Lancern
Copy link
Member

@Lancern Lancern commented Dec 19, 2024

This PR adds a new attribute #cir.global_addr to the CIR dialect. This attribute is quite similar to #cir.global_view, except that the new attribute represents the address of the global variable as an integer instead of a pointer. And the new attribute does not have the "indecies" stuff.

CIRGen would not generate this attribute. I'm adding this new attribute because it could be useful during ABI lowering. For example, when doing ABI lowering for a member function pointer constant, ItaniumABI needs to lower the constant into a #cir.const_struct with two fields, first of which is an integer that represents the address of a function. This is where we need this attribute.

This patch adds a new attribute `#cir.global_addr`` to the CIR dialect. This
attribute is quite similar to `#cir.global_view`, except that the new attribute
represents the address of the global variable as an integer instead of a
pointer. And the new attribute does not have the "indecies" stuff.

CIRGen would not generate this attribute. I'm adding this new attribute because
it could be useful during ABI lowering. For example, when doing ABI lowering for
a member function pointer constant, ItaniumABI needs to lower the constant into
a `#cir.const_struct` with two fields, first of which is an integer that
represents the address of a function. This is where we need this attribute.
@@ -198,4 +205,17 @@ module {
}
// MLIR: %0 = llvm.mlir.addressof @zero_array

cir.func @const_global_addr() -> !u64i {
%0 = cir.const #cir.global_addr<@".str"> : !u64i
Copy link
Member

Choose a reason for hiding this comment

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

Sounds like this is equivalent to a global view with zero index? Why can't this be modeled ash cir.const #cir.global_view<@".str"> ?

Copy link
Member Author

Choose a reason for hiding this comment

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

cir.global_addr is of integer types, and I'm not sure if it's appropriate to make cir.global_view an integer type. If it's OK I could just update cir.global_view instead of introducing a new one.

Copy link
Member

Choose a reason for hiding this comment

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

My take is that we want here is a constant pointer coming out of cir.const, followed up by a cast to integer?

Copy link
Member Author

Choose a reason for hiding this comment

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

My take is that we want here is a constant pointer coming out of cir.const, followed up by a cast to integer?

In this case, yes. But I'm actually proposing this attribute for the following scenario where you don't have space for additional operations:

%0 = cir.const #cir.const_struct {
  #cir.global_addr @aaa,
  #cir.global_addr @bbb
}

And I'm looking at this scenario because recently I'm working on the LLVM lowering of member function pointers. Upon ABI lowering, a member function pointer is lowered to a struct with two fields of type ptrdiff_t. When the member function pointer represents a non-virtual member function, the first field stores the address of the target function as an integer. Thus to represent constant member function pointers I need an attribute that works like #cir.global_addr.

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.

2 participants