Problem when using CloneFunctionInto()

Dear all,

I am very new to LLVM. I run into a problem when changing the parameter type and return type of a function. I do it in the following way:

Step 1: create a new function type with new return type and new parameter type

Step 2: map old parameters to new parameters with new type, and add the mapping to VMap

Step3: for each old instruction in the function body which requires a new type, I create a new instruction with new type, and add the mapping to VMap

Step4:using ClondFunctionInto(new_fun, old_fun, VMap, true, Returns) to copy the body of the old function to the new function, with all values in the VMap replaced with its new version.

Then I got the following error:

/llvm/lib/Transforms/Utils/ValueMapper.cpp:444: void llvm::RemapInstruction(llvm::Instruction*, llvm::ValueToValueMapTy&, llvm::RemapFlags, llvm::ValueMapTypeRemapper*, llvm::ValueMaterializer*): Assertion `(Flags & RF_IgnoreMissingEntries) && “Referenced value not in value map!”’ failed.

Is Step3 correct? Since the document of CloneFunctionInto() said “Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values.” Could I also transform the old instructions in the function body to new ones in the VMap?

Thank you very much :slight_smile: