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

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

Add NVPTX Arch to the framework. Fix directory bug in make check.

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