| [51587aa] | 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 | // | 
|---|
| [f1e012b] | 7 | // Specialize.cc -- | 
|---|
| [51587aa] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [8cbf8cd] | 11 | // Last Modified By : Rob Schluntz | 
|---|
| [fea7ca7] | 12 | // Last Modified On : Thu Apr 28 15:17:45 2016 | 
|---|
| [771b3c3] | 13 | // Update Count     : 24 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
|  | 16 | #include <cassert> | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "Specialize.h" | 
|---|
| [7754cde] | 19 | #include "GenPoly.h" | 
|---|
| [51b73452] | 20 | #include "PolyMutator.h" | 
|---|
|  | 21 |  | 
|---|
| [68cd1ce] | 22 | #include "Parser/ParseNode.h" | 
|---|
|  | 23 |  | 
|---|
| [51b73452] | 24 | #include "SynTree/Expression.h" | 
|---|
| [68cd1ce] | 25 | #include "SynTree/Statement.h" | 
|---|
| [51b73452] | 26 | #include "SynTree/Type.h" | 
|---|
| [64a32c6] | 27 | #include "SynTree/Attribute.h" | 
|---|
| [51b73452] | 28 | #include "SynTree/TypeSubstitution.h" | 
|---|
|  | 29 | #include "SynTree/Mutator.h" | 
|---|
|  | 30 | #include "ResolvExpr/FindOpenVars.h" | 
|---|
| [d3b7937] | 31 | #include "Common/UniqueName.h" | 
|---|
|  | 32 | #include "Common/utility.h" | 
|---|
| [aedfd91] | 33 | #include "InitTweak/InitTweak.h" | 
|---|
| [51b73452] | 34 |  | 
|---|
|  | 35 | namespace GenPoly { | 
|---|
| [01aeade] | 36 | const std::list<Label> noLabels; | 
|---|
|  | 37 |  | 
|---|
|  | 38 | class Specialize : public PolyMutator { | 
|---|
|  | 39 | public: | 
|---|
|  | 40 | Specialize( std::string paramPrefix = "_p" ); | 
|---|
|  | 41 |  | 
|---|
|  | 42 | virtual Expression * mutate( ApplicationExpr *applicationExpr ); | 
|---|
|  | 43 | virtual Expression * mutate( AddressExpr *castExpr ); | 
|---|
|  | 44 | virtual Expression * mutate( CastExpr *castExpr ); | 
|---|
| [fea7ca7] | 45 | // virtual Expression * mutate( LogicalExpr *logicalExpr ); | 
|---|
|  | 46 | // virtual Expression * mutate( ConditionalExpr *conditionalExpr ); | 
|---|
|  | 47 | // virtual Expression * mutate( CommaExpr *commaExpr ); | 
|---|
| [01aeade] | 48 |  | 
|---|
|  | 49 | private: | 
|---|
| [698664b3] | 50 | Expression *createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams = 0 ); | 
|---|
| [01aeade] | 51 | Expression *doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams = 0 ); | 
|---|
|  | 52 | void handleExplicitParams( ApplicationExpr *appExpr ); | 
|---|
|  | 53 |  | 
|---|
|  | 54 | UniqueName thunkNamer; | 
|---|
|  | 55 | std::string paramPrefix; | 
|---|
|  | 56 | }; | 
|---|
|  | 57 |  | 
|---|
|  | 58 | void convertSpecializations( std::list< Declaration* >& translationUnit ) { | 
|---|
|  | 59 | Specialize specializer; | 
|---|
|  | 60 | mutateAll( translationUnit, specializer ); | 
|---|
|  | 61 | } | 
|---|
|  | 62 |  | 
|---|
|  | 63 | Specialize::Specialize( std::string paramPrefix ) | 
|---|
|  | 64 | : thunkNamer( "_thunk" ), paramPrefix( paramPrefix ) { | 
|---|
|  | 65 | } | 
|---|
|  | 66 |  | 
|---|
| [698664b3] | 67 | /// Looks up open variables in actual type, returning true if any of them are bound in the environment or formal type. | 
|---|
| [01aeade] | 68 | bool needsSpecialization( Type *formalType, Type *actualType, TypeSubstitution *env ) { | 
|---|
|  | 69 | if ( env ) { | 
|---|
|  | 70 | using namespace ResolvExpr; | 
|---|
|  | 71 | OpenVarSet openVars, closedVars; | 
|---|
|  | 72 | AssertionSet need, have; | 
|---|
|  | 73 | findOpenVars( formalType, openVars, closedVars, need, have, false ); | 
|---|
|  | 74 | findOpenVars( actualType, openVars, closedVars, need, have, true ); | 
|---|
|  | 75 | for ( OpenVarSet::const_iterator openVar = openVars.begin(); openVar != openVars.end(); ++openVar ) { | 
|---|
|  | 76 | Type *boundType = env->lookup( openVar->first ); | 
|---|
|  | 77 | if ( ! boundType ) continue; | 
|---|
|  | 78 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( boundType ) ) { | 
|---|
|  | 79 | if ( closedVars.find( typeInst->get_name() ) == closedVars.end() ) { | 
|---|
|  | 80 | return true; | 
|---|
|  | 81 | } // if | 
|---|
|  | 82 | } else { | 
|---|
|  | 83 | return true; | 
|---|
|  | 84 | } // if | 
|---|
|  | 85 | } // for | 
|---|
|  | 86 | return false; | 
|---|
|  | 87 | } else { | 
|---|
|  | 88 | return false; | 
|---|
|  | 89 | } // if | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
| [698664b3] | 92 | /// Generates a thunk that calls `actual` with type `funType` and returns its address | 
|---|
|  | 93 | Expression * Specialize::createThunkFunction( FunctionType *funType, Expression *actual, InferredParams *inferParams ) { | 
|---|
|  | 94 | FunctionType *newType = funType->clone(); | 
|---|
|  | 95 | if ( env ) { | 
|---|
|  | 96 | TypeSubstitution newEnv( *env ); | 
|---|
|  | 97 | // it is important to replace only occurrences of type variables that occur free in the | 
|---|
|  | 98 | // thunk's type | 
|---|
|  | 99 | newEnv.applyFree( newType ); | 
|---|
|  | 100 | } // if | 
|---|
|  | 101 | // create new thunk with same signature as formal type (C linkage, empty body) | 
|---|
| [0f8e4ac] | 102 | FunctionDecl *thunkFunc = new FunctionDecl( thunkNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, newType, new CompoundStmt( noLabels ), false, false ); | 
|---|
| [698664b3] | 103 | thunkFunc->fixUniqueId(); | 
|---|
|  | 104 |  | 
|---|
| [64a32c6] | 105 | // thunks may be generated and not used - silence warning with attribute | 
|---|
|  | 106 | thunkFunc->get_attributes().push_back( new Attribute( "unused" ) ); | 
|---|
|  | 107 |  | 
|---|
| [698664b3] | 108 | // thread thunk parameters into call to actual function, naming thunk parameters as we go | 
|---|
|  | 109 | UniqueName paramNamer( paramPrefix ); | 
|---|
|  | 110 | ApplicationExpr *appExpr = new ApplicationExpr( actual ); | 
|---|
|  | 111 | for ( std::list< DeclarationWithType* >::iterator param = thunkFunc->get_functionType()->get_parameters().begin(); param != thunkFunc->get_functionType()->get_parameters().end(); ++param ) { | 
|---|
|  | 112 | (*param )->set_name( paramNamer.newName() ); | 
|---|
|  | 113 | appExpr->get_args().push_back( new VariableExpr( *param ) ); | 
|---|
|  | 114 | } // for | 
|---|
|  | 115 | appExpr->set_env( maybeClone( env ) ); | 
|---|
|  | 116 | if ( inferParams ) { | 
|---|
|  | 117 | appExpr->get_inferParams() = *inferParams; | 
|---|
|  | 118 | } // if | 
|---|
|  | 119 |  | 
|---|
|  | 120 | // handle any specializations that may still be present | 
|---|
|  | 121 | std::string oldParamPrefix = paramPrefix; | 
|---|
|  | 122 | paramPrefix += "p"; | 
|---|
|  | 123 | // save stmtsToAdd in oldStmts | 
|---|
|  | 124 | std::list< Statement* > oldStmts; | 
|---|
|  | 125 | oldStmts.splice( oldStmts.end(), stmtsToAdd ); | 
|---|
|  | 126 | handleExplicitParams( appExpr ); | 
|---|
|  | 127 | paramPrefix = oldParamPrefix; | 
|---|
|  | 128 | // write any statements added for recursive specializations into the thunk body | 
|---|
|  | 129 | thunkFunc->get_statements()->get_kids().splice( thunkFunc->get_statements()->get_kids().end(), stmtsToAdd ); | 
|---|
|  | 130 | // restore oldStmts into stmtsToAdd | 
|---|
|  | 131 | stmtsToAdd.splice( stmtsToAdd.end(), oldStmts ); | 
|---|
|  | 132 |  | 
|---|
|  | 133 | // add return (or valueless expression) to the thunk | 
|---|
|  | 134 | Statement *appStmt; | 
|---|
|  | 135 | if ( funType->get_returnVals().empty() ) { | 
|---|
|  | 136 | appStmt = new ExprStmt( noLabels, appExpr ); | 
|---|
|  | 137 | } else { | 
|---|
|  | 138 | appStmt = new ReturnStmt( noLabels, appExpr ); | 
|---|
|  | 139 | } // if | 
|---|
|  | 140 | thunkFunc->get_statements()->get_kids().push_back( appStmt ); | 
|---|
|  | 141 |  | 
|---|
|  | 142 | // add thunk definition to queue of statements to add | 
|---|
|  | 143 | stmtsToAdd.push_back( new DeclStmt( noLabels, thunkFunc ) ); | 
|---|
|  | 144 | // return address of thunk function as replacement expression | 
|---|
|  | 145 | return new AddressExpr( new VariableExpr( thunkFunc ) ); | 
|---|
|  | 146 | } | 
|---|
| [f1e012b] | 147 |  | 
|---|
| [01aeade] | 148 | Expression * Specialize::doSpecialization( Type *formalType, Expression *actual, InferredParams *inferParams ) { | 
|---|
| [771b3c3] | 149 | assert( ! actual->get_results().empty() ); // using front, should have this assert | 
|---|
| [01aeade] | 150 | if ( needsSpecialization( formalType, actual->get_results().front(), env ) ) { | 
|---|
|  | 151 | FunctionType *funType; | 
|---|
| [698664b3] | 152 | if ( ( funType = getFunctionType( formalType ) ) ) { | 
|---|
|  | 153 | ApplicationExpr *appExpr; | 
|---|
|  | 154 | VariableExpr *varExpr; | 
|---|
|  | 155 | if ( ( appExpr = dynamic_cast<ApplicationExpr*>( actual ) ) ) { | 
|---|
|  | 156 | return createThunkFunction( funType, appExpr->get_function(), inferParams ); | 
|---|
|  | 157 | } else if ( ( varExpr = dynamic_cast<VariableExpr*>( actual ) ) ) { | 
|---|
|  | 158 | return createThunkFunction( funType, varExpr, inferParams ); | 
|---|
| [01aeade] | 159 | } else { | 
|---|
| [698664b3] | 160 | // This likely won't work, as anything that could build an ApplicationExpr probably hit one of the previous two branches | 
|---|
|  | 161 | return createThunkFunction( funType, actual, inferParams ); | 
|---|
|  | 162 | } | 
|---|
| [01aeade] | 163 | } else { | 
|---|
|  | 164 | return actual; | 
|---|
|  | 165 | } // if | 
|---|
|  | 166 | } else { | 
|---|
|  | 167 | return actual; | 
|---|
|  | 168 | } // if | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
|  | 171 | void Specialize::handleExplicitParams( ApplicationExpr *appExpr ) { | 
|---|
|  | 172 | // create thunks for the explicit parameters | 
|---|
|  | 173 | assert( ! appExpr->get_function()->get_results().empty() ); | 
|---|
| [698664b3] | 174 | FunctionType *function = getFunctionType( appExpr->get_function()->get_results().front() ); | 
|---|
|  | 175 | assert( function ); | 
|---|
| [01aeade] | 176 | std::list< DeclarationWithType* >::iterator formal; | 
|---|
|  | 177 | std::list< Expression* >::iterator actual; | 
|---|
|  | 178 | for ( formal = function->get_parameters().begin(), actual = appExpr->get_args().begin(); formal != function->get_parameters().end() && actual != appExpr->get_args().end(); ++formal, ++actual ) { | 
|---|
|  | 179 | *actual = doSpecialization( (*formal )->get_type(), *actual, &appExpr->get_inferParams() ); | 
|---|
|  | 180 | } | 
|---|
|  | 181 | } | 
|---|
|  | 182 |  | 
|---|
|  | 183 | Expression * Specialize::mutate( ApplicationExpr *appExpr ) { | 
|---|
|  | 184 | appExpr->get_function()->acceptMutator( *this ); | 
|---|
|  | 185 | mutateAll( appExpr->get_args(), *this ); | 
|---|
|  | 186 |  | 
|---|
| [aedfd91] | 187 | if ( ! InitTweak::isIntrinsicCallExpr( appExpr ) ) { | 
|---|
|  | 188 | // create thunks for the inferred parameters | 
|---|
|  | 189 | // don't need to do this for intrinsic calls, because they aren't actually passed | 
|---|
|  | 190 | for ( InferredParams::iterator inferParam = appExpr->get_inferParams().begin(); inferParam != appExpr->get_inferParams().end(); ++inferParam ) { | 
|---|
|  | 191 | inferParam->second.expr = doSpecialization( inferParam->second.formalType, inferParam->second.expr, &appExpr->get_inferParams() ); | 
|---|
|  | 192 | } | 
|---|
| [01aeade] | 193 |  | 
|---|
| [aedfd91] | 194 | handleExplicitParams( appExpr ); | 
|---|
|  | 195 | } | 
|---|
| [01aeade] | 196 |  | 
|---|
|  | 197 | return appExpr; | 
|---|
|  | 198 | } | 
|---|
|  | 199 |  | 
|---|
|  | 200 | Expression * Specialize::mutate( AddressExpr *addrExpr ) { | 
|---|
|  | 201 | addrExpr->get_arg()->acceptMutator( *this ); | 
|---|
| [8cbf8cd] | 202 | assert( ! addrExpr->get_results().empty() ); | 
|---|
| [01aeade] | 203 | addrExpr->set_arg( doSpecialization( addrExpr->get_results().front(), addrExpr->get_arg() ) ); | 
|---|
|  | 204 | return addrExpr; | 
|---|
|  | 205 | } | 
|---|
|  | 206 |  | 
|---|
|  | 207 | Expression * Specialize::mutate( CastExpr *castExpr ) { | 
|---|
|  | 208 | castExpr->get_arg()->acceptMutator( *this ); | 
|---|
| [803deb1] | 209 | if ( castExpr->get_results().empty() ) { | 
|---|
|  | 210 | // can't specialize if we don't have a return value | 
|---|
|  | 211 | return castExpr; | 
|---|
|  | 212 | } | 
|---|
| [698664b3] | 213 | Expression *specialized = doSpecialization( castExpr->get_results().front(), castExpr->get_arg() ); | 
|---|
|  | 214 | if ( specialized != castExpr->get_arg() ) { | 
|---|
|  | 215 | // assume here that the specialization incorporates the cast | 
|---|
|  | 216 | return specialized; | 
|---|
|  | 217 | } else { | 
|---|
|  | 218 | return castExpr; | 
|---|
|  | 219 | } | 
|---|
| [01aeade] | 220 | } | 
|---|
|  | 221 |  | 
|---|
| [fea7ca7] | 222 | // Removing these for now. Richard put these in for some reason, but it's not clear why. | 
|---|
|  | 223 | // In particular, copy constructors produce a comma expression, and with this code the parts | 
|---|
|  | 224 | // of that comma expression are not specialized, which causes problems. | 
|---|
| [01aeade] | 225 |  | 
|---|
| [fea7ca7] | 226 | // Expression * Specialize::mutate( LogicalExpr *logicalExpr ) { | 
|---|
|  | 227 | //      return logicalExpr; | 
|---|
|  | 228 | // } | 
|---|
| [01aeade] | 229 |  | 
|---|
| [fea7ca7] | 230 | // Expression * Specialize::mutate( ConditionalExpr *condExpr ) { | 
|---|
|  | 231 | //      return condExpr; | 
|---|
|  | 232 | // } | 
|---|
|  | 233 |  | 
|---|
|  | 234 | // Expression * Specialize::mutate( CommaExpr *commaExpr ) { | 
|---|
|  | 235 | //      return commaExpr; | 
|---|
|  | 236 | // } | 
|---|
| [51b73452] | 237 | } // namespace GenPoly | 
|---|
| [01aeade] | 238 |  | 
|---|
| [51587aa] | 239 | // Local Variables: // | 
|---|
|  | 240 | // tab-width: 4 // | 
|---|
|  | 241 | // mode: c++ // | 
|---|
|  | 242 | // compile-command: "make install" // | 
|---|
|  | 243 | // End: // | 
|---|