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

Last change on this file since 5732 was 5732, checked in by cameron, 2 years ago

More changes in preparation for LLVM 3.9, 4.0

File size: 7.7 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 <UCD/UCD_Config.h>
9#include <llvm/CodeGen/CommandFlags.h>
10#include <llvm/Support/raw_ostream.h>
11
12using namespace llvm;
13
14#ifndef NDEBUG
15#define IN_DEBUG_MODE true
16#else
17#define IN_DEBUG_MODE false
18#endif
19
20namespace codegen {
21
22static cl::OptionCategory CodeGenOptions("Code Generation Options", "These options control code generation.");
23
24static cl::bits<DebugFlags>
25DebugOptions(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
38static 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
42static cl::opt<std::string> ASMOutputFilenameOption("asm-output", cl::init(""),
43                                                    cl::desc("output ASM filename"), cl::cat(CodeGenOptions));
44
45static cl::opt<bool> AsmVerbose("asm-verbose", cl::init(true),
46                                cl::desc("Add comments to directives."), cl::cat(CodeGenOptions));
47#endif
48
49static 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
53static cl::opt<bool, true> EnableObjectCacheOption("enable-object-cache", cl::location(EnableObjectCache), cl::init(true),
54                                                   cl::desc("Enable object caching"), cl::cat(CodeGenOptions));
55
56static cl::opt<std::string> ObjectCacheDirOption("object-cache-dir", cl::init(""),
57                                                 cl::desc("Path to the object cache diretory"), cl::cat(CodeGenOptions));
58
59
60static 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
64static cl::opt<int, true> SegmentSizeOption("segment-size", cl::location(SegmentSize), cl::init(1),
65                                            cl::desc("Segment Size"), cl::value_desc("positive integer"));
66
67static 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
71static 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
75static 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   
78static 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
81static cl::opt<bool, true> NVPTXOption("NVPTX", cl::location(NVPTX), cl::init(false),
82                                 cl::desc("Run on GPU only."), cl::cat(CodeGenOptions));
83
84static 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
87CodeGenOpt::Level OptLevel;
88
89bool PipelineParallel;
90
91bool SegmentPipelineParallel;
92
93const char * ASMOutputFilename;
94
95const char * IROutputFilename;
96
97const char * ObjectCacheDir;
98
99int BlockSize;
100
101int SegmentSize;
102
103int BufferSegments;
104
105int ThreadNum;
106
107bool EnableObjectCache;
108
109bool 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
118int GroupNum;
119
120const llvm::Reloc::Model RelocModel = ::RelocModel;
121
122const llvm::CodeModel::Model CMModel = ::CMModel;
123
124const std::string MArch = ::MArch;
125
126const llvm::TargetMachine::CodeGenFileType FileType = ::FileType;
127
128TargetOptions Options;
129
130const cl::OptionCategory * codegen_flags() {
131    return &CodeGenOptions;
132}
133
134bool DebugOptionIsSet(const DebugFlags flag) {
135    if (IN_DEBUG_MODE && (flag == EnableAsserts)) return true;
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
151std::string ProgramName;
152
153void 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
188void printParabixVersion () {
189    outs() << "Unicode version " << UCD::UnicodeVersion << "\n";
190    outs() << "Parabix (http://parabix.costar.sfu.ca/):\n  " << "Parabix revision " << PARABIX_VERSION << "\n";
191}
192
193void AddParabixVersionPrinter() {
194    cl::AddExtraVersionPrinter(&printParabixVersion);
195}
196
197bool AVX2_available() {
198    StringMap<bool> HostCPUFeatures;
199    if (sys::getHostCPUFeatures(HostCPUFeatures)) {
200        auto f = HostCPUFeatures.find("avx2");
201        return ((f != HostCPUFeatures.end()) && f->second);
202    }
203    return false;
204}
Note: See TracBrowser for help on using the repository browser.