Dávid Bolvanský via llvm-dev
2018-May-17 22:53 UTC
[llvm-dev] Constants propagation into printf format string
Hello, I was thinking about a new possible simplification in InstCombine which would transform code like this: const char * NAME = "Prog"; void printer(char **arr, int len) { for (int i = 0; i < len; ++i) { printf("%s: %s", NAME, arr[i]); } } into void printer(char **arr, int len) { for (int i = 0; i < len; ++i) { printf("Prog: %s", arr[i]); } } This transformation would take constant strings/integers/chars and put them to the format string. In the example I used "printf" but it could be applied also for fprintf and sprintf. Do you consider this as good to be implemented? Or not worth to do at all? I implemented it some time ago personally just for fun to try it. I would reimplement it and send a patch to LLVM if any interest. Ideas? Complains? Suggestions? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180518/64adf876/attachment.html>
Davide Italiano via llvm-dev
2018-May-17 23:04 UTC
[llvm-dev] Constants propagation into printf format string
On Thu, May 17, 2018 at 3:53 PM, Dávid Bolvanský via llvm-dev <llvm-dev at lists.llvm.org> wrote:> Hello, > > I was thinking about a new possible simplification in InstCombine which > would transform code like this: > > const char * NAME = "Prog"; > > void printer(char **arr, int len) { > for (int i = 0; i < len; ++i) { > printf("%s: %s", NAME, arr[i]); > } > } > > into > > void printer(char **arr, int len) { > for (int i = 0; i < len; ++i) { > printf("Prog: %s", arr[i]); > } > } > > This transformation would take constant strings/integers/chars and put them > to the format string. >This is not a peephole optimization, therefore it doesn't belong to instcombine. If you want to try to implement something like this, I'd recommend taking a look at ConstantFolding or SCCP (and try to understand why the value doesn't get propagated). Thanks, -- Davide
Dávid Bolvanský via llvm-dev
2018-May-17 23:19 UTC
[llvm-dev] Constants propagation into printf format string
Thanks, I will look at it. 2018-05-18 1:04 GMT+02:00 Davide Italiano <davide at freebsd.org>:> On Thu, May 17, 2018 at 3:53 PM, Dávid Bolvanský via llvm-dev > <llvm-dev at lists.llvm.org> wrote: > > Hello, > > > > I was thinking about a new possible simplification in InstCombine which > > would transform code like this: > > > > const char * NAME = "Prog"; > > > > void printer(char **arr, int len) { > > for (int i = 0; i < len; ++i) { > > printf("%s: %s", NAME, arr[i]); > > } > > } > > > > into > > > > void printer(char **arr, int len) { > > for (int i = 0; i < len; ++i) { > > printf("Prog: %s", arr[i]); > > } > > } > > > > This transformation would take constant strings/integers/chars and put > them > > to the format string. > > > > This is not a peephole optimization, therefore it doesn't belong to > instcombine. > If you want to try to implement something like this, I'd recommend > taking a look at ConstantFolding or SCCP (and try to understand why > the value doesn't get propagated). > > Thanks, > > -- > Davide >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180518/8f827c45/attachment.html>
Friedman, Eli via llvm-dev
2018-May-18 00:10 UTC
[llvm-dev] Constants propagation into printf format string
On 5/17/2018 4:04 PM, Davide Italiano via llvm-dev wrote:> On Thu, May 17, 2018 at 3:53 PM, Dávid Bolvanský via llvm-dev > <llvm-dev at lists.llvm.org> wrote: >> Hello, >> >> I was thinking about a new possible simplification in InstCombine which >> would transform code like this: >> >> const char * NAME = "Prog"; >> >> void printer(char **arr, int len) { >> for (int i = 0; i < len; ++i) { >> printf("%s: %s", NAME, arr[i]); >> } >> } >> >> into >> >> void printer(char **arr, int len) { >> for (int i = 0; i < len; ++i) { >> printf("Prog: %s", arr[i]); >> } >> } >> >> This transformation would take constant strings/integers/chars and put them >> to the format string. >> > This is not a peephole optimization, therefore it doesn't belong to instcombine. > If you want to try to implement something like this, I'd recommend > taking a look at ConstantFolding or SCCP (and try to understand why > the value doesn't get propagated).What? Transforming `printf("FOO: %s", "BAR")` to `printf("FOO: BAR")` looks like a peephole to me. -Eli -- Employee of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project