<div dir="ltr">Hi,<br><br>I wan't to clone function from another bitcode module and set it AlwaysInline so it can be inlined.<br>But error occurs when applying a such custom pass. (It seems cause is usage of CloneFunction()) How can I fix it?<br>I'm using LLVM 3.6.0 on OS X and target is Android armeabi-v7a. (It should be same on other target)<br>I'll appreciate any advice. Thanks.<br><br><br>* callee.cpp (compiled to bitcode module callee.bc)<br>---------------------------------------<br>#include <jni.h><br>#include <android/log.h><br><br>#ifdef __cplusplus<br>extern "C" {<br>#endif<br><br>void callee(char *str, int num)<br>{<br>        __android_log_print(ANDROID_LOG_DEBUG, "target", "%s, 0x%x", str, num);<br>}<br><br>#ifdef __cplusplus<br>}<br>#endif<br>---------------------------------------<br><br><br>* test.cpp (module custom pass is applied)<br>---------------------------------------<br>#include <jni.h><br><br>#ifdef __cplusplus<br>extern "C" {<br>#endif<br><br>void caller()<br>{<br>}<br><br>void Java_com_android_test_TestActivity_test(JNIEnv *env, jobject thiz)<br>{<br>        caller();<br>}<br><br>#ifdef __cplusplus<br>}<br>#endif<br>---------------------------------------<br><br><br>* TestPass.cpp (custom pass)<br>---------------------------------------<br>#include "llvm/IR/Module.h"<br>#include "llvm/IR/Function.h"<br>#include "llvm/IR/Type.h"<br>#include "llvm/IR/InstIterator.h"<br>#include "llvm/IR/Instructions.h"<br>#include "llvm/IR/GlobalValue.h"<br>#include "llvm/IR/IRBuilder.h"<br>#include "llvm/Support/SourceMgr.h"<br>#include "llvm/IRReader/IRReader.h"<br>#include "llvm/Transforms/Utils/Cloning.h"<br>#include "llvm/IR/LegacyPassManager.h"<br>#include "llvm/Transforms/IPO/PassManagerBuilder.h"<br><br>using namespace llvm;<br><br>namespace {<br>        struct TestPass : public ModulePass {<br>                static char ID;<br><br>                TestPass() : ModulePass(ID) {}<br><br>                virtual bool runOnModule(Module &M)<br>                {<br>                        for (Module::iterator F = M.begin(), E = M.end(); F!= E; ++F)<br>                        {<br>                                if (F->getName() == "caller")<br>                                {<br>                                        inst_iterator I = inst_begin(F);<br>                                        Instruction *inst = &*I;<br><br>                                        SMDiagnostic error;<br>                                        LLVMContext context;<br>                                        std::unique_ptr<Module> m = parseIRFile("callee.bc", error, context);<br>                                        for (Module::iterator f = m->getFunctionList().begin(); f != m->getFunctionList().end(); f++)<br>                                        {<br>                                                if (f->getName() == "callee")<br>                                                {<br>                                                        // Copy callee.bc callee function to current module and set AlwaysInline<br>                                                        ValueToValueMapTy VMap;<br>                                                        Function* callee_clone = CloneFunction(f, VMap, false);<br>                                                        callee_clone->setName("callee_clone");<br>                                                        callee_clone->addFnAttr(Attribute::AlwaysInline);<br>                                                        M.getFunctionList().push_back(callee_clone);<br><br>                                                        // Add call to copied callee function<br>                                                        GlobalVariable* str = new GlobalVariable(M,<br>                                                                        ArrayType::get(IntegerType::get(M.getContext(), 8), 8),<br>                                                                        false,<br>                                                                        GlobalValue::ExternalLinkage,<br>                                                                        0,<br>                                                                        "str");<br>                                                        str->setAlignment(1);<br>                                                        Constant* str_init = ConstantDataArray::getString(M.getContext(), "abcdefgh", false);<br>                                                        str->setInitializer(str_init);<br><br>                                                        ConstantInt* param1_indice1 = ConstantInt::get(M.getContext(), APInt(32, StringRef("0"), 10));<br>                                                        ConstantInt* param1_indice2 = ConstantInt::get(M.getContext(), APInt(32, StringRef("0"), 10));<br>                                                        std::vector<Constant*> param1_indices;<br>                                                        param1_indices.push_back(param1_indice1);<br>                                                        param1_indices.push_back(param1_indice2);<br>                                                        Constant* param1 = ConstantExpr::getGetElementPtr(str, param1_indices);<br><br>                                                        GlobalVariable* num = new GlobalVariable(M,<br>                                                                        IntegerType::get(M.getContext(), 32),<br>                                                                        false,<br>                                                                        GlobalValue::ExternalLinkage,<br>                                                                        0,<br>                                                                        "num");<br>                                                        num->setAlignment(4);<br>                                                        ConstantInt* num_init = ConstantInt::get(M.getContext(), APInt(32, StringRef("12345"), 10));<br>                                                        num->setInitializer(num_init);<br><br>                                                        LoadInst* param2 = new LoadInst(num, "", false, inst);<br>                                                        param2->setAlignment(4);<br><br>                                                        std::vector<Value*> params;<br>                                                        params.push_back(param1);<br>                                                        params.push_back(param2);<br><br>                                                        CallInst* ci = CallInst::Create(callee_clone, params, "", inst);<br>                                                        ci->setCallingConv(CallingConv::C);<br>                                                        ci->setTailCall(false);<br>                                                }<br>                                        }<br>                                }<br>                        }<br><br>                        return true;<br>                }<br>        };<br>}<br><br>char TestPass::ID = 0;<br>static RegisterPass<TestPass> X("TestPass", "TestPass", false, false);<br><br>static void registerTestPass(const PassManagerBuilder &, legacy::PassManagerBase &PM)<br>{<br>        PM.add(new TestPass());<br>}<br>static RegisterStandardPasses RegisterTestPass(PassManagerBuilder::EP_ModuleOptimizerEarly, registerTestPass);<br>---------------------------------------<br><br>* Error when applying custom pass<br>---------------------------------------<br>[...snip...]<br>Assertion failed: ((i >= FTy->getNumParams() || FTy->getParamType(i) == Args[i]->getType()) && "Calling a function with a bad signature!"), function init, file /private/tmp/llvm/llvm-3.6.0.src/lib/IR/Instructions.cpp, line 281.<br>0  clang++                  0x000000010cc03909 llvm::sys::PrintStackTrace(__sFILE*) + 57<br>1  clang++                  0x000000010cc0407b SignalHandler(int) + 555<br>2  libsystem_platform.dylib 0x00007fffb7debb3a _sigtramp + 26<br>3  libsystem_platform.dylib 0x0000000100000001 _sigtramp + 1210139873<br>4  clang++                  0x000000010cc03e36 abort + 22<br>5  clang++                  0x000000010cc03e11 __assert_rtn + 81<br>6  clang++                  0x000000010cb63714 llvm::CallInst::init(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Twine const&) + 580<br>7  TestPass.dylib           0x000000010dc032c2 (anonymous namespace)::TestPass::runOnModule(llvm::Module&) + 1842<br>8  clang++                  0x000000010cb83c4e llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1054<br>9  clang++                  0x000000010aaf549a clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::raw_ostream*) + 7098<br>10 clang++                  0x000000010ac2a33d clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 509<br>11 clang++                  0x000000010acbf954 clang::ParseAST(clang::Sema&, bool, bool) + 468<br>12 clang++                  0x000000010a930a53 clang::FrontendAction::Execute() + 67<br>13 clang++                  0x000000010a902b2c clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 956<br>14 clang++                  0x000000010a8bd63a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 4154<br>15 clang++                  0x000000010a8b465c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1068<br>16 clang++                  0x000000010a8bb7ec main + 11660<br>17 libdyld.dylib            0x00007fffb7bdc235 start + 1<br>18 libdyld.dylib            0x0000000000000066 start + 1212300850<br>Stack dump:<br>0.      Program arguments: /opt/llvm-3.6.0/build/Release+Asserts/bin/clang++ -cc1 -triple thumbv7-none-linux-androideabi -S -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 1 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -no-integrated-as -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu cortex-a8 -target-feature +soft-float-abi -target-feature +vfp3 -target-feature +d16 -target-feature -neon -target-abi aapcs-linux -mfloat-abi soft -target-linker-version 253.3 -g -dwarf-column-info -ffunction-sections -coverage-file /opt/test/obj/local/armeabi-v7a/objs/test/test.o -resource-dir /opt/llvm-3.6.0/build/Release+Asserts/bin/../lib/clang/3.6.0 -dependency-file /opt/test/obj/local/armeabi-v7a/objs/test/test.o.d -MT /opt/test/obj/local/armeabi-v7a/objs/test/test.o -MP -D NDEBUG -D ANDROID -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libcxx/include -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/libcxxabi/include -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../../android/support/include -I /opt/test/jni -I /Applications/android-ndk-r10e/platforms/android-21/arch-arm/usr/include -internal-isystem /usr/local/include -internal-isystem /opt/llvm-3.6.0/build/Release+Asserts/bin/../lib/clang/3.6.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O1 -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -Wformat -Werror=format-security -std=c++11 -fdeprecated-macro -fno-dwarf-directory-asm -fdebug-compilation-dir /opt/test/jni -ferror-limit 19 -fmessage-length 152 -stack-protector 2 -mstackrealign -fno-rtti -fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -load /opt/test/TestPass.dylib -mllvm -debug-pass=Structure -o /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-42d9da.s -x c++ /opt/test/jni/test.cpp<br>1.      <eof> parser at end of file<br>2.      Per-module optimization passes<br>3.      Running pass 'TestPass' on module '/opt/test/jni/test.cpp'.<br>clang++: error: unable to execute command: Illegal instruction: 4<br>clang++: error: clang frontend command failed due to signal (use -v to see invocation)<br>clang version 3.6.0 (tags/RELEASE_360/final)<br>Target: armv7-none-linux-androideabi<br>Thread model: posix<br>clang++: note: diagnostic msg: PLEASE submit a bug report to <a href="http://llvm.org/bugs/">http://llvm.org/bugs/</a> and include the crash backtrace, preprocessed source, and associated run script.<br>clang++: note: diagnostic msg:<br>********************<br><br>PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:<br>Preprocessed source(s) and associated run script(s) are located at:<br>clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-dc4bda.cpp<br>clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-dc4bda.sh<br>clang++: note: diagnostic msg:<br><br>********************<br>make: *** [/opt/test/obj/local/armeabi-v7a/objs/test/test.o] Error 254<br>---------------------------------------<br><br><br>* Error when commenting out below CloneFunction()<br>---------------------------------------<br>[...snip...]<br>                                                        // Copy callee.bc callee function to current module and set AlwaysInline<br>                                                        ValueToValueMapTy VMap;<br>                                                        Function* callee_clone = CloneFunction(f, VMap, false);<br>/*<br>                                                        callee_clone->setName("callee_clone");<br>                                                        callee_clone->addFnAttr(Attribute::AlwaysInline);<br>                                                        M.getFunctionList().push_back(callee_clone);<br><br>                                                        // Add call to copied callee function<br>[...snip...]<br>                                                        CallInst* ci = CallInst::Create(callee_clone, params, "", inst);<br>                                                        ci->setCallingConv(CallingConv::C);<br>                                                        ci->setTailCall(false);<br>*/<br>[...snip...]<br>---------------------------------------<br><br>---------------------------------------<br>[...snip...]<br>While deleting: [7 x i8]* %.str<br>Use still stuck around after Def is destroyed:@.str = private unnamed_addr constant [7 x i8] <null operand!>, align 1<br>Assertion failed: (use_empty() && "Uses remain when a value is destroyed!"), function ~Value, file /private/tmp/llvm/llvm-3.6.0.src/lib/IR/Value.cpp, line 81.<br>0  clang++                  0x000000011154d909 llvm::sys::PrintStackTrace(__sFILE*) + 57<br>1  clang++                  0x000000011154e07b SignalHandler(int) + 555<br>2  libsystem_platform.dylib 0x00007fffb7debb3a _sigtramp + 26<br>3  libsystem_platform.dylib 0x00000000509ff1d0 _sigtramp + 2562799280<br>4  clang++                  0x000000011154de36 abort + 22<br>5  clang++                  0x000000011154de11 __assert_rtn + 81<br>6  clang++                  0x00000001114f2104 llvm::Value::~Value() + 660<br>7  clang++                  0x00000001114a63d1 llvm::GlobalVariable::~GlobalVariable() + 97<br>8  clang++                  0x00000001114e3ba9 llvm::Module::~Module() + 105<br>9  TestPass.dylib           0x000000011254d7dc (anonymous namespace)::TestPass::runOnModule(llvm::Module&) + 524<br>10 clang++                  0x00000001114cdc4e llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1054<br>11 clang++                  0x000000010f43f49a clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::raw_ostream*) + 7098<br>12 clang++                  0x000000010f57433d clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 509<br>13 clang++                  0x000000010f609954 clang::ParseAST(clang::Sema&, bool, bool) + 468<br>14 clang++                  0x000000010f27aa53 clang::FrontendAction::Execute() + 67<br>15 clang++                  0x000000010f24cb2c clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 956<br>16 clang++                  0x000000010f20763a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 4154<br>17 clang++                  0x000000010f1fe65c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1068<br>18 clang++                  0x000000010f2057ec main + 11660<br>19 libdyld.dylib            0x00007fffb7bdc235 start + 1<br>20 libdyld.dylib            0x0000000000000066 start + 1212300850<br>Stack dump:<br>0.      Program arguments: /opt/llvm-3.6.0/build/Release+Asserts/bin/clang++ -cc1 -triple thumbv7-none-linux-androideabi -S -disable-free -main-file-name test.cpp -mrelocation-model pic -pic-level 1 -mthread-model posix -relaxed-aliasing -fmath-errno -masm-verbose -no-integrated-as -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu cortex-a8 -target-feature +soft-float-abi -target-feature +vfp3 -target-feature +d16 -target-feature -neon -target-abi aapcs-linux -mfloat-abi soft -target-linker-version 253.3 -g -dwarf-column-info -ffunction-sections -coverage-file /opt/test/obj/local/armeabi-v7a/objs/test/test.o -resource-dir /opt/llvm-3.6.0/build/Release+Asserts/bin/../lib/clang/3.6.0 -dependency-file /opt/test/obj/local/armeabi-v7a/objs/test/test.o.d -MT /opt/test/obj/local/armeabi-v7a/objs/test/test.o -MP -D NDEBUG -D ANDROID -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libcxx/include -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/libcxxabi/include -I /Applications/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../../android/support/include -I /opt/test/jni -I /Applications/android-ndk-r10e/platforms/android-21/arch-arm/usr/include -internal-isystem /usr/local/include -internal-isystem /opt/llvm-3.6.0/build/Release+Asserts/bin/../lib/clang/3.6.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O1 -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -Wformat -Werror=format-security -std=c++11 -fdeprecated-macro -fno-dwarf-directory-asm -fdebug-compilation-dir /opt/test/jni -ferror-limit 19 -fmessage-length 152 -stack-protector 2 -mstackrealign -fno-rtti -fno-signed-char -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -load /opt/test/TestPass.dylib -mllvm -debug-pass=Structure -o /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-f2326e.s -x c++ /opt/test/jni/test.cpp<br>1.      <eof> parser at end of file<br>2.      Per-module optimization passes<br>3.      Running pass 'TestPass' on module '/opt/test/jni/test.cpp'.<br>clang++: error: unable to execute command: Illegal instruction: 4<br>clang++: error: clang frontend command failed due to signal (use -v to see invocation)<br>clang version 3.6.0 (tags/RELEASE_360/final)<br>Target: armv7-none-linux-androideabi<br>Thread model: posix<br>clang++: note: diagnostic msg: PLEASE submit a bug report to <a href="http://llvm.org/bugs/">http://llvm.org/bugs/</a> and include the crash backtrace, preprocessed source, and associated run script.<br>clang++: note: diagnostic msg:<br>********************<br><br>PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:<br>Preprocessed source(s) and associated run script(s) are located at:<br>clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-773901.cpp<br>clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-773901.sh<br>clang++: note: diagnostic msg:<br><br>********************<br>make: *** [/opt/test/obj/local/armeabi-v7a/objs/test/test.o] Error 254<br>---------------------------------------<br></div>