source: src/ResolvExpr/ResolveAssertions.cc@ 09a767e

Last change on this file since 09a767e was 8f06277, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Some clean-up in Common/utility.h. Deleted some unused declarations and moved others to one of two new headers.

  • Property mode set to 100644
File size: 18.3 KB
RevLine 
[6d6e829]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// ResolveAssertions.cc --
8//
9// Author : Aaron B. Moss
10// Created On : Fri Oct 05 13:46:00 2018
[7d01cf44]11// Last Modified By : Andrew Beach
12// Last Modified On : Thu Aug 8 16:47:00 2019
13// Update Count : 3
[6d6e829]14//
15
16#include "ResolveAssertions.h"
17
[b3bd772]18#include <algorithm> // for sort
[83ab931]19#include <cassert> // for assertf
20#include <list> // for list
[b3bd772]21#include <memory> // for unique_ptr
[4d2d45f9]22#include <sstream> // for ostringstream
23#include <string> // for string
[83ab931]24#include <unordered_map> // for unordered_map, unordered_multimap
25#include <utility> // for move
26#include <vector> // for vector
27
[5bf3976]28#include "AdjustExprType.hpp" // for adjustExprType
[83ab931]29#include "Alternative.h" // for Alternative, AssertionItem, AssertionList
30#include "Common/FilterCombos.h" // for filterCombos
[4d2d45f9]31#include "Common/Indenter.h" // for Indenter
[d76c588]32#include "GenPoly/GenPoly.h" // for getFunctionType
[fed6a0f]33#include "ResolvExpr/AlternativeFinder.h" // for computeConversionCost
[83ab931]34#include "ResolvExpr/RenameVars.h" // for renameTyVars
[5bf3976]35#include "SpecCost.hpp" // for specCost
[83ab931]36#include "SymTab/Indexer.h" // for Indexer
[b3bd772]37#include "SymTab/Mangler.h" // for Mangler
[83ab931]38#include "SynTree/Expression.h" // for InferredParams
39#include "TypeEnvironment.h" // for TypeEnvironment, etc.
40#include "Unify.h" // for unify
[6d6e829]41
42namespace ResolvExpr {
43 /// Unified assertion candidate
44 struct AssnCandidate {
45 SymTab::Indexer::IdData cdata; ///< Satisfying declaration
46 Type* adjType; ///< Satisfying type
47 TypeEnvironment env; ///< Post-unification environment
48 AssertionSet have; ///< Post-unification have-set
49 AssertionSet need; ///< Post-unification need-set
50 OpenVarSet openVars; ///< Post-unification open-var set
[0b00df0]51 UniqueId resnSlot; ///< Slot for any recursive assertion IDs
[6d6e829]52
[aaeacf4]53 AssnCandidate( const SymTab::Indexer::IdData& cdata, Type* adjType, TypeEnvironment&& env,
54 AssertionSet&& have, AssertionSet&& need, OpenVarSet&& openVars, UniqueId resnSlot )
55 : cdata(cdata), adjType(adjType), env(std::move(env)), have(std::move(have)),
[0b00df0]56 need(std::move(need)), openVars(std::move(openVars)), resnSlot(resnSlot) {}
[6d6e829]57 };
58
59 /// List of candidate assertion resolutions
60 using CandidateList = std::vector<AssnCandidate>;
61
62 /// Reference to single deferred item
63 struct DeferRef {
[052cd71]64 const DeclarationWithType* decl;
65 const AssertionSetValue& info;
[6d6e829]66 const AssnCandidate& match;
67 };
68
69 /// Wrapper for the deferred items from a single assertion resolution.
70 /// Acts like indexed list of DeferRef
71 struct DeferItem {
[052cd71]72 const DeclarationWithType* decl;
73 const AssertionSetValue& info;
74 CandidateList matches;
[6d6e829]75
[85dac33]76 DeferItem( const DeclarationWithType* decl, const AssertionSetValue& info, CandidateList&& matches )
[052cd71]77 : decl(decl), info(info), matches(std::move(matches)) {}
[6d6e829]78
[052cd71]79 bool empty() const { return matches.empty(); }
[6d6e829]80
[052cd71]81 CandidateList::size_type size() const { return matches.size(); }
[4d2d45f9]82
[052cd71]83 DeferRef operator[] ( unsigned i ) const { return { decl, info, matches[i] }; }
[6d6e829]84 };
85
86 /// List of deferred resolution items
87 using DeferList = std::vector<DeferItem>;
88
[aaeacf4]89 /// Combo iterator that combines candidates into an output list, merging their environments.
[6d6e829]90 /// Rejects an appended candidate if the environments cannot be merged.
91 class CandidateEnvMerger {
92 /// Current list of merged candidates
93 std::vector<DeferRef> crnt;
94 /// Stack of environments to support backtracking
95 std::vector<TypeEnvironment> envs;
96 /// Stack of open variables to support backtracking
97 std::vector<OpenVarSet> varSets;
98 /// Indexer to use for merges
99 const SymTab::Indexer& indexer;
[aaeacf4]100
[6d6e829]101 public:
102 /// The merged environment/open variables and the list of candidates
103 struct OutType {
104 TypeEnvironment env;
105 OpenVarSet openVars;
106 std::vector<DeferRef> assns;
[c519942]107 Cost cost;
[6d6e829]108
[aaeacf4]109 OutType( const TypeEnvironment& env, const OpenVarSet& openVars,
[6d6e829]110 const std::vector<DeferRef>& assns )
[c519942]111 : env(env), openVars(openVars), assns(assns), cost(Cost::infinity) {}
[6d6e829]112 };
113
[aaeacf4]114 CandidateEnvMerger( const TypeEnvironment& env, const OpenVarSet& openVars,
[6d6e829]115 const SymTab::Indexer& indexer )
116 : crnt(), envs{ env }, varSets{ openVars }, indexer(indexer) {}
117
118 bool append( DeferRef i ) {
119 TypeEnvironment env = envs.back();
120 OpenVarSet openVars = varSets.back();
121 mergeOpenVars( openVars, i.match.openVars );
122
123 if ( ! env.combine( i.match.env, openVars, indexer ) ) return false;
124
125 crnt.emplace_back( i );
126 envs.emplace_back( env );
127 varSets.emplace_back( openVars );
128 return true;
129 }
130
131 void backtrack() {
132 crnt.pop_back();
133 envs.pop_back();
134 varSets.pop_back();
135 }
136
137 OutType finalize() { return { envs.back(), varSets.back(), crnt }; }
138 };
139
[aaeacf4]140 /// Comparator for CandidateEnvMerger outputs that sums their costs and caches the stored
[fbecee5]141 /// sums
142 struct CandidateCost {
143 using Element = CandidateEnvMerger::OutType;
144 private:
145 const SymTab::Indexer& indexer; ///< Indexer for costing
146
147 public:
148 CandidateCost( const SymTab::Indexer& indexer ) : indexer(indexer) {}
149
150 /// reports the cost of an element
[c519942]151 Cost get( Element& x ) const {
152 // check cached cost
153 if ( x.cost != Cost::infinity ) return x.cost;
154
155 // generate cost
156 Cost k = Cost::zero;
157 for ( const auto& assn : x.assns ) {
[d76c588]158 // compute conversion cost from satisfying decl to assertion
[aaeacf4]159 k += computeConversionCost(
[7d01cf44]160 assn.match.adjType, assn.decl->get_type(), false, indexer, x.env );
[aaeacf4]161
[c519942]162 // mark vars+specialization cost on function-type assertions
[d76c588]163 FunctionType* func = GenPoly::getFunctionType( assn.match.cdata.id->get_type() );
[c519942]164 if ( ! func ) continue;
[aaeacf4]165
[c519942]166 for ( DeclarationWithType* formal : func->parameters ) {
167 k.decSpec( specCost( formal->get_type() ) );
168 }
169 k.incVar( func->forall.size() );
170 for ( TypeDecl* td : func->forall ) {
171 k.decSpec( td->assertions.size() );
[fbecee5]172 }
173 }
[c519942]174
175 // cache and return
176 x.cost = k;
177 return k;
[fbecee5]178 }
[aaeacf4]179
[fbecee5]180 /// compares elements by cost
[c519942]181 bool operator() ( Element& a, Element& b ) const {
[fbecee5]182 return get( a ) < get( b );
183 }
184 };
185
[0b00df0]186 /// Set of assertion resolutions, grouped by resolution ID
187 using InferCache = std::unordered_map< UniqueId, InferredParams >;
188
[b408364]189 /// Lexicographically-ordered vector of costs
190 using CostVec = std::vector< Cost >;
191
192 int compare( const CostVec & a, const CostVec & b ) {
193 unsigned i = 0;
194 do {
195 // lex-compare where shorter one is less
196 if ( i == a.size() ) {
197 return i == b.size() ? 0 : -1;
198 }
199 if ( i == b.size() /* && i < a.size() */ ) return 1;
[6f096d2]200
[b408364]201 int c = a[i].compare( b[i] );
202 if ( c != 0 ) return c;
203 } while ( ++i );
204 assert(!"unreachable");
205 }
206
207 bool operator< ( const CostVec & a, const CostVec & b ) { return compare( a, b ) < 0; }
208
[6d6e829]209 /// Flag for state iteration
210 enum IterateFlag { IterateState };
211
212 /// State needed to resolve a set of assertions
213 struct ResnState {
[0b00df0]214 Alternative alt; ///< Alternative assertion is rooted on
215 AssertionList need; ///< Assertions to find
216 AssertionSet newNeed; ///< New assertions for current resolutions
217 DeferList deferred; ///< Deferred matches
218 InferCache inferred; ///< Cache of already-inferred parameters
[b408364]219 CostVec costs; ///< Costs of recursive assertion satisfaction for disambiguation
[0b00df0]220 SymTab::Indexer& indexer; ///< Name lookup (depends on previous assertions)
[6d6e829]221
222 /// Initial resolution state for an alternative
[6f096d2]223 ResnState( Alternative & a, SymTab::Indexer & indexer )
[b408364]224 : alt(a), need(), newNeed(), deferred(), inferred(), costs{ Cost::zero }, indexer(indexer) {
[6d6e829]225 need.swap( a.need );
226 }
227
228 /// Updated resolution state with new need-list
[6f096d2]229 ResnState( ResnState && o, IterateFlag )
[aaeacf4]230 : alt(std::move(o.alt)), need(o.newNeed.begin(), o.newNeed.end()), newNeed(), deferred(),
[b408364]231 inferred(std::move(o.inferred)), costs(o.costs), indexer(o.indexer) {
232 costs.emplace_back( Cost::zero );
233 }
[6d6e829]234 };
235
236 /// Binds a single assertion, updating resolution state
[6f096d2]237 void bindAssertion( const DeclarationWithType * decl, AssertionSetValue info, Alternative & alt,
238 AssnCandidate & match, InferCache & inferred ) {
[aaeacf4]239
[6f096d2]240 const DeclarationWithType * candidate = match.cdata.id;
241 assertf( candidate->uniqueId, "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() );
[6d6e829]242
[6f096d2]243 Expression * varExpr = match.cdata.combine( alt.cvtCost );
[2c187378]244 delete varExpr->result;
245 varExpr->result = match.adjType->clone();
[0b00df0]246 if ( match.resnSlot ) { varExpr->resnSlots.push_back( match.resnSlot ); }
[6d6e829]247
[0b00df0]248 // place newly-inferred assertion in proper place in cache
249 inferred[ info.resnSlot ][ decl->get_uniqueId() ] = ParamEntry{
[6f096d2]250 candidate->uniqueId, candidate->clone(), match.adjType->clone(), decl->get_type()->clone(),
[9aaacc27]251 varExpr };
[6d6e829]252 }
253
254 /// Adds a captured assertion to the symbol table
[6f096d2]255 void addToIndexer( AssertionSet & assertSet, SymTab::Indexer & indexer ) {
[e1f7eef]256 for ( auto& i : assertSet ) {
257 if ( i.second.isUsed ) {
258 indexer.addId( i.first );
[6d6e829]259 }
260 }
261 }
262
[0b00df0]263 // in AlternativeFinder.cc; unique ID for assertion resolutions
264 extern UniqueId globalResnSlot;
265
[6d6e829]266 /// Resolve a single assertion, in context
[6f096d2]267 bool resolveAssertion( AssertionItem & assn, ResnState & resn ) {
[6d6e829]268 // skip unused assertions
269 if ( ! assn.info.isUsed ) return true;
270
[052cd71]271 // lookup candidates for this assertion
272 std::list< SymTab::Indexer::IdData > candidates;
273 resn.indexer.lookupId( assn.decl->name, candidates );
274
275 // find the candidates that unify with the desired type
276 CandidateList matches;
[6f096d2]277 for ( const auto & cdata : candidates ) {
278 const DeclarationWithType * candidate = cdata.id;
[052cd71]279
[2fb35df]280 // ignore deleted candidates.
281 // NOTE: this behavior is different from main resolver.
282 // further investigations might be needed to determine
283 // if we should implement the same rule here
284 // (i.e. error if unique best match is deleted)
285 if (candidate->isDeleted) continue;
286
287 // build independent unification context. for candidate
[052cd71]288 AssertionSet have, newNeed;
289 TypeEnvironment newEnv{ resn.alt.env };
290 OpenVarSet newOpenVars{ resn.alt.openVars };
[6f096d2]291 Type * adjType = candidate->get_type()->clone();
[052cd71]292 adjustExprType( adjType, newEnv, resn.indexer );
293 renameTyVars( adjType );
294
295 // keep unifying candidates
[aaeacf4]296 if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars,
[052cd71]297 resn.indexer ) ) {
298 // set up binding slot for recursive assertions
299 UniqueId crntResnSlot = 0;
300 if ( ! newNeed.empty() ) {
301 crntResnSlot = ++globalResnSlot;
302 for ( auto& a : newNeed ) {
303 a.second.resnSlot = crntResnSlot;
[0b00df0]304 }
[2fd9f24]305 }
[e1f7eef]306
[aaeacf4]307 matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have),
[052cd71]308 std::move(newNeed), std::move(newOpenVars), crntResnSlot );
309 } else {
310 delete adjType;
311 }
[6d6e829]312 }
313
314 // break if no suitable assertion
315 if ( matches.empty() ) return false;
316
317 // defer if too many suitable assertions
318 if ( matches.size() > 1 ) {
[052cd71]319 resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) );
[6d6e829]320 return true;
321 }
322
323 // otherwise bind current match in ongoing scope
324 AssnCandidate& match = matches.front();
325 addToIndexer( match.have, resn.indexer );
326 resn.newNeed.insert( match.need.begin(), match.need.end() );
[052cd71]327 resn.alt.env = std::move(match.env);
328 resn.alt.openVars = std::move(match.openVars);
[6d6e829]329
[0b00df0]330 bindAssertion( assn.decl, assn.info, resn.alt, match, resn.inferred );
[6d6e829]331 return true;
332 }
333
[0b00df0]334 /// Associates inferred parameters with an expression
335 struct InferMatcher {
336 InferCache& inferred;
337
338 InferMatcher( InferCache& inferred ) : inferred( inferred ) {}
339
340 Expression* postmutate( Expression* expr ) {
[40290497]341 // defer missing inferred parameters until they are hopefully found later
342 std::vector<UniqueId> missingSlots;
[0b00df0]343 // place inferred parameters into resolution slots
344 for ( UniqueId slot : expr->resnSlots ) {
345 // fail if no matching parameters found
[40290497]346 auto it = inferred.find( slot );
347 if ( it == inferred.end() ) {
348 missingSlots.push_back( slot );
349 continue;
350 }
351 InferredParams& inferParams = it->second;
[aaeacf4]352
[0b00df0]353 // place inferred parameters into proper place in expression
354 for ( auto& entry : inferParams ) {
355 // recurse on inferParams of resolved expressions
356 entry.second.expr = postmutate( entry.second.expr );
357 // xxx - look at entry.second.inferParams?
[462a7c7]358 auto res = expr->inferParams.emplace( entry.first, entry.second );
359 assert(res.second);
[0b00df0]360 }
361 }
362
363 // clear resolution slots and return
[40290497]364 expr->resnSlots.swap( missingSlots );
[0b00df0]365 return expr;
366 }
367 };
368
[b408364]369 /// Map of alternative return types to recursive assertion satisfaction costs
370 using PruneMap = std::unordered_map<std::string, CostVec>;
371
372 /// Gets the pruning key for an alternative
373 std::string pruneKey( const Alternative & alt ) {
374 Type* resType = alt.expr->result->clone();
375 alt.env.apply( resType );
376 std::string resKey = SymTab::Mangler::mangleType( resType );
377 delete resType;
[1d760934]378 return resKey;
[b408364]379 }
[6f096d2]380
381 /// Replace resnSlots with inferParams and add alternative to output list, if meets pruning
[b408364]382 /// threshold.
383 void finalizeAssertions( ResnState& resn, PruneMap & pruneThresholds, AltList& out ) {
384 // prune if cheaper alternative for same key has already been generated
385 std::string resKey = pruneKey( resn.alt );
386 auto it = pruneThresholds.find( resKey );
387 if ( it != pruneThresholds.end() ) {
388 if ( it->second < resn.costs ) return;
389 } else {
390 pruneThresholds.emplace_hint( it, resKey, resn.costs );
391 }
392
393 // replace resolution slots with inferred params, add to output
394 PassVisitor<InferMatcher> matcher{ resn.inferred };
395 resn.alt.expr = resn.alt.expr->acceptMutator( matcher );
396 out.emplace_back( resn.alt );
[0b00df0]397 }
398
399 /// Limit to depth of recursion of assertion satisfaction
[aca6a54c]400 static const int recursionLimit = 7;
[d3b2c32a]401 /// Maximum number of simultaneously-deferred assertions to attempt concurrent satisfaction of
402 static const int deferLimit = 10;
[6d6e829]403
[4d2d45f9]404 void resolveAssertions( Alternative& alt, const SymTab::Indexer& indexer, AltList& out, std::list<std::string>& errors ) {
[6d6e829]405 // finish early if no assertions to resolve
406 if ( alt.need.empty() ) {
407 out.emplace_back( alt );
408 return;
409 }
410
411 // build list of possible resolutions
412 using ResnList = std::vector<ResnState>;
413 SymTab::Indexer root_indexer{ indexer };
414 ResnList resns{ ResnState{ alt, root_indexer } };
415 ResnList new_resns{};
[6f096d2]416
[b408364]417 // Pruning thresholds by result type of the output alternatives.
418 // Alternatives *should* be generated in sorted order, so no need to retroactively prune
419 PruneMap thresholds;
[6d6e829]420
421 // resolve assertions in breadth-first-order up to a limited number of levels deep
422 for ( unsigned level = 0; level < recursionLimit; ++level ) {
423 // scan over all mutually-compatible resolutions
424 for ( auto& resn : resns ) {
[b408364]425 // stop this branch if already found a better option
426 auto it = thresholds.find( pruneKey( resn.alt ) );
427 if ( it != thresholds.end() && it->second < resn.costs ) goto nextResn;
428
[6d6e829]429 // make initial pass at matching assertions
430 for ( auto& assn : resn.need ) {
431 // fail early if any assertion is not resolvable
[052cd71]432 if ( ! resolveAssertion( assn, resn ) ) {
[99d4584]433 Indenter tabs{ 3 };
[4d2d45f9]434 std::ostringstream ss;
435 ss << tabs << "Unsatisfiable alternative:\n";
436 resn.alt.print( ss, ++tabs );
[d3b2c32a]437 ss << (tabs-1) << "Could not satisfy assertion:\n";
438 assn.decl->print( ss, tabs );
[aaeacf4]439
[4d2d45f9]440 errors.emplace_back( ss.str() );
441 goto nextResn;
442 }
[6d6e829]443 }
444
445 if ( resn.deferred.empty() ) {
446 // either add successful match or push back next state
447 if ( resn.newNeed.empty() ) {
[b408364]448 finalizeAssertions( resn, thresholds, out );
[6d6e829]449 } else {
450 new_resns.emplace_back( std::move(resn), IterateState );
451 }
[d3b2c32a]452 } else if ( resn.deferred.size() > deferLimit ) {
453 // too many deferred assertions to attempt mutual compatibility
454 Indenter tabs{ 3 };
455 std::ostringstream ss;
456 ss << tabs << "Unsatisfiable alternative:\n";
457 resn.alt.print( ss, ++tabs );
458 ss << (tabs-1) << "Too many non-unique satisfying assignments for "
459 "assertions:\n";
460 for ( const auto& d : resn.deferred ) {
461 d.decl->print( ss, tabs );
462 }
463
464 errors.emplace_back( ss.str() );
465 goto nextResn;
[6d6e829]466 } else {
[b3bd772]467 // resolve deferred assertions by mutual compatibility
[6d6e829]468 std::vector<CandidateEnvMerger::OutType> compatible = filterCombos(
[aaeacf4]469 resn.deferred,
[6d6e829]470 CandidateEnvMerger{ resn.alt.env, resn.alt.openVars, resn.indexer } );
[4d2d45f9]471 // fail early if no mutually-compatible assertion satisfaction
472 if ( compatible.empty() ) {
[99d4584]473 Indenter tabs{ 3 };
[4d2d45f9]474 std::ostringstream ss;
475 ss << tabs << "Unsatisfiable alternative:\n";
476 resn.alt.print( ss, ++tabs );
[d3b2c32a]477 ss << (tabs-1) << "No mutually-compatible satisfaction for assertions:\n";
[4d2d45f9]478 for ( const auto& d : resn.deferred ) {
[052cd71]479 d.decl->print( ss, tabs );
[4d2d45f9]480 }
481
482 errors.emplace_back( ss.str() );
483 goto nextResn;
484 }
[b408364]485 // sort by cost for overall pruning
[b3bd772]486 CandidateCost coster{ resn.indexer };
487 std::sort( compatible.begin(), compatible.end(), coster );
488
489 for ( auto& compat : compatible ) {
[6d6e829]490 ResnState new_resn = resn;
491
492 // add compatible assertions to new resolution state
493 for ( DeferRef r : compat.assns ) {
494 AssnCandidate match = r.match;
495 addToIndexer( match.have, new_resn.indexer );
496 new_resn.newNeed.insert( match.need.begin(), match.need.end() );
497
[052cd71]498 bindAssertion( r.decl, r.info, new_resn.alt, match, new_resn.inferred );
[6d6e829]499 }
500
501 // set mutual environment into resolution state
502 new_resn.alt.env = std::move(compat.env);
503 new_resn.alt.openVars = std::move(compat.openVars);
504
[b408364]505 // mark cost of this path
506 new_resn.costs.back() += compat.cost;
507
[6d6e829]508 // either add sucessful match or push back next state
509 if ( new_resn.newNeed.empty() ) {
[b408364]510 finalizeAssertions( new_resn, thresholds, out );
[6d6e829]511 } else {
512 new_resns.emplace_back( std::move(new_resn), IterateState );
513 }
514 }
515 }
516 nextResn:; }
517
518 // finish or reset for next round
519 if ( new_resns.empty() ) return;
520 resns.swap( new_resns );
521 new_resns.clear();
522 }
[aaeacf4]523
[6d6e829]524 // exceeded recursion limit if reaches here
525 if ( out.empty() ) {
526 SemanticError( alt.expr->location, "Too many recursive assertions" );
527 }
528 }
529} // namespace ResolvExpr
530
531// Local Variables: //
532// tab-width: 4 //
533// mode: c++ //
534// compile-command: "make install" //
535// End: //
Note: See TracBrowser for help on using the repository browser.