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

Last change on this file since 5618 was 5618, checked in by cameron, 21 months ago

Dynamic buffer tracing; calculations for available items/blocks in reverse mode

File size: 7.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 <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                        clEnumVal(TraceDynamicBuffers, "Show dynamic buffer allocations and deallocations."),
32                        clEnumValEnd), cl::cat(CodeGenOptions));
33
34static cl::opt<std::string> IROutputFilenameOption("dump-generated-IR-output", cl::init(""),
35                                                       cl::desc("output IR filename"), cl::cat(CodeGenOptions));
36
37#ifndef USE_LLVM_3_6
38static cl::opt<std::string> ASMOutputFilenameOption("asm-output", cl::init(""),
39                                                    cl::desc("output ASM filename"), cl::cat(CodeGenOptions));
40
41static cl::opt<bool> AsmVerbose("asm-verbose", cl::init(true),
42                                cl::desc("Add comments to directives."), cl::cat(CodeGenOptions));
43#endif
44
45static cl::opt<char> OptLevelOption("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] (default = '-O1')"),
46                                    cl::cat(CodeGenOptions), cl::Prefix, cl::ZeroOrMore, cl::init('1'));
47
48
49static cl::opt<bool, true> EnableObjectCacheOption("enable-object-cache", cl::location(EnableObjectCache), cl::init(true),
50                                                   cl::desc("Enable object caching"), cl::cat(CodeGenOptions));
51
52static cl::opt<std::string> ObjectCacheDirOption("object-cache-dir", cl::init(""),
53                                                 cl::desc("Path to the object cache diretory"), cl::cat(CodeGenOptions));
54
55
56static cl::opt<int, true> BlockSizeOption("BlockSize", cl::location(BlockSize), cl::init(0),
57                                          cl::desc("specify a block size (defaults to widest SIMD register width in bits)."), cl::cat(CodeGenOptions));
58
59
60static cl::opt<int, true> SegmentSizeOption("segment-size", cl::location(SegmentSize), cl::init(1),
61                                            cl::desc("Segment Size"), cl::value_desc("positive integer"));
62
63static cl::opt<int, true> BufferSegmentsOption("buffer-segments", cl::location(BufferSegments), cl::init(1),
64                                               cl::desc("Buffer Segments"), cl::value_desc("positive integer"));
65
66
67static cl::opt<int, true> ThreadNumOption("thread-num", cl::location(ThreadNum), cl::init(2),
68                                          cl::desc("Number of threads used for segment pipeline parallel"), cl::value_desc("positive integer"));
69
70
71static cl::opt<bool, true> EnableAssertsOption("ea", cl::location(EnableAsserts), cl::init(IN_DEBUG_MODE),
72                                               cl::desc("Enable Asserts"), cl::cat(CodeGenOptions));
73
74static cl::opt<bool, true> EnableCycleCountOption("ShowKernelCycles", cl::location(EnableCycleCounter), cl::init(false),
75                                             cl::desc("Count and report CPU cycles per kernel"), cl::cat(CodeGenOptions));
76
77static cl::opt<bool, true> pipelineParallelOption("enable-pipeline-parallel", cl::location(PipelineParallel), cl::init(false),
78                                                  cl::desc("Enable multithreading with pipeline parallelism."), cl::cat(CodeGenOptions));
79   
80static cl::opt<bool, true> segmentPipelineParallelOption("enable-segment-pipeline-parallel", cl::location(SegmentPipelineParallel),
81                                                         cl::desc("Enable multithreading with segment pipeline parallelism."), cl::cat(CodeGenOptions));
82
83static cl::opt<bool, true> NVPTXOption("NVPTX", cl::location(NVPTX), cl::init(false),
84                                 cl::desc("Run on GPU only."), cl::cat(CodeGenOptions));
85
86static cl::opt<int, true> GroupNumOption("group-num", cl::location(GroupNum), cl::init(256),
87                                         cl::desc("NUmber of groups declared on GPU"), cl::value_desc("positive integer"), cl::cat(CodeGenOptions));
88
89CodeGenOpt::Level OptLevel;
90
91bool PipelineParallel;
92
93bool SegmentPipelineParallel;
94
95const char * ASMOutputFilename;
96
97const char * IROutputFilename;
98
99const char * ObjectCacheDir;
100
101int BlockSize;
102
103int SegmentSize;
104
105int BufferSegments;
106
107int ThreadNum;
108
109bool EnableAsserts;
110
111bool EnableCycleCounter;
112
113bool EnableObjectCache;
114
115bool NVPTX = [](const bool nvptx) {
116    #ifndef CUDA_ENABLED
117    if (nvptx) {
118        report_fatal_error("CUDA compiler is not supported.");
119    }
120    #endif
121    return nvptx;
122}(NVPTXOption);
123
124int GroupNum;
125
126const llvm::Reloc::Model RelocModel = ::RelocModel;
127
128const llvm::CodeModel::Model CMModel = ::CMModel;
129
130const std::string MArch = ::MArch;
131
132const std::string RunPass = ::RunPass;
133
134const llvm::TargetMachine::CodeGenFileType FileType = ::FileType;
135
136const std::string StopAfter = ::StopAfter;
137
138const std::string StartAfter = ::StartAfter;
139
140TargetOptions Options;
141
142const cl::OptionCategory * codegen_flags() {
143    return &CodeGenOptions;
144}
145
146bool DebugOptionIsSet(const DebugFlags flag) {
147    return DebugOptions.isSet(flag);
148}
149
150std::string getCPUStr() {
151    return ::getCPUStr();
152}
153
154std::string getFeaturesStr() {
155    return ::getFeaturesStr();
156}
157
158void setFunctionAttributes(llvm::StringRef CPU, llvm::StringRef Features, llvm::Module &M) {
159    return ::setFunctionAttributes(CPU, Features, M);
160}
161
162std::string ProgramName;
163
164void ParseCommandLineOptions(int argc, const char * const *argv, std::initializer_list<const cl::OptionCategory *> hiding) {
165    AddParabixVersionPrinter();
166    codegen::ProgramName = argv[0];
167    #ifndef USE_LLVM_3_6
168    if (hiding.size() != 0) {
169        cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *>(hiding));
170    }
171    #endif
172    cl::ParseCommandLineOptions(argc, argv);
173    if (DebugOptions.getBits()) {
174        EnableObjectCache = false;
175    }
176    ObjectCacheDir = ObjectCacheDirOption.empty() ? nullptr : ObjectCacheDirOption.data();
177    IROutputFilename = IROutputFilenameOption.empty() ? nullptr : IROutputFilenameOption.data();
178    ASMOutputFilename = ASMOutputFilenameOption.empty() ? nullptr : ASMOutputFilenameOption.data();
179    Options = InitTargetOptionsFromCodeGenFlags();
180    #ifndef USE_LLVM_3_6
181    Options.MCOptions.AsmVerbose = AsmVerbose;
182    #endif
183    switch (OptLevelOption) {
184        case '0': OptLevel = CodeGenOpt::None; break;
185        case '1': OptLevel = CodeGenOpt::Less; break;
186        case '2': OptLevel = CodeGenOpt::Default; break;
187        case '3': OptLevel = CodeGenOpt::Aggressive; break;
188        default: report_fatal_error(std::string(1, OptLevelOption) + " is an invalid optimization level.");
189    }
190    #ifndef CUDA_ENABLED
191    if (NVPTX) {
192        report_fatal_error("CUDA compiler is not supported.");
193    }
194    #endif
195}
196
197}
198
199void printParabixVersion () {
200    outs() << "Parabix (http://parabix.costar.sfu.ca/):\n  " << "Parabix revision " << PARABIX_VERSION << "\n";
201}
202
203void AddParabixVersionPrinter() {
204    cl::AddExtraVersionPrinter(&printParabixVersion);
205}
206
207bool AVX2_available() {
208    StringMap<bool> HostCPUFeatures;
209    if (sys::getHostCPUFeatures(HostCPUFeatures)) {
210        auto f = HostCPUFeatures.find("avx2");
211        return ((f != HostCPUFeatures.end()) && f->second);
212    }
213    return false;
214}
Note: See TracBrowser for help on using the repository browser.