source: icGREP/icgrep-devel/icgrep/toolchain.cpp @ 5037

Last change on this file since 5037 was 5037, checked in by nmedfort, 3 years ago

UnicodeSet? bug fix and compile warning clean-up.

File size: 4.3 KB
Line 
1/*
2 *  Copyright (c) 2016 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#include <sstream>
11
12#include <toolchain.h>
13#include <llvm/IR/Function.h>
14#include <llvm/IR/Module.h>
15#include <llvm/ExecutionEngine/ExecutionEngine.h>
16#include <llvm/ExecutionEngine/MCJIT.h>
17#include <llvm/IRReader/IRReader.h>
18#include <llvm/Support/CommandLine.h>
19#include <llvm/CodeGen/CommandFlags.h>
20#include <llvm/Support/SourceMgr.h>
21#include <llvm/Support/TargetSelect.h>
22#include <llvm/Support/Host.h>
23#include <llvm/Support/raw_ostream.h>
24
25#include <util/ispc.h>
26
27#include <object_cache.h>
28
29using namespace llvm;
30
31namespace codegen {
32
33static cl::OptionCategory CodeGenOptions("Code Generation Options", "These options control code generation.");
34
35
36static cl::opt<bool> DumpGeneratedIR("dump-generated-IR", cl::init(false), cl::desc("Print LLVM IR generated by Pablo Compiler."), cl::cat(CodeGenOptions));
37static cl::opt<std::string> IROutputFilename("dump-generated-IR-output", cl::init(""), cl::desc("output IR filename"), cl::cat(CodeGenOptions));
38
39char OptLevel;
40static cl::opt<char, true> OptLevelOption("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] (default = '-O1')"), cl::location(OptLevel),
41                              cl::cat(CodeGenOptions), cl::Prefix, cl::ZeroOrMore, cl::init('1'));
42
43
44static cl::opt<bool> EnableObjectCache("enable-object-cache", cl::init(false), cl::desc("Enable object caching"), cl::cat(CodeGenOptions));
45
46static cl::opt<std::string> ObjectCacheDir("object-cache-dir", cl::init(""), cl::desc("Path to the object cache diretory"), cl::cat(CodeGenOptions));
47
48
49int BlockSize;
50int SegmentSize;
51
52static cl::opt<int, true> BlockSizeOption("BlockSize", cl::location(BlockSize), cl::init(0), cl::desc("specify a block size (defaults to widest SIMD register width in bits)."), cl::cat(CodeGenOptions));
53static cl::opt<int, true> SegmentSizeOption("segment-size", cl::location(SegmentSize), cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
54
55const cl::OptionCategory * codegen_flags() {return &CodeGenOptions;}
56
57}
58
59ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
60
61    InitializeNativeTarget();
62    InitializeNativeTargetAsmPrinter();
63    InitializeNativeTargetAsmParser();
64
65    PassRegistry * Registry = PassRegistry::getPassRegistry();
66    initializeCore(*Registry);
67    initializeCodeGen(*Registry);
68    initializeLowerIntrinsicsPass(*Registry);
69
70    std::string errMessage;
71    EngineBuilder builder{std::unique_ptr<Module>(m)};
72    builder.setErrorStr(&errMessage);
73    builder.setMCPU(sys::getHostCPUName());
74    TargetOptions opts = InitTargetOptionsFromCodeGenFlags();
75    builder.setTargetOptions(opts);
76    CodeGenOpt::Level optLevel = CodeGenOpt::Level::None;
77    switch (codegen::OptLevel) {
78        case '0': optLevel = CodeGenOpt::None; break;
79        case '1': optLevel = CodeGenOpt::Less; break;
80        case '2': optLevel = CodeGenOpt::Default; break;
81        case '3': optLevel = CodeGenOpt::Aggressive; break;
82        default: errs() << codegen::OptLevel << " is an invalid optimization level.\n";
83    }
84    builder.setOptLevel(optLevel);
85
86    if ((strncmp(lGetSystemISA(), "avx2", 4) == 0)) {
87        std::vector<std::string> attrs;
88        attrs.push_back("avx2");
89        builder.setMAttrs(attrs);
90    }
91    // builder.selectTarget();
92
93    if (LLVM_UNLIKELY(codegen::DumpGeneratedIR)) {
94        if (codegen::IROutputFilename.empty()) {
95            m->dump();
96        } else {
97            std::error_code error;
98            llvm::raw_fd_ostream out(codegen::IROutputFilename, error, sys::fs::OpenFlags::F_None);
99            m->print(out, nullptr);
100        }
101    }
102
103    ExecutionEngine * engine = builder.create();
104    if (engine == nullptr) {
105        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
106    }   
107    return engine;
108}
109
110void ApplyObjectCache(ExecutionEngine * e) {
111    ICGrepObjectCache * cache = nullptr;
112    if (codegen::EnableObjectCache) {
113        if (codegen::ObjectCacheDir.empty())
114            // Default is $HOME/.cache/icgrep
115            cache = new ICGrepObjectCache();
116        else
117            cache = new ICGrepObjectCache(codegen::ObjectCacheDir);
118        e->setObjectCache(cache);
119    }   
120}
Note: See TracBrowser for help on using the repository browser.