Paste number 60596: float return

Index of paste annotations: 1

Paste number 60596: float return
Pasted by: zack
5 days, 10 hours ago
None
Paste contents:
Raw Source | XML | Display As
#include <assert.h>
#include <llvm/Module.h>
#include <llvm/ModuleProvider.h>
#include <llvm/Function.h>
#include <llvm/PassManager.h>
#include <llvm/CallingConv.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/Assembly/PrintModulePass.h>
#include <llvm/Support/LLVMBuilder.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include "llvm/ExecutionEngine/GenericValue.h"
#include <iostream>

using namespace llvm;


static Function *
make_mul_add(Module *mod)
{
   Constant *c = mod->getOrInsertFunction("mul_add",
                                          Type::FloatTy, // ret type
                                          Type::FloatTy, // args
                                          Type::FloatTy,
                                          Type::FloatTy,
                                          NULL /* terminator*/);

   Function *func = cast<Function>(c);

   func->setCallingConv(CallingConv::C);

   Function::arg_iterator args = func->arg_begin();
   Value* x = args++;
   x->setName("x");
   Value* y = args++;
   y->setName("y");
   Value* z = args++;
   z->setName("z");

   BasicBlock* block = new BasicBlock::BasicBlock("block", func);

   LLVMBuilder builder(block);

   /* tmp = x * y */
   Value* tmp = builder.CreateBinOp(Instruction::Mul, x, y, "tmp");
   /* tmp2 = tmp + z */
   Value* tmp2 = builder.CreateBinOp(Instruction::Add, tmp, z, "tmp2");

   builder.CreateRet(tmp2);

   return func;
}

typedef float (*my_mul_add)(float, float, float);
static float
eval_function(ExecutionEngine *eng, Function *func,
              float a, float b, float c)
{
#if 0
  std::vector<GenericValue> Args(3);
  Args[0].FloatVal = a;
  Args[1].FloatVal = b;
  Args[2].FloatVal = c;
  GenericValue GV = eng->runFunction(func, Args);
#else
  void *gen_func = eng->getPointerToFunction(func);
  fprintf(stderr, "gen_func is %p\n", gen_func);
  my_mul_add myma = reinterpret_cast<my_mul_add>(gen_func);
  return myma(a, b, c);
#endif
}


int
main(int argc, char**argv)
{
  Module *Mod = new Module("test");
  Function *mul_add = make_mul_add(Mod);
  verifyModule(*Mod, PrintMessageAction);

  {
     PassManager PM;
     PM.add(new PrintModulePass(&llvm::cout));
     PM.run(*Mod);
  }

  // Create the JIT.
  ExistingModuleProvider* MP = new ExistingModuleProvider(Mod);
  ExecutionEngine *ExecEng = ExecutionEngine::create(MP, false);

  // run the function
  float result = eval_function(ExecEng, Mod->getFunction("mul_add"), 3.f, 5.f, 4.f);
  std::cout << "Result is " << result << "\n";

  return 0;
}

Annotations for this paste:

Annotation number 1: 2.3 version
Pasted by: sabre
5 days, 10 hours ago
Paste contents:
Raw Source | Display As
#include <assert.h>
#include <llvm/Module.h>
#include <llvm/ModuleProvider.h>
#include <llvm/Function.h>
#include <llvm/PassManager.h>
#include <llvm/CallingConv.h>
#include <llvm/Analysis/Verifier.h>
#include <llvm/Assembly/PrintModulePass.h>
#include <llvm/Support/IRBuilder.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include "llvm/ExecutionEngine/GenericValue.h"
#include <iostream>

using namespace llvm;


static Function *
make_mul_add(Module *mod)
{
   Constant *c = mod->getOrInsertFunction("mul_add",
                                          Type::FloatTy, // ret type
                                          Type::FloatTy, // args
                                          Type::FloatTy,
                                          Type::FloatTy,
                                          NULL /* terminator*/);

   Function *func = cast<Function>(c);

   func->setCallingConv(CallingConv::C);

   Function::arg_iterator args = func->arg_begin();
   Value* x = args++;
   x->setName("x");
   Value* y = args++;
   y->setName("y");
   Value* z = args++;
   z->setName("z");

   BasicBlock* block = BasicBlock::Create("block", func);

   IRBuilder builder(block);

   /* tmp = x * y */
   Value* tmp = builder.CreateBinOp(Instruction::Mul, x, y, "tmp");
   /* tmp2 = tmp + z */
   Value* tmp2 = builder.CreateBinOp(Instruction::Add, tmp, z, "tmp2");

   builder.CreateRet(tmp2);

   return func;
}

typedef float (*my_mul_add)(float, float, float);
static float
eval_function(ExecutionEngine *eng, Function *func,
              float a, float b, float c)
{
#if 0
  std::vector<GenericValue> Args(3);
  Args[0].FloatVal = a;
  Args[1].FloatVal = b;
  Args[2].FloatVal = c;
  GenericValue GV = eng->runFunction(func, Args);
#else
  void *gen_func = eng->getPointerToFunction(func);
  fprintf(stderr, "gen_func is %p\n", gen_func);
  my_mul_add myma = reinterpret_cast<my_mul_add>(gen_func);
  return myma(a, b, c);
#endif
}


int
main(int argc, char**argv)
{
  Module *Mod = new Module("test");
  Function *mul_add = make_mul_add(Mod);
  verifyModule(*Mod, PrintMessageAction);

  {
     PassManager PM;
     PM.add(new PrintModulePass(&llvm::cout));
     PM.run(*Mod);
  }

  // Create the JIT.
  ExistingModuleProvider* MP = new ExistingModuleProvider(Mod);
  ExecutionEngine *ExecEng = ExecutionEngine::create(MP, false);

  // run the function
  float result = eval_function(ExecEng, Mod->getFunction("mul_add"), 3.f, 5.f, 4.f);
  std::cout << "Result is " << result << "\n";

  return 0;
}

Colorize as:
Show Line Numbers
Index of paste annotations: 1

Ads absolutely not by Google

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.