//===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // Parallel JIT // // This test program creates two LLVM functions then calls them from three // separate threads. It requires the pthreads library. // The three threads are created and then block waiting on a condition variable. // Once all threads are blocked on the conditional variable, the main thread // wakes them up. This complicated work is performed so that all three threads // call into the JIT at the same time (or the best possible approximation of the // same time). This test had assertion errors until I got the locking right. // //===----------------------------------------------------------------------===// #include "llvm/ADT/APInt.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/GenericValue.h" #include "llvm/ExecutionEngine/MCJIT.h" #include "llvm/IR/Argument.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include "llvm/Support/Casting.h" #include "llvm/Support/TargetSelect.h" #include <algorithm> #include <cassert> #include <cstddef> #include <cstdint> #include <iostream> #include <memory> #include <vector> #include <pthread.h> usingnamespacellvm; static Function* createAdd1(Module *M) { … } static Function *CreateFibFunction(Module *M) { … } struct threadParams { … }; // We block the subthreads just before they begin to execute: // we want all of them to call into the JIT at the same time, // to verify that the locking is working correctly. class WaitForThreads { … }; static WaitForThreads synchronize; void* callFunc( void* param ) { … } int main() { … }