 Timestamp:
 Feb 14, 2019, 11:40:17 PM (4 years ago)
 Branches:
 aaronthesis, armeh, cleanupdtors, enum, forallpointerdecay, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr, pthreademulation, qualifiedEnum
 Children:
 4c41b17
 Parents:
 96bf5781
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

doc/theses/aaron_moss_PhD/phd/resolutionheuristics.tex
r96bf5781 r7c6eb64 28 28 \begin{itemize} 29 29 \item If either operand is a floatingpoint type, the common type is the size of the largest floatingpoint type. If either operand is !_Complex!, the common type is also !_Complex!. 30 \item If both operands are of integral type, the common type has the same size\footnote{Technically, the C standard defines a notion of \emph{rank} , a distinct value for each \lstinline{signed} and \lstinline{unsigned} pair; integral types of the same size thus may have distinct ranks. For instance, if \lstinline{int} and \lstinline{long} are the same size, \lstinline{long} will have greater rank. The standarddefined types are declared to have greater rank than any types of the same size added as compiler extensions.} as the larger type.30 \item If both operands are of integral type, the common type has the same size\footnote{Technically, the C standard defines a notion of \emph{rank} in \cite[\S{}6.3.1.1]{C11}, a distinct value for each \lstinline{signed} and \lstinline{unsigned} pair; integral types of the same size thus may have distinct ranks. For instance, if \lstinline{int} and \lstinline{long} are the same size, \lstinline{long} will have greater rank. The standarddefined types are declared to have greater rank than any types of the same size added as compiler extensions.} as the larger type. 31 31 \item If the operands have opposite signedness, the common type is !signed! if the !signed! operand is strictly larger, or !unsigned! otherwise. If the operands have the same signedness, the common type shares it. 32 32 \end{itemize} 33 33 34 Beginning with the work of Bilson\cite{Bilson03}, \CFA{} has defineda \emph{conversion cost} for each function call in a way that generalizes C's conversion rules.34 Beginning with the work of Bilson\cite{Bilson03}, \CFA{} defines a \emph{conversion cost} for each function call in a way that generalizes C's conversion rules. 35 35 Loosely defined, the conversion cost counts the implicit conversions utilized by an interpretation. 36 36 With more specificity, the cost is a lexicographicallyordered tuple, where each element corresponds to a particular kind of conversion. 37 In Bilson's \CFA{}design, conversion cost is a 3tuple, $(unsafe, poly, safe)$, where $unsafe$ is the count of unsafe (narrowing) conversions, $poly$ is the count of polymorphic type bindings, and $safe$ is the sum of the degree of safe (widening) conversions.38 Degree of safe conversion is calculated as path weight in a weighted directed graph of safe conversions between types; the current version of this graph isin Figure~\ref{safeconvgraphfig}.39 The safe conversion graph designed such that the common type $c$ of two types $u$ and $v$ is compatible with the C standard definitions from \cite[\S{}6.3.1.8]{C11} and can be calculated as the unique type minimizing the sum of the path weights of $\overrightarrow{uc}$ and $\overrightarrow{vc}$.37 In Bilson's design, conversion cost is a 3tuple, $(unsafe, poly, safe)$, where $unsafe$ is the count of unsafe (narrowing) conversions, $poly$ is the count of polymorphic type bindings, and $safe$ is the sum of the degree of safe (widening) conversions. 38 Degree of safe conversion is calculated as path weight in a directed graph of safe conversions between types; both Bilson's version and the current version of this graph are in Figure~\ref{safeconvgraphfig}. 39 The safe conversion graph is designed such that the common type $c$ of two types $u$ and $v$ is compatible with the C standard definitions from \cite[\S{}6.3.1.8]{C11} and can be calculated as the unique type minimizing the sum of the path weights of $\overrightarrow{uc}$ and $\overrightarrow{vc}$. 40 40 The following example lists the cost in the Bilson model of calling each of the following functions with two !int! parameters: 41 41 42 42 \begin{cfa} 43 43 void f(char, long); $\C{// (1,0,1)}$ 44 void f(short, long); $\C{// (1,0,1)}$ 44 45 forall(otype T) void f(T, long); $\C{// (0,1,1)}$ 45 46 void f(long, long); $\C{// (0,0,2)}$ … … 48 49 \end{cfa} 49 50 50 Note that safe and unsafe conversions are handled differently; \CFA{} counts distance of safe conversions (\eg{} !int! to !long! is cheaper than !int! to !unsigned long!), while only counting the number of unsafe conversions (\eg{} !int! to !char! and !int! to !short! both have unsafe cost 1). 51 Note that safe and unsafe conversions are handled differently; \CFA{} counts distance of safe conversions (\eg{} !int! to !long! is cheaper than !int! to !unsigned long!), while only counting the number of unsafe conversions (\eg{} !int! to !char! and !int! to !short! both have unsafe cost 1, as in the first fwo declarations above). 52 These costs are summed over the paramters in a call; in the example above, the cost of the two !int! to !long! conversions for the fourth declaration sum equal to the one !int! to !unsigned long! conversion in the fifth. 51 53 52 54 As part of adding reference types to \CFA{} (see Section~\ref{typefeaturessec}), Schluntz added a new $reference$ element to the cost tuple, which counts the number of implicit referencetorvalue conversions performed so that candidate interpretations can be distinguished by how closely they match the nesting of reference types; since references are meant to act almost indistinguishably from lvalues, this $reference$ element is the least significant in the lexicographic comparison of cost tuples. 53 55 54 56 I have also refined the \CFA{} cost model as part of this thesis work. 55 Bilson's \CFA{} cost model includes the cost of polymorphic type bindings from a function's type assertions in the $poly$ element of the cost tuple; this has the effect of making moreconstrained functions more expensive than lessconstrained functions. 57 Bilson's \CFA{} cost model includes the cost of polymorphic type bindings from a function's type assertions in the $poly$ element of the cost tuple; this has the effect of making moreconstrained functions more expensive than lessconstrained functions, as in the following example: 58 59 \begin{cfa} 60 forall(dtype T  { T& ++?(T&); }) T& advance(T&, int); 61 forall(dtype T  { T& ++?(T&); T& ?+=?(T&, int)}) T& advance(T&, int); 62 \end{cfa} 63 64 In resolving a call to !advance!, the binding to the !T&! parameter in the assertions is added to the $poly$ cost in Bilson's model. 56 65 However, type assertions actually make a function \emph{less} polymorphic, and as such functions with more type assertions should be preferred in type resolution. 57 As an example, some iteratorbased algorithms can work on a forward iterator that only provides an increment operator, but are more efficient on a randomaccess iterator that can be incremented by an arbitrary number of steps in a single operation. 58 The randomaccess iterator has more type constraints, but should be chosen whenever those constraints can be satisfied. 59 As such, I have added a $specialization$ element to the \CFA{} cost tuple, the values of which are always negative. 60 Each type assertion subtracts 1 from $specialization$, so that moreconstrained functions will cost less and thus be chosen over lessconstrained functions, all else being equal. 66 In the example above, the moreconstrained second function can be implemented more efficiently, and as such should be chosen whenever its added constraint can be satisfied. 67 As such, a $specialization$ element is now included in the \CFA{} cost tuple, the values of which are always negative. 68 Each type assertion subtracts 1 from $specialization$, so that moreconstrained functions cost less, and thus are chosen over lessconstrained functions, all else being equal. 61 69 A more sophisticated design would define a partial order over sets of type assertions by set inclusion (\ie{} one function would only cost less than another if it had a strict superset of assertions, rather than just more total assertions), but I did not judge the added complexity of computing and testing this order to be worth the gain in specificity. 62 70 63 I havealso incorporated an unimplemented aspect of Ditchfield's earlier cost model.71 I also incorporated an unimplemented aspect of Ditchfield's earlier cost model. 64 72 In the example below, adapted from \cite[p.89]{Ditchfield92}, Bilson's cost model only distinguished between the first two cases by accounting extra cost for the extra set of !otype! parameters, which, as discussed above, is not a desirable solution: 65 73 … … 71 79 \end{cfa} 72 80 73 I accountfor the fact that functions with more polymorphic variables are less constrained by introducing a $var$ cost element that counts the number of type variables on a candidate function.81 The new cost model accounts for the fact that functions with more polymorphic variables are less constrained by introducing a $var$ cost element that counts the number of type variables on a candidate function. 74 82 In the example above, the first !f! has $var = 2$, while the remainder have $var = 1$. 75 My new \CFA{}cost model also accounts for a nuance unhandled by Ditchfield or Bilson, in that it makes the more specific fourth function above cheaper than the more generic third function.83 The new cost model also accounts for a nuance unhandled by Ditchfield or Bilson, in that it makes the more specific fourth function above cheaper than the more generic third function. 76 84 The fourth function is presumably somewhat optimized for handling pointers, but the prior \CFA{} cost model could not account for the more specific binding, as it simply counted the number of polymorphic unifications. 77 85 78 In my modified model, each level of constraint on a polymorphic type in the parameter list results in a decrement of the $specialization$ cost element.79 Thus, all else equal, if both a binding to !T! and a binding to !T*! are available, \CFA{} will pick the more specific !T*! binding.80 This process is recursive, such that !T**! produces a 2 specialization cost, as opposed to the 1 cost for !T*!.81 This works similarly for generic types, \eg{} !box(T)! also has specialization cost 1.86 In the modified model, each level of constraint on a polymorphic type in the parameter list results in a decrement of the $specialization$ cost element, which is shared with the count of assertions due to their common nature as constraints on polymorphic type bindings. 87 Thus, all else equal, if both a binding to !T! and a binding to !T*! are available, the model chooses the more specific !T*! binding with $specialization = 1$. 88 This process is recursive, such that !T**! has $specialization = 2$. 89 This calculation works similarly for generic types, \eg{} !box(T)! also has specialization cost 1. 82 90 For multiargument generic types, the leastspecialized polymorphic parameter sets the specialization cost, \eg{} the specialization cost of !pair(T, S*)! is 1 (from !T!) rather than 2 (from !S!). 83 Since the user programmer provides parameters, but cannot provide guidance on return type, specialization cost is not counted for the return type list. 91 Specialization cost is not counted on the return type list; since $specialization$ is a property of the function declaration, a lower specialization cost prioritizes one declaration over another. 92 User programmers can choose between functions with varying parameter lists by adjusting the arguments, but the same is not true of varying return types, so the return types are omitted from the $specialization$ element. 84 93 Since both $vars$ and $specialization$ are properties of the declaration rather than any particular interpretation, they are prioritized less than the interpretationspecific conversion costs from Bilson's original 3tuple. 85 94 86 A final refinement I have made to the \CFA{} cost model is with regard to choosing betweenarithmetic conversions.87 The C standard states that the common type of !int! and !unsigned int! is !unsigned int! and that the common type of !int! and !long! is !long!, but does not provide guidance for making a choice between those twoconversions.88 Bilson's \CFACC{} use d conversion costs based off a graph similar to that in Figure~\ref{safeconvgraphfig}, but with arcs selectively removed to disambiguate the costs of such conversions.89 However, the arc removal in Bilson's design resultedin inconsistent and somewhat surprising costs, with conversion to the nextlarger samesign type generally (but not always) double the cost of conversion to the !unsigned! type of the same size.90 In my redesign, for consistency with the approach of the usual arithmetic conversions,which select a common type primarily based on size, but secondarily on sign, the costs of arcs in the new graph are defined to be $1$ to go to a larger size, but $1 + \varepsilon$ to change the sign.95 A final refinement I have made to the \CFA{} cost model is with regard to choosing among arithmetic conversions. 96 The C standard \cite[\S{}6.3.1.8]{C11} states that the common type of !int! and !unsigned int! is !unsigned int! and that the common type of !int! and !long! is !long!, but does not provide guidance for making a choice among conversions. 97 Bilson's \CFACC{} uses conversion costs based off the left graph in Figure~\ref{safeconvgraphfig}. 98 However, Bilson's design results in inconsistent and somewhat surprising costs, with conversion to the nextlarger samesign type generally (but not always) double the cost of conversion to the !unsigned! type of the same size. 99 In the redesign, for consistency with the approach of the usual arithmetic conversions, which select a common type primarily based on size, but secondarily on sign, arcs in the new graph are annotated with whether they represent a sign change, and such sign changes are summed in a new $sign$ cost element that lexicographically succeeds than $safe$. 91 100 This means that sign conversions are approximately the same cost as widening conversions, but slightly more expensive (as opposed to less expensive in Bilson's graph). 92 The $\varepsilon$ portion of the arc cost is implemented by adding a new $sign$ cost lexicographically after $safe$ which counts sign conversions.93 101 94 102 \begin{figure} 95 103 \centering 96 104 \includegraphics{figures/safeconvgraph} 97 \caption[Safe conversion graph .]{Safe conversion graph; plain arcs have cost $1$ while dashed signconversion arcs have cost $1+ \varepsilon$. The arc from \lstinline{unsigned long} to \lstinline{long long} is deliberately omitted, as on the presented system \lstinline{sizeof(long) == sizeof(long long)}.}105 \caption[Safe conversion graphs.]{Safe conversion graphs; Bilson's on the left, the extended graph on the right. In both graphs, plain arcs have cost $safe = 1, sign = 0$ while dashed signconversion arcs have cost $safe = 1, sign = 1$. As per \cite[\S{}6.3.1.8]{C11}, types promote to types of the same signedness with greater rank, from \lstinline{signed} to \lstinline{unsigned} with the same rank, and from \lstinline{unsigned} to \lstinline{signed} with greater size. The arc from \lstinline{unsigned long} to \lstinline{long long} is deliberately omitted in the modified graph, as on the presented system \lstinline{sizeof(long) == sizeof(long long)}.} 98 106 \label{safeconvgraphfig} 99 107 \end{figure} … … 105 113 \end{equation*} 106 114 107 \subsection{Expression Cost} 115 \subsection{Expression Cost} \label{exprcostsec} 108 116 109 117 The mapping from \CFA{} expressions to cost tuples is described by Bilson in \cite{Bilson03}, and remains effectively unchanged modulo the refinements to the cost tuple described above. … … 364 372 365 373 % Mention relevance of work to C++20 concepts 374 375 % Mention more compact representations of the (growing) cost tuple
Note: See TracChangeset
for help on using the changeset viewer.