source: icGREP/icgrep-devel/icgrep/toolchain/driver.h @ 5913

Last change on this file since 5913 was 5856, checked in by nmedfort, 20 months ago

Revised pipeline structure to better control I/O rates

File size: 2.9 KB
Line 
1#ifndef DRIVER_H
2#define DRIVER_H
3
4#include <IR_Gen/FunctionTypeBuilder.h>
5#include <llvm/ExecutionEngine/GenericValue.h>
6#include <kernels/streamset.h>
7#include <kernels/kernel.h>
8#include <string>
9#include <vector>
10#include <memory>
11
12namespace llvm { class Function; }
13namespace kernel { class KernelBuilder; }
14
15class Driver {
16    friend class CBuilder;
17public:
18    Driver(std::string && moduleName);
19
20    virtual ~Driver() = default;
21
22    const std::unique_ptr<kernel::KernelBuilder> & getBuilder() {
23        return iBuilder;
24    }
25
26    template<typename BufferType, typename... Args>
27    parabix::StreamSetBuffer * addBuffer(Args &&... args) {
28        BufferType * const b = new BufferType(std::forward<Args>(args) ...);
29        mOwnedBuffers.emplace_back(b);
30        if (!std::is_same<BufferType, parabix::ExternalBuffer>::value) {
31            mOwnedBuffers.back()->allocateBuffer(iBuilder);
32        }
33        return b;
34    }
35
36    template<typename KernelType, typename... Args>
37    kernel::Kernel * addKernelInstance(Args &&... args) {
38        KernelType * const k = new KernelType(std::forward<Args>(args) ...);
39        mOwnedKernels.emplace_back(k);
40        return k;
41    }
42
43    virtual void makeKernelCall(kernel::Kernel * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) = 0;
44
45    virtual void generatePipelineIR() = 0;
46
47    template <typename ExternalFunctionType>
48    llvm::Function * LinkFunction(kernel::Kernel & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
49
50    virtual bool hasExternalFunction(const llvm::StringRef functionName) const = 0;
51
52    void deallocateBuffers();
53   
54    virtual void finalizeObject() = 0;
55   
56    virtual void * getMain() = 0; // "main" exists until the driver is deleted
57   
58    virtual void performIncrementalCacheCleanupStep() = 0;
59
60protected:
61
62    virtual llvm::Function * addLinkFunction(llvm::Module * mod, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const = 0;
63
64protected:
65    std::unique_ptr<llvm::LLVMContext>                      mContext;
66    llvm::Module * const                                    mMainModule;
67    std::unique_ptr<kernel::KernelBuilder>                  iBuilder;
68    std::vector<std::unique_ptr<kernel::Kernel>>            mOwnedKernels;
69    std::vector<std::unique_ptr<parabix::StreamSetBuffer>>  mOwnedBuffers;
70    std::vector<kernel::Kernel *>                           mPipeline;
71};
72
73template <typename ExternalFunctionType>
74llvm::Function * Driver::LinkFunction(kernel::Kernel & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
75    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(*mContext.get());
76    assert ("FunctionTypeBuilder did not resolve a function type." && type);
77    return addLinkFunction(kb.getModule(), name, type, reinterpret_cast<void *>(functionPtr));
78}
79
80#endif // DRIVER_H
Note: See TracBrowser for help on using the repository browser.