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

Last change on this file since 5464 was 5464, checked in by nmedfort, 2 years ago

Restructuring work for the Driver classes. Start of work to eliminate the memory leaks with the ExecutionEngine?. Replaced custom AlignedMalloc? with backend call to std::aligned_malloc. Salvaged some work on DistributionPass? for reevaluation.

File size: 7.0 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 <toolchain/toolchain.h>
8#include <llvm/CodeGen/CommandFlags.h>
9#include <llvm/Support/raw_ostream.h>
10
11using namespace llvm;
12
13#ifndef NDEBUG
14#define IN_DEBUG_MODE true
15#else
16#define IN_DEBUG_MODE false
17#endif
18
19namespace codegen {
20
21static cl::OptionCategory CodeGenOptions("Code Generation Options", "These options control code generation.");
22
23static cl::bits<DebugFlags>
24DebugOptions(cl::values(clEnumVal(ShowUnoptimizedIR, "Print generated LLVM IR."),
25                        clEnumVal(ShowIR, "Print optimized LLVM IR."),
26                        clEnumVal(VerifyIR, "Run the IR verification pass."),
27#ifndef USE_LLVM_3_6
28                        clEnumVal(ShowASM, "Print assembly code."),
29#endif
30                        clEnumVal(SerializeThreads, "Force segment threads to run sequentially."),
31                        clEnumValEnd), cl::cat(CodeGenOptions));
32
33static cl::opt<std::string> IROutputFilenameOption("dump-generated-IR-output", cl::init(""),
34                                                       cl::desc("output IR filename"), cl::cat(CodeGenOptions));
35
36#ifndef USE_LLVM_3_6
37static cl::opt<std::string> ASMOutputFilenameOption("asm-output", cl::init(""),
38                                                    cl::desc("output ASM filename"), cl::cat(CodeGenOptions));
39
40static cl::opt<bool> AsmVerbose("asm-verbose", cl::init(true),
41                                cl::desc("Add comments to directives."), cl::cat(CodeGenOptions));
42#endif
43
44static cl::opt<char> OptLevelOption("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] (default = '-O1')"),
45                                    cl::cat(CodeGenOptions), cl::Prefix, cl::ZeroOrMore, cl::init('1'));
46
47
48static cl::opt<bool> EnableObjectCacheOption("enable-object-cache",
49                                             cl::init(true), cl::desc("Enable object caching"), cl::cat(CodeGenOptions));
50
51static cl::opt<std::string> ObjectCacheDirOption("object-cache-dir",
52                                                 cl::init(""), cl::desc("Path to the object cache diretory"), cl::cat(CodeGenOptions));
53
54
55static cl::opt<int, true> BlockSizeOption("BlockSize", cl::location(BlockSize), cl::init(0),
56                                          cl::desc("specify a block size (defaults to widest SIMD register width in bits)."), cl::cat(CodeGenOptions));
57
58
59static cl::opt<int, true> SegmentSizeOption("segment-size", cl::location(SegmentSize),
60                                            cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
61
62static cl::opt<int, true> BufferSegmentsOption("buffer-segments", cl::location(BufferSegments), cl::init(1),
63                                               cl::desc("Buffer Segments"), cl::value_desc("positive integer"));
64
65
66static cl::opt<int, true> ThreadNumOption("thread-num", cl::location(ThreadNum), cl::init(2),
67                                          cl::desc("Number of threads used for segment pipeline parallel"), cl::value_desc("positive integer"));
68
69
70static cl::opt<bool, true> EnableAssertsOption("ea", cl::location(EnableAsserts), cl::init(IN_DEBUG_MODE),
71                                               cl::desc("Enable Asserts"));
72
73static cl::opt<bool, true> EnableCycleCountOption("ShowKernelCycles", cl::location(EnableCycleCounter), cl::init(false),
74                                                  cl::desc("Count and report CPU cycles per kernel"), cl::cat(CodeGenOptions));
75
76static cl::opt<bool, true> pipelineParallelOption("enable-pipeline-parallel", cl::location(pipelineParallel),
77                                                  cl::desc("Enable multithreading with pipeline parallelism."), cl::cat(CodeGenOptions));
78   
79static cl::opt<bool, true> segmentPipelineParallelOption("enable-segment-pipeline-parallel", cl::location(segmentPipelineParallel),
80                                                         cl::desc("Enable multithreading with segment pipeline parallelism."), cl::cat(CodeGenOptions));
81
82static cl::opt<bool> USENVPTX("NVPTX", cl::init(false),
83                              cl::desc("Run on GPU only."));
84
85static cl::opt<int, true> GroupNumOption("group-num", cl::location(GroupNum), cl::init(256),
86                                         cl::desc("NUmber of groups declared on GPU"), cl::value_desc("positive integer"));
87
88
89const CodeGenOpt::Level OptLevel = [](const char optLevel) {
90    switch (optLevel) {
91        case '0': return CodeGenOpt::None;
92        case '1': return CodeGenOpt::Less;
93        case '2': return CodeGenOpt::Default;
94        case '3': return CodeGenOpt::Aggressive;
95        default: report_fatal_error(optLevel + " is an invalid optimization level.");
96    }
97}(OptLevelOption);
98
99bool pipelineParallel;
100bool segmentPipelineParallel;
101const std::string ASMOutputFilename = ASMOutputFilenameOption;
102const std::string IROutputFilename = IROutputFilenameOption;
103const std::string ObjectCacheDir = ObjectCacheDirOption;
104int BlockSize;
105int SegmentSize;
106int BufferSegments;
107int ThreadNum;
108bool EnableAsserts;
109bool EnableCycleCounter;
110const bool EnableObjectCache = EnableObjectCacheOption && (DebugOptions.getBits() == 0);
111bool NVPTX;
112int GroupNum;
113
114const llvm::Reloc::Model RelocModel = ::RelocModel;
115const llvm::CodeModel::Model CMModel = ::CMModel;
116const std::string MArch = ::MArch;
117const std::string RunPass = ::RunPass;
118const llvm::TargetMachine::CodeGenFileType FileType = ::FileType;
119const std::string StopAfter = ::StopAfter;
120const std::string StartAfter = ::StartAfter;
121#ifndef USE_LLVM_3_6
122const TargetOptions Options = [](const bool asmVerbose) {
123    TargetOptions opt = InitTargetOptionsFromCodeGenFlags();
124    opt.MCOptions.AsmVerbose = AsmVerbose;
125    return opt;
126}(AsmVerbose);
127#else
128const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
129#endif
130
131const cl::OptionCategory * codegen_flags() {
132    return &CodeGenOptions;
133}
134
135bool DebugOptionIsSet(const DebugFlags flag) {
136    return DebugOptions.isSet(flag);
137}
138
139std::string getCPUStr() {
140    return ::getCPUStr();
141}
142
143std::string getFeaturesStr() {
144    return ::getFeaturesStr();
145}
146
147void setFunctionAttributes(llvm::StringRef CPU, llvm::StringRef Features, llvm::Module &M) {
148    return ::setFunctionAttributes(CPU, Features, M);
149}
150
151
152}
153
154void setNVPTXOption() {
155    codegen::NVPTX = codegen::USENVPTX; 
156    if (codegen::NVPTX) {
157        #ifndef CUDA_ENABLED
158        report_fatal_error("CUDA compiler is not supported.");
159        #endif
160    }
161}
162
163void printParabixVersion () {
164    raw_ostream &OS = outs();
165    OS << "Parabix (http://parabix.costar.sfu.ca/):\n  " << "Parabix revision " << PARABIX_VERSION << "\n";
166}
167
168void AddParabixVersionPrinter() {
169    cl::AddExtraVersionPrinter(&printParabixVersion);
170}
171
172bool AVX2_available() {
173    StringMap<bool> HostCPUFeatures;
174    if (sys::getHostCPUFeatures(HostCPUFeatures)) {
175        auto f = HostCPUFeatures.find("avx2");
176        return ((f != HostCPUFeatures.end()) && f->second);
177    }
178    return false;
179}
Note: See TracBrowser for help on using the repository browser.