llvm / clangir

A new (MLIR based) high-level IR for clang.
https://clangir.org
Other
312 stars 86 forks source link

[CIR][CodeGen] Support trailing_zeros for constant string literals #617

Closed ivanmurashko closed 1 month ago

ivanmurashko commented 1 month ago

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>
ivanmurashko commented 1 month ago

@bcardosolopes, could you look at the patch?

ivanmurashko commented 1 month ago

@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