无法从指向现有变量的指针创建唯一指针

时间:2018-03-17 08:38:36

标签: c++11 llvm unique-ptr

我正在尝试修改LLVM代码的某些部分。我在这里面临困难:

{这段代码只是为了创建一个传递给另一个模块的OptimizationRemark}

OptimizationRemarkAnalysis &LoopAccessInfo::recordAnalysis(StringRef RemarkName,
                                                       Instruction *I) {
  assert(!Report && "Multiple reports generated");

  Value *CodeRegion = TheLoop->getHeader();
  DebugLoc DL = TheLoop->getStartLoc();

  if (I) {
    CodeRegion = I->getParent();
    // If there is no debug location attached to the instruction, revert back to
    // using the loop's.
    if (I->getDebugLoc())
      DL = I->getDebugLoc();
  }

  OptimizationRemarkAnalysis R(DEBUG_TYPE, RemarkName, DL, CodeRegion);
  Report = std::unique_ptr<OptimizationRemarkAnalysis>{&R};

  return *Report;
}

当我尝试构建它时,它工作正常。但是当我在基准测试中运行时,它会出现这样的错误:

#0 0x0000563337f8bb9f llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/Support/Unix/Signals.inc:398:0
#1 0x0000563337f8bc32 PrintStackTraceSignalHandler(void*) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/Support/Unix/Signals.inc:462:0
#2 0x0000563337f89e07 llvm::sys::RunSignalHandlers() /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/Support/Signals.cpp:49:0
#3 0x0000563337f8b40b SignalHandler(int) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/Support/Unix/Signals.inc:252:0
#4 0x00007f3ec0e82150 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x13150)
#5 0x0000563338f6f61a std::default_delete<llvm::OptimizationRemarkAnalysis>::operator()(llvm::OptimizationRemarkAnalysis*) const /usr/include/c++/7/bits/unique_ptr.h:78:0
#6 0x0000563338f69699 std::unique_ptr<llvm::OptimizationRemarkAnalysis, std::default_delete<llvm::OptimizationRemarkAnalysis> >::~unique_ptr() /usr/include/c++/7/bits/unique_ptr.h:269:0
#7 0x0000563338f4942c llvm::LoopAccessInfo::~LoopAccessInfo() /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/include/llvm/Analysis/LoopAccessAnalysis.h:493:0
#8 0x00005633391b9612 std::default_delete<llvm::LoopAccessInfo>::operator()(llvm::LoopAccessInfo*) const /usr/include/c++/7/bits/unique_ptr.h:78:0
#9 0x00005633391b73c1 std::unique_ptr<llvm::LoopAccessInfo, std::default_delete<llvm::LoopAccessInfo> >::~unique_ptr() /usr/include/c++/7/bits/unique_ptr.h:269:0
#10 0x00005633391b54a9 llvm::DenseMapBase<llvm::DenseMap<llvm::Loop*, std::unique_ptr<llvm::LoopAccessInfo, std::default_delete<llvm::LoopAccessInfo> >, llvm::DenseMapInfo<llvm::Loop*>, llvm::detail::DenseMapPair<llvm::Loop*, std::unique_ptr<llvm::LoopAccessInfo, std::default_delete<llvm::LoopAccessInfo> > > >, llvm::Loop*, std::unique_ptr<llvm::LoopAccessInfo, std::default_delete<llvm::LoopAccessInfo> >, llvm::DenseMapInfo<llvm::Loop*>, llvm::detail::DenseMapPair<llvm::Loop*, std::unique_ptr<llvm::LoopAccessInfo, std::default_delete<llvm::LoopAccessInfo> > > >::clear() /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/include/llvm/ADT/DenseMap.h:129:0
#11 0x00005633391b4f2a llvm::LoopAccessLegacyAnalysis::releaseMemory() /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/include/llvm/Analysis/LoopAccessAnalysis.h:702:0
#12 0x00005633377d7eda llvm::PMDataManager::freePass(llvm::Pass*, llvm::StringRef, llvm::PassDebuggingString) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/IR/LegacyPassManager.cpp:950:0
#13 0x00005633377d7dd3 llvm::PMDataManager::removeDeadPasses(llvm::Pass*, llvm::StringRef, llvm::PassDebuggingString) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/IR/LegacyPassManager.cpp:939:0
#14 0x00005633377da887 llvm::FPPassManager::runOnFunction(llvm::Function&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/IR/LegacyPassManager.cpp:1507:0
#15 0x00005633377da935 llvm::FPPassManager::runOnModule(llvm::Module&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/IR/LegacyPassManager.cpp:1541:0
#16 0x00005633377dacbd (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/IR/LegacyPassManager.cpp:1597:0
#17 0x00005633377db3e7 llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/IR/LegacyPassManager.cpp:1700:0
#18 0x00005633377db5df llvm::legacy::PassManager::run(llvm::Module&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/lib/IR/LegacyPassManager.cpp:1732:0
#19 0x000056333827078d (anonymous namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:801:0
#20 0x00005633382734c9 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/CodeGen/BackendUtil.cpp:1198:0
#21 0x0000563338e748e9 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/CodeGen/CodeGenAction.cpp:292:0
#22 0x0000563339c2b6f8 clang::ParseAST(clang::Sema&, bool, bool) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/Parse/ParseAST.cpp:171:0
#23 0x0000563338936f01 clang::ASTFrontendAction::ExecuteAction() /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/Frontend/FrontendAction.cpp:1005:0
#24 0x0000563338e72778 clang::CodeGenAction::ExecuteAction() /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/CodeGen/CodeGenAction.cpp:1041:0
#25 0x0000563338936944 clang::FrontendAction::Execute() /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/Frontend/FrontendAction.cpp:908:0
#26 0x00005633388d0954 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp:992:0
#27 0x0000563338a8bf2b clang::ExecuteCompilerInvocation(clang::CompilerInstance*) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/lib/FrontendTool/ExecuteCompilerInvocation.cpp:255:0
#28 0x0000563335d19fa6 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/tools/driver/cc1_main.cpp:221:0
#29 0x0000563335d0eced ExecuteCC1Tool(llvm::ArrayRef<char const*>, llvm::StringRef) /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/tools/driver/driver.cpp:309:0
#30 0x0000563335d0f9ad main /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm/tools/clang/tools/driver/driver.cpp:389:0
#31 0x00007f3ebfd8e1c1 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x211c1)
#32 0x0000563335d0c3ca _start (/home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm_build/bin/clang-7.0+0x1e223ca)
Stack dump:
0.  Program arguments: /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm_build/bin/clang-7.0 -cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -disable-free -main-file-name tsc.c -mrelocation-model static -mthread-model posix -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fno-trapping-math -ffp-contract=fast -ffast-math -ffinite-math-only -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-feature +avx2 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -coverage-notes-file /home/cs15btech11044/list_of_llvms/TSVC/TSVC_/tsc.gcno -resource-dir /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm_build/lib/clang/7.0.0 -internal-isystem /usr/local/include -internal-isystem /home/cs15btech11044/list_of_llvms/LLVM_second_remark/LLVM_for_commits_2/llvm_build/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Rpass-analysis=loop-vectorize -Rpass=loop-vectorize -fdebug-compilation-dir /home/cs15btech11044/list_of_llvms/TSVC/TSVC_ -ferror-limit 19 -fmessage-length 150 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o tsc.ll -x c tsc.c 
1.  <eof> parser at end of file
2.  Per-module optimization passes
3.  Running pass 'Function Pass Manager' on module 'tsc.c'.
4.  Releasing pass 'Loop Access Analysis'
clang-7.0: error: unable to execute command: Segmentation fault (core dumped)
clang-7.0: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 7.0.0 (https://git.llvm.org/git/clang.git/ d925bc6affe1ed96737cc016f0dc53cdd89bee5b) (https://git.llvm.org/git/llvm.git/ 1cb64026709ddf4b7cbb090faef8457968df581a)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/cs15btech11044/list_of_llvms/TSVC/TSVC_/../../LLVM_second_remark/LLVM_for_commits_2/llvm_build/bin
clang-7.0: note: diagnostic msg: PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace, preprocessed source, and associated run script.
clang-7.0: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-7.0: note: diagnostic msg: /tmp/tsc-c03557.c
clang-7.0: note: diagnostic msg: /tmp/dummy-ddbe6a.c
clang-7.0: note: diagnostic msg: /tmp/tsc-c03557.sh
clang-7.0: note: diagnostic msg: 
********************

主要是由于这些问题引起的问题,

OptimizationRemarkAnalysis R(DEBUG_TYPE, RemarkName, DL, CodeRegion);
Report = std::unique_ptr<OptimizationRemarkAnalysis>{&R};

我想知道为什么这是不可能的,是否有任何其他方法可以将任何指针转换为C ++ 11中的唯一指针。

1 个答案:

答案 0 :(得分:4)

这会失败,因为R超出了范围,并且它仍然在堆栈上 - 所以你不能拥有一个指向其地址的唯一指针而不会使其无效。试试这个:

auto Report = std::unique_ptr<OptimizationRemarkAnalysis>{ 
    new OptimizationRemarkAnalysis(DEBUG_TYPE, RemarkName, DL, CodeRegion) };

或在C ++ 14中:

auto Report = std::make_unique<OptimizationRemarkAnalysis>(
    DEBUG_TYPE, RemarkName, DL, CodeRegion);