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

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

DumpASM option

File size: 5.1 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 <util/ispc.h>
28
29#include <object_cache.h>
30
31using namespace llvm;
32
33namespace codegen {
34
35static cl::OptionCategory CodeGenOptions("Code Generation Options", "These options control code generation.");
36
37
38static cl::opt<bool> DumpGeneratedIR("dump-generated-IR", cl::init(false), cl::desc("Print LLVM IR generated by Pablo Compiler."), cl::cat(CodeGenOptions));
39static cl::opt<std::string> IROutputFilename("dump-generated-IR-output", cl::init(""), cl::desc("output IR filename"), cl::cat(CodeGenOptions));
40static cl::opt<bool> DumpASM("DumpASM", cl::init(false), cl::desc("Print Assembly Code."), cl::cat(CodeGenOptions));
41
42char OptLevel;
43static cl::opt<char, true> OptLevelOption("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] (default = '-O1')"), cl::location(OptLevel),
44                              cl::cat(CodeGenOptions), cl::Prefix, cl::ZeroOrMore, cl::init('1'));
45
46
47static cl::opt<bool> EnableObjectCache("enable-object-cache", cl::init(false), cl::desc("Enable object caching"), cl::cat(CodeGenOptions));
48
49static cl::opt<std::string> ObjectCacheDir("object-cache-dir", cl::init(""), cl::desc("Path to the object cache diretory"), cl::cat(CodeGenOptions));
50
51
52int BlockSize;
53int SegmentSize;
54
55static 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));
56static cl::opt<int, true> SegmentSizeOption("segment-size", cl::location(SegmentSize), cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
57
58const cl::OptionCategory * codegen_flags() {return &CodeGenOptions;}
59
60}
61
62
63void setAllFeatures(EngineBuilder &builder) {
64    llvm::StringMap<bool> HostCPUFeatures;
65    if (llvm::sys::getHostCPUFeatures(HostCPUFeatures)) {
66        std::vector<std::string> attrs;
67        for (auto &flag : HostCPUFeatures) {
68            auto enabled = flag.second ? "+" : "-";
69            attrs.push_back(enabled + flag.first().str());
70        }
71        builder.setMAttrs(attrs);
72    }
73}
74
75void WriteAssembly (llvm::TargetMachine *TM, Module * m) {
76    llvm::legacy::PassManager PM;
77
78    llvm::SmallString<128> Str;
79    llvm::raw_svector_ostream dest(Str); 
80
81    if (TM->addPassesToEmitFile( PM , dest , llvm::TargetMachine::CGFT_AssemblyFile ) ) {
82      std::cout << "addPassesToEmitFile failed\n";
83      exit(1);
84    }
85    PM.run(*m);
86    std::cerr << std::string( Str.c_str() ) << "\n";
87}
88
89ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
90
91    InitializeNativeTarget();
92    InitializeNativeTargetAsmPrinter();
93    InitializeNativeTargetAsmParser();
94
95    PassRegistry * Registry = PassRegistry::getPassRegistry();
96    initializeCore(*Registry);
97    initializeCodeGen(*Registry);
98    initializeLowerIntrinsicsPass(*Registry);
99
100    std::string errMessage;
101    EngineBuilder builder{std::unique_ptr<Module>(m)};
102    builder.setErrorStr(&errMessage);
103    TargetOptions opts = InitTargetOptionsFromCodeGenFlags();
104    builder.setTargetOptions(opts);
105    CodeGenOpt::Level optLevel = CodeGenOpt::Level::None;
106    switch (codegen::OptLevel) {
107        case '0': optLevel = CodeGenOpt::None; break;
108        case '1': optLevel = CodeGenOpt::Less; break;
109        case '2': optLevel = CodeGenOpt::Default; break;
110        case '3': optLevel = CodeGenOpt::Aggressive; break;
111        default: errs() << codegen::OptLevel << " is an invalid optimization level.\n";
112    }
113    builder.setOptLevel(optLevel);
114
115    setAllFeatures(builder);
116
117    if (LLVM_UNLIKELY(codegen::DumpGeneratedIR)) {
118        if (codegen::IROutputFilename.empty()) {
119            m->dump();
120        } else {
121            std::error_code error;
122            llvm::raw_fd_ostream out(codegen::IROutputFilename, error, sys::fs::OpenFlags::F_None);
123            m->print(out, nullptr);
124        }
125    }
126
127    if (codegen::DumpASM) {
128      WriteAssembly(builder.selectTarget(), m);
129    }
130    ExecutionEngine * engine = builder.create();
131    if (engine == nullptr) {
132        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
133    }   
134    return engine;
135}
136
137void ApplyObjectCache(ExecutionEngine * e) {
138    ICGrepObjectCache * cache = nullptr;
139    if (codegen::EnableObjectCache) {
140        if (codegen::ObjectCacheDir.empty())
141            // Default is $HOME/.cache/icgrep
142            cache = new ICGrepObjectCache();
143        else
144            cache = new ICGrepObjectCache(codegen::ObjectCacheDir);
145        e->setObjectCache(cache);
146    }   
147}
148
149
Note: See TracBrowser for help on using the repository browser.