ThreadSafeModule构造函数的奇怪之处

时间:2019-05-15 15:21:21

标签: c++ llvm unique-ptr llvm-c++-api

我正在尝试调用一个ThreadSafeModule时需要Module*的LLVM API。我查找ThreadSafeModule,它具有以下构造函数:

ThreadSafeModule(ThreadSafeModule &&Other) = default;

/// Construct a ThreadSafeModule from a unique_ptr<Module> and a
/// unique_ptr<LLVMContext>. This creates a new ThreadSafeContext from the
/// given context.
ThreadSafeModule(std::unique_ptr<Module> M, std::unique_ptr<LLVMContext> Ctx)
    : M(std::move(M)), TSCtx(std::move(Ctx)) {}

/// Construct a ThreadSafeModule from a unique_ptr<Module> and an
/// existing ThreadSafeContext.
ThreadSafeModule(std::unique_ptr<Module> M, ThreadSafeContext TSCtx)
    : M(std::move(M)), TSCtx(std::move(TSCtx)) {}

第二个或第三个似乎合适,所以我尝试了几种变体:

ThreadSafeContext tsc;

ThreadSafeModule tsm(*module, tsc);
ThreadSafeModule tsm(module, tsc);
ThreadSafeModule tsm(std::move(*module), tsc);
ThreadSafeModule tsm(std::move(module), tsc);
auto tsm = new ThreadSafeModule(*module, tsc);
auto tsm = new ThreadSafeModule(module, tsc);

和上面的内容用LLVMContext*而不是ThreadSafeContext,在所有情况下,我都会得到基本相同的错误消息:

jit.cpp(21): error C2664: 'llvm::orc::ThreadSafeModule::ThreadSafeModule(const llvm::orc::ThreadSafeModule &)': cannot convert argument 1 from 'llvm::Module *' to 'std::unique_ptr<llvm::Module,std::default_delete<_Ty>>'
    with
    [
        _Ty=llvm::Module
    ]
jit.cpp(21): note: Constructor for class 'std::unique_ptr<llvm::Module,std::default_delete<_Ty>>' is declared 'explicit'
    with
    [
        _Ty=llvm::Module
    ]

错误消息的第一部分似乎是由于某种原因试图调用复制构造函数,但这似乎与下一部分矛盾。

你知道这是怎么回事,正确的咒语是什么?

编辑:

此行可以使用:

std::unique_ptr<Module> up(module);

但是这仍然失败:

ThreadSafeModule tsm(up, tsc);

,错误消息略有不同:

jit.cpp(23): error C2280: 'std::unique_ptr<llvm::Module,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': attempting to reference a deleted function
    with
    [
        _Ty=llvm::Module
    ]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\memory(2337): note: see declaration of 'std::unique_ptr<llvm::Module,std::default_delete<_Ty>>::unique_ptr'
    with
    [
        _Ty=llvm::Module
    ]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\memory(2337): note: 'std::unique_ptr<llvm::Module,std::default_delete<_Ty>>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)': function was explicitly deleted
    with
    [
        _Ty=llvm::Module
    ] (compiling source file jit.cpp)

1 个答案:

答案 0 :(得分:1)

感谢#llvm上的抢劫:答案是unique_ptr必须单独构造然后再移动:

std::unique_ptr<Module> up(module);
ThreadSafeModule tsm(std::move(up), tsc);