Vuo 2.4.4
Loading...
Searching...
No Matches
VuoCompilerChain.cc
Go to the documentation of this file.
1
10#include "VuoCompilerChain.hh"
12#include "VuoCompilerNode.hh"
13#include "VuoStringUtilities.hh"
14
21VuoCompilerChain::VuoCompilerChain(vector<VuoCompilerNode *> nodes, bool lastNodeInLoop)
22{
23 this->nodes = nodes;
24 this->lastNodeInLoop = lastNodeInLoop;
25}
26
31Value * VuoCompilerChain::generateMakeContext(Module *module, BasicBlock *block, Value *compositionStateValue, Value *eventIdValue)
32{
33 PointerType *voidPointerType = PointerType::get(IntegerType::get(module->getContext(), 8), 0);
34
35 // unsigned long *eventIdPtr = (unsigned long *)malloc(sizeof(unsigned long));
36 // *eventIdPtr = eventId;
37
38 Value *eventIdPtrValue = VuoCompilerCodeGenUtilities::generateMemoryAllocation(module, block, eventIdValue->getType(), 1);
39 new StoreInst(eventIdValue, eventIdPtrValue, "", block);
40
41 // size_t contextBytes = 2 * sizeof(void *);
42 // void **context = (void **)malloc(contextBytes);
43 // context[0] = (void *)compositionState;
44 // context[1] = (void *)eventIdPtr;
45
46 Value *contextValue = VuoCompilerCodeGenUtilities::generateMemoryAllocation(module, block, voidPointerType, 2);
47 contextValue->setName("chainWorkerContext");
48
49 BitCastInst *compositionStateAsVoidPointer = new BitCastInst(compositionStateValue, voidPointerType, "", block);
50 VuoCompilerCodeGenUtilities::generateSetArrayElement(module, block, contextValue, 0, compositionStateAsVoidPointer);
51
52 BitCastInst *eventIdPtrValueAsVoidPointer = new BitCastInst(eventIdPtrValue, voidPointerType, "", block);
53 VuoCompilerCodeGenUtilities::generateSetArrayElement(module, block, contextValue, 1, eventIdPtrValueAsVoidPointer);
54
55 // VuoRegister(context, vuoFreeChainWorkerContext);
56
58
59 // VuoRetain(compositionState);
60
61 VuoCompilerCodeGenUtilities::generateRetainCall(module, block, compositionStateValue);
62
63 return new BitCastInst(contextValue, voidPointerType, "", block);
64}
65
69Value * VuoCompilerChain::generateCompositionStateValue(Module *module, BasicBlock *block, Value *contextValue)
70{
71 Type *compositionStatePointerType = PointerType::get(VuoCompilerCodeGenUtilities::getCompositionStateType(module), 0);
72 Type *voidPointerType = contextValue->getType();
73 Type *voidPointerPointerType = PointerType::get(voidPointerType, 0);
74
75 // VuoCompositionState *compositionState = (VuoCompositionState *)((void **)context)[0]);
76
77 Value *contextValueAsVoidPointerArray = new BitCastInst(contextValue, voidPointerPointerType, "", block);
78 Value *compositionStateAsVoidPointer = VuoCompilerCodeGenUtilities::generateGetArrayElement(module, block, contextValueAsVoidPointerArray, (size_t)0);
79 return new BitCastInst(compositionStateAsVoidPointer, compositionStatePointerType, "", block);
80}
81
85Value * VuoCompilerChain::generateEventIdValue(Module *module, BasicBlock *block, Value *contextValue)
86{
87 Type *eventIdPtrType = PointerType::get(IntegerType::get(module->getContext(), 64), 0);
88 Type *voidPointerType = contextValue->getType();
89 Type *voidPointerPointerType = PointerType::get(voidPointerType, 0);
90
91 // unsigned long *eventIdPtr = (unsigned long *)((void **)context)[1]);
92 // unsigned long eventId = *eventIdPtr;
93
94 Value *contextValueAsVoidPointerArray = new BitCastInst(contextValue, voidPointerPointerType, "", block);
95 Value *eventIdPtrValueAsVoidPointer = VuoCompilerCodeGenUtilities::generateGetArrayElement(module, block, contextValueAsVoidPointerArray, 1);
96 Value *eventIdPtrValue = new BitCastInst(eventIdPtrValueAsVoidPointer, eventIdPtrType, "", block);
97 return new LoadInst(eventIdPtrValue, "", block);
98}
99
104{
105 // void vuoFreeChainWorkerContext(void *context)
106 // {
107 // VuoRelease(((void **)context)[0]);
108 // free(((void **)context)[1]);
109 // free(context);
110 // }
111
112 const char *functionName = "vuoFreeChainWorkerContext";
113 Function *function = module->getFunction(functionName);
114 if (! function)
115 {
116 PointerType *voidPointerType = PointerType::get(IntegerType::get(module->getContext(), 8), 0);
117 PointerType *voidPointerPointerType = PointerType::get(voidPointerType, 0);
118 ConstantInt *zeroValue = ConstantInt::get(module->getContext(), APInt(64, 0));
119 ConstantInt *oneValue = ConstantInt::get(module->getContext(), APInt(64, 1));
120
121 Function *freeFunction = VuoCompilerCodeGenUtilities::getFreeFunction(module);
122
123 FunctionType *functionType = FunctionType::get(Type::getVoidTy(module->getContext()), voidPointerType, false);
124 function = Function::Create(functionType, GlobalValue::InternalLinkage, functionName, module);
125 BasicBlock *block = BasicBlock::Create(module->getContext(), "", function, 0);
126
127 Value *contextValue = function->arg_begin();
128 BitCastInst *contextValueAsVoidPointerArray = new BitCastInst(contextValue, voidPointerPointerType, "", block);
129
130 Value *compositionStateVariable = GetElementPtrInst::Create(voidPointerType, contextValueAsVoidPointerArray, zeroValue, "", block);
131 Value *compositionStateValue = new LoadInst(compositionStateVariable, "", block);
132 VuoCompilerCodeGenUtilities::generateReleaseCall(module, block, compositionStateValue);
133
134 Value *eventIdPtrVariable = GetElementPtrInst::Create(voidPointerType, contextValueAsVoidPointerArray, oneValue, "", block);
135 Value *eventIdPtrValue = new LoadInst(eventIdPtrVariable, "", block);
136 CallInst::Create(freeFunction, eventIdPtrValue, "", block);
137
138 CallInst::Create(freeFunction, contextValue, "", block);
139
140 ReturnInst::Create(module->getContext(), block);
141 }
142
143 return function;
144}
145
151Function * VuoCompilerChain::generateScheduleWorker(Module *module, BasicBlock *block, Value *compositionStateValue,
152 Value *contextValue, string triggerIdentifier,
153 int minThreadsNeeded, int maxThreadsNeeded, size_t chainIndex,
154 vector<size_t> upstreamChainIndices)
155{
156 Type *voidPointerType = contextValue->getType();
157 FunctionType *workerFunctionType = FunctionType::get(Type::getVoidTy(module->getContext()), voidPointerType, false);
158
159 string workerFunctionName = VuoStringUtilities::prefixSymbolName(nodes.front()->getIdentifier(), triggerIdentifier);
160 if (lastNodeInLoop)
161 workerFunctionName += "__loop";
162 workerFunctionName += "__worker";
163 Function *workerFunction = Function::Create(workerFunctionType, GlobalValue::InternalLinkage, workerFunctionName, module);
164
165 Value *globalQueue = VuoCompilerCodeGenUtilities::generateGetGlobalDispatchQueue(module, block);
166
167 Value *eventIdValue = generateEventIdValue(module, block, contextValue);
168
169 VuoCompilerCodeGenUtilities::generateScheduleChainWorker(module, block, globalQueue, contextValue, workerFunction,
170 minThreadsNeeded, maxThreadsNeeded,
171 eventIdValue, compositionStateValue, chainIndex, upstreamChainIndices);
172
173 return workerFunction;
174}
175
179vector<VuoCompilerNode *> VuoCompilerChain::getNodes(void)
180{
181 return nodes;
182}
183
188{
189 return lastNodeInLoop;
190}