[llvm-dev] Adding instructions in pass directly from string containing IR

Johannes Doerfert via llvm-dev llvm-dev at lists.llvm.org
Wed Jun 9 11:40:52 PDT 2021


I would link in the function with the code via clang early:
-Xclang -mlink-bitcode file.bc  (not tested)
and then insert calls with the always inline attribute (or add
it on the function).

Hope this helps,
   Johannes

On 6/9/21 8:54 AM, Przemyslaw Ossowski wrote:
> This is an interesting approach, but in general I am trying to avoid
> function calls with additional code but paste the code into another
> function.
> Or maybe I didn't get the exact goal of ifunc.
> And as I understood Johannes it should be possible to use LLVM IR Linker
> (in the LLVM IR pass???), then inline the code and optimize everything as
> one function. Then any dynamic resolving of shouldn't be needed.
>
> Thanks,
> Przemek
>
>
> On Wed, Jun 9, 2021 at 3:08 PM Blower, Melanie I <melanie.blower at intel.com>
> wrote:
>
>> How about using ifunc?  The linker calls a resolver function which returns
>> the address of which function to call. The callsite is fixed up with the
>> resolved address as the program is loaded.
>>
>>
>>
>> Some info here,
>> https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Function-Attributes.html#index-g_t_0040code_007bifunc_007d-function-attribute-3095
>>
>>
>>
>> *From:* llvm-dev <llvm-dev-bounces at lists.llvm.org> *On Behalf Of *Przemyslaw
>> Ossowski via llvm-dev
>> *Sent:* Wednesday, June 9, 2021 9:01 AM
>> *To:* Johannes Doerfert <johannesdoerfert at gmail.com>
>> *Cc:* llvm-dev <llvm-dev at lists.llvm.org>
>> *Subject:* Re: [llvm-dev] Adding instructions in pass directly from
>> string containing IR
>>
>>
>>
>> Thanks,
>>
>> but when I went into details I'm wondering if using linker in a simple
>> pass is possible.
>>
>> Usually passes are executed per module.
>>
>> Is it possible to use linker and later replace the original module with a
>> newly linked module in simple pass?
>>
>> Module is passed by reference to runOnModule, so probably it should be
>> possible...
>>
>>
>>
>> Przemek
>>
>>
>>
>>
>>
>> On Tue, Jun 8, 2021 at 6:01 PM Johannes Doerfert <
>> johannesdoerfert at gmail.com> wrote:
>>
>> If you insert a call prior to the inliner and mark it with the
>> `alwaysinline` attribute, it will be inlined for sure.
>>
>> Reduces your problem to insert a call to a function :)
>>
>> ~ Johannes
>>
>>
>> On 6/7/21 10:51 AM, Przemyslaw Ossowski wrote:
>>> Thank you Johannes for your suggestion.
>>>
>>> In case a call to the function which would contain the code I would like
>> to
>>> paste is inserted into another function, then is it still possible to
>>> simply inline the code?
>>> I would like to avoid calling the function with the "pasted" code. I
>> assume
>>> there is a pass which inlines functions which should be executed later.
>>>
>>> Thanks,
>>> Przemek
>>>
>>>
>>> On Wed, Jun 2, 2021 at 4:13 AM Johannes Doerfert <
>> johannesdoerfert at gmail.com>
>>> wrote:
>>>
>>>> Hi Przemek,
>>>>
>>>> I doubt that such functionality exists right now. The main
>>>> problem I see is the name matching to embed it with other code.
>>>> If the new code is not connected, I'd recommend the pass to
>>>> read in an IR file with a function in it, link it into the module,
>>>> and insert a call wherever you want to "paste" the instructions.
>>>>
>>>> Much cleaner than dealing with strings, IMHO.
>>>>
>>>> ~ Johannes
>>>>
>>>>
>>>> On 6/1/21 12:16 PM, Przemyslaw Ossowski via llvm-dev wrote:
>>>>> Hello,
>>>>>
>>>>>
>>>>>
>>>>> I would like to add a pass which modifies IR by insertion set of IR
>>>>> instructions somewhere in the middle of the basicblock,
>>>>>
>>>>> like example below:
>>>>>
>>>>>
>>>>>
>>>>>      %address = call i32* @llvm.target_intrinsic.getaddress()
>>>>>
>>>>>      %value = load i32, i32* %address
>>>>>
>>>>>      %addvalue = add nsw i32 %value, 10
>>>>>
>>>>>      store i32 %addvalue, i32* %address
>>>>>
>>>>>
>>>>>
>>>>> Is there any function available which would allow for parsing the
>> string
>>>>> (containing set of instructions) and generating instructions
>>>> automatically
>>>>> instead of manually calling functions creating instructions from
>>>> IRBuilder?
>>>>> ...at least for simple set of basic instructions
>>>>>
>>>>>
>>>>> Regards,
>>>>>
>>>>> Przemek
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> LLVM Developers mailing list
>>>>> llvm-dev at lists.llvm.org
>>>>> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>>


More information about the llvm-dev mailing list