source: src/Concurrency/Actors.cpp@ d40555e

ADT ast-experimental
Last change on this file since d40555e was 046ba23, checked in by caparsons <caparson@…>, 3 years ago

small comment cleanup

  • Property mode set to 100644
File size: 19.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Actors.cpp -- generate code needed by the actor system
8//
9// Author : Colby Parsons
10// Created On : Thurs Jan 19 15:34:00 2023
11// Last Modified By : Colby Parsons
12// Last Modified On : Thurs Jan 19 15:34:00 2023
13// Update Count : 0
14//
15
16#include "AST/Print.hpp"
17#include "AST/Decl.hpp"
18#include "AST/Pass.hpp"
19#include "AST/Type.hpp"
20#include "AST/Stmt.hpp"
21#include "AST/TranslationUnit.hpp"
22#include "AST/Expr.hpp"
23#include <algorithm>
24using namespace ast;
25using namespace std;
26
27namespace Concurrency {
28
29struct CollectactorStructDecls : public ast::WithGuards {
30 unordered_set<const StructDecl *> & actorStructDecls;
31 unordered_set<const StructDecl *> & messageStructDecls;
32 const StructDecl ** requestDecl;
33 const EnumDecl ** allocationDecl;
34 const StructDecl ** actorDecl;
35 const StructDecl ** msgDecl;
36 StructDecl * parentDecl;
37 bool insideStruct = false;
38 bool namedDecl = false;
39
40 // finds and sets a ptr to the Allocation enum, which is needed in the next pass
41 void previsit( const EnumDecl * decl ) {
42 if( decl->name == "Allocation" ) *allocationDecl = decl;
43 }
44
45 // finds and sets a ptr to the actor, message, and request structs, which are needed in the next pass
46 void previsit( const StructDecl * decl ) {
47 GuardValue(insideStruct);
48 insideStruct = true;
49 parentDecl = mutate( decl );
50 if( decl->name == "actor" ) {
51 if ( actorDecl ) actorStructDecls.insert( decl ); // skip inserting fwd decl
52 *actorDecl = decl;
53 }
54 if( decl->name == "message" ) {
55 if ( msgDecl ) messageStructDecls.insert( decl ); // skip inserting fwd decl
56 *msgDecl = decl;
57 }
58 if( decl->name == "request" ) *requestDecl = decl;
59 }
60
61 // this catches structs of the form:
62 // struct dummy_actor { actor a; };
63 // since they should be:
64 // struct dummy_actor { inline actor; };
65 void previsit ( const ObjectDecl * decl ) {
66 if ( insideStruct && ! decl->name.empty() ) {
67 GuardValue(namedDecl);
68 namedDecl = true;
69 }
70 }
71
72 // this collects the valid actor and message struct decl pts
73 void postvisit( const StructInstType * node ) {
74 if ( ! *actorDecl || ! *msgDecl ) return;
75 if ( insideStruct && !namedDecl ) {
76 if ( node->aggr() == *actorDecl ) {
77 actorStructDecls.insert( parentDecl );
78 } else if ( node->aggr() == *msgDecl ) {
79 messageStructDecls.insert( parentDecl );
80 }
81 }
82 }
83
84 public:
85 CollectactorStructDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
86 const StructDecl ** requestDecl, const EnumDecl ** allocationDecl, const StructDecl ** actorDecl, const StructDecl ** msgDecl )
87 : actorStructDecls( actorStructDecls ), messageStructDecls( messageStructDecls ), requestDecl( requestDecl ),
88 allocationDecl( allocationDecl ), actorDecl(actorDecl), msgDecl(msgDecl) {}
89};
90
91// keeps track of all fwdDecls of message routines so that we can hoist them to right after the appropriate decls
92class FwdDeclTable {
93
94 // tracks which decls we have seen so that we can hoist the FunctionDecl to the highest point possible
95 struct FwdDeclData {
96 const StructDecl * actorDecl;
97 const StructDecl * msgDecl;
98 FunctionDecl * fwdDecl;
99 bool actorFound;
100 bool msgFound;
101
102 bool readyToInsert() { return actorFound && msgFound; }
103 bool foundActor() { actorFound = true; return readyToInsert(); }
104 bool foundMsg() { msgFound = true; return readyToInsert(); }
105
106 FwdDeclData( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) :
107 actorDecl(actorDecl), msgDecl(msgDecl), fwdDecl(fwdDecl), actorFound(false), msgFound(false) {}
108 };
109
110 // map indexed by actor struct ptr
111 // value is map of all FwdDeclData that contains said actor struct ptr
112 // inner map is indexed by the message struct ptr of FwdDeclData
113 unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> actorMap;
114
115 // this map is the same except the outer map is indexed by message ptr and the inner is indexed by actor ptr
116 unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> msgMap;
117
118 void insert( const StructDecl * decl, const StructDecl * otherDecl, unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map, FwdDeclData * data ) {
119 auto iter = map.find( decl );
120 if ( iter != map.end() ) { // if decl exists in map append data to existing inner map
121 iter->second.emplace( make_pair( otherDecl, data ) );
122 } else { // else create inner map for key
123 map.emplace( make_pair( decl, unordered_map<const StructDecl *, FwdDeclData *>( { make_pair( otherDecl, data ) } ) ) );
124 }
125 }
126
127 public:
128 // insert decl into table so that we can fwd declare it later (average cost: O(1))
129 void insertDecl( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) {
130 FwdDeclData * declToInsert = new FwdDeclData( actorDecl, msgDecl, fwdDecl );
131 insert( actorDecl, msgDecl, actorMap, declToInsert );
132 insert( msgDecl, actorDecl, msgMap, declToInsert );
133 }
134
135 // returns list of decls to insert after current struct decl
136 // Over the entire pass the runtime of this routine is O(r) where r is the # of receive routines
137 list<FunctionDecl *> updateDecl( const StructDecl * decl, bool isMsg ) {
138 unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map = isMsg ? msgMap : actorMap;
139 unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & otherMap = isMsg ? actorMap : msgMap;
140 auto iter = map.find( decl );
141 list<FunctionDecl *> toInsertAfter; // this is populated with decls that are ready to insert
142 if ( iter == map.end() ) return toInsertAfter;
143
144 // iterate over inner map
145 unordered_map<const StructDecl *, FwdDeclData *> & currInnerMap = iter->second;
146 for ( auto innerIter = currInnerMap.begin(); innerIter != currInnerMap.end(); ) {
147 FwdDeclData * currentDatum = innerIter->second;
148 bool readyToInsert = isMsg ? currentDatum->foundMsg() : currentDatum->foundActor();
149 if ( ! readyToInsert ) { ++innerIter; continue; }
150
151 // readyToInsert is true so we are good to insert the forward decl of the message fn
152 toInsertAfter.push_back( currentDatum->fwdDecl );
153
154 // need to remove from other map before deleting
155 // find inner map in other map ( other map is actor map if original is msg map and vice versa )
156 const StructDecl * otherDecl = isMsg ? currentDatum->actorDecl : currentDatum->msgDecl;
157 auto otherMapIter = otherMap.find( otherDecl );
158
159 unordered_map<const StructDecl *, FwdDeclData *> & otherInnerMap = otherMapIter->second;
160
161 // find the FwdDeclData we need to remove in the other inner map
162 auto otherInnerIter = otherInnerMap.find( decl );
163
164 // remove references to deleted FwdDeclData from current inner map
165 innerIter = currInnerMap.erase( innerIter ); // this does the increment so no explicit inc needed
166
167 // remove references to deleted FwdDeclData from other inner map
168 otherInnerMap.erase( otherInnerIter );
169
170 // if other inner map is now empty, remove key from other outer map
171 if ( otherInnerMap.empty() )
172 otherMap.erase( otherDecl );
173
174 // now we are safe to delete the FwdDeclData since we are done with it
175 // and we have removed all references to it from our data structures
176 delete currentDatum;
177 }
178
179 // if current inner map is now empty, remove key from outer map.
180 // Have to do this after iterating for safety
181 if ( currInnerMap.empty() )
182 map.erase( decl );
183
184 return toInsertAfter;
185 }
186};
187
188struct GenReceiveDecls : public ast::WithDeclsToAdd<> {
189 unordered_set<const StructDecl *> & actorStructDecls;
190 unordered_set<const StructDecl *> & messageStructDecls;
191 const StructDecl ** requestDecl;
192 const EnumDecl ** allocationDecl;
193 const StructDecl ** actorDecl;
194 const StructDecl ** msgDecl;
195 FwdDeclTable & forwardDecls;
196
197 void postvisit( const FunctionDecl * decl ) {
198 // return if not of the form receive( param1, param2 ) or if it is a forward decl
199 if ( decl->name != "receive" || decl->params.size() != 2 || !decl->stmts ) return;
200
201 // the params should be references
202 const ReferenceType * derivedActorRef = dynamic_cast<const ReferenceType *>(decl->params.at(0)->get_type());
203 const ReferenceType * derivedMsgRef = dynamic_cast<const ReferenceType *>(decl->params.at(1)->get_type());
204 if ( !derivedActorRef || !derivedMsgRef ) return;
205
206 // the references should be to struct instances
207 const StructInstType * arg1InstType = dynamic_cast<const StructInstType *>(derivedActorRef->base.get());
208 const StructInstType * arg2InstType = dynamic_cast<const StructInstType *>(derivedMsgRef->base.get());
209 if ( !arg1InstType || !arg2InstType ) return;
210
211 // If the struct instances are derived actor and message types then generate the message send routine
212 auto actorIter = actorStructDecls.find( arg1InstType->aggr() );
213 auto messageIter = messageStructDecls.find( arg2InstType->aggr() );
214 if ( actorIter != actorStructDecls.end() && messageIter != messageStructDecls.end() ) {
215
216 // check that we have found all the decls we need from <actor.hfa>
217 if ( !*allocationDecl || !*requestDecl )
218 SemanticError( decl->location, "using actors requires a header, add #include <actor.hfa>\n" );
219
220 //////////////////////////////////////////////////////////////////////
221 // The following generates this send message operator routine for all receive(derived_actor &, derived_msg &) functions
222 /*
223 static inline derived_actor & ?|?( derived_actor & receiver, derived_msg & msg ) {
224 request new_req;
225 Allocation (*my_work_fn)( derived_actor &, derived_msg & ) = receive;
226 __receive_fn fn = (__receive_fn)my_work_fn;
227 new_req{ &receiver, &msg, fn };
228 send( receiver, new_req );
229 return receiver;
230 }
231 */
232 CompoundStmt * sendBody = new CompoundStmt( decl->location );
233
234 // Generates: request new_req;
235 sendBody->push_back( new DeclStmt(
236 decl->location,
237 new ObjectDecl(
238 decl->location,
239 "new_req",
240 new StructInstType( *requestDecl )
241 )
242 ));
243
244 // Function type is: Allocation (*)( derived_actor &, derived_msg & )
245 FunctionType * derivedReceive = new FunctionType();
246 derivedReceive->params.push_back( ast::deepCopy( derivedActorRef ) );
247 derivedReceive->params.push_back( ast::deepCopy( derivedMsgRef ) );
248 derivedReceive->returns.push_back( new EnumInstType( *allocationDecl ) );
249
250 // Generates: Allocation (*my_work_fn)( derived_actor &, derived_msg & ) = receive;
251 sendBody->push_back( new DeclStmt(
252 decl->location,
253 new ObjectDecl(
254 decl->location,
255 "my_work_fn",
256 new PointerType( derivedReceive ),
257 new SingleInit( decl->location, new NameExpr( decl->location, "receive" ) )
258 )
259 ));
260
261 // Function type is: Allocation (*)( actor &, message & )
262 FunctionType * genericReceive = new FunctionType();
263 genericReceive->params.push_back( new ReferenceType( new StructInstType( *actorDecl ) ) );
264 genericReceive->params.push_back( new ReferenceType( new StructInstType( *msgDecl ) ) );
265 genericReceive->returns.push_back( new EnumInstType( *allocationDecl ) );
266
267 // Generates: Allocation (*fn)( actor &, message & ) = (Allocation (*)( actor &, message & ))my_work_fn;
268 // More readable synonymous code:
269 // typedef Allocation (*__receive_fn)(actor &, message &);
270 // __receive_fn fn = (__receive_fn)my_work_fn;
271 sendBody->push_back( new DeclStmt(
272 decl->location,
273 new ObjectDecl(
274 decl->location,
275 "fn",
276 new PointerType( genericReceive ),
277 new SingleInit( decl->location,
278 new CastExpr( decl->location, new NameExpr( decl->location, "my_work_fn" ), new PointerType( genericReceive ), ExplicitCast )
279 )
280 )
281 ));
282
283 // Generates: new_req{ &receiver, &msg, fn };
284 sendBody->push_back( new ExprStmt(
285 decl->location,
286 new UntypedExpr (
287 decl->location,
288 new NameExpr( decl->location, "?{}" ),
289 {
290 new NameExpr( decl->location, "new_req" ),
291 new AddressExpr( new NameExpr( decl->location, "receiver" ) ),
292 new AddressExpr( new NameExpr( decl->location, "msg" ) ),
293 new NameExpr( decl->location, "fn" )
294 }
295 )
296 ));
297
298 // Generates: send( receiver, new_req );
299 sendBody->push_back( new ExprStmt(
300 decl->location,
301 new UntypedExpr (
302 decl->location,
303 new NameExpr( decl->location, "send" ),
304 {
305 {
306 new NameExpr( decl->location, "receiver" ),
307 new NameExpr( decl->location, "new_req" )
308 }
309 }
310 )
311 ));
312
313 // Generates: return receiver;
314 sendBody->push_back( new ReturnStmt( decl->location, new NameExpr( decl->location, "receiver" ) ) );
315
316 // put it all together into the complete function decl from above
317 FunctionDecl * sendOperatorFunction = new FunctionDecl(
318 decl->location,
319 "?<<?",
320 {}, // forall
321 {
322 new ObjectDecl(
323 decl->location,
324 "receiver",
325 ast::deepCopy( derivedActorRef )
326 ),
327 new ObjectDecl(
328 decl->location,
329 "msg",
330 ast::deepCopy( derivedMsgRef )
331 )
332 }, // params
333 {
334 new ObjectDecl(
335 decl->location,
336 "receiver_ret",
337 ast::deepCopy( derivedActorRef )
338 )
339 },
340 nullptr, // body
341 { Storage::Static }, // storage
342 Linkage::Cforall, // linkage
343 {}, // attributes
344 { Function::Inline }
345 );
346
347 // forward decls to resolve use before decl problem for '|' routines
348 forwardDecls.insertDecl( *actorIter, *messageIter , ast::deepCopy( sendOperatorFunction ) );
349 // forwardDecls.push_back( ast::deepCopy( sendOperatorFunction ) );
350
351 sendOperatorFunction->stmts = sendBody;
352 declsToAddAfter.push_back( sendOperatorFunction );
353 }
354 }
355
356 public:
357 GenReceiveDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
358 const StructDecl ** requestDecl, const EnumDecl ** allocationDecl, const StructDecl ** actorDecl, const StructDecl ** msgDecl,
359 FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
360 requestDecl(requestDecl), allocationDecl(allocationDecl), actorDecl(actorDecl), msgDecl(msgDecl), forwardDecls(forwardDecls) {}
361};
362
363struct GenFwdDecls : public ast::WithDeclsToAdd<> {
364 unordered_set<const StructDecl *> & actorStructDecls;
365 unordered_set<const StructDecl *> & messageStructDecls;
366 FwdDeclTable & forwardDecls;
367
368 void postvisit( const StructDecl * decl ) {
369 list<FunctionDecl *> toAddAfter;
370 auto actorIter = actorStructDecls.find( decl );
371 if ( actorIter != actorStructDecls.end() ) { // this is a derived actor decl
372 // get list of fwd decls that we can now insert
373 toAddAfter = forwardDecls.updateDecl( decl, false );
374
375 // get rid of decl from actorStructDecls since we no longer need it
376 actorStructDecls.erase( actorIter );
377 } else {
378 auto messageIter = messageStructDecls.find( decl );
379 if ( messageIter == messageStructDecls.end() ) return;
380
381 toAddAfter = forwardDecls.updateDecl( decl, true );
382
383 // get rid of decl from messageStructDecls since we no longer need it
384 messageStructDecls.erase( messageIter );
385 }
386
387 // add the fwd decls to declsToAddAfter
388 for ( FunctionDecl * func : toAddAfter ) {
389 declsToAddAfter.push_back( func );
390 }
391 }
392
393 public:
394 GenFwdDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
395 FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
396 forwardDecls(forwardDecls) {}
397};
398
399void implementActors( TranslationUnit & translationUnit ) {
400 // unordered_maps to collect all derived actor and message types
401 unordered_set<const StructDecl *> actorStructDecls;
402 unordered_set<const StructDecl *> messageStructDecls;
403 FwdDeclTable forwardDecls;
404
405 // for storing through the passes
406 // these are populated with various important struct decls
407 const StructDecl * requestDeclPtr = nullptr;
408 const EnumDecl * allocationDeclPtr = nullptr;
409 const StructDecl * actorDeclPtr = nullptr;
410 const StructDecl * msgDeclPtr = nullptr;
411
412 // double pointer to modify local ptrs above
413 const StructDecl ** requestDecl = &requestDeclPtr;
414 const EnumDecl ** allocationDecl = &allocationDeclPtr;
415 const StructDecl ** actorDecl = &actorDeclPtr;
416 const StructDecl ** msgDecl = &msgDeclPtr;
417
418 // first pass collects ptrs to Allocation enum, request type, and generic receive fn typedef
419 // also populates maps of all derived actors and messages
420 Pass<CollectactorStructDecls>::run( translationUnit, actorStructDecls, messageStructDecls, requestDecl,
421 allocationDecl, actorDecl, msgDecl );
422
423 // second pass locates all receive() routines that overload the generic receive fn
424 // it then generates the appropriate operator '|' send routines for the receive routines
425 Pass<GenReceiveDecls>::run( translationUnit, actorStructDecls, messageStructDecls, requestDecl,
426 allocationDecl, actorDecl, msgDecl, forwardDecls );
427
428 // The third pass forward declares operator '|' send routines
429 Pass<GenFwdDecls>::run( translationUnit, actorStructDecls, messageStructDecls, forwardDecls );
430}
431
432
433} // namespace Concurrency
434
435// Local Variables: //
436// tab-width: 4 //
437// mode: c++ //
438// compile-command: "make install" //
439// End: //
440
Note: See TracBrowser for help on using the repository browser.