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