loopSplitter_test.cpp 2.5 KB
Newer Older
guruhegde's avatar
guruhegde committed
1 2 3 4 5 6 7 8 9 10
#include "catch.hpp"

#include "LoopSplitter.h"
#include "Util.h"
#include "ToolUtil.h"

#include <llvm/AsmParser/Parser.h>
#include <llvm/ADT/PostOrderIterator.h>
#include <llvm/ADT/SCCIterator.h>
#include <llvm/ADT/SmallVector.h>
guruhegde's avatar
guruhegde committed
11
#include <llvm/Analysis/LoopInfo.h>
guruhegde's avatar
guruhegde committed
12 13
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/IR/CFG.h>
guruhegde's avatar
guruhegde committed
14
#include <llvm/IR/Dominators.h>
guruhegde's avatar
guruhegde committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#include <llvm/IR/Function.h>
#include <llvm/IR/Instruction.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/TypeFinder.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/SourceMgr.h>

extern std::string input_dir; // This contains path to input test files

using namespace llvm;
using namespace std;
using namespace tas;

static LLVMContext C;
static SMDiagnostic Err; 

static unique_ptr<Module> parseIR(string Filename, string FileDir) {
  std::unique_ptr<Module> M (parseIRFile(FileDir + Filename,  Err, C));
  if (!M)
    Err.print("Error parsing IR: ", errs());
  return M;
}

TEST_CASE("fn with single loop") {
  std::string filePrefix = "loopsplitter_test1";
guruhegde's avatar
guruhegde committed
43
  std::string functionName = "fn";
guruhegde's avatar
guruhegde committed
44

guruhegde's avatar
guruhegde committed
45 46 47 48 49 50 51 52
  auto IR = generateIR(filePrefix + string(".c"), input_dir);

  /*
  vector<string> OptList {"indvars", "loop-simplify", "lcssa"};
  IR= runOpt(IR, input_dir, OptList);
  */

  auto M = parseIR(IR, input_dir);
guruhegde's avatar
guruhegde committed
53 54 55 56
  REQUIRE(M != nullptr);

  // Function with 2 arguments and int return type.
  auto F = M->getFunction(functionName);
guruhegde's avatar
guruhegde committed
57 58 59 60 61 62

  DominatorTree DT(*F);
  LoopInfo LI(DT);

  LoopSplitter LS(F, &LI);
  LS.run();
guruhegde's avatar
guruhegde committed
63

guruhegde's avatar
guruhegde committed
64 65
  REQUIRE(LS.getStats().AnnotatedVarsSize == 1);
  REQUIRE(LS.getStats().VarUsePointsSize == 1);
guruhegde's avatar
guruhegde committed
66 67 68 69 70 71 72 73 74 75 76

  //F->print(errs());
  auto asmFile = writeToAsmFile(*M);
  auto TestObject = generateObject(asmFile);

  auto binary = linkObjects(vector<string>{TestObject}, string("loopSplitter_test11"));

  // Run the binary
  binary.insert(0, "./");
  auto ret = system(binary.c_str());
  REQUIRE(ret == 0);
guruhegde's avatar
guruhegde committed
77
}
guruhegde's avatar
guruhegde committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

TEST_CASE("fast_flows_packet loop split") {
  std::string filePrefix = "fast_flows";
  auto M = parseIR(generateIR(filePrefix + string(".c"), input_dir, true), input_dir);
  REQUIRE(M != nullptr);
  M->setSourceFileName(filePrefix + string("_batch.ll"));
  {
    std::string functionName = "fast_flows_packet";

    auto F = M->getFunction(functionName);
    DominatorTree DT(*F);
    LoopInfo LI(DT);

    LoopSplitter LS(F, &LI);
    LS.run();

    //F->print(errs());
    auto asmFile = writeToAsmFile(*M);
    auto TestObject = generateObject(asmFile);
  }
}