Changeset 3d9d017
- Timestamp:
- Nov 6, 2023, 2:19:37 PM (14 months ago)
- Branches:
- master
- Children:
- ba0e1bc
- Parents:
- 49ae2bc
- Location:
- src
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r49ae2bc r3d9d017 656 656 } 657 657 658 const ast::Stmt * visit( const ast::CoforStmt * node ) override final { 659 // There is no old-AST CoforStmt, so this should never be called. 660 assert( !node ); 661 return nullptr; 662 } 663 658 664 TypeSubstitution * convertTypeSubstitution(const ast::TypeSubstitution * src) { 659 665 -
src/AST/Fwd.hpp
r49ae2bc r3d9d017 68 68 class MutexStmt; 69 69 class CorunStmt; 70 class CoforStmt; 70 71 71 72 class Expr; -
src/AST/Node.cpp
r49ae2bc r3d9d017 194 194 template class ast::ptr_base< ast::CorunStmt, ast::Node::ref_type::weak >; 195 195 template class ast::ptr_base< ast::CorunStmt, ast::Node::ref_type::strong >; 196 template class ast::ptr_base< ast::CoforStmt, ast::Node::ref_type::weak >; 197 template class ast::ptr_base< ast::CoforStmt, ast::Node::ref_type::strong >; 196 198 template class ast::ptr_base< ast::Expr, ast::Node::ref_type::weak >; 197 199 template class ast::ptr_base< ast::Expr, ast::Node::ref_type::strong >; -
src/AST/Pass.hpp
r49ae2bc r3d9d017 172 172 const ast::Stmt * visit( const ast::MutexStmt * ) override final; 173 173 const ast::Stmt * visit( const ast::CorunStmt * ) override final; 174 const ast::Stmt * visit( const ast::CoforStmt * ) override final; 174 175 const ast::Expr * visit( const ast::ApplicationExpr * ) override final; 175 176 const ast::Expr * visit( const ast::UntypedExpr * ) override final; -
src/AST/Pass.impl.hpp
r49ae2bc r3d9d017 1134 1134 1135 1135 //-------------------------------------------------------------------------- 1136 // CoforStmt 1137 template< typename core_t > 1138 const ast::Stmt * ast::Pass< core_t >::visit( const ast::CoforStmt * node ) { 1139 VISIT_START( node ); 1140 1141 if ( __visit_children() ) { 1142 // for statements introduce a level of scope (for the initialization) 1143 guard_symtab guard { *this }; 1144 maybe_accept( node, &CoforStmt::inits ); 1145 maybe_accept_top( node, &CoforStmt::cond ); 1146 maybe_accept_top( node, &CoforStmt::inc ); 1147 maybe_accept_as_compound( node, &CoforStmt::body ); 1148 } 1149 1150 VISIT_END( Stmt, node ); 1151 } 1152 1153 //-------------------------------------------------------------------------- 1136 1154 // ApplicationExpr 1137 1155 template< typename core_t > -
src/AST/Print.cpp
r49ae2bc r3d9d017 934 934 } 935 935 936 virtual const ast::Stmt * visit( const ast::CoforStmt * node ) override final { 937 os << "Cofor Statement" << endl; 938 939 if ( ! node->inits.empty() ) { 940 os << indent << "... initialization:" << endl; 941 ++indent; 942 for ( const ast::Stmt * stmt : node->inits ) { 943 os << indent+1; 944 safe_print( stmt ); 945 } 946 --indent; 947 } 948 949 if ( node->cond ) { 950 os << indent << "... condition:" << endl; 951 ++indent; 952 os << indent; 953 node->cond->accept( *this ); 954 --indent; 955 } 956 957 if ( node->inc ) { 958 os << indent << "... increment:" << endl; 959 ++indent; 960 os << indent; 961 node->inc->accept( *this ); 962 --indent; 963 } 964 965 if ( node->body ) { 966 os << indent << "... with body:" << endl; 967 ++indent; 968 os << indent; 969 node->body->accept( *this ); 970 --indent; 971 } 972 os << endl; 973 print( node->labels ); 974 975 return node; 976 } 977 936 978 virtual const ast::Expr * visit( const ast::ApplicationExpr * node ) override final { 937 979 ++indent; -
src/AST/Stmt.hpp
r49ae2bc r3d9d017 546 546 }; 547 547 548 // Corun Statement 549 class CoforStmt final : public Stmt { 550 public: 551 std::vector<ptr<Stmt>> inits; 552 ptr<Expr> cond; 553 ptr<Expr> inc; 554 ptr<Stmt> body; 555 556 CoforStmt( const CodeLocation & loc, const std::vector<ptr<Stmt>> && inits, const Expr * cond, 557 const Expr * inc, const Stmt * body, const std::vector<Label> && label = {} ) 558 : Stmt(loc, std::move(label)), inits(std::move(inits)), cond(cond), inc(inc), body(body) {} 559 560 const Stmt * accept( Visitor & v ) const override { return v.visit( this ); } 561 private: 562 CoforStmt * clone() const override { return new CoforStmt{ *this }; } 563 MUTATE_FRIEND 564 }; 565 548 566 } // namespace ast 549 567 -
src/AST/Visitor.hpp
r49ae2bc r3d9d017 60 60 virtual const ast::Stmt * visit( const ast::MutexStmt * ) = 0; 61 61 virtual const ast::Stmt * visit( const ast::CorunStmt * ) = 0; 62 virtual const ast::Stmt * visit( const ast::CoforStmt * ) = 0; 62 63 virtual const ast::Expr * visit( const ast::ApplicationExpr * ) = 0; 63 64 virtual const ast::Expr * visit( const ast::UntypedExpr * ) = 0; -
src/Common/CodeLocationTools.cpp
r49ae2bc r3d9d017 138 138 macro(MutexStmt, Stmt) \ 139 139 macro(CorunStmt, Stmt) \ 140 macro(CoforStmt, Stmt) \ 140 141 macro(ApplicationExpr, Expr) \ 141 142 macro(UntypedExpr, Expr) \ -
src/Concurrency/Corun.cpp
r49ae2bc r3d9d017 27 27 struct CorunKeyword : public WithDeclsToAdd<>, public WithStmtsToAdd<> { 28 28 UniqueName CorunFnNamer = "__CFA_corun_lambda_"s; 29 UniqueName CoforFnNamer = "__CFA_cofor_lambda_"s; 30 // UniqueName CoforFnVarNamer = "__CFA_cofor_lambda_var"s; 29 31 UniqueName RunnerBlockNamer = "__CFA_corun_block_"s; 32 33 string coforArgName = "__CFA_cofor_lambda_arg"; 34 string numProcsName = "__CFA_cofor_num_procs"; 35 string currProcsName = "__CFA_cofor_curr_procs"; 36 string thdArrName = "__CFA_cofor_thread_array"; 37 string loopTempName = "__CFA_cofor_loop_temp"; 38 30 39 31 40 const StructDecl * runnerBlockDecl = nullptr; 32 33 // Finds select_node decl 41 const StructDecl * coforRunnerDecl = nullptr; 42 43 // Finds runner_block (corun task) and cofor_runner (cofor task) decls 34 44 void previsit( const StructDecl * decl ) { 35 45 if ( !decl->body ) { … … 38 48 assert( !runnerBlockDecl ); 39 49 runnerBlockDecl = decl; 50 } else if ( "cofor_runner" == decl->name ) { 51 assert( !coforRunnerDecl ); 52 coforRunnerDecl = decl; 40 53 } 41 54 } 42 55 56 // codegen for cofor statements 57 Stmt * postvisit( const CoforStmt * stmt ) { 58 if ( !runnerBlockDecl || !coforRunnerDecl ) 59 SemanticError( stmt->location, "To use cofor statements add #include <cofor.hfa>\n" ); 60 61 if ( stmt->inits.size() != 1 ) 62 SemanticError( stmt->location, "Cofor statements must have a single initializer in the loop control\n" ); 63 64 if ( !stmt->body ) 65 return nullptr; 66 67 const CodeLocation & loc = stmt->location; 68 const string fnName = CoforFnNamer.newName(); 69 70 CompoundStmt * body = new CompoundStmt( loc ); 71 72 // push back cofor initializer to generated body 73 body->push_back( deepCopy( stmt->inits.at(0) ) ); 74 75 CompoundStmt * fnBody = new CompoundStmt( loc ); 76 77 const DeclStmt * declStmtPtr = dynamic_cast<const DeclStmt *>(stmt->inits.at(0).get()); 78 if ( ! declStmtPtr ) 79 SemanticError( stmt->location, "Cofor statement initializer is somehow not a decl statement?\n" ); 80 81 const Decl * declPtr = dynamic_cast<const Decl *>(declStmtPtr->decl.get()); 82 if ( ! declPtr ) 83 SemanticError( stmt->location, "Cofor statement initializer is somehow not a decl?\n" ); 84 85 Type * initType = new TypeofType( new NameExpr( loc, declPtr->name ) ); 86 87 // Generates: 88 // typeof(init) __CFA_cofor_lambda_var = *((typeof(init) *)val); 89 fnBody->push_back( new DeclStmt( loc, 90 new ObjectDecl( loc, 91 declPtr->name, 92 initType, 93 new SingleInit( loc, 94 UntypedExpr::createDeref( loc, 95 new CastExpr( loc, 96 new NameExpr( loc, coforArgName ), 97 new PointerType( initType ), ExplicitCast 98 ) 99 ) 100 ) 101 ) 102 )); 103 104 // push rest of cofor body into loop lambda 105 fnBody->push_back( deepCopy( stmt->body ) ); 106 107 // Generates: 108 // void __CFA_cofor_lambda_() { 109 // typeof(init) __CFA_cofor_lambda_var = *((typeof(init) *)val); 110 // stmt->body; 111 // } 112 Stmt * coforLambda = new DeclStmt( loc, 113 new FunctionDecl( loc, 114 fnName, // name 115 {}, // forall 116 { 117 new ObjectDecl( loc, 118 coforArgName, 119 new ast::PointerType( new ast::VoidType() ) 120 ) 121 }, // params 122 {}, // return 123 fnBody // body 124 ) 125 ); 126 body->push_back( coforLambda ); 127 128 // Generates: 129 // unsigned __CFA_cofor_num_procs = get_proc_count(); 130 body->push_back( new DeclStmt( loc, 131 new ObjectDecl( loc, 132 numProcsName, 133 new BasicType( BasicType::Kind::UnsignedInt ), 134 new SingleInit( loc, 135 new UntypedExpr( loc, 136 new NameExpr( loc, "get_proc_count" ), 137 {} 138 ) 139 ) 140 ) 141 ) 142 ); 143 144 // Generates: 145 // unsigned __CFA_cofor_curr_procs = 0; 146 body->push_back( new DeclStmt( loc, 147 new ObjectDecl( loc, 148 currProcsName, 149 new BasicType( BasicType::Kind::UnsignedInt ), 150 new SingleInit( loc, ConstantExpr::from_int( loc, 0 ) ) 151 ) 152 ) 153 ); 154 155 // Generates: 156 // unsigned cofor_runner __CFA_cofor_thread_array[nprocs]; 157 body->push_back( new DeclStmt( loc, 158 new ObjectDecl( loc, 159 thdArrName, 160 new ast::ArrayType( 161 new StructInstType( coforRunnerDecl ), 162 new NameExpr( loc, numProcsName ), 163 ast::FixedLen, 164 ast::DynamicDim 165 ) 166 ) 167 ) 168 ); 169 170 // Generates: 171 // start_runners( __CFA_cofor_thread_array, __CFA_cofor_num_procs, __CFA_cofor_lambda_ ); 172 body->push_back( new ExprStmt( loc, 173 new UntypedExpr( loc, 174 new NameExpr( loc, "start_runners" ), 175 { 176 new NameExpr( loc, thdArrName ), 177 new NameExpr( loc, numProcsName ), 178 new NameExpr( loc, fnName ) 179 } 180 ) 181 )); 182 183 // Generates: 184 // typeof(initializer) * __CFA_cofor_loop_temp = malloc(); 185 CompoundStmt * forLoopBody = new CompoundStmt( loc ); 186 forLoopBody->push_back( new DeclStmt( loc, 187 new ObjectDecl( loc, 188 loopTempName, 189 new PointerType( initType ), 190 new SingleInit( loc, 191 new UntypedExpr( loc, 192 new NameExpr( loc, "malloc" ), 193 {} 194 ) 195 ) 196 ) 197 ) 198 ); 199 200 // Generates: 201 // *__CFA_cofor_loop_temp = initializer; 202 forLoopBody->push_back( new ExprStmt( loc, 203 UntypedExpr::createAssign( loc, 204 UntypedExpr::createDeref( loc, new NameExpr( loc, loopTempName ) ), 205 new NameExpr( loc, declPtr->name ) 206 ) 207 )); 208 209 // Generates: 210 // send_work( __CFA_cofor_thread_array, __CFA_cofor_num_procs, 211 // __CFA_cofor_curr_procs, __CFA_cofor_loop_temp ); 212 forLoopBody->push_back( new ExprStmt( loc, 213 new UntypedExpr( loc, 214 new NameExpr( loc, "send_work" ), 215 { 216 new NameExpr( loc, thdArrName ), 217 new NameExpr( loc, numProcsName ), 218 new NameExpr( loc, currProcsName ), 219 new NameExpr( loc, loopTempName ) 220 } 221 ) 222 )); 223 224 body->push_back( new ForStmt( loc, 225 {}, 226 deepCopy( stmt->cond ), 227 deepCopy( stmt->inc ), 228 forLoopBody 229 )); 230 231 // Generates: 232 // end_runners( __CFA_cofor_thread_array, __CFA_cofor_num_procs ); 233 body->push_back( new ExprStmt( loc, 234 new UntypedExpr( loc, 235 new NameExpr( loc, "end_runners" ), 236 { 237 new NameExpr( loc, thdArrName ), 238 new NameExpr( loc, numProcsName ) 239 } 240 ) 241 )); 242 243 return body; 244 } 245 246 // codegen for corun statements 43 247 Stmt * postvisit( const CorunStmt * stmt ) { 44 if ( !runnerBlockDecl )248 if ( !runnerBlockDecl || !coforRunnerDecl ) 45 249 SemanticError( stmt->location, "To use corun statements add #include <cofor.hfa>\n" ); 46 250 -
src/Parser/StatementNode.cc
r49ae2bc r3d9d017 503 503 } // build_corun 504 504 505 ast::Stmt * build_cofor( const CodeLocation & location, ForCtrl * forctl, StatementNode * stmt ) { 506 std::vector<ast::ptr<ast::Stmt>> astinit; // maybe empty 507 buildMoveList( forctl->init, astinit ); 508 509 ast::Expr * astcond = nullptr; // maybe empty 510 astcond = notZeroExpr( maybeMoveBuild( forctl->condition ) ); 511 512 ast::Expr * astincr = nullptr; // maybe empty 513 astincr = maybeMoveBuild( forctl->change ); 514 delete forctl; 515 516 return new ast::CoforStmt( location, 517 std::move( astinit ), 518 astcond, 519 astincr, 520 buildMoveSingle( stmt ) 521 ); 522 } // build_cofor 523 505 524 // Local Variables: // 506 525 // tab-width: 4 // -
src/Parser/StatementNode.h
r49ae2bc r3d9d017 106 106 ast::Stmt * build_mutex( const CodeLocation &, ExpressionNode * exprs, StatementNode * stmt ); 107 107 ast::Stmt * build_corun( const CodeLocation &, StatementNode * stmt ); 108 ast::Stmt * build_cofor( const CodeLocation & location, ForCtrl * forctl, StatementNode * stmt ); -
src/Parser/parser.yy
r49ae2bc r3d9d017 1725 1725 cofor_statement: 1726 1726 COFOR '(' for_control_expression_list ')' statement 1727 { SemanticError( yylloc, "cofor statement is currently unimplemented." ); $$ = nullptr; }1727 { $$ = new StatementNode( build_cofor( yylloc, $3, maybe_build_compound( yylloc, $5 ) ) ); } 1728 1728 ; 1729 1729
Note: See TracChangeset
for help on using the changeset viewer.