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

Last change on this file since 5543 was 5543, checked in by cameron, 23 months ago

Ensure size of dynamic buffers is a multiple of cache line size

File size: 11.6 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 <IR_Gen/FunctionTypeBuilder.h>
9#include <llvm/IR/IRBuilder.h>
10#include <llvm/IR/Constants.h>
11#include <llvm/ADT/Triple.h>
12#ifndef NDEBUG
13#include <llvm/IR/Function.h>
14#endif
15
16namespace kernels { class KernelBuilder; }
17namespace llvm { class Function; }
18namespace llvm { class IntegerType; }
19namespace llvm { class Module; }
20namespace llvm { class PointerType; }
21namespace llvm { class Type; }
22namespace llvm { class Value; }
23
24class Driver;
25
26class CBuilder : public llvm::IRBuilder<> {
27    using Predicate = llvm::CmpInst::Predicate;
28public:
29
30    CBuilder(llvm::LLVMContext & C);
31   
32    virtual ~CBuilder() {}
33
34    llvm::Module * getModule() const {
35        #ifndef NDEBUG
36        llvm::BasicBlock * const bb = GetInsertBlock();
37        if (bb) {
38            llvm::Function * const f = bb->getParent();
39            assert ("CBuilder has an insert point that is not contained within a Function" && f);
40            assert ("CBuilder module differs from insertion point module" && (mModule == f->getParent()));
41        }
42        #endif
43        return mModule;
44    }
45
46    void setModule(llvm::Module * module) {
47        mModule = module;
48        ClearInsertionPoint();
49    }
50   
51    // UDiv and URem with optimization for division by power-of-2 constants
52    llvm::Value * CreateUDiv(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
53    llvm::Value * CreateURem(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
54
55    // Division with rounding up to the ceiling
56    // Equivalent to CreateUDiv(CreateAdd(number, CreateSub(divisor, ConstantInt::get(divisor->getType(), 1))), divisor)
57    llvm::Value * CreateUDivCeil(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
58   
59    // Round up to a multiple of divisor.
60    llvm::Value * CreateRoundUp(llvm::Value * number, llvm::Value * divisor, const llvm::Twine &Name = "");
61           
62    llvm::Value * CreateMalloc(llvm::Value * size);
63
64    llvm::Value * CreateAlignedMalloc(llvm::Value * size, const unsigned alignment);
65   
66    void CreateFree(llvm::Value * const ptr);
67
68    llvm::Value * CreateRealloc(llvm::Value * ptr, llvm::Value * size);
69
70    llvm::CallInst * CreateMemZero(llvm::Value * ptr, llvm::Value * size, const unsigned alignment = 1) {
71        return CreateMemSet(ptr, getInt8(0), size, alignment);
72    }
73
74    llvm::AllocaInst * CreateCacheAlignedAlloca(llvm::Type * Ty, llvm::Value * ArraySize = nullptr) {
75        llvm::AllocaInst * instr = CreateAlloca(Ty, ArraySize);
76        instr->setAlignment(getCacheAlignment());
77        return instr;
78    }
79
80    llvm::Value * CreateCacheAlignedMalloc(llvm::Value * size) {
81        return CreateAlignedMalloc(size, getCacheAlignment());
82    }
83
84    // stdio.h functions
85    //
86    //  Create a call to:  FILE * fopen(const char *filename, const char *mode);
87    llvm::Value * CreateFOpenCall(llvm::Value * filename, llvm::Value * mode);
88    //  Create a call to:  size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream);
89    llvm::Value * CreateFReadCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
90    //  Create a call to:  size_t fwrite(const void *ptr, size_t size, size_t nitems, FILE *stream));
91    llvm::Value * CreateFWriteCall(llvm::Value * ptr, llvm::Value * size, llvm::Value * nitems, llvm::Value * stream);
92    //  Create a call to:  int fclose ( FILE * stream );
93    llvm::Value * CreateFCloseCall(llvm::Value * stream);
94    //  Create a call to:  int remove(const char *path);
95    llvm::Value * CreateRemoveCall(llvm::Value * path);
96   
97    //  Create a call to:  int rename(const char *old, const char *new);
98    llvm::Value * CreateRenameCall(llvm::Value * oldName, llvm::Value * newName);
99   
100    llvm::Function * GetPrintf();
101
102    llvm::Function * GetDprintf();
103
104    //  Create calls to unistd.h functions.
105    //
106    //  Create a call to:  int open(const char *filename, int oflag, ...);
107    llvm::Value * CreateOpenCall(llvm::Value * filename, llvm::Value * oflag, llvm::Value * mode);
108    //  Create a call to:  ssize_t write(int fildes, const void *buf, size_t nbyte);
109    llvm::Value * CreateWriteCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
110    //  Create a call to:  ssize_t read(int fildes, void *buf, size_t nbyte);
111    llvm::Value * CreateReadCall(llvm::Value * fileDescriptor, llvm::Value * buf, llvm::Value * nbyte);
112    //  Create a call to:  int close(int filedes);
113    llvm::Value * CreateCloseCall(llvm::Value * fileDescriptor);
114    //  Create a call to:  int unlink(const char *path);
115    llvm::Value * CreateUnlinkCall(llvm::Value * path);
116
117    llvm::Value * CreateFileSize(llvm::Value * fileDescriptor);
118
119    //  Create calls to stdlib.h functions.
120    //
121    //  Create a call to:  int mkstemp (char *template);
122    llvm::Value * CreateMkstempCall(llvm::Value * ftemplate);
123   
124    //  Create a call to:  size_t strlen(const char *str);
125    llvm::Value * CreateStrlenCall(llvm::Value * str);
126   
127    llvm::Value * CreateAnonymousMMap(llvm::Value * size);
128
129    llvm::Value * CreateFileSourceMMap(llvm::Value * fd, llvm::Value * size);
130
131    enum Advice {
132        ADVICE_NORMAL
133        , ADVICE_RANDOM
134        , ADVICE_SEQUENTIAL
135        , ADVICE_WILLNEED
136        , ADVICE_DONTNEED
137    };
138
139    llvm::Value * CreateMAdvise(llvm::Value * addr, llvm::Value * length, Advice advice);
140
141    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);
142
143    llvm::Value * CreateMRemap(llvm::Value * addr, llvm::Value * oldSize, llvm::Value * newSize);
144
145    llvm::Value * CreateMUnmap(llvm::Value * addr, llvm::Value * size);
146
147    //  Posix thread (pthread.h) functions.
148    //
149    //  Create a call to:  int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
150    //                    void *(*start_routine)(void*), void *arg);
151    llvm::Value * CreatePThreadCreateCall(llvm::Value * thread, llvm::Value * attr, llvm::Function * start_routine, llvm::Value * arg);
152
153    //  Create a call to:  int pthread_yield(void);
154    llvm::Value * CreatePThreadYield();
155   
156    //  Create a call to:  void pthread_exit(void *value_ptr);
157    llvm::Value * CreatePThreadExitCall(llvm::Value * value_ptr);
158   
159    //  Create a call to:  int pthread_join(pthread_t thread, void **value_ptr);
160    llvm::Value * CreatePThreadJoinCall(llvm::Value * thread, llvm::Value * value_ptr);
161   
162    void CallPrintInt(const std::string & name, llvm::Value * const value);
163   
164    void CallPrintIntToStderr(const std::string & name, llvm::Value * const value);
165   
166    llvm::Value * GetString(llvm::StringRef Str);
167   
168    void CallPrintMsgToStderr(const std::string & message);
169
170    inline llvm::IntegerType * getSizeTy() const {
171        assert (mSizeType);
172        return mSizeType;
173    }
174   
175    inline llvm::ConstantInt * getSize(const size_t value) {
176        return llvm::ConstantInt::get(getSizeTy(), value);
177    }
178   
179    llvm::PointerType * getVoidPtrTy() const;
180
181    llvm::PointerType * getFILEptrTy();
182   
183    inline unsigned getCacheAlignment() const {
184        return mCacheLineAlignment;
185    }
186   
187    virtual llvm::LoadInst* CreateAtomicLoadAcquire(llvm::Value * ptr);
188
189    virtual llvm::StoreInst *  CreateAtomicStoreRelease(llvm::Value * val, llvm::Value * ptr);
190
191    void CreateAssert(llvm::Value * assertion, llvm::StringRef failureMessage) {
192        if (LLVM_UNLIKELY(assertion->getType()->isVectorTy())) {
193            assertion = CreateBitCast(assertion, getIntNTy(assertion->getType()->getPrimitiveSizeInBits()));
194        }
195        return __CreateAssert(CreateIsNotNull(assertion), failureMessage);
196    }
197
198    void CreateAssertZero(llvm::Value * assertion, llvm::StringRef failureMessage) {
199        if (LLVM_UNLIKELY(assertion->getType()->isVectorTy())) {
200            assertion = CreateBitCast(assertion, getIntNTy(assertion->getType()->getPrimitiveSizeInBits()));
201        }
202        return __CreateAssert(CreateIsNull(assertion), failureMessage);
203    }
204
205    void CreateExit(const int exitCode);
206
207    llvm::BranchInst * CreateLikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90);
208
209    llvm::BranchInst * CreateUnlikelyCondBr(llvm::Value * Cond, llvm::BasicBlock * True, llvm::BasicBlock * False, const int probability = 90) {
210        return CreateLikelyCondBr(Cond, True, False, 100 - probability);
211    }
212
213    llvm::BasicBlock * CreateBasicBlock(std::string && name);
214
215    virtual bool supportsIndirectBr() const {
216        return true;
217    }
218
219    llvm::Value * CreatePopcount(llvm::Value * bits);
220
221    llvm::Value * CreateCountForwardZeroes(llvm::Value * value);
222
223    llvm::Value * CreateCountReverseZeroes(llvm::Value * value);
224   
225    // Useful bit manipulation operations 
226    llvm::Value * CreateResetLowestBit(llvm::Value * bits);   
227   
228    llvm::Value * CreateIsolateLowestBit(llvm::Value * bits);
229   
230    llvm::Value * CreateMaskToLowestBitInclusive(llvm::Value * bits);
231   
232    llvm::Value * CreateMaskToLowestBitExclusive(llvm::Value * bits);
233   
234    llvm::Value * CreateExtractBitField(llvm::Value * bits, llvm::Value * start, llvm::Value * length);
235   
236    llvm::Value * CreateCeilLog2(llvm::Value * value);
237   
238    llvm::Value * CreateReadCycleCounter();
239
240    template <typename ExternalFunctionType>
241    llvm::Function * LinkFunction(llvm::StringRef name, ExternalFunctionType * functionPtr) const;
242
243    #ifdef HAS_ADDRESS_SANITIZER
244    virtual llvm::LoadInst * CreateLoad(llvm::Value * Ptr, const char * Name);
245
246    virtual llvm::LoadInst * CreateLoad(llvm::Value * Ptr, const llvm::Twine & Name = "");
247
248    virtual llvm::LoadInst * CreateLoad(llvm::Type * Ty, llvm::Value * Ptr, const llvm::Twine & Name = "");
249
250    virtual llvm::LoadInst * CreateLoad(llvm::Value * Ptr, bool isVolatile, const llvm::Twine & Name = "");
251
252    virtual llvm::StoreInst * CreateStore(llvm::Value * Val, llvm::Value * Ptr, bool isVolatile = false);
253    #endif
254
255    llvm::LoadInst * CreateAlignedLoad(llvm::Value * Ptr, unsigned Align, const char * Name);
256
257    llvm::LoadInst * CreateAlignedLoad(llvm::Value * Ptr, unsigned Align, const llvm::Twine & Name = "");
258
259    llvm::LoadInst * CreateAlignedLoad(llvm::Value * Ptr, unsigned Align, bool isVolatile, const llvm::Twine & Name = "");
260
261    llvm::StoreInst * CreateAlignedStore(llvm::Value * Val, llvm::Value * Ptr, unsigned Align, bool isVolatile = false);
262
263    void setDriver(Driver * const driver) {
264        mDriver = driver;
265    }
266
267protected:
268
269    bool hasAlignedAlloc() const;
270
271    bool hasPosixMemalign() const;
272
273    bool hasAddressSanitizer() const;
274
275    void __CreateAssert(llvm::Value * assertion, llvm::StringRef failureMessage);
276
277    llvm::Function * LinkFunction(llvm::StringRef name, llvm::FunctionType * type, void * functionPtr) const;
278
279protected:
280
281    llvm::Module *                  mModule;
282    unsigned                        mCacheLineAlignment;
283    llvm::IntegerType * const       mSizeType;
284    llvm::StructType *              mFILEtype;
285    Driver *                        mDriver;   
286    llvm::LLVMContext               mContext;
287    const std::string               mTriple;
288};
289
290template <typename ExternalFunctionType>
291llvm::Function *CBuilder::LinkFunction(llvm::StringRef name, ExternalFunctionType * functionPtr) const {
292    llvm::FunctionType * const type = FunctionTypeBuilder<ExternalFunctionType>::get(getContext());
293    assert ("FunctionTypeBuilder did not resolve a function type." && type);
294    return LinkFunction(name, type, reinterpret_cast<void *>(functionPtr));
295}
296
297#endif
Note: See TracBrowser for help on using the repository browser.