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

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

Initial support for -q, -l, -L modes, grep options reorganization

File size: 7.0 KB
RevLine 
[4801]1/*
[5033]2 *  Copyright (c) 2016 International Characters.
[4801]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
[5464]7#include <toolchain/toolchain.h>
8#include <llvm/CodeGen/CommandFlags.h>
9#include <llvm/Support/raw_ostream.h>
[5398]10
[5033]11using namespace llvm;
[4939]12
[5446]13#ifndef NDEBUG
14#define IN_DEBUG_MODE true
15#else
16#define IN_DEBUG_MODE false
17#endif
18
[5033]19namespace codegen {
[4939]20
[5033]21static cl::OptionCategory CodeGenOptions("Code Generation Options", "These options control code generation.");
[5347]22
[5295]23static cl::bits<DebugFlags>
[5454]24DebugOptions(cl::values(clEnumVal(ShowUnoptimizedIR, "Print generated LLVM IR."),
25                        clEnumVal(ShowIR, "Print optimized LLVM IR."),
[5444]26                        clEnumVal(VerifyIR, "Run the IR verification pass."),
[5401]27#ifndef USE_LLVM_3_6
[5295]28                        clEnumVal(ShowASM, "Print assembly code."),
29#endif
30                        clEnumVal(SerializeThreads, "Force segment threads to run sequentially."),
31                        clEnumValEnd), cl::cat(CodeGenOptions));
[4939]32
[5464]33static cl::opt<std::string> IROutputFilenameOption("dump-generated-IR-output", cl::init(""),
34                                                       cl::desc("output IR filename"), cl::cat(CodeGenOptions));
35
[5401]36#ifndef USE_LLVM_3_6
[5464]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));
[5295]42#endif
[4939]43
[5464]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'));
[4801]46
[4959]47
[5464]48static cl::opt<bool> EnableObjectCacheOption("enable-object-cache",
49                                             cl::init(true), cl::desc("Enable object caching"), cl::cat(CodeGenOptions));
[4959]50
[5464]51static cl::opt<std::string> ObjectCacheDirOption("object-cache-dir",
52                                                 cl::init(""), cl::desc("Path to the object cache diretory"), cl::cat(CodeGenOptions));
[4959]53
[4801]54
[5464]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));
[4962]57
58
[5464]59static cl::opt<int, true> SegmentSizeOption("segment-size", cl::location(SegmentSize),
60                                            cl::desc("Segment Size"), cl::value_desc("positive integer"), cl::init(1));
[4962]61
[5464]62static cl::opt<int, true> BufferSegmentsOption("buffer-segments", cl::location(BufferSegments), cl::init(1),
63                                               cl::desc("Buffer Segments"), cl::value_desc("positive integer"));
[5295]64
[5151]65
[5464]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"));
[5458]68
[4801]69
[5464]70static cl::opt<bool, true> EnableAssertsOption("ea", cl::location(EnableAsserts), cl::init(IN_DEBUG_MODE),
71                                               cl::desc("Enable Asserts"));
[5458]72
[5464]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));
[5151]75
[5464]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));
[5373]81
[5464]82static cl::opt<bool> USENVPTX("NVPTX", cl::init(false),
83                              cl::desc("Run on GPU only."));
[5067]84
[5464]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"));
[5108]87
[5425]88
[5464]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;
[5473]95        default: report_fatal_error(std::string(1,optLevel) + " is an invalid optimization level.");
[5391]96    }
[5464]97}(OptLevelOption);
[5425]98
[5464]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;
[5425]113
[5464]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
[5398]130
[5464]131const cl::OptionCategory * codegen_flags() {
132    return &CodeGenOptions;
[5409]133}
134
[5464]135bool DebugOptionIsSet(const DebugFlags flag) {
136    return DebugOptions.isSet(flag);
[5409]137}
138
[5464]139std::string getCPUStr() {
140    return ::getCPUStr();
[5414]141}
[5409]142
[5464]143std::string getFeaturesStr() {
144    return ::getFeaturesStr();
[5391]145}
146
[5464]147void setFunctionAttributes(llvm::StringRef CPU, llvm::StringRef Features, llvm::Module &M) {
148    return ::setFunctionAttributes(CPU, Features, M);
[5414]149}
150
[5391]151
[5398]152}
153
[5464]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
[5454]160    }
161}
162
[5464]163void printParabixVersion () {
164    raw_ostream &OS = outs();
165    OS << "Parabix (http://parabix.costar.sfu.ca/):\n  " << "Parabix revision " << PARABIX_VERSION << "\n";
[5436]166}
167
[5464]168void AddParabixVersionPrinter() {
169    cl::AddExtraVersionPrinter(&printParabixVersion);
[5391]170}
[5402]171
[5464]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;
[5402]179}
Note: See TracBrowser for help on using the repository browser.