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

Last change on this file since 5769 was 5759, checked in by cameron, 20 months ago

Small fixes

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