source: src/GenPoly/SpecializeNew.cpp @ a8dd247

ADTast-experimentalpthread-emulation
Last change on this file since a8dd247 was 13d326ec, checked in by Andrew Beach <ajbeach@…>, 2 years ago

More clean-up, including some removing some duplicate functions.

  • Property mode set to 100644
File size: 16.0 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// SpecializeNew.cpp -- Generate thunks to specialize polymorphic functions.
8//
9// Author           : Andrew Beach
10// Created On       : Tue Jun  7 13:37:00 2022
11// Last Modified By : Andrew Beach
12// Last Modified On : Tue Jun  7 13:37:00 2022
13// Update Count     : 0
14//
15
16#include "Specialize.h"
17
18#include "AST/Pass.hpp"
19#include "AST/TypeEnvironment.hpp"       // for OpenVarSet, AssertionSet
20#include "Common/UniqueName.h"           // for UniqueName
21#include "GenPoly/GenPoly.h"             // for getFunctionType
22#include "InitTweak/InitTweak.h"         // for isIntrinsicCallExpr
23#include "ResolvExpr/FindOpenVars.h"     // for findOpenVars
24#include "ResolvExpr/TypeEnvironment.h"  // for FirstOpen, FirstClosed
25
26#include "AST/Print.hpp"
27
28namespace GenPoly {
29
30namespace {
31
32struct SpecializeCore final :
33                public ast::WithConstTypeSubstitution,
34                public ast::WithDeclsToAdd<>,
35                public ast::WithVisitorRef<SpecializeCore> {
36        std::string paramPrefix = "_p";
37
38        ast::ApplicationExpr * handleExplicitParams(
39                const ast::ApplicationExpr * expr );
40        const ast::Expr * createThunkFunction(
41                const CodeLocation & location,
42                const ast::FunctionType * funType,
43                const ast::Expr * actual,
44                const ast::InferredParams * inferParams );
45        const ast::Expr * doSpecialization(
46                const CodeLocation & location,
47                const ast::Type * formalType,
48                const ast::Expr * actual,
49                const ast::InferredParams * inferParams );
50
51        const ast::Expr * postvisit( const ast::ApplicationExpr * expr );
52        const ast::Expr * postvisit( const ast::CastExpr * expr );
53};
54
55const ast::InferredParams * getInferredParams( const ast::Expr * expr ) {
56        const ast::Expr::InferUnion & inferred = expr->inferred;
57        if ( inferred.hasParams() ) {
58                return &inferred.inferParams();
59        } else {
60                return nullptr;
61        }
62}
63
64// Check if both types have the same structure. The leaf (non-tuple) types
65// don't have to match but the tuples must match.
66bool isTupleStructureMatching( const ast::Type * t0, const ast::Type * t1 ) {
67        const ast::TupleType * tt0 = dynamic_cast<const ast::TupleType *>( t0 );
68        const ast::TupleType * tt1 = dynamic_cast<const ast::TupleType *>( t1 );
69        if ( tt0 && tt1 ) {
70                if ( tt0->size() != tt1->size() ) {
71                        return false;
72                }
73                for ( auto types : group_iterate( tt0->types, tt1->types ) ) {
74                        if ( !isTupleStructureMatching(
75                                        std::get<0>( types ), std::get<1>( types ) ) ) {
76                                return false;
77                        }
78                }
79                return true;
80        }
81        return (!tt0 && !tt1);
82}
83
84// The number of elements in a type if it is a flattened tuple.
85size_t flatTupleSize( const ast::Type * type ) {
86        if ( auto tuple = dynamic_cast<const ast::TupleType *>( type ) ) {
87                size_t sum = 0;
88                for ( auto t : *tuple ) {
89                        sum += flatTupleSize( t );
90                }
91                return sum;
92        } else {
93                return 1;
94        }
95}
96
97// Find the total number of components in a parameter list.
98size_t functionParameterSize( const ast::FunctionType * type ) {
99        size_t sum = 0;
100        for ( auto param : type->params ) {
101                sum += flatTupleSize( param );
102        }
103        return sum;
104}
105
106bool needsPolySpecialization(
107                const ast::Type * formalType,
108                const ast::Type * actualType,
109                const ast::TypeSubstitution * subs ) {
110        if ( !subs ) {
111                return false;
112        }
113
114        using namespace ResolvExpr;
115        ast::OpenVarSet openVars, closedVars;
116        ast::AssertionSet need, have;
117        findOpenVars( formalType, openVars, closedVars, need, have, FirstClosed );
118        findOpenVars( actualType, openVars, closedVars, need, have, FirstOpen );
119        for ( const ast::OpenVarSet::value_type & openVar : openVars ) {
120                const ast::Type * boundType = subs->lookup( openVar.first );
121                // If the variable is not bound, move onto the next variable.
122                if ( !boundType ) continue;
123
124                // Is the variable cound to another type variable?
125                if ( auto inst = dynamic_cast<const ast::TypeInstType *>( boundType ) ) {
126                        if ( closedVars.find( *inst ) == closedVars.end() ) {
127                                return true;
128                        }
129                // Otherwise, the variable is bound to a concrete type.
130                } else {
131                        return true;
132                }
133        }
134        // None of the type variables are bound.
135        return false;
136}
137
138bool needsTupleSpecialization(
139                const ast::Type * formalType, const ast::Type * actualType ) {
140        // Needs tuple specialization if the structure of the formal type and
141        // actual type do not match.
142
143        // This is the case if the formal type has ttype polymorphism, or if the structure  of tuple types
144        // between the function do not match exactly.
145        if ( const ast::FunctionType * ftype = getFunctionType( formalType ) ) {
146                // A pack in the parameter or return type requires specialization.
147                if ( ftype->isTtype() ) {
148                        return true;
149                }
150                // Conversion of 0 to a function type does not require specialization.
151                if ( dynamic_cast<const ast::ZeroType *>( actualType ) ) {
152                        return false;
153                }
154                const ast::FunctionType * atype =
155                        getFunctionType( actualType->stripReferences() );
156                assertf( atype,
157                        "formal type is a function type, but actual type is not: %s",
158                        toString( actualType ).c_str() );
159                // Can't tuple specialize if parameter sizes deeply-differ.
160                if ( functionParameterSize( ftype ) != functionParameterSize( atype ) ) {
161                        return false;
162                }
163                // If tuple parameter size matches but actual parameter sizes differ
164                // then there needs to be specialization.
165                if ( ftype->params.size() != atype->params.size() ) {
166                        return true;
167                }
168                // Total parameter size can be the same, while individual parameters
169                // can have different structure.
170                for ( auto pairs : group_iterate( ftype->params, atype->params ) ) {
171                        if ( !isTupleStructureMatching(
172                                        std::get<0>( pairs ), std::get<1>( pairs ) ) ) {
173                                return true;
174                        }
175                }
176        }
177        return false;
178}
179
180bool needsSpecialization(
181                const ast::Type * formalType, const ast::Type * actualType,
182                const ast::TypeSubstitution * subs ) {
183        return needsPolySpecialization( formalType, actualType, subs )
184                || needsTupleSpecialization( formalType, actualType );
185}
186
187ast::ApplicationExpr * SpecializeCore::handleExplicitParams(
188                const ast::ApplicationExpr * expr ) {
189        assert( expr->func->result );
190        const ast::FunctionType * func = getFunctionType( expr->func->result );
191        assert( func );
192
193        ast::ApplicationExpr * mut = ast::mutate( expr );
194
195        std::vector<ast::ptr<ast::Type>>::const_iterator formal;
196        std::vector<ast::ptr<ast::Expr>>::iterator actual;
197        for ( formal = func->params.begin(), actual = mut->args.begin() ;
198                        formal != func->params.end() && actual != mut->args.end() ;
199                        ++formal, ++actual ) {
200                *actual = doSpecialization( (*actual)->location,
201                        *formal, *actual, getInferredParams( expr ) );
202        }
203        return mut;
204}
205
206// Explode assuming simple cases: either type is pure tuple (but not tuple
207// expr) or type is non-tuple.
208template<typename OutputIterator>
209void explodeSimple( const CodeLocation & location,
210                const ast::Expr * expr, OutputIterator out ) {
211        // Recurse on tuple types using index expressions on each component.
212        if ( auto tuple = expr->result.as<ast::TupleType>() ) {
213                ast::ptr<ast::Expr> cleanup = expr;
214                for ( unsigned int i = 0 ; i < tuple->size() ; ++i ) {
215                        explodeSimple( location,
216                                new ast::TupleIndexExpr( location, expr, i ), out );
217                }
218        // For a non-tuple type, output a clone of the expression.
219        } else {
220                *out++ = expr;
221        }
222}
223
224// Restructures arguments to match the structure of the formal parameters
225// of the actual function. Returns the next structured argument.
226template<typename Iterator>
227const ast::Expr * structureArg(
228                const CodeLocation& location, const ast::ptr<ast::Type> & type,
229                Iterator & begin, const Iterator & end ) {
230        if ( auto tuple = type.as<ast::TupleType>() ) {
231                std::vector<ast::ptr<ast::Expr>> exprs;
232                for ( const ast::ptr<ast::Type> & t : *tuple ) {
233                        exprs.push_back( structureArg( location, t, begin, end ) );
234                }
235                return new ast::TupleExpr( location, std::move( exprs ) );
236        } else {
237                assertf( begin != end, "reached the end of the arguments while structuring" );
238                return *begin++;
239        }
240}
241
242namespace {
243        struct TypeInstFixer : public ast::WithShortCircuiting {
244                std::map<const ast::TypeDecl *, std::pair<int, int>> typeMap;
245
246                void previsit(const ast::TypeDecl *) { visit_children = false; }
247                const ast::TypeInstType * postvisit(const ast::TypeInstType * typeInst) {
248                        if (typeMap.count(typeInst->base)) {
249                                ast::TypeInstType * newInst = mutate(typeInst);
250                                auto const & pair = typeMap[typeInst->base];
251                                newInst->expr_id = pair.first;
252                                newInst->formal_usage = pair.second;
253                                return newInst;
254                        }
255                        return typeInst;
256                }
257        };
258}
259
260const ast::Expr * SpecializeCore::createThunkFunction(
261                const CodeLocation & location,
262                const ast::FunctionType * funType,
263                const ast::Expr * actual,
264                const ast::InferredParams * inferParams ) {
265        // One set of unique names per program.
266        static UniqueName thunkNamer("_thunk");
267
268        const ast::FunctionType * newType = ast::deepCopy( funType );
269        if ( typeSubs ) {
270                // Must replace only occurrences of type variables
271                // that occure free in the thunk's type.
272                auto result = typeSubs->applyFree( newType );
273                newType = result.node.release();
274        }
275
276        using DWTVector = std::vector<ast::ptr<ast::DeclWithType>>;
277        using DeclVector = std::vector<ast::ptr<ast::TypeDecl>>;
278
279        UniqueName paramNamer( paramPrefix );
280
281        // Create new thunk with same signature as formal type.
282        ast::Pass<TypeInstFixer> fixer;
283        for (const auto & kv : newType->forall) {
284                if (fixer.core.typeMap.count(kv->base)) {
285                        std::cerr << location << ' ' << kv->base->name
286                                << ' ' << kv->expr_id << '_' << kv->formal_usage
287                                << ',' << fixer.core.typeMap[kv->base].first
288                                << '_' << fixer.core.typeMap[kv->base].second << std::endl;
289                        assertf(false, "multiple formals in specialize");
290                }
291                else {
292                        fixer.core.typeMap[kv->base] = std::make_pair(kv->expr_id, kv->formal_usage);
293                }
294        }
295
296        ast::CompoundStmt * thunkBody = new ast::CompoundStmt( location );
297        ast::FunctionDecl * thunkFunc = new ast::FunctionDecl(
298                location,
299                thunkNamer.newName(),
300                map_range<DeclVector>( newType->forall, []( const ast::TypeInstType * inst ) {
301                        return ast::deepCopy( inst->base );
302                } ),
303                map_range<DWTVector>( newType->assertions, []( const ast::VariableExpr * expr ) {
304                        return ast::deepCopy( expr->var );
305                } ),
306                map_range<DWTVector>( newType->params, [&location, &paramNamer]( const ast::Type * type ) {
307                        return new ast::ObjectDecl( location, paramNamer.newName(), ast::deepCopy( type ) );
308                } ),
309                map_range<DWTVector>( newType->returns, [&location, &paramNamer]( const ast::Type * type ) {
310                        return new ast::ObjectDecl( location, paramNamer.newName(), ast::deepCopy( type ) );
311                } ),
312                thunkBody,
313                ast::Storage::Classes(),
314                ast::Linkage::C
315                );
316
317        thunkFunc->fixUniqueId();
318
319        // Thunks may be generated and not used, avoid them.
320        thunkFunc->attributes.push_back( new ast::Attribute( "unused" ) );
321
322        // Global thunks must be static to avoid collitions.
323        // Nested thunks must not be unique and hence, not static.
324        thunkFunc->storage.is_static = !isInFunction();
325
326        // Weave thunk parameters into call to actual function,
327        // naming thunk parameters as we go.
328        ast::ApplicationExpr * app = new ast::ApplicationExpr( location, actual );
329
330        const ast::FunctionType * actualType = ast::deepCopy( getFunctionType( actual->result ) );
331        if ( typeSubs ) {
332                // Need to apply the environment to the actual function's type,
333                // since it may itself be polymorphic.
334                auto result = typeSubs->apply( actualType );
335                actualType = result.node.release();
336        }
337
338        ast::ptr<ast::FunctionType> actualTypeManager = actualType;
339
340        std::vector<ast::ptr<ast::Expr>> args;
341        for ( ast::ptr<ast::DeclWithType> & param : thunkFunc->params ) {
342                // Name each thunk parameter and explode it.
343                // These are then threaded back into the actual function call.
344                ast::DeclWithType * mutParam = ast::mutate( param.get() );
345                explodeSimple( location, new ast::VariableExpr( location, mutParam ),
346                        std::back_inserter( args ) );
347        }
348
349        // Walk parameters to the actual function alongside the exploded thunk
350        // parameters and restructure the arguments to match the actual parameters.
351        std::vector<ast::ptr<ast::Expr>>::iterator
352                argBegin = args.begin(), argEnd = args.end();
353        for ( const auto & actualArg : actualType->params ) {
354                app->args.push_back(
355                        structureArg( location, actualArg.get(), argBegin, argEnd ) );
356        }
357        assertf( argBegin == argEnd, "Did not structure all arguments." );
358
359        app->accept(fixer); // this should modify in place
360
361        app->env = ast::TypeSubstitution::newFromExpr( app, typeSubs );
362        if ( inferParams ) {
363                app->inferred.inferParams() = *inferParams;
364        }
365
366        // Handle any specializations that may still be present.
367        {
368                std::string oldParamPrefix = paramPrefix;
369                paramPrefix += "p";
370                std::list<ast::ptr<ast::Decl>> oldDecls;
371                oldDecls.splice( oldDecls.end(), declsToAddBefore );
372
373                app->accept( *visitor );
374                // Write recursive specializations into the thunk body.
375                for ( const ast::ptr<ast::Decl> & decl : declsToAddBefore ) {
376                        thunkBody->push_back( new ast::DeclStmt( decl->location, decl ) );
377                }
378
379                declsToAddBefore = std::move( oldDecls );
380                paramPrefix = std::move( oldParamPrefix );
381        }
382
383        // Add return (or valueless expression) to the thunk.
384        ast::Stmt * appStmt;
385        if ( funType->returns.empty() ) {
386                appStmt = new ast::ExprStmt( app->location, app );
387        } else {
388                appStmt = new ast::ReturnStmt( app->location, app );
389        }
390        thunkBody->push_back( appStmt );
391
392        // Add the thunk definition:
393        declsToAddBefore.push_back( thunkFunc );
394
395        // Return address of thunk function as replacement expression.
396        return new ast::AddressExpr( location,
397                new ast::VariableExpr( location, thunkFunc ) );
398}
399
400const ast::Expr * SpecializeCore::doSpecialization(
401                const CodeLocation & location,
402                const ast::Type * formalType,
403                const ast::Expr * actual,
404                const ast::InferredParams * inferParams ) {
405        assertf( actual->result, "attempting to specialize an untyped expression" );
406        if ( needsSpecialization( formalType, actual->result, typeSubs ) ) {
407                if ( const ast::FunctionType * type = getFunctionType( formalType ) ) {
408                        if ( const ast::ApplicationExpr * expr =
409                                        dynamic_cast<const ast::ApplicationExpr *>( actual ) ) {
410                                return createThunkFunction( location, type, expr->func, inferParams );
411                        } else if ( auto expr =
412                                        dynamic_cast<const ast::VariableExpr *>( actual ) ) {
413                                return createThunkFunction( location, type, expr, inferParams );
414                        } else {
415                                // (I don't even know what that comment means.)
416                                // This likely won't work, as anything that could build an ApplicationExpr probably hit one of the previous two branches
417                                return createThunkFunction( location, type, actual, inferParams );
418                        }
419                } else {
420                        return actual;
421                }
422        } else {
423                return actual;
424        }
425}
426
427const ast::Expr * SpecializeCore::postvisit(
428                const ast::ApplicationExpr * expr ) {
429        if ( InitTweak::isIntrinsicCallExpr( expr ) ) {
430                return expr;
431        }
432
433        // Create thunks for the inferred parameters.
434        // This is not needed for intrinsic calls, because they aren't
435        // actually passed to the function. It needs to handle explicit params
436        // before inferred params so that explicit params do not recieve a
437        // changed set of inferParams (and change them again).
438        // Alternatively, if order starts to matter then copy expr's inferParams
439        // and pass them to handleExplicitParams.
440        ast::ApplicationExpr * mut = handleExplicitParams( expr );
441        if ( !mut->inferred.hasParams() ) {
442                return mut;
443        }
444        ast::InferredParams & inferParams = mut->inferred.inferParams();
445        for ( ast::InferredParams::value_type & inferParam : inferParams ) {
446                inferParam.second.expr = doSpecialization(
447                        inferParam.second.expr->location,
448                        inferParam.second.formalType,
449                        inferParam.second.expr,
450                        getInferredParams( inferParam.second.expr )
451                );
452        }
453        return mut;
454}
455
456const ast::Expr * SpecializeCore::postvisit( const ast::CastExpr * expr ) {
457        if ( expr->result->isVoid() ) {
458                // No specialization if there is no return value.
459                return expr;
460        }
461        const ast::Expr * specialized = doSpecialization(
462                expr->location, expr->result, expr->arg, getInferredParams( expr ) );
463        if ( specialized != expr->arg ) {
464                // Assume that the specialization incorporates the cast.
465                return specialized;
466        } else {
467                return expr;
468        }
469}
470
471} // namespace
472
473void convertSpecializations( ast::TranslationUnit & translationUnit ) {
474        ast::Pass<SpecializeCore>::run( translationUnit );
475}
476
477} // namespace GenPoly
478
479// Local Variables: //
480// tab-width: 4 //
481// mode: c++ //
482// compile-command: "make install" //
483// End: //
Note: See TracBrowser for help on using the repository browser.