Changeset 522f71c


Ignore:
Timestamp:
Jan 12, 2025, 9:13:38 PM (3 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
445b281
Parents:
b0708ea (diff), f886608 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
doc
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified doc/LaTeXmacros/common.tex

    rb0708ea r522f71c  
    1111%% Created On       : Sat Apr  9 10:06:17 2016
    1212%% Last Modified By : Peter A. Buhr
    13 %% Last Modified On : Sun Nov  3 09:11:30 2024
    14 %% Update Count     : 684
     13%% Last Modified On : Fri Dec 27 13:44:46 2024
     14%% Update Count     : 686
    1515%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    1616
     
    3232%\renewcommand{\labelitemi}{{\raisebox{0.25ex}{\footnotesize$\bullet$}}}
    3333\setlist[enumerate]{parsep=0.25ex,itemsep=0.25ex,listparindent=\parindent}% global
    34 \setlist[enumerate,2]{leftmargin=\parindent,labelsep=*,align=parleft,label=\alph*.}% local
     34\setlist[enumerate,2]{labelsep=*,align=parleft}% local
    3535\setlist[description]{itemsep=0pt,listparindent=\parindent,leftmargin=\parindent,labelsep=1.5ex}
    3636
  • TabularUnified doc/theses/fangren_yu_MMath/content2.tex

    rb0708ea r522f71c  
    22\label{c:content2}
    33
    4 The \CFA's type-system provides expressive polymorphism: variables can be overloaded, functions can be overloaded by argument and return types, tuple types, generic (polymorphic) functions and types (aggregates) can have multiple type parameters with associated restrictions, and C's multiple implicit type-conversions must be respected.
    5 This generality leads to internal complexity and correspondingly higher compilation cost.
     4The \CFA's type-system provides expressive polymorphism: variables can be overloaded, functions can be overloaded by argument and return types, tuple types, generic (polymorphic) functions and types (aggregates) can have multiple type parameters with assertion restrictions;
     5in addition, C's multiple implicit type-conversions must be respected.
     6This generality leads to internal complexity and correspondingly higher compilation cost directly related to type resolution.
    67The reason is that the type resolver must analyze \emph{each} component of an expression with many possible forms of overloading, type restrictions, and conversions.
    7 Designing a ruleset that behaves as expected, \ie matches with C programmer expectations, and implementing an efficient algorithm is a very challenging task.
     8Designing a ruleset that behaves as expected, \ie matches C programmer expectations, and implementing an efficient algorithm is a very challenging task.
    89
    910My first work on the \CFA type-system was as a co-op student.
    1011At that time, compiling a medium-sized \CFA program using advanced polymorphism took multiple minutes (5+ minutes)~\cite[\S~5]{Yu20}.
    11 After finding and fixing the following resolution problems:
     12I found a number of type-resolution problems, which resulted in the following changes to the type-resolution algorithm.
    1213\begin{enumerate}[itemsep=0pt]
    1314\item
     
    2223skip pruning on expressions for function types
    2324\end{enumerate}
    24 \VRef[Table]{t:SelectedFileByCompilerBuild} shows the improvement of selected tests with accumulated reductions in compile time across each of the 5 fixes.
    25 The large reduction in compilation times, significantly improved the development of the \CFA's runtime because of its frequent compilation cycles.
     25\VRef[Table]{t:SelectedFileByCompilerBuild} shows improvements for selected tests with accumulated reductions in compile time across each of the 5 fixes.
     26The large reduction in compilation time significantly improved the development of the \CFA's runtime because of its frequent compilation cycles.
    2627
    2728\begin{table}[htb]
     
    4445@test/thread@   & 210.9 & 73.5  & 17.0  & 15.1  & 12.6  & 11.8  \\
    4546\end{tabular}
    46 \smallskip
     47\medskip
    4748\newline
    4849Results are average of 5 runs (3 runs if time exceeds 100 seconds)
     
    5960\section{Expression Cost-Model}
    6061
    61 \CFA has been using an expression cost-model to resolve ambiguity of overloaded expressions from the very beginning.
     62\CFA has been using a total-order expression cost-model to resolve ambiguity of overloaded expressions from the very beginning.
    6263Most \CFA operators can be overloaded in \CFA\footnote{
    63 Excluding the comma operator, short-circuit logical operators \lstinline{&&} and \lstinline{||} (control structures) and ternary conditional operator \lstinline{?} (control structure).
    64 While \CC overloads the short-circuit logical operators, the overloaded operators do not exhibit the short-circuit semantics, which is confusing.};
    65 hence, they are treated the same way as other function calls, and the same rules for overload resolution must apply to them as well.
    66 
    67 In \CFA, candidates of an overloaded expression are ranked by numerical cost elements, that accounts for the type conversions needed from the argument type to the corresponding declared function parameter type, as well as the polymorphic types and restrictions introduces by the @forall@ clause.
    68 Currently the expression cost used in \CFA has the following components, ranked from higher to lower by importance:
     64\CFA excludes overloading the comma operator, short-circuit logical operators \lstinline{&&} and \lstinline{||} (control structures), and ternary conditional operator \lstinline{?} (control structure).
     65\CC overloads the comma and short-circuit logical operators, where the overloaded short-circuit operators do not exhibit short-circuit semantics, which is confusing.};
     66hence, they are treated the same way as other function calls with the same rules for overload resolution.
     67
     68In \CFA, candidates of an overloaded expression are ranked by numerical cost elements, which account any type conversions needed from a call-site argument type to the matching function parameter type, as well as polymorphic types and restrictions introduced by the @forall@ clause.
     69Currently, the expression cost has the following components, ranked from highest to lowest.
    6970\begin{enumerate}
    70 \item \textbf{Unsafe} cost representing a narrowing conversion of arithmetic types, e.g.
    71 @int@ to @short@, and qualifier-dropping conversions for pointer and reference types;
    72 \item \textbf{Polymorphic} cost where the function parameter type is or contains a polymorphic type variable;
    73 \item \textbf{Safe} cost representing a widening conversion e.g.
    74 @short@ to @int@, qualifier-adding conversions for pointer and reference types, and value conversion for enumeration constants.
    75 \item \textbf{Variable} cost that counts the number of polymorphic variables, if any, introduced by the forall clause in the function declaration;
    76 \item \textbf{Specialization} cost that counts the number of restrictions introduced by the type assertions.
    77 \end{enumerate}
    78 The comparison of cost tuple is by lexicographical order, starting from the highest importance term (unsafe cost) and the lower one has lower cost, with ties going to the second term (polymorphic cost) and so on.
    79 At a sub-expression level, the lowest cost candidate for each result type is included as a possible interpretation of the expression; at the top level all possible interpretations of different types are considered and the overall lowest cost is selected as the final interpretation of the expression.
    80 
    81 In many languages that support function and operator overloading, such as \CC and Java, a partial ordering system decides which interpretation of the expression gets selected, which means that sometimes the candidates are incomparable (none of those are considered a better match) and only when one candidate is considered better than all others (maximal with respect to the partial order) is the expression unambiguous.
    82 
    83 In \CFA trying to use such a system is problematic because of the presence of return type overloading of functions, and overloading of variables.
    84 The resolution algorithms used in \CC and Java are greedy, as they select the best match for each sub-expression without considering the higher level ones, and therefore at each step of resolution, the arguments are already given unique interpretations, so the ordering only needs to consider comparing different sets of conversion targets (function parameter types) on the same set of input.
    85 However, in \CFA expression resolution considers multiple interpretations of argument sub-expressions with different types, so it is possible that both the selected function and the set of arguments are different, and cannot be compared if we choose to use some kind of partial ordering system.
    86 Since this situation can arise quite often in \CFA, even in the simplest form such as an expression \textbf{f(a)} where both the function name \textbf{f} and variable name \textbf{a} are overloaded.
    87 We do not want the resolution algorithm to report too many expressions as ambiguous (which would therefore be compilation errors) and restrict the flexibility of \CFA by too much.
    88 The previous documentations and papers on \CFA expression resolution never explained why such a cost system is used; this could be a plausible guess of the original motivation of introducing the cost system to \CFA.
    89 
    90 On the contrary, using such a cost-based model can sometimes make \CFA expression resolution too permissive; the system will always attempt to select the lowest cost option, and only when there are multiple options tied at the lowest cost it reports the expression as ambiguous.
    91 With so many elements in the cost tuple, ties are expected to be uncommon.
    92 Other than the case of multiple exact matches which would all have cost of zero, incomparable candidates under a partial ordering of being more specific can often have different expression costs since different kinds of implicit conversions are involved, resulting in seemingly arbitrary overload selections.
     71\item \textbf{Unsafe} cost representing a narrowing conversion of arithmetic types, \eg @int@ to @short@, and qualifier-dropping conversions for pointer and reference types.
     72\item \textbf{Polymorphic} cost for a function parameter type that is or contains a polymorphic type variable.
     73\item \textbf{Safe} cost representing a widening conversion \eg @short@ to @int@, qualifier-adding conversions for pointer and reference types, and value conversion for enumeration constants.
     74\item \textbf{Variable} cost that counts the number of polymorphic variables, if any, introduced by the @forall@ clause in the function declaration.
     75\item \textbf{Specialization} cost counting the number of restrictions introduced by type assertions.
     76\end{enumerate}
     77The comparison of cost tuple is by lexicographical order, from unsafe (highest) to specialization (lowest), with ties moving to the next lowest item.
     78At a subexpression level, the lowest cost candidate for each result type is included as a possible interpretation of the expression;
     79at the top level, all possible interpretations of different types are considered (generating a total ordering) and the overall lowest cost is selected as the final interpretation of the expression.
     80Glen Ditchfield first proposed this costing model~\cite[\S~4.4.5]{Ditchfield92} to generate a resolution behaviour that is reasonable to C programmers based on existing conversions in the language.
     81This model carried over into the first implementation of the \CFA type-system by Richard Bilson~\cite[\S~2.2]{Bilson03}, and was extended but not redesigned by Aaron Moss~\cite[chap.~4]{Moss19}.
     82Moss's work began to show problems with the underlying costing model;
     83these design issues are part of this work.
     84
     85\begin{comment}
     86From: Richard Bilson <rcbilson@gmail.com>
     87Date: Tue, 10 Dec 2024 09:54:59 -0500
     88Subject: Re: cost model
     89To: "Peter A. Buhr" <pabuhr@fastmail.fm>
     90
     91I didn't invent it although I may have refined it somewhat. The idea of the
     92conversion cost is from Glen's thesis, see for instance page 90
     93
     94On Tue, Dec 10, 2024 at 9:35AM Peter A. Buhr <pabuhr@fastmail.fm> wrote:
     95> Cforall has a costing model based on an N-tuple using lexicographical ordering.
     96> (unsafe, polymorphic, safe, variable, specialization)
     97>
     98> Did you invent this costing model as a cheap and cheerful way to encode Glen's
     99> type system?
     100
     101From: Richard Bilson <rcbilson@gmail.com>
     102Date: Tue, 10 Dec 2024 17:04:26 -0500
     103Subject: Re: cost model
     104To: "Peter A. Buhr" <pabuhr@fastmail.fm>
     105
     106Yes, I think that's fair to say.
     107
     108On Tue, Dec 10, 2024 at 2:22PM Peter A. Buhr <pabuhr@fastmail.fm> wrote:
     109> Found it thanks. But Glen never implemented anything (small examples). So it
     110> feels like you took his conversion-cost idea and created an implementation table
     111> with the lexicographical comparison.
     112\end{comment}
     113
     114In many languages that support function/method overloading, such as \CC and Java, a partial-order system decides which interpretation of the expression gets selected.
     115Hence, sometimes the candidates are incomparable (none are considered a better match), and only when one candidate is considered better than all others (maximal with respect to the partial order) is the expression unambiguous.
     116Specifically, the resolution algorithms used in \CC and Java are greedy, as they select the best match for each sub-expression without considering the higher level ones, and therefore at each step of resolution, the arguments are already given unique interpretations, so the ordering only needs to consider comparing different sets of conversion targets (function parameter types) on the same set of input.
     117\begin{cfa}
     118@generate a C++ example here@
     119\end{cfa}
     120
     121In \CFA, trying to use such a system is problematic because of the presence of return-type overloading of functions and variable.
     122Specifically, \CFA expression resolution considers multiple interpretations of argument subexpressions with different types, \eg:
     123\begin{cfa}
     124@generate a CFA example here@
     125\end{cfa}
     126so it is possible that both the selected function and the set of arguments are different, and cannot be compared with a partial-ordering system.
     127This situation arises often in \CFA, even in the simple expression @f(x)@, where both the function name @f@ and variable name @x@ are overloaded.
    93128
    94129Ada is another programming language that has overloading based on return type.
    95130Although Ada also allows implicit type conversions of function arguments, it is fairly conservative on resolving ambiguities.
    96 There are only two "preference" rules in Ada overload resolution, one for primitive arithmetic operators and one for universal access types (analogous to void* in C); any other cases where an expression have multiple legal interpretations are considered ambiguous.
     131\begin{cfa}
     132@generate an Ada example here@
     133\end{cfa}
     134There are only two preference rules in Ada overload resolution, one for primitive arithmetic operators and one for universal access types (analogous to @void *@ in C);
     135any other cases where an expression has multiple legal interpretations are considered ambiguous.
    97136The current overload resolution system for \CFA is on the other end of the spectrum, as it tries to order every legal interpretations of an expression and chooses the best one according to cost, occasionally giving unexpected results.
    98137
     138Interestingly, the \CFA cost-based model can sometimes make expression resolution too permissive because it always attempts to select the lowest cost option, and only when there are multiple options tied at the lowest cost does it report the expression is ambiguous.
     139The reason is that there are so many elements in the cost tuple, the type system ``tries too hard to discover a match'', and therefore, ties are uncommon.
     140Other than the case of multiple exact matches, where all have cost zero, incomparable candidates under a partial ordering of being more specific can often have different expression costs since different kinds of implicit conversions are involved, resulting in seemingly arbitrary overload selections.
     141
    99142There are currently at least three different situations where the polymorphic cost element of the cost model does not yield a candidate selection that is clearly justifiable, and one of them is straight up wrong.
    100 Here are those three cases:
    101 \begin{enumerate}
    102 \item Polymorphic exact match versus non-polymorphic inexact match: consider the following declarations
    103 
    104 \begin{cfa}
    105 forall (T) void f (T); // 1
    106 void f (long); // 2
    107 
    108 f (42); // currently selects 2
    109 \end{cfa}
    110 Under the current cost model, option 1 incurs a polymorphic cost from matching the argument type \textbf{int} to type variable \textbf{T}, and option 2 incurs a safe cost from integer promotion of type \textbf{int} to \textbf{long}.
     143\begin{enumerate}[leftmargin=*]
     144\item Polymorphic exact match versus non-polymorphic inexact match.
     145\begin{cfa}
     146forall( T ) void f( T );                $\C[2.5in]{// 1}$
     147void f( long );                                 $\C{// 2}$
     148f( 42 );                                                $\C{// currently selects 2}\CRT$
     149\end{cfa}
     150Under the current cost model, option 1 incurs a polymorphic cost from matching the argument type @int@ to type variable @T@, and option 2 incurs a safe cost from integer promotion of type @int@ to @long@.
    111151Since polymorphic cost is ranked above safe conversion cost, option 2 is considered to have lower cost and gets selected.
    112152
    113 
    114 In contrast, the template deduction and overload resolution rules in \CC selects option 1 instead (converting forall to the equivalent function template declaration).
    115 \CC performs template argument deduction and overload candidate ranking in two separate steps: in the first step the type parameters are deduced for each primary function template, and if the corresponding template instantiation succeeds, the resulting function prototype is added to the resolution candidate set.
    116 In the second step the implicit conversions (if any) applied to argument types are compared after taking away top-level qualifiers and references, and it prefers an exact match, followed by basic type promotions (roughly corresponds to safe conversion in \CFA), and then other kinds of conversions (roughly corresponds to unsafe conversion in \CFA).
     153In contrast, the template deduction and overload resolution rules in \CC selects option 1 (converting @forall@ to the equivalent function template declaration).
     154\CC performs template argument deduction and overload candidate ranking in two separate steps.
     155\begin{itemize}
     156\item
     157In the first step, the type parameters are deduced for each primary function template, and if the corresponding template instantiation succeeds, the resulting function prototype is added to the resolution candidate set.
     158\item
     159In the second step, the implicit conversions (if any) applied to argument types are compared after taking away top-level qualifiers and references.
     160It then prefers an exact match, followed by basic type promotions (roughly corresponds to safe conversion in \CFA), and then other kinds of conversions (roughly corresponds to unsafe conversion in \CFA).
    117161Only when the type conversions are the same does it prioritize a non-template candidate.
    118 In this example, option 1 produces the prototype void f(int) which gives an exact match and therefore takes priority.
    119 The \CC resolution rules effectively makes option 2 a specialization that only applies to type \textbf{long} exactly,\footnote{\CC does have explicit template specializations, however they do not participate directly in overload resolution and can sometimes lead to unintuitive results.} while the current \CFA rules make option 2 apply for all integral types below \textbf{long}.
    120 Such a discrepancy could be explained as a design decision that since \CFA polymorphic functions are real entities and are separately compiled, calling them would require passing type information and thus have an actual runtime cost.
    121 
    122 \item Having a lower total polymorphic cost does not always mean a function is more specialized.
    123 The following example is taken from Aaron Moss's thesis, which discusses some improvements to the \CFA expression cost model, where he claims the following function prototypes are increasingly more constrained:
    124 
    125 \begin{cfa}
    126 forall(T, U) void f(T, U); //1, polymorphic
    127 forall(T) void f(T, T); //2, less polymorphic
    128 forall(T) void f(T, int); //3, even less polymorphic
    129 forall(T) void f(T*, int); //4, least polymorphic
    130 \end{cfa}
    131 
     162\end{itemize}
     163In this example, option 1 produces the prototype @void f( int )@, which gives an exact match and therefore takes priority.
     164The \CC resolution rules effectively makes option 2 a specialization that only applies to type @long@ exactly,\footnote{\CC does have explicit template specializations, however they do not participate directly in overload resolution and can sometimes lead to unintuitive results.} while the current \CFA rules make option 2 apply for all integral types below @long@.
     165This difference could be explained as compensating for \CFA polymorphic functions being separately compiled versus template inlining;
     166hence, calling them requires passing type information and assertions increasing the runtime cost.
     167\item
     168Having a lower total polymorphic cost does not always mean a function is more specialized.
     169The following example is from Moss's thesis, which discusses some improvements to the \CFA cost-model.
     170He claims the following function prototypes are increasingly more constrained:
     171\begin{cfa}
     172forall( T, U ) void f( T, U );  $\C[2.75in]{// 1 polymorphic}$
     173forall( T ) void f( T, T );             $\C{// 2 less polymorphic}$
     174forall( T ) void f( T, int );   $\C{// 3 even less polymorphic}$
     175forall( T ) void f( T *, int ); $\C{// 4 least polymorphic}\CRT$
     176\end{cfa}
    132177This argument is not entirely correct.
    133 Although it is true that both the sequence 1,2 and 1,3,4 are increasingly more constrained on the argument  types, the option 2 is not comparable to either of option 3 or 4; they actually describe independent constraints on the two arguments.
    134 In natural language, option 3 says that the second argument must have type \textbf{int}, while option 2 says that the two arguments must have the same type.
    135 These two constraints can independently be satisfied, therefore neither should be considered a better match when trying to resolve a call to f with argument types (int, int), and reporting such an expression as ambiguous is the most appropriate action.
    136 This is a limitation of using a numerical cost value as it cannot these logically complicated cases.
    137 
    138 \item Finally, the introduction of generic types means that it may require more type variables to describe a more specific type and that means simply counting the number of polymorphic type variables is no longer correct in general to order the function candidates as being more constrained.
    139 Suppose we have a generic pair type defined and writing a function that takes an arbitrary pair would require using two type variables
    140 \begin{cfa}
    141 forall (T,U) void f (pair(T,U)); // 1
    142 \end{cfa}
    143 and compare that with a function that takes any type at all:
    144 \begin{cfa}
    145 forall (T) void f (T); // 2
    146 \end{cfa}
    147 
    148 Passing a pair variable to f gives a cost of 1 poly, 2 variable for the pair overload, and a cost of 1 poly, 1 variable for the unconstrained overload.
    149 Clearly we would like the former to be chosen but the cost model cannot handle that correctly.
    150 
    151 \end{enumerate}
    152 
    153 These inconsistencies do not seem to be easily solvable and currently the \CFA codebase has to work around with these known defects.
    154 One potential path that could possibly be taken is a mix of the conversion cost and \CC-like partial ordering of specializations.
    155 Observe that in the \CFA cost tuple, the first three elements (unsafe, polymorphic and safe conversions) are related to the argument types, while the other elements (polymorphic variable and assertion counts) are properties of the function declarations independent of the arguments.
    156 This means it may be reasonable to have an ordering that compares the argument conversion costs first and uses the partial ordering of specializations as a tiebreaker.
    157 The algorithm used by \CC template specialization ordering can be applied for \CFA with some slight modifications.
    158 
     178Although it is true that both the sequence 1, 2 and 1, 3, 4 are increasingly more constrained on the argument types, option 2 is not comparable to either of option 3 or 4;
     179they actually describe independent constraints on the two arguments.
     180Specifically, option 2 says the two arguments must have the same type, while option 3 states the second argument must have type @int@,
     181Because two constraints can independently be satisfied, neither should be considered a better match when trying to resolve a call to @f@ with argument types @(int, int)@;
     182reporting such an expression as ambiguous is more appropriate.
     183The example illustrates the limitation of using a numerical cost value as it cannot represent these complicated cases.
     184\item
     185A generic type can require more type variables to describe a more specific type.
     186For example, a generic function taking a @pair@-type, requires two type variables.
     187\begin{cfa}
     188forall( T, U ) void f( pair( T, U ) ); $\C[2.75in]{// 1}$
     189\end{cfa}
     190Add a function taking any type.
     191\begin{cfa}
     192forall( T ) void f( T );                $\C{// 2}\CRT$
     193\end{cfa}
     194Passing a @pair@ variable to @f@ gives a cost of 1 poly, 2 variable for the @pair@ overload, versus a cost of 1 poly, 1 variable for the unconstrained overload.
     195Programmer expectation is to select the former, but the cost model selects the latter;
     196either could work.
     197As a result, simply counting the number of polymorphic type variables is no longer correct to order the function candidates as being more constrained.
     198\end{enumerate}
     199
     200These inconsistencies are not easily solvable in the current cost-model, meaning the currently \CFA codebase has to workaround these defects.
     201One potential solution is to mix the conversion cost and \CC-like partial ordering of specializations.
     202For example, observe that the first three elements (unsafe, polymorphic and safe conversions) in the \CFA cost-tuple are related to the argument/parameter types, while the other two elements (polymorphic variable and assertion counts) are properties of the function declaration.
     203Using this observation, it is reasonable to have an ordering that compares the argument/parameter conversion costs first and uses the partial ordering of specializations as a tiebreaker.
     204Hence, the \CC template-specialization ordering can be applied to \CFA with a slight modification.
    159205
    160206At the meantime, some other improvements have been made to the expression cost system.
     
    162208While implementing the change, there are also two detailed issues that need to be addressed for the new rules to fully work.
    163209
    164 The first one deals with an idiom commonly used in \CFA that would cause a lot of overloads to have equal costs.
    165 These kinds of expressions are so ubiquitous in \CFA code that we do not want them to be deemed ambiguous in the language.
    166 Many C library functions have multiple versions for different argument types, for example there are absolute value functions defined for basic arithmetic types with different names, since C does not support any kind of overloading:
    167 \begin{cfa}
    168 int abs (int);
    169 long labs (long);
    170 double fabs (double);
    171 float fabsf (float);
    172 \end{cfa}
    173 It is cumbersome for the programmers to remember all these different function names and select the correct ones, and even worse, if the incorrect version is picked, the program still compiles but with undesired conversions, which can sometimes even change the result, such as using the int version for floating point argument.
    174 In \CFA all of these functions are renamed to simply @abs@.
    175 This causes multiple overloads to have the same total cost when some conversion is needed.
    176 For example @long x = abs(42);@ could be either calling @long abs(long)@ with the argument 42 converted to @long@ or calling @int abs(int)@ and converting the result to @long@.
    177 In this example the choice could be arbitrary because both yields identical results.
    178 In some other cases, the choice can have an actual impact on the final result.
    179 While testing the effects of using the updated cost rule we found this piece of code in \CFA standard library:
    180 \begin{cfa}
    181 static inline __readyQ_avg_t __to_readyQ_avg(unsigned long long intsc) {
    182         if(unlikely(0 == intsc)) return 0.0;
    183         else return log2(intsc); // implicit conversion happens here
    184 } // __readyQ_avg_t is defined to be double
    185 \end{cfa}
    186 This is a helper function for performance logging that calculate the geometric mean of a counter value, and it does so by summing up the logarithm value of the counter.
    187 The function @log2@ is similarly overloaded in \CFA for both integer and floating point types, however in this case, the integer overload returns an integer, so the fractional part of logarithm is truncated.
    188 With the previous cost rules the integer version of @log2@ is selected, and when experimenting the updated cost rules this got picked up as an ambiguous expression at first.
    189 I reported this issue to the author of library code and got the reply that the expectation was that \CFA would choose the floating point overload, by the return type overloading selection.
    190 This mistake went unnoticed since it is only inside a performance logging function and does not serve any critical purposes, and the only effect it has caused is that the performance data becomes inaccurate as the fractional parts got truncated before the sum.
    191 Investigating this example leads to the decision that matching the return type higher up in the expression tree is prioritized, in case the total expression cost is equal.
    192 
    193 Another change addresses the issue that C arithmetic expressions have unique meanings governed by the arithmetic promotion rules, however in \CFA they are all modelled as function calls for overload resolution purposes.
    194 The previous, partially greedy resolution rules will pick the locally optimal match and it matches the C rules naturally.
    195 Care needs to be taken to maintain the C semantics when switching to the total expression cost approach.
    196 
    197 
    198 This problem is already partially recognized, when Aaron Moss suggested overload resolution by total cost, in the form of handling cast expressions.
    199 To quote directly the example:
    200 
    201 If a cast argument has an unambiguous interpretation as a conversion argument then it must be interpreted as such, even if the ascription interpretation would have a lower overall cost.
     210The first deals with a common idiom in \CFA that creates many overloads with equal cost.
     211Many C library functions have multiple versions for different argument types.
     212For example, the absolute-value function is defined for basic arithmetic types with different names, since C does not support overloading.
     213\begin{cquote}
     214\begin{tabular}{@{}lll@{}}
     215\begin{cfa}
     216int abs( int );
     217long labs( long );
     218long long int llabs( long long int );
     219\end{cfa}
     220&
     221\begin{cfa}
     222double fabs( double );
     223float fabsf( float );
     224long double fabsl( long double );
     225\end{cfa}
     226&
     227\begin{cfa}
     228cabs, cabsf, $and$ cabsl
     229$for$ _Complex
     230
     231\end{cfa}
     232\end{tabular}
     233\end{cquote}
     234It is cumbersome for programmers to remember these function names and select the correct one.
     235If an incorrect version is picked, the program compiles but with potential negative consequences such as using an integral version with a floating-point argument.
     236In \CFA, these functions are wrapped by functions with the overloaded name @abs@, which results in multiple overloads with the same total cost when some conversion is needed.
     237For example, @long x = abs( 42 )@ resolves to @long abs( long )@ with @int@ argument 42 converted to @long@ or @int abs( int )@ converting the result to @long@.
     238In this example, the choice could be arbitrary because both yield identical results.
     239However, for @int i = abs( -9223372036854775807LL )@, the result is @-1@, suggesting some kind of type error rather than silently generating a logically incorrect result.
     240There are multiple overload groupings of C functions into a single \CFA name, so usage should not report an ambiguity or warning unless there is a significant chance of error.
     241
     242While testing the effects of the updated cost rule, the following example was found in the \CFA standard library.
     243\begin{cfa}
     244static inline double __to_readyQ_avg( unsigned long long intsc ) {
     245        if ( unlikely( 0 == intsc ) ) return 0.0;
     246        else return log2( @intsc@ ); // implicit conversion happens here
     247}
     248\end{cfa}
     249This helper function is used for performance logging as part of computing a geometric;
     250it is called during summing of logarithmic values.
     251However, the function name @log2@ is overloaded in \CFA for both integer and floating point types.
     252In this case, the integer overload returns an integral result, truncating any small fractional part of the logarithm, so the sum is slightly incorrect.
     253When experimenting with the updated cost rules, it flagged the @log2@ call as an ambiguous expression.
     254When asked, the developer expected the floating-point overload because of return-type overloading.
     255This mistake went unnoticed because the truncated component was insignificant in the performance logging.
     256\PAB{Not sure I understand this: The conclusion is that matching the return type higher up in the expression tree is better, in cases where the total expression cost is equal.}
     257
     258Another change addresses the issue that C arithmetic expressions have unique meanings governed by its arithmetic conversion rules.
     259\begin{enumerate}[leftmargin=*,topsep=5pt,itemsep=4pt]
     260\item
     261First, if the corresponding real type of either operand is @long double@, the other operand is converted, without change of type domain, to a type whose corresponding real type is @long double@.
     262\item
     263Otherwise, if the corresponding real type of either operand is @double@, the other operand is converted, without change of type domain, to a type whose corresponding real type is @double@.
     264\item
     265Otherwise, if the corresponding real type of either operand is @float@, the other operand is converted, without change of type domain, to a type whose corresponding real type is @float@.\footnote{
     266For example, addition of a \lstinline{double _Complex} and a \lstinline{float} entails just the conversion of the \lstinline{float} operand to \lstinline{double} (and yields a \lstinline{double _Complex} result).}
     267\item
     268Otherwise, the integer promotions are performed on both operands.
     269Then the following rules are applied to the promoted operands:
     270\begin{enumerate}[topsep=5pt,itemsep=4pt]
     271\item
     272If both operands have the same type, then no further conversion is needed.
     273\item
     274Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
     275\item
     276\label{p:SignedToUnsignedSafe}
     277Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
     278\item
     279\label{p:UnsignedToSignedUnsafe}
     280Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
     281\item
     282\label{p:Miscellaneous}
     283Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
     284
     285\hfill\cite[\S~6.3.1.8]{C11}
     286\end{enumerate}
     287\end{enumerate}
     288\VRef[Figure]{f:CExpressionConversions} shows the C arithmetic conversions graphically.
     289\VRef[Rule]{p:SignedToUnsignedSafe} says an unsigned type is safely convertible to an signed type with greater rank, while \VRef[rule]{p:UnsignedToSignedUnsafe} says a signed type is unsafely convertible to an unsigned type.
     290Therefore, these two rules cover every possible case.
     291\VRef[Rule]{p:Miscellaneous} is the catch-all to accommodate any kinds of exotic integral representations.
     292These conversions are applied greedily at local points within an expression.
     293Because there is no overloading in C, except for builtin operators, no cost model is needed to differentiate among alternatives that could result in ambiguous matches.
     294
     295\begin{figure}
     296\input{C_expression_conversion.pstex_t}
     297\caption{C Expression Conversions: T1 operator T2}
     298\label{f:CExpressionConversions}
     299
     300\smallskip
     301\input{CFA_curr_arithmetic_conversion.pstex_t}
     302\caption{\CFA Total-Ordering Expression Conversions}
     303\label{f:CFACurrArithmeticConversions}
     304
     305\smallskip
     306\input{CFA_arithmetic_conversion.pstex_t}
     307\caption{\CFA Partial-Ordering Expression Conversions}
     308\label{f:CFAArithmeticConversions}
     309\end{figure}
     310
     311\VRef[Figure]{f:CFACurrArithmeticConversions} shows the current \CFA total-order arithmetic-conversions graphically.
     312Here, the unsafe cost of signed to unsigned is factored into the ranking, so the safe conversion is selected over an unsafe one.
     313Furthermore, an integral option is taken before considering a floating option.
     314This model locally matches the C approach, but provides an ordering when there are many overloaded alternative.
     315However, as Moss pointed out overload resolution by total cost has problems, \eg handling cast expressions.
     316\begin{cquote}
     317\ldots if a cast argument has an unambiguous interpretation as a conversion argument then it must be interpreted as such, even if the ascription interpretation would have a lower overall cost.
    202318This is demonstrated in the following example, adapted from the C standard library:
    203319\begin{cfa}
    204320unsigned long long x;
    205 (unsigned)(x >> 32);
    206 \end{cfa}
     321(unsigned)( x >> 32 );
     322\end{cfa}
     323\vspace{5pt}
    207324In C semantics, this example is unambiguously upcasting 32 to @unsigned long long@, performing the shift, then downcasting the result to @unsigned@, at cost (1, 0, 3, 1, 0, 0, 0).
    208 If ascription were allowed to be a first-class interpretation of a cast expression, it would be cheaper to select the @unsigned@ interpretation of @?>>?@ by downcasting x to @unsigned@ and upcasting 32 to @unsigned@, at a total cost of (1, 0, 1, 1, 0, 0, 0).
    209 However, this break from C semantics is not backwards compatible, so to maintain C compatibility, the \CFA resolver selects the lowest-cost interpretation of the cast argument for which a conversion or coercion to the target type exists (upcasting to @unsigned long long@ in the example above, due to the lack of unsafe downcasts), using the cost of the conversion itself only as a tie-breaker.
    210 
     325If ascription were allowed to be a first-class interpretation of a cast expression, it would be cheaper to select the @unsigned@ interpretation of @?>>?@ by downcasting @x@ to @unsigned@ and upcasting 32 to @unsigned@, at a total cost of (1, 0, 1, 1, 0, 0, 0).
     326\PAB{This example feels incorrect. Assume a word size of 4 bits (long long). Given the value 1001, shift it >> 2, gives 10, which fits in a half word (int). Now truncate 1001 to a halfword 01 and shift it >> 2, gives 00. So the lower-cost alternative does generate the correct result. Basically truncation is an unsafe operation and hence has a huge negative cost.}
     327However, this break from C semantics is not backwards compatible, so to maintain C compatibility, the \CFA resolver selects the lowest-cost interpretation of the cast argument for which a conversion or coercion to the target type exists (upcasting to @unsigned long long@ in the example above, due to the lack of unsafe downcasts), using the cost of the conversion itself only as a tie-breaker.\cite[pp.~46-47]{Moss19}
     328\end{cquote}
    211329However, a cast expression is not necessary to have such inconsistency to C semantics.
    212 With any implicit argument-parameter type conversion in function calls we can replicate this issue without an explicit cast.
    213 For example
     330An implicit argument-parameter type conversion in a function calls can replicate this issue without an explicit cast.
    214331\begin{cfa}
    215332unsigned long long x;
    216 void f (unsigned);
    217 f (x >> 32);
    218 \end{cfa}
    219 This has the same effect as using an explicit cast to coerce the type of expression @x >> 32@ to @unsigned@.
    220 This shows that fundamentally the problem is not coming from the cast expressions, but modelling the C built-in operators as overloaded functions.
    221 A different rule is enforced in selecting the built-in function candidates to fix this problem.
    222 If an expression has any legal interpretations as a C built-in operation, only the lowest cost one is kept, regardless of the result types.
     333void f( unsigned );
     334f( x >> 32 );
     335\end{cfa}
     336The argument generation has the same effect as using an explicit cast to coerce the type of expression @x >> 32@ to @unsigned@.
     337This example shows the problem is not coming from the cast expressions, but from modelling the C builtin operators as overloaded functions.
     338As a result, a different rule is used to select the builtin function candidates to fix this problem:
     339if an expression has any legal interpretations as a C builtin operation, only the lowest cost one is kept, regardless of the result type.
     340
     341\VRef[Figure]{f:CFAArithmeticConversions} shows an alternative \CFA partial-order arithmetic-conversions graphically.
     342The idea here is to first look for the best integral alternative because integral calculations are exact and cheap.
     343If no integral solution is found, than there are different rules to select among floating-point alternatives.
     344This approach reduces the search space by partitioning: only look at integral operations, and then only look at float-point operations.
    223345
    224346
     
    227349Type unification is the algorithm that assigns values to each (free) type parameters such that the types of the provided arguments and function parameters match.
    228350
    229 
    230 \CFA does not attempt to do any type \textit{inference}: it has no anonymous functions (i.e.
    231 lambdas, commonly found in functional programming and also used in \CC and Java), and the variable types must all be explicitly defined (no auto typing).
    232 This makes the unification problem more tractable in \CFA as the argument types at each call site are usually all specified.
    233 There is a single exception case, which happens when the function return type contains a free type variable that does not occur in any of the argument types, and subsequently passed into the parent expression.
    234 A top level expression whose type still contains an unbounded type variable is considered ill-formed as such expression is inherently ambiguous.
     351\CFA does not attempt to do any type \textit{inference} \see{\VRef{s:IntoTypeInferencing}}: it has no anonymous functions (\ie lambdas, commonly found in functional programming and also used in \CC and Java), and the variable types must all be explicitly defined (no auto typing).
     352This restriction makes the unification problem more tractable in \CFA, as the argument types at each call site are usually all specified.
     353There is a single exception case when the function return type contains a free type variable that does not occur in any of the argument types, and subsequently passed into the parent expression.
     354\begin{cfa}
     355example ... does malloc work here
     356\end{cfa}
     357A top level expression whose type still contains an unbounded type variable is considered ill-formed as such an expression is inherently ambiguous.
    235358
    236359The unification algorithm in \CFA is originally presented in Richard Bilson's thesis and it has remained as the basis of the algorithm currently in use.
     
    238361To summarize, the \CFA type unification has two minor variants: an \textit{exact} mode and an \textit{inexact} mode.
    239362The inexact mode is applied at top level argument-parameter matching, and attempts to find an assignment to the type variables such that the argument types can be converted to parameter types with minimal cost as defined in the previous section.
    240 The exact mode is required since the type matching algorithm operates recursively and the inner types often have to match exactly, for example there is no common type for the pointer types \textbf{int*} and \textbf{long*} while there is for \textbf{int} and \textbf{long}.
     363The exact mode is required since the type matching algorithm operates recursively and the inner types often have to match exactly, for example there is no common type for the pointer types \textbf{int*} and \textbf{long*} while there is for @int@ and @long@.
    241364With the introduction of generic record types, the parameters must match exactly as well; currently there are no covariance or contravariance supported for the generics.
    242365
     
    252375On each invocation the types to be operate on can be determined from the arguments provided, and therefore there should not be a need to pass a polymorphic function pointer, which can take any type in principle.
    253376
    254 For example, consider a polymorphic function that takes one argument of type \textbf{T} and another pointer to a subroutine
     377For example, consider a polymorphic function that takes one argument of type @T@ and another pointer to a subroutine
    255378\begin{cfa}
    256379forall (T) void f (T x, forall (U) void (*g) (U));
    257380\end{cfa}
    258 Making \textbf{g} polymorphic in this context would almost certainly be unnecessary, since it can only be called inside the body of \textbf{f} and the types of the argument would have been known anyways, although it can potentially depend on \textbf{T}.
    259 Moreover, requesting a function parameter to be able to potentially work on any input type at all would always impose too much constraint on the arguments, as it only needs to make each calls inside the body of \textbf{f} valid.
     381Making @g@ polymorphic in this context would almost certainly be unnecessary, since it can only be called inside the body of @f@ and the types of the argument would have been known anyways, although it can potentially depend on @T@.
     382Moreover, requesting a function parameter to be able to potentially work on any input type at all would always impose too much constraint on the arguments, as it only needs to make each calls inside the body of @f@ valid.
    260383
    261384Rewriting the prototype to
     
    263386forall (T) void f (T x, void (*g) (T));
    264387\end{cfa}
    265 will be sufficient (or potentially, some compound type synthesized from \textbf{T}), in which case \textbf{g} is no longer a polymorphic type on itself.
    266 The "monomorphization" conversion is readily supported in \CFA, either by explicitly assigning a polymorphic function name to a compatible function pointer type, or implicitly done in deducing assertion parameters (which will be discussed in the next section).
     388will be sufficient (or potentially, some compound type synthesized from @T@), in which case @g@ is no longer a polymorphic type on itself.
     389The ``monomorphization'' conversion is readily supported in \CFA, either by explicitly assigning a polymorphic function name to a compatible function pointer type, or implicitly done in deducing assertion parameters (which will be discussed in the next section).
    267390Such technique can be directly applied to argument passing, which is essentially just assignment to function parameter variables.
    268 There might be some edge cases where the supplied subroutine \textbf{g} is called on arguments of different types inside the body of \textbf{f} and so declared as polymorphic, but such use case is rare and the benefit of allowing such constructs seems to be minimal in practice.
    269 
    270 
    271 The result of this change is that the unification algorithm no longer needs to distinguish "open" and "closed" type variables, as the latter is not allowed to exist.
    272 The only type variables that need to be handled are those introduced by the \textbf{forall} clause from the function prototype.
     391There might be some edge cases where the supplied subroutine @g@ is called on arguments of different types inside the body of @f@ and so declared as polymorphic, but such use case is rare and the benefit of allowing such constructs seems to be minimal in practice.
     392
     393The result of this change is that the unification algorithm no longer needs to distinguish open and closed type-variables, as the latter is not allowed to exist.
     394The only type variables that need to be handled are those introduced by the @forall@ clause from the function prototype.
    273395The subtype relationship between function types is now also rendered redundant since none of the function parameter or return types can be polymorphic, and no basic types or non-polymorphic function types are subtypes of any other type.
    274396Therefore the goal of (exact) type unification now simply becomes finding a substitution that produces identical types.
     
    336458Suppose an unbound type variable @T@ appears in two assertions, one can be uniquely resolved and allow the type @T@ to be inferred immediately, and another has many ways to be resolved, each results in @T@ being bound to a different concrete type.
    337459If the first assertion is examined first by the algorithm, the deducted type can then be utilized in resolving the second assertion and eliminate many incorrect options without producing the list of candidates pending further checks.
    338 In practice, this have a significant impact when an unbound type @T@ is declared to satisfy the basic "object assertions"\footnote{The term is borrowed from object-oriented languages although \CFA is not object-oriented in principle.} of having a default constructor, destructor, and copy assignment operations.
     460In practice, this have a significant impact when an unbound type @T@ is declared to satisfy the basic \emph{object assertions}\footnote{The term is borrowed from object-oriented languages although \CFA is not object-oriented in principle.} of having a default constructor, destructor, and copy assignment operations.
    339461Since they are defined for every type currently in scope, there are often hundreds or even thousands of matches to these functions with an unspecified operand type, and in most of the cases the value of @T@ can be deduced by resolving another assertion first, which then allows specific object lifetime functions to be looked up since they are sorted internally by the operand type, and greatly reduces the number of wasted resolution attempts.
    340462
  • TabularUnified doc/theses/fangren_yu_MMath/intro.tex

    rb0708ea r522f71c  
    165165
    166166\section{Type Inferencing}
     167\label{s:IntoTypeInferencing}
    167168
    168169Every variable has a type, but association between them can occur in different ways:
Note: See TracChangeset for help on using the changeset viewer.