[clang] [clang-repl] Factor out CreateJITBuilder() and allow specialization in derived classes (PR #84461)
Stefan Gränitz via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 8 03:27:51 PST 2024
================
@@ -102,4 +124,97 @@ 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(); }
+};
+
+static void initArmTarget() {
+ static llvm::once_flag F;
+ llvm::call_once(F, [] {
+ LLVMInitializeARMTarget();
+ LLVMInitializeARMTargetInfo();
+ LLVMInitializeARMTargetMC();
+ LLVMInitializeARMAsmPrinter();
+ });
+}
+
+llvm::llvm_shutdown_obj Shutdown;
+
+TEST(InterpreterExtensionsTest, DefaultCrossJIT) {
+ 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));
+
+ initArmTarget();
+ cantFail(Interp.CreateExecutor());
+}
+
+TEST(InterpreterExtensionsTest, CustomCrossJIT) {
+ 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([&]() {
+ initArmTarget();
+ 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);
----------------
weliveindetail wrote:
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.
https://github.com/llvm/llvm-project/pull/84461
More information about the cfe-commits
mailing list