void ASTCodeGenVisitor::Visit(DecrData* s) { IRBuilder<> builder = builders_.top(); Value* ptr_val = builder.CreateLoad(ptr_); Value* result = builder.CreateAdd(ptr_val, neg_one); builder.CreateStore(result, ptr_); VisitNextASTNode(s); }
Value* Id::codeGen(CodeGenContext &context) { if (context.locals().find(name) == context.locals().end()) { return NULL; } IRBuilder<> *builder = context.currentBuilder(); return builder->CreateLoad(context.locals()[name], name); }
int main(int argc, char **argv) { InitializeNativeTarget(); LLVMContext &Context = getGlobalContext(); Module *m = new Module("test", Context); Type *intTy = Type::getInt64Ty(Context); StructType* structTy = StructType::create(Context, "struct.list"); std::vector<Type*> fields; fields.push_back(intTy); fields.push_back(PointerType::get(structTy, 0)); if (structTy->isOpaque()) { structTy->setBody(fields, false); } /* * int f1(struct x *p) { return p->next->l1; } */ std::vector<Type*> args_type; args_type.push_back(PointerType::get(structTy, 0)); FunctionType *fnTy = FunctionType::get(intTy, args_type, false); Function *func = Function::Create(fnTy, GlobalValue::ExternalLinkage, "f1", m); Value *v = func->arg_begin(); BasicBlock *bb = BasicBlock::Create(Context, "EntryBlock", func); IRBuilder<> *builder = new IRBuilder<>(bb); v = builder->CreateStructGEP(v, 1); v = builder->CreateLoad(v, "load0"); v = builder->CreateStructGEP(v, 0); v = builder->CreateLoad(v, "load1"); builder->CreateRet(v); (*m).dump(); { ExecutionEngine *ee = EngineBuilder(m). setEngineKind(EngineKind::JIT).create(); void *f = ee->getPointerToFunction(func); typedef int (*func_t) (struct x *); struct x o = {10, NULL}, v = {}; v.next = &o; std::cout << ((func_t)f)(&v) << std::endl; } return 0; }
void CNodeCodeGenVisitor::Visit(COutput* s) { IRBuilder<> builder = builders_.top(); Value* offset_ptr = builder.CreateGEP(ptr_, GetPtrOffset(s->GetOffset())); Value* ptr_value = builder.CreateLoad(offset_ptr); builder.CreateCall(put_char_, ptr_value); VisitNextCNode(s); }
vector<Value*> AssertionSiteInstrumenter::CollectArgs( Instruction *Before, const Automaton& A, Module& Mod, IRBuilder<>& Builder) { // Find named values to be passed to instrumentation. std::map<string,Value*> ValuesInScope; for (auto G = Mod.global_begin(); G != Mod.global_end(); G++) ValuesInScope[G->getName()] = G; auto *Fn = Before->getParent()->getParent(); for (auto& Arg : Fn->getArgumentList()) ValuesInScope[Arg.getName()] = &Arg; auto& EntryBlock(*Fn->begin()); for (auto& I : EntryBlock) { auto *Inst = dyn_cast<AllocaInst>(&I); if (!Inst) break; ValuesInScope[Inst->getName()] = Builder.CreateLoad(Inst); } int ArgSize = 0; for (auto& Arg : A.getAssertion().argument()) if (!Arg.free()) ArgSize = std::max(ArgSize + 1, Arg.index()); vector<Value*> Args(ArgSize, NULL); for (auto& Arg : A.getAssertion().argument()) { if (Arg.free()) continue; string Name(BaseName(Arg)); if (ValuesInScope.find(Name) == ValuesInScope.end()) { string s; raw_string_ostream Out(s); for (auto v : ValuesInScope) { Out << " \"" << v.first << "\": "; v.second->getType()->print(Out); Out << "\n"; } panic("assertion references non-existent variable '" + BaseName(Arg) + "'; was it defined under '#ifdef TESLA'?\n\n" "Variables in scope are:\n" + Out.str()); } Args[Arg.index()] = GetArgumentValue(ValuesInScope[Name], Arg, Builder, true); } return Args; }
Value* code_emitter::lookup_value (value_t *value) { g_assert(value->index >= 0); if (value_map.find(value) != value_map.end()) return value_map[value]; g_assert(compiler_is_permanent_const_value(value)); return builder->CreateLoad(emit_const_value_addr(value)); }
Value* WyvernFunction::getNamedValue (string name) { Value* value; value = namedValues[name]; if (dynamic_cast<AllocaInst*> (value) == NULL) return value; return Builder.CreateLoad (value); }
CodeGenBlock::CodeGenBlock(int args, int locals, CodeGenLexicalScope *enclosingScope, CodeGenModule *Mod) : CodeGenLexicalScope(Mod), parentScope(enclosingScope) { Value *enclosingContext = enclosingScope->getContext(); // Define the layout of a block BlockTy = StructType::get( Mod->Context, IdTy, // 0 - isa. IMPTy, // 1 - Function pointer. Type::getInt32Ty(Mod->Context),// 2 - Number of args. enclosingContext->getType(), // 3 - Context. NULL); std::vector<const Type*> argTy; argTy.push_back(PointerType::getUnqual(BlockTy)); // FIXME: Broken on Etoile runtime - _cmd needs to be a GEP on _call argTy.push_back(SelTy); for (int i=0 ; i<args ; ++i) { argTy.push_back(IdTy); } FunctionType *BlockFunctionTy = FunctionType::get(IdTy, argTy, false); IRBuilder<> *MethodBuilder = enclosingScope->getBuilder(); // Create the block object // The NewBlock function gets a block from a pool. It should really be // inlined. Block = MethodBuilder->CreateAlloca(BlockTy); Module *TheModule = CGM->getModule(); // Create the block function CurrentFunction = Function::Create(BlockFunctionTy, GlobalValue::InternalLinkage, "BlockFunction", TheModule); InitialiseFunction(Args, Locals, locals); // Set the isa pointer Value *isa = MethodBuilder->CreateLoad( TheModule->getGlobalVariable(".smalltalk_block_stack_class", true)); storeInStruct(MethodBuilder, Block, isa, 0); // Store the block function in the object storeInStruct(MethodBuilder, Block, MethodBuilder->CreateBitCast(CurrentFunction, IMPTy), 1); // Store the number of arguments storeInStruct(MethodBuilder, Block, ConstantInt::get(Type::getInt32Ty(Mod->Context), args), 2); // Set the context storeInStruct(MethodBuilder, Block, enclosingScope->getContext(), 3); }
void CNodeCodeGenVisitor::Visit(CMul* s) { IRBuilder<> builder = builders_.top(); int op_offset = s->GetOpOffset(); int target_offset = s->GetTargetOffset(); int amt = s->GetAmt(); Value* op_offset_ptr = builder.CreateGEP(ptr_, GetPtrOffset(op_offset)); Value* target_offset_ptr = builder.CreateGEP(ptr_, GetPtrOffset(target_offset)); Value* mul_val = GetDataOffset(amt); Value* op_val = builder.CreateLoad(op_offset_ptr); Value* target_val = builder.CreateLoad(target_offset_ptr); Value* mul_result = builder.CreateMul(op_val, mul_val); Value* add_result = builder.CreateAdd(target_val, mul_result); builder.CreateStore(add_result, target_offset_ptr); VisitNextCNode(s); }
void CNodeCodeGenVisitor::Visit(CAdd* s) { IRBuilder<> builder = builders_.top(); Value* offset_ptr = builder.CreateGEP(ptr_, GetPtrOffset(s->GetOffset())); Value* offset_val = builder.CreateLoad(offset_ptr); Value* add_val = GetDataOffset(s->GetAmt()); Value* result = builder.CreateAdd(offset_val, add_val); builder.CreateStore(result, offset_ptr); VisitNextCNode(s); }
Value* ColExpression::getValue() { IRBuilder<>* builder = codegen::getBuilder(); DataType dt = codegen::getAttType(index); Value* tupleptr = codegen::getTupleptr(); Value *indices[1]; indices[0] = ConstantInt::get(Type::getInt32Ty(getGlobalContext()), (uint64_t) index); ArrayRef<Value*> indicesRef(indices); Value *dataptr = builder->CreateInBoundsGEP(tupleptr, indicesRef); switch(dt){ case DOUBLE: return builder->CreateLoad( builder->CreateBitCast(dataptr, Type::getDoublePtrTy(getGlobalContext())) ); case LONG: case STRING: case DATE: return builder->CreateLoad(dataptr); } }
/// compile_put - Emit code for '.' void BrainFTraceRecorder::compile_put(BrainFTraceNode *node, IRBuilder<>& builder) { Value *Loaded = builder.CreateLoad(DataPtr); Value *Print = builder.CreateSExt(Loaded, IntegerType::get(Loaded->getContext(), 32)); builder.CreateCall(putchar_func, Print); if (node->left != (BrainFTraceNode*)~0ULL) compile_opcode(node->left, builder); else { HeaderPHI->addIncoming(DataPtr, builder.GetInsertBlock()); builder.CreateBr(Header); } }
void WinEHStatePass::unlinkExceptionRegistration(IRBuilder<> &Builder) { // Clone Link into the current BB for better address mode folding. if (auto *GEP = dyn_cast<GetElementPtrInst>(Link)) { GEP = cast<GetElementPtrInst>(GEP->clone()); Builder.Insert(GEP); Link = GEP; } Type *LinkTy = getEHLinkRegistrationType(); // [fs:00] = Link->Next Value *Next = Builder.CreateLoad(Builder.CreateStructGEP(LinkTy, Link, 0)); Constant *FSZero = Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257)); Builder.CreateStore(Next, FSZero); }
/// compile_minus - Emit code for '-' void BrainFTraceRecorder::compile_minus(BrainFTraceNode *node, IRBuilder<>& builder) { Value *CellValue = builder.CreateLoad(DataPtr); Constant *One = ConstantInt::get(IntegerType::getInt8Ty(Header->getContext()), 1); Value *UpdatedValue = builder.CreateSub(CellValue, One); builder.CreateStore(UpdatedValue, DataPtr); if (node->left != (BrainFTraceNode*)~0ULL) compile_opcode(node->left, builder); else { HeaderPHI->addIncoming(DataPtr, builder.GetInsertBlock()); builder.CreateBr(Header); } }
void WinEHStatePass::linkExceptionRegistration(IRBuilder<> &Builder, Function *Handler) { // Emit the .safeseh directive for this function. Handler->addFnAttr("safeseh"); Type *LinkTy = getEHLinkRegistrationType(); // Handler = Handler Value *HandlerI8 = Builder.CreateBitCast(Handler, Builder.getInt8PtrTy()); Builder.CreateStore(HandlerI8, Builder.CreateStructGEP(LinkTy, Link, 1)); // Next = [fs:00] Constant *FSZero = Constant::getNullValue(LinkTy->getPointerTo()->getPointerTo(257)); Value *Next = Builder.CreateLoad(FSZero); Builder.CreateStore(Next, Builder.CreateStructGEP(LinkTy, Link, 0)); // [fs:00] = Link Builder.CreateStore(Link, FSZero); }
void AddressSanitizer::instrumentAddress(Instruction *OrigIns, IRBuilder<> &IRB, Value *Addr, uint32_t TypeSize, bool IsWrite) { Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); Type *ShadowTy = IntegerType::get( *C, std::max(8U, TypeSize >> MappingScale)); Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); Value *ShadowPtr = memToShadow(AddrLong, IRB); Value *CmpVal = Constant::getNullValue(ShadowTy); Value *ShadowValue = IRB.CreateLoad( IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy)); Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal); Instruction *CheckTerm = splitBlockAndInsertIfThen( cast<Instruction>(Cmp)->getNextNode(), Cmp); IRBuilder<> IRB2(CheckTerm); size_t Granularity = 1 << MappingScale; if (TypeSize < 8 * Granularity) { // Addr & (Granularity - 1) Value *Lower3Bits = IRB2.CreateAnd( AddrLong, ConstantInt::get(IntptrTy, Granularity - 1)); // (Addr & (Granularity - 1)) + size - 1 Value *LastAccessedByte = IRB2.CreateAdd( Lower3Bits, ConstantInt::get(IntptrTy, TypeSize / 8 - 1)); // (uint8_t) ((Addr & (Granularity-1)) + size - 1) LastAccessedByte = IRB2.CreateIntCast( LastAccessedByte, IRB.getInt8Ty(), false); // ((uint8_t) ((Addr & (Granularity-1)) + size - 1)) >= ShadowValue Value *Cmp2 = IRB2.CreateICmpSGE(LastAccessedByte, ShadowValue); CheckTerm = splitBlockAndInsertIfThen(CheckTerm, Cmp2); } IRBuilder<> IRB1(CheckTerm); Instruction *Crash = generateCrashCode(IRB1, AddrLong, IsWrite, TypeSize); Crash->setDebugLoc(OrigIns->getDebugLoc()); ReplaceInstWithInst(CheckTerm, new UnreachableInst(*C)); }
Value* code_emitter::convert_complex_return_value (Value *result) { /* The result is complex, whose representation differs between archs, and we need to transform it into another arch-dependent representation. */ if (sizeof(gpointer) == 4) { Value *local = complex_copy_var; Value *local_ptr = builder->CreateBitCast(local, PointerType::getUnqual(Type::Int64Ty)); builder->CreateStore(result, local_ptr); result = builder->CreateLoad(local); } else if (sizeof(gpointer) == 8) result = builder->CreateExtractValue(result, 0); else g_assert_not_reached(); return result; }
// Rewrite final suspend point handling. We do not use suspend index to // represent the final suspend point. Instead we zero-out ResumeFnAddr in the // coroutine frame, since it is undefined behavior to resume a coroutine // suspended at the final suspend point. Thus, in the resume function, we can // simply remove the last case (when coro::Shape is built, the final suspend // point (if present) is always the last element of CoroSuspends array). // In the destroy function, we add a code sequence to check if ResumeFnAddress // is Null, and if so, jump to the appropriate label to handle cleanup from the // final suspend point. static void handleFinalSuspend(IRBuilder<> &Builder, Value *FramePtr, coro::Shape &Shape, SwitchInst *Switch, bool IsDestroy) { assert(Shape.HasFinalSuspend); auto FinalCaseIt = std::prev(Switch->case_end()); BasicBlock *ResumeBB = FinalCaseIt->getCaseSuccessor(); Switch->removeCase(FinalCaseIt); if (IsDestroy) { BasicBlock *OldSwitchBB = Switch->getParent(); auto *NewSwitchBB = OldSwitchBB->splitBasicBlock(Switch, "Switch"); Builder.SetInsertPoint(OldSwitchBB->getTerminator()); auto *GepIndex = Builder.CreateConstInBoundsGEP2_32(Shape.FrameTy, FramePtr, 0, 0, "ResumeFn.addr"); auto *Load = Builder.CreateLoad(GepIndex); auto *NullPtr = ConstantPointerNull::get(cast<PointerType>(Load->getType())); auto *Cond = Builder.CreateICmpEQ(Load, NullPtr); Builder.CreateCondBr(Cond, ResumeBB, NewSwitchBB); OldSwitchBB->getTerminator()->eraseFromParent(); } }
static void IncrementTimeCounter(Value* Inc, Value* calle, unsigned Index, GlobalVariable* Counters, IRBuilder<>& Builder, Value* Point) { LLVMContext &Context = Inc->getContext(); //In order to insert instruction after Point, we use the nextIns function. Value* nextIns = getNextIns(Point); Builder.SetInsertPoint(dyn_cast<Instruction>(nextIns)); // Create the getelementptr constant expression std::vector<Constant*> Indices(2); Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context)); Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), Index); Constant *ElementPtr = ConstantExpr::getGetElementPtr(Counters, Indices); // Load, increment and store the value back. // Use this formula: a = a + end_time - start_time ArrayRef<Value*> args; CallInst* Inc_end = Builder.CreateCall(calle, args, ""); Value* OldVal = Builder.CreateLoad(ElementPtr, "OldTimeCounter"); Value* TmpVal = Builder.CreateFSub(OldVal, Inc, "TmpTimeCounter"); Value* NewVal = Builder.CreateFAdd(TmpVal, Inc_end, "NewTimeCounter"); Builder.CreateStore(NewVal, ElementPtr); }
void AddressSanitizer::instrumentAddress(AsanFunctionContext &AFC, Instruction *OrigIns, IRBuilder<> &IRB, Value *Addr, uint32_t TypeSize, bool IsWrite) { Value *AddrLong = IRB.CreatePointerCast(Addr, IntptrTy); Type *ShadowTy = IntegerType::get( *C, std::max(8U, TypeSize >> MappingScale)); Type *ShadowPtrTy = PointerType::get(ShadowTy, 0); Value *ShadowPtr = memToShadow(AddrLong, IRB); Value *CmpVal = Constant::getNullValue(ShadowTy); Value *ShadowValue = IRB.CreateLoad( IRB.CreateIntToPtr(ShadowPtr, ShadowPtrTy)); Value *Cmp = IRB.CreateICmpNE(ShadowValue, CmpVal); size_t AccessSizeIndex = TypeSizeToSizeIndex(TypeSize); size_t Granularity = 1 << MappingScale; TerminatorInst *CrashTerm = 0; if (ClAlwaysSlowPath || (TypeSize < 8 * Granularity)) { TerminatorInst *CheckTerm = splitBlockAndInsertIfThen(Cmp, false); assert(dyn_cast<BranchInst>(CheckTerm)->isUnconditional()); BasicBlock *NextBB = CheckTerm->getSuccessor(0); IRB.SetInsertPoint(CheckTerm); Value *Cmp2 = createSlowPathCmp(IRB, AddrLong, ShadowValue, TypeSize); BasicBlock *CrashBlock = BasicBlock::Create(*C, "", &AFC.F, NextBB); CrashTerm = new UnreachableInst(*C, CrashBlock); BranchInst *NewTerm = BranchInst::Create(CrashBlock, NextBB, Cmp2); ReplaceInstWithInst(CheckTerm, NewTerm); } else { CrashTerm = splitBlockAndInsertIfThen(Cmp, true); } Instruction *Crash = generateCrashCode(CrashTerm, AddrLong, IsWrite, AccessSizeIndex); Crash->setDebugLoc(OrigIns->getDebugLoc()); }
codegen_value ast::distribution::createConstructor(Module *module, IRBuilder<> &builder, const string &ctor_name, Type *parameter_type, const vector<type_spec> ¶m_type_list, Value *eval, Value *sample, Value *pdf, Value *emit, Function *dtor) { //create function accepting parameters as arguments vector<Type*> arg_types; for (auto it = param_type_list.begin(); it != param_type_list.end(); ++it) arg_types.push_back((*it)->llvm_type()); FunctionType *ft = FunctionType::get(state->types["dfunc"]->llvm_type(), arg_types, false); Function *f = Function::Create(ft, Function::ExternalLinkage, ctor_name, module); BasicBlock *bb = BasicBlock::Create(getGlobalContext(), "func_entry", f); builder.SetInsertPoint(bb); //setup arguments for the alloc call Value *gd_scene = module->getNamedGlobal(".__gd_scene"); assert(gd_scene != NULL); Value *scene_ptr = builder.CreateLoad(gd_scene); //compute the shader flags codegen_value flag_val = codegen_all_flags(module, builder); return errors::codegen_call(flag_val, [&] (Value *&flag_bitmask) -> codegen_value { //get memory for a new distribution object Value *dfunc_ptr = state->types["dfunc"]->allocate(module, builder); //initialize the object and dynamically allocate parameter memory (calling a builtin function) Type* int_ptr_ty = Type::getInt32Ty(getGlobalContext())->getPointerTo(); vector<Type*> alloc_arg_types({state->types["scene_ptr"]->llvm_type(), Type::getInt32Ty(getGlobalContext()), state->types["shader_flag"]->llvm_type(), int_ptr_ty, int_ptr_ty, int_ptr_ty, int_ptr_ty, dtor->getType(), dfunc_ptr->getType()}); FunctionType *alloc_type = FunctionType::get(Type::getInt32PtrTy(getGlobalContext()), alloc_arg_types, false); Function *alloc_func = GetExternalFunction(module, "gd_builtin_alloc_dfunc", alloc_type); int param_data_size = DataLayout(module).getTypeAllocSize(parameter_type); Constant *param_size_arg = ConstantInt::get(getGlobalContext(), APInt(8*sizeof(int), param_data_size)); vector<Value*> alloc_args({scene_ptr, param_size_arg, flag_bitmask, builder.CreatePointerCast(eval, int_ptr_ty), builder.CreatePointerCast(sample, int_ptr_ty), builder.CreatePointerCast(pdf, int_ptr_ty), builder.CreatePointerCast(emit, int_ptr_ty), dtor, dfunc_ptr}); Value *param_ptr = builder.CreatePointerCast(builder.CreateCall(alloc_func, alloc_args), parameter_type->getPointerTo(), "dfunc_param_ptr"); //set each parameter auto arg_it = f->arg_begin(); unsigned int field_idx = 0; for (auto it = param_type_list.begin(); it != param_type_list.end(); ++it, ++arg_it, ++field_idx) { Value *param_copy = (*it)->copy(arg_it, module, builder); (*it)->store(param_copy, builder.CreateStructGEP(param_ptr, field_idx), module, builder); } //return the object Value *rt_val = builder.CreateLoad(dfunc_ptr, "dist_ref"); builder.CreateRet(rt_val); return f; }); }
void ASTCodeGenVisitor::Visit(Output* s) { IRBuilder<> builder = builders_.top(); Value* output = builder.CreateLoad(ptr_); builder.CreateCall(put_char_, output); VisitNextASTNode(s); }
/// compile_if - Emit code for '[' void BrainFTraceRecorder::compile_if(BrainFTraceNode *node, IRBuilder<>& builder) { BasicBlock *ZeroChild = 0; BasicBlock *NonZeroChild = 0; BasicBlock *Parent = builder.GetInsertBlock(); LLVMContext &Context = Header->getContext(); // If both directions of the branch go back to the trace-head, just // jump there directly. if (node->left == (BrainFTraceNode*)~0ULL && node->right == (BrainFTraceNode*)~0ULL) { HeaderPHI->addIncoming(DataPtr, builder.GetInsertBlock()); builder.CreateBr(Header); return; } // Otherwise, there are two cases to handle for each direction: // ~0ULL - A branch back to the trace head // 0 - A branch out of the trace // * - A branch to a node we haven't compiled yet. // Go ahead and generate code for both targets. if (node->left == (BrainFTraceNode*)~0ULL) { NonZeroChild = Header; HeaderPHI->addIncoming(DataPtr, Parent); } else if (node->left == 0) { NonZeroChild = BasicBlock::Create(Context, "exit_left_"+utostr(node->pc), Header->getParent()); builder.SetInsertPoint(NonZeroChild); // Set the extension leaf, which is a pointer to the leaf of the trace // tree from which we are side exiting. ConstantInt *ExtLeaf = ConstantInt::get(int_type, (intptr_t)node); builder.CreateStore(ExtLeaf, ext_leaf); ConstantInt *NewPc = ConstantInt::get(int_type, node->pc+1); Value *BytecodeIndex = builder.CreateConstInBoundsGEP1_32(bytecode_array, node->pc+1); Value *Target = builder.CreateLoad(BytecodeIndex); CallInst *Call =cast<CallInst>(builder.CreateCall2(Target, NewPc, DataPtr)); Call->setTailCall(); builder.CreateRetVoid(); } else { NonZeroChild = BasicBlock::Create(Context, utostr(node->left->pc), Header->getParent()); builder.SetInsertPoint(NonZeroChild); compile_opcode(node->left, builder); } if (node->right == (BrainFTraceNode*)~0ULL) { ZeroChild = Header; HeaderPHI->addIncoming(DataPtr, Parent); } else if (node->right == 0) { ZeroChild = BasicBlock::Create(Context, "exit_right_"+utostr(node->pc), Header->getParent()); builder.SetInsertPoint(ZeroChild); // Set the extension leaf, which is a pointer to the leaf of the trace // tree from which we are side exiting. ConstantInt *ExtLeaf = ConstantInt::get(int_type, (intptr_t)node); builder.CreateStore(ExtLeaf, ext_leaf); ConstantInt *NewPc = ConstantInt::get(int_type, JumpMap[node->pc]+1); Value *BytecodeIndex = builder.CreateConstInBoundsGEP1_32(bytecode_array, JumpMap[node->pc]+1); Value *Target = builder.CreateLoad(BytecodeIndex); CallInst *Call =cast<CallInst>(builder.CreateCall2(Target, NewPc, DataPtr)); Call->setTailCall(); builder.CreateRetVoid(); } else { ZeroChild = BasicBlock::Create(Context, utostr(node->right->pc), Header->getParent()); builder.SetInsertPoint(ZeroChild); compile_opcode(node->right, builder); } // Generate the test and branch to select between the targets. builder.SetInsertPoint(Parent); Value *Loaded = builder.CreateLoad(DataPtr); Value *Cmp = builder.CreateICmpEQ(Loaded, ConstantInt::get(Loaded->getType(), 0)); builder.CreateCondBr(Cmp, ZeroChild, NonZeroChild); }
JITFunction* CompilePipeline(Pipeline *pipeline, Thread *thread) { size_t i = 0; size_t size = 0; std::unique_ptr<Module> owner = make_unique<Module>("PipelineFunction", thread->context); Module *module = owner.get(); std::string fname = std::string("PipelineFunction") + std::to_string(thread->functions++); size_t input_count = pipeline->inputData->objects.size(); size_t output_count = pipeline->outputData->objects.size(); size_t function_arg_count = 6; size_t arg_count = input_count + output_count + (function_arg_count - 2); size_t start_addr = input_count + output_count; size_t end_addr = start_addr + 1; size_t result_sizes_addr = end_addr + 1; size_t thread_nr_addr = result_sizes_addr + 1; IRBuilder<> *builder = &thread->builder; auto passmanager = CreatePassManager(module, thread->jit.get()); module->setDataLayout(thread->jit->getTargetMachine().createDataLayout()); Type *int8_tpe = Type::getInt8Ty(thread->context); Type *int8ptr_tpe = PointerType::get(int8_tpe, 0); Type *int8ptrptr_tpe = PointerType::get(int8ptr_tpe, 0); Type *int64_tpe = Type::getInt64Ty(thread->context); Type *int64ptr_tpe = PointerType::get(int64_tpe, 0); JITInformation info; // arguments of the function // the arguments are (void **result, void** inputs, size_t start, size_t end); // note that we don't actually use void**, we use int8**, because LLVM does not support void pointers std::vector<Type*> arguments(function_arg_count); i = 0; arguments[i++] = int8ptrptr_tpe; // void** results arguments[i++] = int8ptrptr_tpe; // void** inputs arguments[i++] = int64_tpe; // size_t start arguments[i++] = int64_tpe; // size_t end arguments[i++] = int64ptr_tpe; // size_t* result_sizes arguments[i++] = int64_tpe; // size_t thread_nr assert(i == function_arg_count); /*for(auto inputs = pipeline->inputData->objects.begin(); inputs != pipeline->inputData->objects.end(); inputs++, i++) { arguments[i] = PointerType::get(getLLVMType(thread->context, inputs->type), 0); } for(auto outputs = pipeline->outputData->objects.begin(); outputs != pipeline->outputData->objects.end(); outputs++, i++) { arguments[i] = PointerType::get(getLLVMType(thread->context, outputs->type), 0); }*/ // create the LLVM function FunctionType *prototype = FunctionType::get(int64_tpe, arguments, false); Function *function = Function::Create(prototype, GlobalValue::ExternalLinkage, fname, module); function->setCallingConv(CallingConv::C); // create the basic blocks BasicBlock *loop_entry = BasicBlock::Create(thread->context, "entry", function, 0); BasicBlock *loop_cond = BasicBlock::Create(thread->context, "for.cond", function, 0); BasicBlock *loop_body = BasicBlock::Create(thread->context, "for.body", function, 0); BasicBlock *loop_inc = BasicBlock::Create(thread->context, "for.inc", function, 0); BasicBlock *loop_end = BasicBlock::Create(thread->context, "for.end", function, 0); info.builder = &thread->builder; info.context = &thread->context; info.function = function; info.loop_entry = loop_entry; info.loop_cond = loop_cond; info.loop_body = loop_body; info.loop_inc = loop_inc; info.loop_end = loop_end; info.current = loop_body; #ifndef _NOTDEBUG // argument names (for debug purposes only) std::vector<std::string> argument_names(arg_count); i = 0; for(auto inputs = pipeline->inputData->objects.begin(); inputs != pipeline->inputData->objects.end(); inputs++, i++) { argument_names[i] = std::string("inputs") + std::to_string(i); } for(auto outputs = pipeline->outputData->objects.begin(); outputs != pipeline->outputData->objects.end(); outputs++, i++) { argument_names[i] = std::string("outputs") + std::to_string(i - input_count); } argument_names[i++] = "start"; argument_names[i++] = "end"; argument_names[i++] = "result_sizes"; argument_names[i++] = "thread_nr"; #endif std::vector<AllocaInst*> argument_addresses(arg_count); builder->SetInsertPoint(loop_entry); { // allocate space for the arguments auto args = function->arg_begin(); i = 0; for(auto outputs = pipeline->outputData->objects.begin(); outputs != pipeline->outputData->objects.end(); outputs++, i++) { Type *column_type = PointerType::get(getLLVMType(thread->context, outputs->source->type), 0); Value *voidptrptr = builder->CreateGEP(int8ptr_tpe, &*args, ConstantInt::get(int64_tpe, i, true)); Value *voidptr = builder->CreateLoad(voidptrptr, "voidptr"); Value *columnptr = builder->CreatePointerCast(voidptr, column_type); argument_addresses[i] = builder->CreateAlloca(column_type, nullptr, argument_names[i]); builder->CreateStore(columnptr, argument_addresses[i]); outputs->alloca_address = (void*) argument_addresses[i]; if (size == 0 || size == 1) { assert(outputs->source->size >= 0); size = outputs->source->size; } assert(size == outputs->source->size || outputs->source->size == 1); } args++; for(auto inputs = pipeline->inputData->objects.begin(); inputs != pipeline->inputData->objects.end(); inputs++, i++) { Type *column_type = PointerType::get(getLLVMType(thread->context, inputs->source->type), 0); Value *voidptrptr = builder->CreateGEP(int8ptr_tpe, &*args, ConstantInt::get(int64_tpe, i - output_count, true)); Value *voidptr = builder->CreateLoad(voidptrptr, "voidptr"); Value *columnptr = builder->CreatePointerCast(voidptr, column_type); argument_addresses[i] = builder->CreateAlloca(column_type, nullptr, argument_names[i]); builder->CreateStore(columnptr, argument_addresses[i]); inputs->alloca_address = (void*) argument_addresses[i]; if (size == 0 || size == 1) { assert(inputs->source->size >= 0); size = inputs->source->size; } assert(size == inputs->source->size || inputs->source->size == 1); } args++; argument_addresses[i] = builder->CreateAlloca(arguments[2], nullptr, argument_names[i]); builder->CreateStore(&*args, argument_addresses[i]); args++; i++; argument_addresses[i] = builder->CreateAlloca(arguments[3], nullptr, argument_names[i]); builder->CreateStore(&*args, argument_addresses[i]); args++; i++; argument_addresses[i] = builder->CreateAlloca(arguments[4], nullptr, argument_names[i]); builder->CreateStore(&*args, argument_addresses[i]); args++; i++; argument_addresses[i] = builder->CreateAlloca(arguments[5], nullptr, argument_names[i]); builder->CreateStore(&*args, argument_addresses[i]); args++; i++; assert(args == function->arg_end()); assert(i == arg_count); info.index_addr = argument_addresses[start_addr]; info.thread_addr = argument_addresses[thread_nr_addr]; PerformInitialization(info, pipeline->operation); builder->CreateBr(loop_cond); } // for loop condition: index < end builder->SetInsertPoint(loop_cond); { LoadInst *index = builder->CreateLoad(argument_addresses[start_addr], "index"); LoadInst *end = builder->CreateLoad(argument_addresses[end_addr], "end"); Value *condition = builder->CreateICmpSLT(index, end, "index < end"); builder->CreateCondBr(condition, loop_body, loop_end); } // loop body: perform the computation builder->SetInsertPoint(loop_body); { LoadInst *index = builder->CreateLoad(argument_addresses[start_addr], "index"); info.index = index; info.index_addr = argument_addresses[start_addr]; // perform the computation over the given index // we don't use the return value because the final assignment has already taken place Value *v = PerformOperation(info, thread->builder, thread->context, pipeline->operation, pipeline->inputData, pipeline->outputData); if (v == NULL) { // failed to perform operation printf("Failed to compile pipeline %s\n", pipeline->name); return NULL; } builder->CreateBr(loop_inc); } // loop increment: index++ builder->SetInsertPoint(loop_inc); { LoadInst *index = builder->CreateLoad(argument_addresses[start_addr], "index"); Value *incremented_index = builder->CreateAdd(index, ConstantInt::get(int64_tpe, 1, true), "index++"); builder->CreateStore(incremented_index, argument_addresses[start_addr]); builder->CreateBr(loop_cond); } // loop end: return; (nothing happens here because we have no return value) builder->SetInsertPoint(loop_end); { // return the output size of each of the columns int i = 0; Value *result_sizes = builder->CreateLoad(argument_addresses[result_sizes_addr], "result_sizes[]"); for(auto it = pipeline->outputData->objects.begin(); it != pipeline->outputData->objects.end(); it++) { Value* output_count; if (it->index_addr) { output_count = builder->CreateLoad((Value*) it->index_addr, "count"); } else { output_count = ConstantInt::get(int64_tpe, 1, true); } Value *output_addr = builder->CreateGEP(int64_tpe, result_sizes, ConstantInt::get(int64_tpe, i, true)); builder->CreateStore(output_count, output_addr); i++; } builder->CreateRet(ConstantInt::get(int64_tpe, 0, true)); } #ifndef _NOTDEBUG verifyFunction(*function); verifyModule(*module); #endif //printf("LLVM for pipeline %s\n", pipeline->name); module->dump(); passmanager->run(*function); // dump generated LLVM code //module->dump(); auto handle = thread->jit->addModule(std::move(owner)); jit_function compiled_function = (jit_function) thread->jit->findSymbol(fname).getAddress(); if (!compiled_function) { printf("Error creating function.\n"); return NULL; } JITFunction *jf = CreateJITFunction(thread, pipeline); jf->size = size; jf->function = compiled_function; jf->jit = thread->jit.get(); jf->handle = handle; assert(jf->function); return jf; }