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

Last change on this file since 5165 was 5165, checked in by lindanl, 3 years ago

Add segment pipeline parallel strategy. Move ballot function to IDISA NVPTX.

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