ku nanashi via llvm-dev
2017-Nov-22 07:01 UTC
[llvm-dev] Error cloning function from another module and setting it alwaysinline on custom pass
Hi, I wan't to clone function from another bitcode module and set it AlwaysInline so it can be inlined. But error occurs when applying a such custom pass. (It seems cause is usage of CloneFunction()) How can I fix it? I'm using LLVM 3.6.0 on OS X and target is Android armeabi-v7a. (It should be same on other target) I'll appreciate any advice. Thanks. * callee.cpp (compiled to bitcode module callee.bc) --------------------------------------- #include <jni.h> #include <android/log.h> #ifdef __cplusplus extern "C" { #endif void callee(char *str, int num) { __android_log_print(ANDROID_LOG_DEBUG, "target", "%s, 0x%x", str, num); } #ifdef __cplusplus } #endif --------------------------------------- * test.cpp (module custom pass is applied) --------------------------------------- #include <jni.h> #ifdef __cplusplus extern "C" { #endif void caller() { } void Java_com_android_test_TestActivity_test(JNIEnv *env, jobject thiz) { caller(); } #ifdef __cplusplus } #endif --------------------------------------- * TestPass.cpp (custom pass) --------------------------------------- #include "llvm/IR/Module.h" #include "llvm/IR/Function.h" #include "llvm/IR/Type.h" #include "llvm/IR/InstIterator.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/IRBuilder.h" #include "llvm/Support/SourceMgr.h" #include "llvm/IRReader/IRReader.h" #include "llvm/Transforms/Utils/Cloning.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h" using namespace llvm; namespace { struct TestPass : public ModulePass { static char ID; TestPass() : ModulePass(ID) {} virtual bool runOnModule(Module &M) { for (Module::iterator F = M.begin(), E = M.end(); F!= E; ++F) { if (F->getName() == "caller") { inst_iterator I = inst_begin(F); Instruction *inst = &*I; SMDiagnostic error; LLVMContext context; std::unique_ptr<Module> m parseIRFile("callee.bc", error, context); for (Module::iterator f m->getFunctionList().begin(); f != m->getFunctionList().end(); f++) { if (f->getName() ="callee") { // Copy callee.bc callee function to current module and set AlwaysInline ValueToValueMapTy VMap; Function* callee_clone = CloneFunction(f, VMap, false); callee_clone->setName("callee_clone"); callee_clone->addFnAttr(Attribute::AlwaysInline); M.getFunctionList().push_back(callee_clone); // Add call to copied callee function GlobalVariable* str = new GlobalVariable(M, ArrayType::get(IntegerType::get(M.getContext(), 8), 8), false, GlobalValue::ExternalLinkage, 0, "str"); str->setAlignment(1); Constant* str_init = ConstantDataArray::getString(M.getContext(), "abcdefgh", false); str->setInitializer(str_init); ConstantInt* param1_indice1 = ConstantInt::get(M.getContext(), APInt(32, StringRef("0"), 10)); ConstantInt* param1_indice2 = ConstantInt::get(M.getContext(), APInt(32, StringRef("0"), 10)); std::vector<Constant*> param1_indices; param1_indices.push_back(param1_indice1); param1_indices.push_back(param1_indice2); Constant* param1 ConstantExpr::getGetElementPtr(str, param1_indices); GlobalVariable* num = new GlobalVariable(M, IntegerType::get(M.getContext(), 32), false, GlobalValue::ExternalLinkage, 0, "num"); num->setAlignment(4); ConstantInt* num_init = ConstantInt::get(M.getContext(), APInt(32, StringRef("12345"), 10)); num->setInitializer(num_init); LoadInst* param2 new LoadInst(num, "", false, inst); param2->setAlignment(4); std::vector<Value*> params; params.push_back(param1); params.push_back(param2); CallInst* ci CallInst::Create(callee_clone, params, "", inst); ci->setCallingConv(CallingConv::C); ci->setTailCall(false); } } } } return true; } }; } char TestPass::ID = 0; static RegisterPass<TestPass> X("TestPass", "TestPass", false, false); static void registerTestPass(const PassManagerBuilder &, legacy::PassManagerBase &PM) { PM.add(new TestPass()); } static RegisterStandardPasses RegisterTestPass(PassManagerBuilder::EP_ModuleOptimizerEarly, registerTestPass); --------------------------------------- * Error when applying custom pass --------------------------------------- [...snip...] 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. 0 clang++ 0x000000010cc03909 llvm::sys::PrintStackTrace(__sFILE*) + 57 1 clang++ 0x000000010cc0407b SignalHandler(int) + 555 2 libsystem_platform.dylib 0x00007fffb7debb3a _sigtramp + 26 3 libsystem_platform.dylib 0x0000000100000001 _sigtramp + 1210139873 4 clang++ 0x000000010cc03e36 abort + 22 5 clang++ 0x000000010cc03e11 __assert_rtn + 81 6 clang++ 0x000000010cb63714 llvm::CallInst::init(llvm::Value*, llvm::ArrayRef<llvm::Value*>, llvm::Twine const&) + 580 7 TestPass.dylib 0x000000010dc032c2 (anonymous namespace)::TestPass::runOnModule(llvm::Module&) + 1842 8 clang++ 0x000000010cb83c4e llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1054 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 10 clang++ 0x000000010ac2a33d clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 509 11 clang++ 0x000000010acbf954 clang::ParseAST(clang::Sema&, bool, bool) + 468 12 clang++ 0x000000010a930a53 clang::FrontendAction::Execute() + 67 13 clang++ 0x000000010a902b2c clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 956 14 clang++ 0x000000010a8bd63a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 4154 15 clang++ 0x000000010a8b465c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1068 16 clang++ 0x000000010a8bb7ec main + 11660 17 libdyld.dylib 0x00007fffb7bdc235 start + 1 18 libdyld.dylib 0x0000000000000066 start + 1212300850 Stack dump: 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 1. <eof> parser at end of file 2. Per-module optimization passes 3. Running pass 'TestPass' on module '/opt/test/jni/test.cpp'. clang++: error: unable to execute command: Illegal instruction: 4 clang++: error: clang frontend command failed due to signal (use -v to see invocation) clang version 3.6.0 (tags/RELEASE_360/final) Target: armv7-none-linux-androideabi Thread model: posix clang++: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script. clang++: note: diagnostic msg: ******************** PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-dc4bda.cpp clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-dc4bda.sh clang++: note: diagnostic msg: ******************** make: *** [/opt/test/obj/local/armeabi-v7a/objs/test/test.o] Error 254 --------------------------------------- * Error when commenting out below CloneFunction() --------------------------------------- [...snip...] // Copy callee.bc callee function to current module and set AlwaysInline ValueToValueMapTy VMap; Function* callee_clone = CloneFunction(f, VMap, false); /* callee_clone->setName("callee_clone"); callee_clone->addFnAttr(Attribute::AlwaysInline); M.getFunctionList().push_back(callee_clone); // Add call to copied callee function [...snip...] CallInst* ci CallInst::Create(callee_clone, params, "", inst); ci->setCallingConv(CallingConv::C); ci->setTailCall(false); */ [...snip...] --------------------------------------- --------------------------------------- [...snip...] While deleting: [7 x i8]* %.str Use still stuck around after Def is destroyed:@.str = private unnamed_addr constant [7 x i8] <null operand!>, align 1 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. 0 clang++ 0x000000011154d909 llvm::sys::PrintStackTrace(__sFILE*) + 57 1 clang++ 0x000000011154e07b SignalHandler(int) + 555 2 libsystem_platform.dylib 0x00007fffb7debb3a _sigtramp + 26 3 libsystem_platform.dylib 0x00000000509ff1d0 _sigtramp + 2562799280 4 clang++ 0x000000011154de36 abort + 22 5 clang++ 0x000000011154de11 __assert_rtn + 81 6 clang++ 0x00000001114f2104 llvm::Value::~Value() + 660 7 clang++ 0x00000001114a63d1 llvm::GlobalVariable::~GlobalVariable() + 97 8 clang++ 0x00000001114e3ba9 llvm::Module::~Module() + 105 9 TestPass.dylib 0x000000011254d7dc (anonymous namespace)::TestPass::runOnModule(llvm::Module&) + 524 10 clang++ 0x00000001114cdc4e llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1054 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 12 clang++ 0x000000010f57433d clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 509 13 clang++ 0x000000010f609954 clang::ParseAST(clang::Sema&, bool, bool) + 468 14 clang++ 0x000000010f27aa53 clang::FrontendAction::Execute() + 67 15 clang++ 0x000000010f24cb2c clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 956 16 clang++ 0x000000010f20763a clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 4154 17 clang++ 0x000000010f1fe65c cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 1068 18 clang++ 0x000000010f2057ec main + 11660 19 libdyld.dylib 0x00007fffb7bdc235 start + 1 20 libdyld.dylib 0x0000000000000066 start + 1212300850 Stack dump: 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 1. <eof> parser at end of file 2. Per-module optimization passes 3. Running pass 'TestPass' on module '/opt/test/jni/test.cpp'. clang++: error: unable to execute command: Illegal instruction: 4 clang++: error: clang frontend command failed due to signal (use -v to see invocation) clang version 3.6.0 (tags/RELEASE_360/final) Target: armv7-none-linux-androideabi Thread model: posix clang++: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script. clang++: note: diagnostic msg: ******************** PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT: Preprocessed source(s) and associated run script(s) are located at: clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-773901.cpp clang++: note: diagnostic msg: /var/folders/vq/53lxh89j7ts1rtc9gfg_s7f80000gq/T/test-773901.sh clang++: note: diagnostic msg: ******************** make: *** [/opt/test/obj/local/armeabi-v7a/objs/test/test.o] Error 254 --------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20171122/80f93843/attachment.html>