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

Last change on this file since 5832 was 5755, checked in by nmedfort, 20 months ago

Bug fixes and simplified MultiBlockKernel? logic

File size: 3.1 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    void addKernelCall(kernel::Kernel & kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) {
44        return makeKernelCall(&kb, inputs, outputs);
45    }
46
47    virtual void makeKernelCall(kernel::Kernel * kb, const std::vector<parabix::StreamSetBuffer *> & inputs, const std::vector<parabix::StreamSetBuffer *> & outputs) = 0;
48
49    virtual void generatePipelineIR() = 0;
50
51    template <typename ExternalFunctionType>
52    llvm::Function * LinkFunction(kernel::Kernel & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const;
53
54    virtual bool hasExternalFunction(const llvm::StringRef functionName) const = 0;
55
56    void deallocateBuffers();
57   
58    virtual void finalizeObject() = 0;
59   
60    virtual void * getMain() = 0; // "main" exists until the driver is deleted
61   
62    virtual void performIncrementalCacheCleanupStep() = 0;
63
64protected:
65
66    virtual llvm::Function * addLinkFunction(llvm::Module * mod, llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const = 0;
67
68protected:
69    std::unique_ptr<llvm::LLVMContext>                      mContext;
70    llvm::Module * const                                    mMainModule;
71    std::unique_ptr<kernel::KernelBuilder>                  iBuilder;
72    std::vector<std::unique_ptr<kernel::Kernel>>            mOwnedKernels;
73    std::vector<std::unique_ptr<parabix::StreamSetBuffer>>  mOwnedBuffers;
74    std::vector<kernel::Kernel *>                           mPipeline;
75};
76
77template <typename ExternalFunctionType>
78llvm::Function * Driver::LinkFunction(kernel::Kernel & kb, llvm::StringRef name, ExternalFunctionType * functionPtr) const {
79    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(*mContext.get());
80    assert ("FunctionTypeBuilder did not resolve a function type." && type);
81    return addLinkFunction(kb.getModule(), name, type, reinterpret_cast<void *>(functionPtr));
82}
83
84#endif // DRIVER_H
Note: See TracBrowser for help on using the repository browser.