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][CodeGen] Support trailing_zeros for constant string literals #617

Merged
merged 4 commits into from
May 24, 2024

Conversation

ivanmurashko
Copy link
Contributor

The patch resolves issue #248. It can be considered a subsequent patch to #373, where the case of empty strings was processed.

The new patch adds processing for non-empty strings that may contain trailing zeros, such as:

char big_string[100000] = "123";

That is converted to

@big_string = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100000>

It's worth noting that ordinary strings that are terminated with a single zero also get the trailing zeros, for instance

char string[] = "whatnow";

Is converted to

@string = #cir.const_array<"whatnow" : !cir.array<!s8i x 7>, trailing_zeros> : !cir.array<!s8i x 8>

The patch resolves [issue llvm#248](llvm#248). It can be considered a subsequent patch to [llvm#373](llvm#373), where the case of empty strings was processed.

The new patch adds processing for non-empty strings that may contain trailing zeros, such as:
```
char big_string[100000] = "123";
```
That is converted to
```
@big_string = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100000>
```

It's worth noting that ordinary strings that are terminated with a single zero also get the trailing zeros, for instance
```
char string[] = "whatnow";
```
Is converted to
```
@string = #cir.const_array<"whatnow" : !cir.array<!s8i x 7>, trailing_zeros> : !cir.array<!s8i x 8>
```
@ivanmurashko
Copy link
Contributor Author

@bcardosolopes, could you look at the patch?

@bcardosolopes bcardosolopes self-requested a review May 19, 2024 23:36
clang/test/CIR/CodeGen/hello.c Outdated Show resolved Hide resolved
Avoid trailing_zeros for constant string literals with only one zero at the end
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.

Awesome, few remaining things:

  • Add a new testcase to test this propagates all the way down to LLVM - see CodeGen/abstract-cond.c and similar for RUN line examples.
  • This is a good opportunity for going the extra length and getting rid of ZeroInitConstOp, but it's fine if you do that as part of another PR.

@ivanmurashko
Copy link
Contributor Author

@bcardosolopes could you look at the test I created?

I used the following command to created the test data for LLVM-IR

bin/clang -cc1 -triple aarch64-none-linux-android21 -emit-llvm  ../clang/test/CIR/CodeGen/string-literals.c -o /tmp/test.ll

I.e. the check data were created without Clang-IR involvement

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!

btw, created more issues to track other follow ups, if you are interested feel free to grab them!

@bcardosolopes bcardosolopes merged commit 8effbcc into llvm:main May 24, 2024
6 checks passed
@ivanmurashko ivanmurashko deleted the trailing_zeros branch May 26, 2024 20:55
bruteforceboy pushed a commit to bruteforceboy/clangir that referenced this pull request Oct 2, 2024
…lvm#617)

The patch resolves [issue
llvm#248](llvm#248). It can be considered
a subsequent patch to [llvm#373](llvm#373),
where the case of empty strings was processed.

The new patch adds processing for non-empty strings that may contain
trailing zeros, such as:
```
char big_string[100000] = "123";
```
That is converted to
```
@big_string = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100000>
```
Hugobros3 pushed a commit to shady-gang/clangir that referenced this pull request Oct 2, 2024
…lvm#617)

The patch resolves [issue
llvm#248](llvm#248). It can be considered
a subsequent patch to [llvm#373](llvm#373),
where the case of empty strings was processed.

The new patch adds processing for non-empty strings that may contain
trailing zeros, such as:
```
char big_string[100000] = "123";
```
That is converted to
```
@big_string = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100000>
```
smeenai pushed a commit to smeenai/clangir that referenced this pull request Oct 9, 2024
…lvm#617)

The patch resolves [issue
llvm#248](llvm#248). It can be considered
a subsequent patch to [llvm#373](llvm#373),
where the case of empty strings was processed.

The new patch adds processing for non-empty strings that may contain
trailing zeros, such as:
```
char big_string[100000] = "123";
```
That is converted to
```
@big_string = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100000>
```
keryell pushed a commit to keryell/clangir that referenced this pull request Oct 19, 2024
…lvm#617)

The patch resolves [issue
llvm#248](llvm#248). It can be considered
a subsequent patch to [llvm#373](llvm#373),
where the case of empty strings was processed.

The new patch adds processing for non-empty strings that may contain
trailing zeros, such as:
```
char big_string[100000] = "123";
```
That is converted to
```
@big_string = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100000>
```
lanza pushed a commit that referenced this pull request Nov 5, 2024
)

The patch resolves [issue
#248](#248). It can be considered
a subsequent patch to [#373](#373),
where the case of empty strings was processed.

The new patch adds processing for non-empty strings that may contain
trailing zeros, such as:
```
char big_string[100000] = "123";
```
That is converted to
```
@big_string = #cir.const_array<"123" : !cir.array<!s8i x 3>, trailing_zeros> : !cir.array<!s8i x 100000>
```
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.

Leverage #cir.zero in large constant strings initializers
2 participants