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

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

Detect and set CPU features dependent on host machine and OS/VM

File size: 4.5 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
59
60void setAllFeatures(EngineBuilder &builder) {
61    llvm::StringMap<bool> HostCPUFeatures;
62    if (llvm::sys::getHostCPUFeatures(HostCPUFeatures)) {
63        std::vector<std::string> attrs;
64        for (auto &flag : HostCPUFeatures) {
65            auto enabled = flag.second ? "+" : "-";
66            attrs.push_back(enabled + flag.first().str());
67        }
68        builder.setMAttrs(attrs);
69    }
70}
71
72
73
74ExecutionEngine * JIT_to_ExecutionEngine (Module * m) {
75
76    InitializeNativeTarget();
77    InitializeNativeTargetAsmPrinter();
78    InitializeNativeTargetAsmParser();
79
80    PassRegistry * Registry = PassRegistry::getPassRegistry();
81    initializeCore(*Registry);
82    initializeCodeGen(*Registry);
83    initializeLowerIntrinsicsPass(*Registry);
84
85    std::string errMessage;
86    EngineBuilder builder{std::unique_ptr<Module>(m)};
87    builder.setErrorStr(&errMessage);
88    TargetOptions opts = InitTargetOptionsFromCodeGenFlags();
89    builder.setTargetOptions(opts);
90    CodeGenOpt::Level optLevel = CodeGenOpt::Level::None;
91    switch (codegen::OptLevel) {
92        case '0': optLevel = CodeGenOpt::None; break;
93        case '1': optLevel = CodeGenOpt::Less; break;
94        case '2': optLevel = CodeGenOpt::Default; break;
95        case '3': optLevel = CodeGenOpt::Aggressive; break;
96        default: errs() << codegen::OptLevel << " is an invalid optimization level.\n";
97    }
98    builder.setOptLevel(optLevel);
99
100    setAllFeatures(builder);
101
102    if (LLVM_UNLIKELY(codegen::DumpGeneratedIR)) {
103        if (codegen::IROutputFilename.empty()) {
104            m->dump();
105        } else {
106            std::error_code error;
107            llvm::raw_fd_ostream out(codegen::IROutputFilename, error, sys::fs::OpenFlags::F_None);
108            m->print(out, nullptr);
109        }
110    }
111
112    ExecutionEngine * engine = builder.create();
113    if (engine == nullptr) {
114        throw std::runtime_error("Could not create ExecutionEngine: " + errMessage);
115    }   
116    return engine;
117}
118
119void ApplyObjectCache(ExecutionEngine * e) {
120    ICGrepObjectCache * cache = nullptr;
121    if (codegen::EnableObjectCache) {
122        if (codegen::ObjectCacheDir.empty())
123            // Default is $HOME/.cache/icgrep
124            cache = new ICGrepObjectCache();
125        else
126            cache = new ICGrepObjectCache(codegen::ObjectCacheDir);
127        e->setObjectCache(cache);
128    }   
129}
Note: See TracBrowser for help on using the repository browser.