void code_emitter::emit_filter_function () { #ifdef DEBUG_OUTPUT printf("emitting filter func for %s\n", filter->name); #endif setup_filter_function(false); x_vars_type = build_const_value_infos(CONST_X); x_vars_var = builder->CreateAlloca(x_vars_type); compiler_slice_code_for_const(filter_code->first_stmt, CONST_X); emit_stmts(filter_code->first_stmt, SLICE_X_CONST); value_map.clear(); y_vars_type = build_const_value_infos(CONST_Y); y_vars_var = builder->CreateAlloca(y_vars_type); compiler_slice_code_for_const(filter_code->first_stmt, CONST_Y); emit_stmts(filter_code->first_stmt, SLICE_Y_CONST); value_map.clear(); compiler_slice_code_for_const(filter_code->first_stmt, CONST_NONE); emit_stmts(filter_code->first_stmt, SLICE_NO_CONST); finish_function(); }
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); }
Value* VariableDef::codeGen(CodeGenContext &context) { IRBuilder<> *builder = context.currentBuilder(); Value *alloc = builder->CreateAlloca(builder->getInt64Ty(), 0 , id.name.c_str()); context.locals()[id.name] = alloc; if ( assExpr != NULL) { context.locals()[id.name] = alloc; Value *idValue = (*assExpr).codeGen(context); return builder->CreateStore(idValue, context.locals()[id.name]); } return alloc; }
static AllocaInst *CreateEntryBlockAlloca(const string &VarName, string type) { if (type == "double") return Builder.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0, VarName.c_str()); else if (type == "doubles") return Builder.CreateAlloca(doublePtr, 0, VarName.c_str()); else if (type == "int") return Builder.CreateAlloca(Type::getInt32Ty(getGlobalContext()), 0, VarName.c_str()); else if (type == "ints") return Builder.CreateAlloca(intPtr32, 0, VarName.c_str()); else if (type == "char") return Builder.CreateAlloca(Type::getInt8Ty(getGlobalContext()), 0, VarName.c_str()); else if (type == "chars") return Builder.CreateAlloca(intPtr8, 0, VarName.c_str()); return 0; }
Value* FunctionDef::codeGen(CodeGenContext &context) { IRBuilder<> *builder = context.currentBuilder(); std::vector<Type*> argsType; ArgsDefList::const_iterator it; for (it = argsDefList.begin(); it != argsDefList.end(); it++) { argsType.push_back(builder->getInt64Ty()); } FunctionType *ftype = FunctionType::get(builder->getInt64Ty() , ArrayRef<Type*>(argsType), false); Function *function = Function::Create(ftype, GlobalValue::InternalLinkage , functionName.name.c_str(), context.module); BasicBlock *fBBlock = BasicBlock::Create(getGlobalContext() , "entry", function, 0); context.pushBlock(fBBlock); builder = context.currentBuilder(); Function::arg_iterator args = function->arg_begin(); for (it = argsDefList.begin(); it != argsDefList.end(); it++) { Value* a = args++; Id &argId = (**it); cout<<"argument name is "<<argId.name.c_str()<<endl; a->setName(argId.name.c_str()); Value *alloc = builder->CreateAlloca(builder->getInt64Ty(), 0 , argId.name.c_str()); context.locals()[argId.name] = alloc; builder->CreateStore(a, context.locals()[argId.name]); } (*functionBlock).codeGen(context); context.popBlock(); return NULL; }
void Context::init_variables(IRBuilder& b) { counter_ = b.CreateAlloca(Int32Ty, 0, "counter_alloca"); out_args_ = b.CreateAlloca(type("Arguments"), 0, "out_args"); }
Value* code_emitter::emit_rhs (rhs_t *rhs) { switch (rhs->kind) { case RHS_PRIMARY : return emit_primary(&rhs->v.primary); case RHS_INTERNAL : return lookup_internal(rhs->v.internal); case RHS_OP : { operation_t *op = rhs->v.op.op; type_t promotion_type = TYPE_NIL; char *function_name = compiler_function_name_for_op_rhs(rhs, &promotion_type); if (promotion_type == TYPE_NIL) assert(op->type_prop == TYPE_PROP_CONST); if (op->type_prop != TYPE_PROP_CONST) assert(promotion_type != TYPE_NIL); Function *func = module->getFunction(string(function_name)); g_assert(func); vector<Value*> args; args.push_back(invocation_arg); args.push_back(closure_arg); args.push_back(pools_arg); for (int i = 0; i < rhs->v.op.op->num_args; ++i) { type_t type = promotion_type == TYPE_NIL ? op->arg_types[i] : promotion_type; Value *val = emit_primary(&rhs->v.op.args[i], type == TYPE_FLOAT); val = promote(val, type); #ifndef __MINGW32__ if (sizeof(gpointer) == 4 && val->getType() == llvm_type_for_type(module, TYPE_COMPLEX)) { Value *copy = builder->CreateAlloca(llvm_type_for_type(module, TYPE_COMPLEX)); builder->CreateStore(val, copy); val = copy; } #endif #ifdef DEBUG_OUTPUT val->dump(); #endif args.push_back(val); } #ifdef DEBUG_OUTPUT func->dump(); #endif Value *result = builder->CreateCall(func, args.begin(), args.end()); /* FIXME: this is ugly - we should check for the type of the operation or resulting value */ if (is_complex_return_type(result->getType())) result = convert_complex_return_value(result); return result; } case RHS_FILTER : { int num_args = compiler_num_filter_args(rhs->v.filter.filter); Value *closure = emit_closure(rhs->v.filter.filter, rhs->v.filter.args); Function *func = lookup_filter_function(module, rhs->v.filter.filter); vector<Value*> args; args.push_back(invocation_arg); args.push_back(closure); args.push_back(emit_primary(&rhs->v.filter.args[num_args - 3])); args.push_back(emit_primary(&rhs->v.filter.args[num_args - 2])); args.push_back(emit_primary(&rhs->v.filter.args[num_args - 1])); args.push_back(pools_arg); return builder->CreateCall(func, args.begin(), args.end()); } case RHS_CLOSURE : return emit_closure(rhs->v.closure.filter, rhs->v.closure.args); case RHS_TUPLE : case RHS_TREE_VECTOR : { Function *set_func = module->getFunction(string("tuple_set")); Value *tuple = builder->CreateCall2(module->getFunction(string("alloc_tuple")), pools_arg, make_int_const(rhs->v.tuple.length)); int i; for (i = 0; i < rhs->v.tuple.length; ++i) { Value *val = emit_primary(&rhs->v.tuple.args[i], true); builder->CreateCall3(set_func, tuple, make_int_const(i), val); } if (rhs->kind == RHS_TREE_VECTOR) { return builder->CreateCall3(module->getFunction(string("alloc_tree_vector")), pools_arg, make_int_const(rhs->v.tuple.length), tuple); } else return tuple; } default : g_assert_not_reached(); } }
void code_emitter::alloc_complex_copy_var () { complex_copy_var = builder->CreateAlloca(llvm_type_for_type(module, TYPE_COMPLEX)); }
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; }