-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-repl] Factor out CreateJITBuilder() and allow specialization in derived classes #84461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,14 +18,21 @@ | |
#include "clang/Sema/Sema.h" | ||
|
||
#include "llvm/ExecutionEngine/Orc/LLJIT.h" | ||
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/TargetSelect.h" | ||
#include "llvm/Support/Threading.h" | ||
#include "llvm/Testing/Support/Error.h" | ||
|
||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
|
||
#include <system_error> | ||
|
||
#if defined(_AIX) | ||
#define CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT | ||
#endif | ||
|
||
using namespace clang; | ||
namespace { | ||
|
||
|
@@ -41,6 +48,10 @@ struct LLVMInitRAII { | |
LLVMInitRAII() { | ||
llvm::InitializeNativeTarget(); | ||
llvm::InitializeNativeTargetAsmPrinter(); | ||
LLVMInitializeARMTarget(); | ||
LLVMInitializeARMTargetInfo(); | ||
LLVMInitializeARMTargetMC(); | ||
LLVMInitializeARMAsmPrinter(); | ||
} | ||
~LLVMInitRAII() { llvm::llvm_shutdown(); } | ||
} LLVMInit; | ||
|
@@ -51,12 +62,30 @@ class TestCreateResetExecutor : public Interpreter { | |
llvm::Error &Err) | ||
: Interpreter(std::move(CI), Err) {} | ||
|
||
llvm::Error testCreateExecutor() { return Interpreter::CreateExecutor(); } | ||
llvm::Error testCreateJITBuilderError() { | ||
JB = nullptr; | ||
return Interpreter::CreateExecutor(); | ||
} | ||
|
||
llvm::Error testCreateExecutor() { | ||
JB = std::make_unique<llvm::orc::LLJITBuilder>(); | ||
return Interpreter::CreateExecutor(); | ||
} | ||
|
||
void resetExecutor() { Interpreter::ResetExecutor(); } | ||
|
||
private: | ||
llvm::Expected<std::unique_ptr<llvm::orc::LLJITBuilder>> | ||
CreateJITBuilder(CompilerInstance &CI) override { | ||
if (JB) | ||
return std::move(JB); | ||
return llvm::make_error<llvm::StringError>("TestError", std::error_code()); | ||
} | ||
|
||
std::unique_ptr<llvm::orc::LLJITBuilder> JB; | ||
}; | ||
|
||
#ifdef _AIX | ||
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT | ||
TEST(InterpreterExtensionsTest, DISABLED_ExecutorCreateReset) { | ||
#else | ||
TEST(InterpreterExtensionsTest, ExecutorCreateReset) { | ||
|
@@ -69,6 +98,8 @@ TEST(InterpreterExtensionsTest, ExecutorCreateReset) { | |
llvm::Error ErrOut = llvm::Error::success(); | ||
TestCreateResetExecutor Interp(cantFail(CB.CreateCpp()), ErrOut); | ||
cantFail(std::move(ErrOut)); | ||
EXPECT_THAT_ERROR(Interp.testCreateJITBuilderError(), | ||
llvm::FailedWithMessage("TestError")); | ||
cantFail(Interp.testCreateExecutor()); | ||
Interp.resetExecutor(); | ||
cantFail(Interp.testCreateExecutor()); | ||
|
@@ -126,4 +157,90 @@ TEST(InterpreterExtensionsTest, FindRuntimeInterface) { | |
EXPECT_EQ(1U, Interp.RuntimeIBPtr->TransformerQueries); | ||
} | ||
|
||
class CustomJBInterpreter : public Interpreter { | ||
using CustomJITBuilderCreatorFunction = | ||
std::function<llvm::Expected<std::unique_ptr<llvm::orc::LLJITBuilder>>()>; | ||
CustomJITBuilderCreatorFunction JBCreator = nullptr; | ||
|
||
public: | ||
CustomJBInterpreter(std::unique_ptr<CompilerInstance> CI, llvm::Error &ErrOut) | ||
: Interpreter(std::move(CI), ErrOut) {} | ||
|
||
~CustomJBInterpreter() override { | ||
// Skip cleanUp() because it would trigger LLJIT default dtors | ||
Interpreter::ResetExecutor(); | ||
} | ||
|
||
void setCustomJITBuilderCreator(CustomJITBuilderCreatorFunction Fn) { | ||
JBCreator = std::move(Fn); | ||
} | ||
|
||
llvm::Expected<std::unique_ptr<llvm::orc::LLJITBuilder>> | ||
CreateJITBuilder(CompilerInstance &CI) override { | ||
if (JBCreator) | ||
return JBCreator(); | ||
return Interpreter::CreateJITBuilder(CI); | ||
} | ||
|
||
llvm::Error CreateExecutor() { return Interpreter::CreateExecutor(); } | ||
}; | ||
|
||
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT | ||
TEST(InterpreterExtensionsTest, DISABLED_DefaultCrossJIT) { | ||
#else | ||
TEST(InterpreterExtensionsTest, DefaultCrossJIT) { | ||
#endif | ||
IncrementalCompilerBuilder CB; | ||
CB.SetTargetTriple("armv6-none-eabi"); | ||
auto CI = cantFail(CB.CreateCpp()); | ||
llvm::Error ErrOut = llvm::Error::success(); | ||
CustomJBInterpreter Interp(std::move(CI), ErrOut); | ||
cantFail(std::move(ErrOut)); | ||
cantFail(Interp.CreateExecutor()); | ||
} | ||
|
||
#ifdef CLANG_INTERPRETER_PLATFORM_CANNOT_CREATE_LLJIT | ||
TEST(InterpreterExtensionsTest, DISABLED_CustomCrossJIT) { | ||
#else | ||
TEST(InterpreterExtensionsTest, CustomCrossJIT) { | ||
#endif | ||
std::string TargetTriple = "armv6-none-eabi"; | ||
|
||
IncrementalCompilerBuilder CB; | ||
CB.SetTargetTriple(TargetTriple); | ||
auto CI = cantFail(CB.CreateCpp()); | ||
llvm::Error ErrOut = llvm::Error::success(); | ||
CustomJBInterpreter Interp(std::move(CI), ErrOut); | ||
cantFail(std::move(ErrOut)); | ||
|
||
using namespace llvm::orc; | ||
LLJIT *JIT = nullptr; | ||
std::vector<std::unique_ptr<llvm::MemoryBuffer>> Objs; | ||
Interp.setCustomJITBuilderCreator([&]() { | ||
auto JTMB = JITTargetMachineBuilder(llvm::Triple(TargetTriple)); | ||
JTMB.setCPU("cortex-m0plus"); | ||
auto JB = std::make_unique<LLJITBuilder>(); | ||
JB->setJITTargetMachineBuilder(JTMB); | ||
JB->setPlatformSetUp(setUpInactivePlatform); | ||
JB->setNotifyCreatedCallback([&](LLJIT &J) { | ||
ObjectLayer &ObjLayer = J.getObjLinkingLayer(); | ||
auto *JITLinkObjLayer = llvm::dyn_cast<ObjectLinkingLayer>(&ObjLayer); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We know that the ARM target uses JITLink, but we could also check explicitly. It seems nice to check that we produce some actual code. Maybe we want checks on the object code in the future? This might be a good template for such tests. |
||
JITLinkObjLayer->setReturnObjectBuffer( | ||
[&Objs](std::unique_ptr<llvm::MemoryBuffer> MB) { | ||
Objs.push_back(std::move(MB)); | ||
}); | ||
JIT = &J; | ||
return llvm::Error::success(); | ||
}); | ||
return JB; | ||
}); | ||
|
||
EXPECT_EQ(0U, Objs.size()); | ||
cantFail(Interp.CreateExecutor()); | ||
cantFail(Interp.ParseAndExecute("int a = 1;")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works cross-platform, because we don't actually execute code. InactivePlatform suppresses execution of the respective initializers. |
||
ExecutorAddr Addr = cantFail(JIT->lookup("a")); | ||
EXPECT_NE(0U, Addr.getValue()); | ||
EXPECT_EQ(1U, Objs.size()); | ||
} | ||
|
||
} // end anonymous namespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We keep creating a new LLJITBuilder for each new IncrementalExecutor. This is questionable, but I didn't want to clutter the patch unnecessarily. I am happy to change that in a follow-up PR.