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

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

IR_Gen subdirectory for all IR generation utility functions

File size: 7.4 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/IR/LegacyPassManager.h"
18
19#include <llvm/IRReader/IRReader.h>
20#include <llvm/Support/CommandLine.h>
21#include <llvm/CodeGen/CommandFlags.h>
22#include <llvm/Support/SourceMgr.h>
23#include <llvm/Support/TargetSelect.h>
24#include <llvm/Support/Host.h>
25#include <llvm/Support/raw_ostream.h>
26
27#include <object_cache.h>
28
29#ifdef CUDA_ENABLED
30#include <IR_Gen/llvm2ptx.h>
31#endif
32 
33using namespace llvm;
34
35namespace codegen {
36
37static cl::OptionCategory CodeGenOptions("Code Generation Options", "These options control code generation.");
38
39
40static cl::opt<bool> DumpGeneratedIR("dump-generated-IR", cl::init(false), cl::desc("Print LLVM IR generated by Pablo Compiler."), cl::cat(CodeGenOptions));
41static cl::opt<std::string> IROutputFilename("dump-generated-IR-output", cl::init(""), cl::desc("output IR filename"), cl::cat(CodeGenOptions));
42#if LLVM_VERSION_MINOR > 6
43static cl::opt<bool> DumpASM("DumpASM", cl::init(false), cl::desc("Print Assembly Code."), cl::cat(CodeGenOptions));
44#endif
45static cl::opt<std::string> ASMOutputFilename("asm-output", cl::init(""), cl::desc("output ASM filename"), cl::cat(CodeGenOptions));
46static cl::opt<bool> AsmVerbose("asm-verbose",
47                                cl::desc("Add comments to directives."),
48                                cl::init(true), cl::cat(CodeGenOptions));
49
50char OptLevel;
51static cl::opt<char, true> OptLevelOption("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] (default = '-O1')"), cl::location(OptLevel),
52                              cl::cat(CodeGenOptions), cl::Prefix, cl::ZeroOrMore, cl::init('1'));
53
54
55static cl::opt<bool> EnableObjectCache("enable-object-cache", cl::init(false), cl::desc("Enable object caching"), cl::cat(CodeGenOptions));
56
57static cl::opt<std::string> ObjectCacheDir("object-cache-dir", cl::init(""), cl::desc("Path to the object cache diretory"), cl::cat(CodeGenOptions));
58
59
60int BlockSize;
61int SegmentSize;
62int BufferSegments;
63int ThreadNum;
64
65static 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));
66static cl::opt<int, true> SegmentSizeOption("segment-size", cl::location(SegmentSize), cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
67static cl::opt<int, true> BufferSegmentsOption("buffer-segments", cl::location(BufferSegments), cl::desc("Buffer Segments"), cl::value_desc("positive integer"), cl::init(1));
68static cl::opt<int, true> ThreadNumOption("thread-num", cl::location(ThreadNum), cl::desc("Number of threads used for segment pipeline parallel"), cl::value_desc("positive integer"), cl::init(2));
69
70const cl::OptionCategory * codegen_flags() {return &CodeGenOptions;}
71
72#ifdef CUDA_ENABLED
73bool NVPTX;
74static cl::opt<bool> USENVPTX("NVPTX", cl::desc("Run on GPU only."), cl::init(false));
75#endif
76
77}
78
79
80#ifdef CUDA_ENABLED
81void setNVPTXOption(){
82    codegen::NVPTX = codegen::USENVPTX;
83}
84
85void Compile2PTX (Module * m, std::string IRFilename, std::string PTXFilename) {
86    InitializeAllTargets();
87    InitializeAllTargetMCs();
88    InitializeAllAsmPrinters();
89    InitializeAllAsmParsers();
90
91    PassRegistry *Registry = PassRegistry::getPassRegistry();
92    initializeCore(*Registry);
93    initializeCodeGen(*Registry);
94    initializeLoopStrengthReducePass(*Registry);
95    initializeLowerIntrinsicsPass(*Registry);
96    initializeUnreachableBlockElimPass(*Registry);
97
98    std::error_code error;
99    llvm::raw_fd_ostream out(IRFilename, error, sys::fs::OpenFlags::F_None);
100    m->print(out, nullptr);
101
102    if (LLVM_UNLIKELY(codegen::DumpGeneratedIR))
103            m->dump();
104
105    llvm2ptx(IRFilename, PTXFilename);
106}
107#endif
108
109
110void setAllFeatures(EngineBuilder &builder) {
111    llvm::StringMap<bool> HostCPUFeatures;
112    if (llvm::sys::getHostCPUFeatures(HostCPUFeatures)) {
113        std::vector<std::string> attrs;
114        for (auto &flag : HostCPUFeatures) {
115            auto enabled = flag.second ? "+" : "-";
116            attrs.push_back(enabled + flag.first().str());
117        }
118        builder.setMAttrs(attrs);
119    }
120}
121
122bool AVX2_available() {
123    llvm::StringMap<bool> HostCPUFeatures;
124    if (llvm::sys::getHostCPUFeatures(HostCPUFeatures)) {
125        auto f = HostCPUFeatures.find("avx2");
126        return ((f != HostCPUFeatures.end()) && f->second);
127    }
128    return false;
129}
130
131#ifndef USE_LLVM_3_6
132void WriteAssembly (llvm::TargetMachine *TM, Module * m) {
133    llvm::legacy::PassManager PM;
134
135    llvm::SmallString<128> Str;
136    llvm::raw_svector_ostream dest(Str);
137
138    if (TM->addPassesToEmitFile(PM, dest, llvm::TargetMachine::CGFT_AssemblyFile ) ) {
139        throw std::runtime_error("LLVM error: addPassesToEmitFile failed.");
140    }
141    PM.run(*m);
142
143    if (codegen::ASMOutputFilename.empty()) {
144        errs() << Str;
145    } else {
146        std::error_code error;
147        llvm::raw_fd_ostream out(codegen::ASMOutputFilename, error, sys::fs::OpenFlags::F_None);
148        out << Str;
149    }
150}
151#endif
152
153ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
154
155    InitializeNativeTarget();
156    InitializeNativeTargetAsmPrinter();
157    InitializeNativeTargetAsmParser();
158
159    PassRegistry * Registry = PassRegistry::getPassRegistry();
160    initializeCore(*Registry);
161    initializeCodeGen(*Registry);
162    initializeLowerIntrinsicsPass(*Registry);
163
164    std::string errMessage;
165    EngineBuilder builder{std::unique_ptr<Module>(m)};
166    builder.setErrorStr(&errMessage);
167    TargetOptions opts = InitTargetOptionsFromCodeGenFlags();
168    opts.MCOptions.AsmVerbose = codegen::AsmVerbose;
169
170    builder.setTargetOptions(opts);
171    builder.setVerifyModules(true);
172    CodeGenOpt::Level optLevel = CodeGenOpt::Level::None;
173    switch (codegen::OptLevel) {
174        case '0': optLevel = CodeGenOpt::None; break;
175        case '1': optLevel = CodeGenOpt::Less; break;
176        case '2': optLevel = CodeGenOpt::Default; break;
177        case '3': optLevel = CodeGenOpt::Aggressive; break;
178        default: errs() << codegen::OptLevel << " is an invalid optimization level.\n";
179    }
180    builder.setOptLevel(optLevel);
181
182    setAllFeatures(builder);
183
184    if (LLVM_UNLIKELY(codegen::DumpGeneratedIR)) {
185        if (codegen::IROutputFilename.empty()) {
186            m->dump();
187        } else {
188            std::error_code error;
189            llvm::raw_fd_ostream out(codegen::IROutputFilename, error, sys::fs::OpenFlags::F_None);
190            m->print(out, nullptr);
191        }
192    }
193#if LLVM_VERSION_MINOR > 6
194
195    if (codegen::DumpASM) {
196      WriteAssembly(builder.selectTarget(), m);
197    }
198#endif
199    ExecutionEngine * engine = builder.create();
200    if (engine == nullptr) {
201        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
202    }   
203    return engine;
204}
205
206void ApplyObjectCache(ExecutionEngine * e) {
207    ICGrepObjectCache * cache = nullptr;
208    if (codegen::EnableObjectCache) {
209        if (codegen::ObjectCacheDir.empty())
210            // Default is $HOME/.cache/icgrep
211            cache = new ICGrepObjectCache();
212        else
213            cache = new ICGrepObjectCache(codegen::ObjectCacheDir);
214        e->setObjectCache(cache);
215    }   
216}
217
218
Note: See TracBrowser for help on using the repository browser.