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][Lowering] Emit llvm.global_ctors list #240

Merged
merged 3 commits into from
Aug 23, 2023

Conversation

htyu
Copy link
Contributor

@htyu htyu commented Aug 17, 2023

Creating the llvm.global_ctors list to hold all global dynamic initializers. The list has the following format:

%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @data }]

The list will be converted to .init_array for ELF by LLVM which will be loaded and executed by the C++ runtime.

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 great, thanks!

Out of curiosity, have you considered an alternative approach where instead of annotating the functions, we instead create an attribute that holds the list of symbols+prio of said global constructors? With that you only need to look at one attribute in buildCtorList instead of walking every function.

Since I might be missing some context: is adding that as a function attribute unlocks any type of analysis you're thinking about in the future?

I don't have a strong preference, just putting the compile-time hat for this one.

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp Outdated Show resolved Hide resolved
clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp Outdated Show resolved Hide resolved
@htyu
Copy link
Contributor Author

htyu commented Aug 21, 2023

This is great, thanks!

Out of curiosity, have you considered an alternative approach where instead of annotating the functions, we instead create an attribute that holds the list of symbols+prio of said global constructors? With that you only need to look at one attribute in buildCtorList instead of walking every function.

Since I might be missing some context: is adding that as a function attribute unlocks any type of analysis you're thinking about in the future?

I don't have a strong preference, just putting the compile-time hat for this one.

I don't have a strong preference either. An attribute listing all global constructors is probably better for compile time. I guess that would be a module attribute and we need to print it, otherwise the serialized IR isn't self-contained.

@bcardosolopes
Copy link
Member

I guess that would be a module attribute and we need to print it, otherwise the serialized IR isn't self-contained.

That works for me too, it can probably be a bit more adhoc for now, given we don't have a CIRModuleOp yet.

@htyu
Copy link
Contributor Author

htyu commented Aug 21, 2023

I guess that would be a module attribute and we need to print it, otherwise the serialized IR isn't self-contained.

That works for me too, it can probably be a bit more adhoc for now, given we don't have a CIRModuleOp yet.

On the second thought, would the printing be too lengthy if there are many globalops?

@bcardosolopes
Copy link
Member

On the second thought, would the printing be too lengthy if there are many globalops?

It's possible, but isn't that the case for any long thing in LLVM already?

@htyu
Copy link
Contributor Author

htyu commented Aug 21, 2023

On the second thought, would the printing be too lengthy if there are many globalops?

It's possible, but isn't that the case for any long thing in LLVM already?

I mean if we print the attribute in the module header, as we do today, it may look a bit weird for a long list. LLVM IR prints attributes in the end.

@bcardosolopes
Copy link
Member

I mean if we print the attribute in the module header, as we do today, it may look a bit weird for a long list. LLVM IR prints attributes in the end.

For real TUs I don't see much problem cause they are noisy/full-of-content regardless, for testcases it's only going to show up whenever we have global initializers w/ ctors. All that to say that I don't think it bothers me (versus paying for compile time).

@htyu htyu force-pushed the staticinit-lowering-llvm branch from e6eaacf to 0c2f374 Compare August 22, 2023 17:59
@htyu htyu force-pushed the staticinit-lowering-llvm branch from 0c2f374 to ee1409b Compare August 22, 2023 18:02
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.

Sweet, almost there, see inline comments.

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td Outdated Show resolved Hide resolved
clang/test/CIR/CodeGen/static.cpp Outdated Show resolved Hide resolved
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.

LGTM with minor changes!

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td Outdated Show resolved Hide resolved
Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
@htyu htyu force-pushed the staticinit-lowering-llvm branch from 2d6f7c3 to 00b2a9c Compare August 23, 2023 22:13
@htyu htyu merged commit 2fae9b7 into llvm:main Aug 23, 2023
@htyu htyu deleted the staticinit-lowering-llvm branch August 23, 2023 22:14
lanza pushed a commit that referenced this pull request Oct 27, 2023
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
lanza pushed a commit that referenced this pull request Dec 20, 2023
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
lanza pushed a commit that referenced this pull request Jan 29, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
lanza pushed a commit that referenced this pull request Mar 23, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Mar 24, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Mar 24, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
lanza pushed a commit that referenced this pull request Apr 29, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
lanza pushed a commit that referenced this pull request Apr 29, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
eZWALT pushed a commit to eZWALT/clangir that referenced this pull request Apr 29, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
lanza pushed a commit that referenced this pull request Apr 29, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
bruteforceboy pushed a commit to bruteforceboy/clangir that referenced this pull request Oct 2, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
lanza pushed a commit that referenced this pull request Nov 5, 2024
Creating the `llvm.global_ctors` list to hold all global dynamic
initializers. The list has the following format:

```
%0 = type { i32, ptr, ptr }
@llvm.global_ctors = appending global [1 x %0] [%0 { i32 65535, ptr @ctor, ptr @DaTa }]
```

The list will be converted to `.init_array` for ELF by LLVM which will
be loaded and executed by the C++ runtime.
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