source: icGREP/icgrep-devel/icgrep/IR_Gen/CBuilder.h @ 5418

Last change on this file since 5418 was 5418, checked in by nmedfort, 2 years ago

Removed non-functional CUDA code from icgrep and consolidated grep and multigrep mode into a single function; allowed segment parallel pipeline to utilize process as its initial thread; modified MMapSourceKernel to map and perform mmap directly and advise the OS to drop consumed data streams.

File size: 7.1 KB
Line 
1/*
2 *  Copyright (c) 2016 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 */
5#ifndef CBUILDER_H
6#define CBUILDER_H
7
8#include <string>
9#include <llvm/IR/IRBuilder.h>
10#include <llvm/IR/Constants.h>
11namespace llvm { class Function; }
12namespace llvm { class IntegerType; }
13namespace llvm { class Module; }
14namespace llvm { class PointerType; }
15namespace llvm { class Type; }
16namespace llvm { class Value; }
17
18class CBuilder : public llvm::IRBuilder<> {
19   
20public:
21
22    CBuilder(llvm::Module * m, const unsigned GeneralRegisterWidthInBits, const bool SupportsIndirectBr, const unsigned CacheLineAlignmentInBytes = 64);
23   
24    virtual ~CBuilder() {}
25
26    llvm::Module * getModule() const {
27        return mMod;
28    }
29   
30    void setModule(llvm::Module * m)  {
31        mMod = m;
32    }
33
34    llvm::Value * CreateMalloc(llvm::Value * size);
35
36    llvm::Value * CreateAlignedMalloc(llvm::Value * size, const unsigned alignment);
37   
38    void CreateFree(llvm::Value * const ptr);
39   
40    void CreateAlignedFree(llvm::Value * const ptr, const bool testForNullAddress = false);
41   
42    llvm::Value * CreateRealloc(llvm::Value * ptr, llvm::Value * size);
43
44    llvm::CallInst * CreateMemZero(llvm::Value * ptr, llvm::Value * size, const unsigned alignment = 1) {
45        return CreateMemSet(ptr, getInt8(0), size, alignment);
46    }
47
48    llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
49        llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
50        instr->setAlignment(getCacheAlignment());
51        return instr;
52    }
53
54    // stdio.h functions
55    //
56    //  Create a call to:  FILE * fopen(const char *filename, const char *mode);
57    llvm::Value * CreateFOpenCall(llvm::Value * filename, llvm::Value * mode);
58    //  Create a call to:  size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
59    llvm::Value * CreateFReadCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
60    //  Create a call to:  size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream));
61    llvm::Value * CreateFWriteCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
62    //  Create a call to:  int fclose ( FILE * stream );
63    llvm::Value * CreateFCloseCall(llvm::Value * stream);
64    //  Create a call to:  int remove(const char *path);
65    llvm::Value * CreateRemoveCall(llvm::Value * path);
66   
67    //  Create a call to:  int rename(const char *old, const char *new);
68    llvm::Value * CreateRenameCall(llvm::Value * oldName, llvm::Value * newName);
69   
70    llvm::Function * GetPrintf();
71
72    //  Create calls to unistd.h functions.
73    //
74    //  Create a call to:  int open(const char *filename, int oflag, ...);
75    llvm::Value * CreateOpenCall(llvm::Value * filename, llvm::Value * oflag, llvm::Value * mode);
76    //  Create a call to:  ssize_t write(int fildes, const void *buf, size_t nbyte);
77    llvm::Value * CreateWriteCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
78    //  Create a call to:  ssize_t read(int fildes, void *buf, size_t nbyte);
79    llvm::Value * CreateReadCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
80    //  Create a call to:  int close(int filedes);
81    llvm::Value * CreateCloseCall(llvm::Value * fileDescriptor);
82    //  Create a call to:  int unlink(const char *path);
83    llvm::Value * CreateUnlinkCall(llvm::Value * path);
84
85    llvm::Value * CreateFileSize(llvm::Value * fileDescriptor);
86
87    //  Create calls to stdlib.h functions.
88    //
89    //  Create a call to:  int mkstemp (char *template);
90    llvm::Value * CreateMkstempCall(llvm::Value * ftemplate);
91   
92    //  Create a call to:  size_t strlen(const char *str);
93    llvm::Value * CreateStrlenCall(llvm::Value * str);
94   
95    llvm::Value * CreateAnonymousMMap(llvm::Value * size);
96
97    llvm::Value * CreateFileSourceMMap(llvm::Value * fd, llvm::Value * size);
98
99    enum MAdviceFlags {
100        MMAP_NORMAL
101        , MMAP_RANDOM
102        , MMAP_SEQUENTIAL
103        , MMAP_WILLNEED
104        , MMAP_DONTNEED
105    };
106
107    llvm::Value * CreateMAdvise(llvm::Value * addr, llvm::Value * length, MAdviceFlags advice) {
108        return CreateMAdvise(addr, length, { advice });
109    }
110
111    llvm::Value * CreateMAdvise(llvm::Value * addr, llvm::Value * length, std::initializer_list<MAdviceFlags> advice);
112
113    llvm::Value * CreateMMap(llvm::Value * const addr, llvm::Value * size, llvm::Value * const prot, llvm::Value * const flags, llvm::Value * const fd, llvm::Value * const offset);
114
115    llvm::Value * CheckMMapSuccess(llvm::Value * const addr);
116
117    llvm::Value * CreateMRemap(llvm::Value * addr, llvm::Value * oldSize, llvm::Value * newSize);
118
119    llvm::Value * CreateMUnmap(llvm::Value * addr, llvm::Value * size);
120
121    //  Posix thread (pthread.h) functions.
122    //
123    //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
124    //                    void *(*start_routine)(void*), void *arg);
125    llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, llvm::Function * start_routine, llvm::Value * arg);
126   
127    //  Create a call to:  void pthread_exit(void *value_ptr);
128    llvm::Value * CreatePThreadExitCall(llvm::Value * value_ptr);
129   
130    //  Create a call to:  int pthread_join(pthread_t thread, void **value_ptr);
131    llvm::Value * CreatePThreadJoinCall(llvm::Value * thread, llvm::Value * value_ptr);
132   
133    void CallPrintInt(const std::string & name, llvm::Value * const value);
134   
135    llvm::Value * GetString(llvm::StringRef Str);
136
137    inline llvm::IntegerType * getSizeTy() const {
138        return mSizeType;
139    }
140   
141    inline llvm::ConstantInt * getSize(const size_t value) {
142        return llvm::ConstantInt::get(getSizeTy(), value);
143    }
144   
145    llvm::PointerType * getVoidPtrTy() const;
146    llvm::PointerType * getFILEptrTy();
147   
148    inline unsigned getCacheAlignment() const {
149        return mCacheLineAlignment;
150    }
151   
152    virtual llvm::LoadInst* CreateAtomicLoadAcquire(llvm::Value * ptr);
153
154    virtual llvm::StoreInst *  CreateAtomicStoreRelease(llvm::Value * val, llvm::Value * ptr);
155
156    void CreateAssert(llvm::Value * assertion, llvm::StringRef failureMessage);
157
158    void CreateExit(const int exitCode);
159
160    llvm::BranchInst * CreateLikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90);
161
162    llvm::BranchInst * CreateUnlikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90) {
163        return CreateLikelyCondBr(Cond, True, False, 100 - probability);
164    }
165
166    bool supportsIndirectBr() const {
167        return mSupportsIndirectBr;
168    }
169
170    llvm::Value * CreatePopcount(llvm::Value * bits);
171
172    llvm::Value * CreateCountForwardZeroes(llvm::Value * value);
173
174    llvm::Value * CreateCountReverseZeroes(llvm::Value * value);
175
176    llvm::Value * CreateCeilLog2(llvm::Value * value);
177
178protected:
179    llvm::Module *                  mMod;
180    unsigned                        mCacheLineAlignment;
181    llvm::IntegerType *             mSizeType;
182    llvm::StructType *              mFILEtype;
183    const bool                      mSupportsIndirectBr;
184};
185
186#endif
Note: See TracBrowser for help on using the repository browser.