source: icGREP/icgrep-devel/icgrep/symboltable.cpp @ 5234

Last change on this file since 5234 was 5176, checked in by cameron, 3 years ago

Engine deletion causes segfaults with LLVM 3.9 changes

File size: 2.5 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <string>
8#include <iostream>
9#include <fstream>
10
11#include "toolchain.h"
12#include "utf_encoding.h"
13#include "pablo/pablo_compiler.h"
14#include <llvm/IR/Function.h>
15#include <llvm/IR/Type.h>
16#include <llvm/IR/Module.h>
17#include <llvm/ExecutionEngine/ExecutionEngine.h>
18#include <llvm/ExecutionEngine/MCJIT.h>
19#include <llvm/IRReader/IRReader.h>
20#include <llvm/Support/SourceMgr.h>
21#include <llvm/Support/CommandLine.h>
22#include <llvm/Support/Debug.h>
23#include <llvm/Support/TargetSelect.h>
24#include <llvm/Support/Host.h>
25#include <llvm/IR/Verifier.h>
26#include <IDISA/idisa_target.h>
27#include <kernels/symboltablepipeline.h>
28
29// mmap system
30#include <boost/filesystem.hpp>
31#include <boost/iostreams/device/mapped_file.hpp>
32
33typedef void (*SymTableFunctionType)(char * byte_data, size_t filesize);
34
35static cl::list<std::string> files(cl::Positional, cl::desc("<input file ...>"), cl::ZeroOrMore);
36
37void process(const std::string & fileName, SymTableFunctionType function) {
38    const boost::filesystem::path filePath(fileName);
39    if (exists(filePath) && !is_directory(filePath)) {
40        size_t fileSize = file_size(filePath);
41        if (fileSize > 0) {
42            boost::iostreams::mapped_file file;
43            file.open(fileName, boost::iostreams::mapped_file::priv, fileSize, 0);
44            char * data = file.data();
45            assert (data);
46            function(data, fileSize);
47            file.close();
48        }
49    }
50}
51
52
53int main(int argc, char *argv[]) {
54
55    LLVMContext TheContext;
56
57    Module * M = new Module("symboltable", TheContext);
58
59    IDISA::IDISA_Builder * idb = IDISA::GetIDISA_Builder(M);
60
61    kernel::SymbolTableBuilder pipelineBuilder(M, idb);
62
63    pipelineBuilder.createKernels();
64
65    llvm::Function * main_IR = pipelineBuilder.ExecuteKernels();
66    if (LLVM_UNLIKELY(main_IR == nullptr)) {
67        throw std::runtime_error("No LLVM function created!");
68    }
69    llvm::ExecutionEngine * engine = JIT_to_ExecutionEngine(M);
70    verifyModule(*M, &dbgs());
71    engine->finalizeObject();
72
73    SymTableFunctionType function = reinterpret_cast<SymTableFunctionType>(engine->getPointerToFunction(main_IR));
74    if (LLVM_UNLIKELY(function == nullptr)) {
75        throw std::runtime_error("filed to compile LLVM function!");
76    }
77
78    for (std::string file : files) {
79        process(file, function);
80    }
81
82    delete engine;
83    delete idb;
84    return 0;
85}
Note: See TracBrowser for help on using the repository browser.