source: icGREP/icgrep-devel/icgrep/kernels/stdout_kernel.cpp @ 5864

Last change on this file since 5864 was 5831, checked in by nmedfort, 18 months ago

Potential bug fix for 32-bit

File size: 4.3 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#include "stdout_kernel.h"
6#include <llvm/IR/Module.h>
7#include <kernels/kernel_builder.h>
8#include <kernels/streamset.h>
9
10namespace llvm { class Type; }
11
12using namespace llvm;
13using namespace parabix;
14
15namespace kernel {
16
17void StdOutKernel::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> & b, llvm::Value * const numOfStrides) {
18    Value * codeUnitBuffer = b->getInputStreamBlockPtr("codeUnitBuffer", b->getInt32(0));
19    codeUnitBuffer = b->CreatePointerCast(codeUnitBuffer, b->getInt8PtrTy());
20    Value * bytesToDo = mAvailableItemCount[0];
21    if (LLVM_UNLIKELY(mCodeUnitWidth > 8)) {
22        bytesToDo = b->CreateMul(bytesToDo, b->getSize(mCodeUnitWidth / 8));
23    } else if (LLVM_UNLIKELY(mCodeUnitWidth < 8)) {
24        bytesToDo = b->CreateUDiv(bytesToDo, b->getSize(8 / mCodeUnitWidth));
25    }
26    b->CreateWriteCall(b->getInt32(1), codeUnitBuffer, bytesToDo);
27}
28
29StdOutKernel::StdOutKernel(const std::unique_ptr<kernel::KernelBuilder> & b, unsigned codeUnitWidth)
30: MultiBlockKernel("stdout", {Binding{b->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}}, {}, {}, {}, {})
31, mCodeUnitWidth(codeUnitWidth) {
32    // setKernelStride(getpagesize());
33}
34
35void FileSink::generateInitializeMethod(const std::unique_ptr<kernel::KernelBuilder> & b) {
36    BasicBlock * setTerminationOnFailure = b->CreateBasicBlock("setTerminationOnFailure");
37    BasicBlock * fileSinkInitExit = b->CreateBasicBlock("fileSinkInitExit");
38    Value * fileName = b->getScalarField("fileName");
39    Value * fileNameLength = b->CreateStrlenCall(fileName);
40    // Make a temporary file name template with the characters "XXXXXX" appended
41    // as required by mkstemp.
42    Constant * suffixPlusNullLength = b->getSize(7);
43    Value * tmpFileNamePtr = b->CreatePointerCast(b->CreateMalloc(b->CreateAdd(fileNameLength, suffixPlusNullLength)), b->getInt8PtrTy());
44    b->setScalarField("tmpFileName", tmpFileNamePtr);
45    b->CreateMemCpy(tmpFileNamePtr, fileName, fileNameLength, 1);
46#ifdef BACKUP_OLDFILE
47    b->CreateMemCpy(b->CreateGEP(tmpFileNamePtr, fileNameLength), b->GetString(".saved"), suffixPlusNullLength, 1);
48    b->CreateRenameCall(fileName, tmpFileNamePtr);
49#else
50    b->CreateUnlinkCall(fileName);
51#endif
52    b->CreateMemCpy(b->CreateGEP(tmpFileNamePtr, fileNameLength), b->GetString("XXXXXX"), suffixPlusNullLength, 1);
53    Value * fileDes = b->CreateMkstempCall(tmpFileNamePtr);
54    b->setScalarField("fileDes", fileDes);
55    Value * failure = b->CreateICmpEQ(fileDes, b->getInt32(-1));
56    b->CreateCondBr(failure, setTerminationOnFailure, fileSinkInitExit);
57
58    b->SetInsertPoint(setTerminationOnFailure);
59    b->setTerminationSignal();
60    b->CreateBr(fileSinkInitExit);
61
62    b->SetInsertPoint(fileSinkInitExit);
63}
64
65void FileSink::generateMultiBlockLogic(const std::unique_ptr<KernelBuilder> & b, Value * const numOfStrides) {
66    Value * const fileDes = b->getScalarField("fileDes");
67    Value * codeUnitBuffer = b->getInputStreamBlockPtr("codeUnitBuffer", b->getInt32(0));
68    codeUnitBuffer = b->CreatePointerCast(codeUnitBuffer, b->getInt8PtrTy());
69    Value * bytesToDo = mAvailableItemCount[0];
70    if (LLVM_UNLIKELY(mCodeUnitWidth > 8)) {
71        bytesToDo = b->CreateMul(bytesToDo, b->getSize(mCodeUnitWidth / 8));
72    } else if (LLVM_UNLIKELY(mCodeUnitWidth < 8)) {
73        bytesToDo = b->CreateUDiv(bytesToDo, b->getSize(8 / mCodeUnitWidth));
74    }   
75    b->CreateWriteCall(fileDes, codeUnitBuffer, bytesToDo);
76}
77
78void FileSink::generateFinalizeMethod(const std::unique_ptr<KernelBuilder> & b) {
79    Value * const fileDes = b->getScalarField("fileDes");
80    b->CreateCloseCall(fileDes);
81    Value * newFileNamePtr = b->getScalarField("fileName");
82    Value * tmpFileNamePtr = b->getScalarField("tmpFileName");
83    b->CreateRenameCall(tmpFileNamePtr, newFileNamePtr);
84    b->CreateFree(tmpFileNamePtr);
85}
86
87FileSink::FileSink(const std::unique_ptr<kernel::KernelBuilder> & b, unsigned codeUnitWidth)
88: MultiBlockKernel("filesink" + std::to_string(codeUnitWidth),
89{Binding{b->getStreamSetTy(1, codeUnitWidth), "codeUnitBuffer"}},
90{},
91{Binding{b->getInt8PtrTy(), "fileName"}}, {}, {Binding{b->getInt8PtrTy(), "tmpFileName"}, Binding{b->getInt32Ty(), "fileDes"}})
92, mCodeUnitWidth(codeUnitWidth) {
93    // setKernelStride(getpagesize());
94}
95
96}
97
98
99
100
Note: See TracBrowser for help on using the repository browser.