source: icGREP/icgrep-devel/icgrep/pablo/pablo_toolchain.cpp @ 5792

Last change on this file since 5792 was 5776, checked in by cameron, 18 months ago

ShowPablo?=filename option

File size: 5.2 KB
Line 
1/*
2 *  Copyright (c) 2015 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 "pablo_toolchain.h"
9#include <pablo/pablo_kernel.h>
10#include <pablo/optimizers/pablo_simplifier.hpp>
11#include <pablo/optimizers/codemotionpass.h>
12#include <pablo/optimizers/distributivepass.h>
13#include <pablo/optimizers/schedulingprepass.h>
14#include <pablo/passes/flattenif.hpp>
15#include <pablo/analysis/pabloverifier.hpp>
16#include <pablo/printer_pablos.h>
17#include <llvm/Support/CommandLine.h>
18#include <llvm/Support/FileSystem.h>
19#include <llvm/Support/raw_ostream.h>
20
21using namespace llvm;
22
23namespace pablo {
24
25static cl::OptionCategory PabloOptions("Pablo Options", "These options control printing, generation and instrumentation of Pablo intermediate code.");
26
27const cl::OptionCategory * pablo_toolchain_flags() {
28    return &PabloOptions;
29}
30   
31   
32static cl::bits<PabloDebugFlags> 
33DebugOptions(cl::values(clEnumVal(VerifyPablo, "Run the Pablo verifier"),
34                        clEnumVal(DumpTrace, "Generate dynamic traces of executed Pablo assignments.")
35                        CL_ENUM_VAL_SENTINEL), cl::cat(PabloOptions));
36   
37std::string ShowPabloOption = codegen::OmittedOption;
38static cl::opt<std::string, true> PabloOutputOption("ShowPablo", cl::location(ShowPabloOption), cl::ValueOptional,
39                                                    cl::desc("Print generated Pablo code to stderr (by omitting =<filename>) or a file"), cl::value_desc("filename"), cl::cat(PabloOptions));
40sys::fs::OpenFlags PabloOutputFileFlag = sys::fs::OpenFlags::F_None;
41   
42std::string ShowOptimizedPabloOption = codegen::OmittedOption;
43static cl::opt<std::string, true> OptimizedPabloOutputOption("ShowOptimizedPablo", cl::location(ShowOptimizedPabloOption), cl::ValueOptional,
44                                                    cl::desc("Print optimized Pablo code to stderr (by omitting =<filename>) or a file"), cl::value_desc("filename"), cl::cat(PabloOptions));
45sys::fs::OpenFlags PabloOptimizedOutputFileFlag = sys::fs::OpenFlags::F_None;
46
47static cl::opt<bool> Flatten("flatten-if", cl::init(false), cl::desc("Flatten all the Ifs in the Pablo AST"), cl::cat(PabloOptions));
48
49static cl::bits<PabloCompilationFlags> 
50    PabloOptimizationsOptions(cl::values(clEnumVal(DisableSimplification, "Disable Pablo Simplification pass (not recommended)"),
51                                         clEnumVal(DisableCodeMotion, "Moves statements into the innermost legal If-scope and moves invariants out of While-loops."),
52                                         clEnumVal(EnableDistribution, "Apply distribution law optimization."),                                         
53                                         clEnumVal(EnableSchedulingPrePass, "Pablo Statement Scheduling Pre-Pass"),
54                                         clEnumVal(EnableProfiling, "Profile branch statistics.")
55                                         CL_ENUM_VAL_SENTINEL), cl::cat(PabloOptions));
56
57bool DebugOptionIsSet(const PabloDebugFlags flag) {return DebugOptions.isSet(flag);}
58   
59bool CompileOptionIsSet(const PabloCompilationFlags flag) {return PabloOptimizationsOptions.isSet(flag);}
60   
61
62
63void pablo_function_passes(PabloKernel * kernel) {
64
65    if (ShowPabloOption != codegen::OmittedOption) {
66        //Print to the terminal the AST that was generated by the pararallel bit-stream compiler.
67        if (ShowPabloOption.empty()) {
68            errs() << "Initial Pablo AST:\n";
69            PabloPrinter::print(kernel, errs());
70        } else {
71            std::error_code error;
72            llvm::raw_fd_ostream out(ShowPabloOption, error, PabloOutputFileFlag);
73            PabloPrinter::print(kernel, out);
74            PabloOutputFileFlag = sys::fs::OpenFlags::F_Append;   // append subsequent Pablo kernels
75        }
76    }
77
78#ifdef NDEBUG
79    if (DebugOptions.isSet(VerifyPablo)) {
80#endif
81        PabloVerifier::verify(kernel, "creation");
82#ifdef NDEBUG
83    }
84#endif
85
86    // Scan through the pablo code and perform DCE and CSE
87    if (Flatten){
88        FlattenIf::transform(kernel);
89    }
90    if (LLVM_LIKELY(!PabloOptimizationsOptions.isSet(DisableSimplification))) {
91        Simplifier::optimize(kernel);
92    }
93    if (PabloOptimizationsOptions.isSet(EnableDistribution)) {
94        DistributivePass::optimize(kernel);
95    }
96    if (LLVM_LIKELY(!PabloOptimizationsOptions.isSet(DisableCodeMotion))) {
97        CodeMotionPass::optimize(kernel);
98    }
99    if (PabloOptimizationsOptions.isSet(EnableSchedulingPrePass)) {
100        SchedulingPrePass::optimize(kernel);
101    }
102    if (ShowOptimizedPabloOption != codegen::OmittedOption) {
103        if (ShowOptimizedPabloOption.empty()) {
104            //Print to the terminal the final Pablo AST after optimization.
105            errs() << "Final Pablo AST:\n";
106            PabloPrinter::print(kernel, errs());
107        } else {
108            std::error_code error;
109            llvm::raw_fd_ostream out(ShowOptimizedPabloOption, error, PabloOptimizedOutputFileFlag);
110            PabloPrinter::print(kernel, out);
111            PabloOptimizedOutputFileFlag = sys::fs::OpenFlags::F_Append;  // append subsequent Pablo kernels
112        }
113    }
114}
115
116}
Note: See TracBrowser for help on using the repository browser.