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