Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing llvm::Function signature after code generation, before last CreateRet

Tags:

c++

llvm

I'm trying to implement the following functionality;

  • a function with no explicit return will by default return the last evaluation in the last executed block

So, currently the process i'm doing is

1) create a Function

llvm::Function* result = llvm::Function::Create(Compiler::Detail::getAnonymousFunctionSignature(llvmContext),
                            llvm::GlobalValue::ExternalLinkage,
                            name,
                            module());
                    result->setCallingConv( llvm::CallingConv::C );

2) add blocks and evaluations to the blocks

builder.createFoo.....

However, only in the second phase i have the llvm::Value* (and compile-time type) that i want to use by default as return value. The problem is that i need to use this type to determine the signature of the created function

Question:

how do i solve the problem?

  • is possible to change the signature after the function is created? is it legal?
  • do i need to create a new function with the updated signature and copy/assign the entry block of the first function to it and thats it? or do i need to reevaluate all the expressions?
  • is possible to not create the function before code generation? if it is so, at what point should i create the function?

a code example of how to achieve this would be awesome. thanks!

like image 623
lurscher Avatar asked Mar 11 '26 22:03

lurscher


1 Answers

You cannot change function signature, because this will mean that it will have different Type (and thus you will need to update all the users, etc.; this procedure in most cases cannot be done automagically).

There are multiple possible solutions, for example, you can create the function with the updated signature, then use the functions from lib/Transforms/Utils/CloneFunction.cpp to copy the function body and then hack on the return type.

like image 154
Anton Korobeynikov Avatar answered Mar 14 '26 11:03

Anton Korobeynikov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!