Skip to content

Commit 8ca5162

Browse files
committed
Fixing creation of Interpreter
1 parent c3384fb commit 8ca5162

File tree

5 files changed

+34
-8
lines changed

5 files changed

+34
-8
lines changed

lib/Interpreter/CXCppInterOp.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,15 @@ static inline compat::Interpreter* getInterpreter(const CXInterpreterImpl* I) {
272272

273273
CXInterpreter clang_createInterpreter(const char* const* argv, int argc) {
274274
auto* I = new CXInterpreterImpl(); // NOLINT(*-owning-memory)
275+
#ifdef CPPINTEROP_USE_CLING
275276
I->Interp = std::make_unique<compat::Interpreter>(argc, argv);
277+
#else
278+
I->Interp = compat::Interpreter::create(argc, argv);
279+
if (!I->Interp) {
280+
delete I;
281+
return nullptr;
282+
}
283+
#endif
276284
// create a bridge between CXTranslationUnit and clang::Interpreter
277285
// auto AU = std::make_unique<ASTUnit>(false);
278286
// AU->FileMgr = I->Interp->getCompilerInstance().getFileManager();

lib/Interpreter/Compatibility.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,11 @@ createClangInterpreter(std::vector<const char*>& args) {
294294
return nullptr;
295295
}
296296
if (CudaEnabled) {
297-
if (auto Err = (*innerOrErr)->LoadDynamicLibrary("libcudart.so"))
297+
if (auto Err = (*innerOrErr)->LoadDynamicLibrary("libcudart.so")) {
298298
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(),
299299
"Failed load libcudart.so runtime:");
300+
return nullptr;
301+
}
300302
}
301303

302304
return std::move(*innerOrErr);

lib/Interpreter/CppInterOp.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,7 +2957,14 @@ namespace Cpp {
29572957
std::back_inserter(ClingArgv),
29582958
[&](const std::string& str) { return str.c_str(); });
29592959

2960+
#ifdef CPPINTEROP_USE_CLING
29602961
auto I = new compat::Interpreter(ClingArgv.size(), &ClingArgv[0]);
2962+
#else
2963+
auto Interp = compat::Interpreter::create(ClingArgv.size(), &ClingArgv[0]);
2964+
if (!Interp)
2965+
return nullptr;
2966+
auto I = Interp.release();
2967+
#endif
29612968

29622969
// Honor -mllvm.
29632970
//

lib/Interpreter/CppInterOpInterpreter.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,14 @@ class Interpreter {
136136
private:
137137
std::unique_ptr<clang::Interpreter> inner;
138138

139+
Interpreter(std::unique_ptr<clang::Interpreter> CI) : inner(std::move(CI)) {}
140+
139141
public:
140-
Interpreter(int argc, const char* const* argv, const char* llvmdir = 0,
141-
const std::vector<std::shared_ptr<clang::ModuleFileExtension>>&
142-
moduleExtensions = {},
143-
void* extraLibHandle = 0, bool noRuntime = true) {
142+
static std::unique_ptr<Interpreter>
143+
create(int argc, const char* const* argv, const char* llvmdir = 0,
144+
const std::vector<std::shared_ptr<clang::ModuleFileExtension>>&
145+
moduleExtensions = {},
146+
void* extraLibHandle = 0, bool noRuntime = true) {
144147
// Initialize all targets (required for device offloading)
145148
llvm::InitializeAllTargetInfos();
146149
llvm::InitializeAllTargets();
@@ -150,7 +153,13 @@ class Interpreter {
150153
std::vector<const char*> vargs(argv + 1, argv + argc);
151154
vargs.push_back("-include");
152155
vargs.push_back("new");
153-
inner = compat::createClangInterpreter(vargs);
156+
auto CI = compat::createClangInterpreter(vargs);
157+
if (!CI) {
158+
llvm::errs() << "Interpreter creation failed\n";
159+
return nullptr;
160+
}
161+
162+
return std::unique_ptr<Interpreter>(new Interpreter(std::move(CI)));
154163
}
155164

156165
~Interpreter() {}

unittests/CppInterOp/CUDATest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static bool HasCudaSDK() {
1313
// FIXME: Enable this for cling.
1414
return false;
1515
#endif // CLANG_VERSION_MAJOR < 16
16-
Cpp::CreateInterpreter({}, {"--cuda"});
16+
if(!Cpp::CreateInterpreter({}, {"--cuda"})) return false;
1717
return Cpp::Declare("__global__ void test_func() {}"
1818
"test_func<<<1,1>>>();") == 0;
1919
};
@@ -30,7 +30,7 @@ static bool HasCudaRuntime() {
3030
if (!HasCudaSDK())
3131
return false;
3232

33-
Cpp::CreateInterpreter({}, {"--cuda"});
33+
if(!Cpp::CreateInterpreter({}, {"--cuda"})) return false;
3434
if (Cpp::Declare("__global__ void test_func() {}"
3535
"test_func<<<1,1>>>();"))
3636
return false;

0 commit comments

Comments
 (0)