Translate DRuntime Hooks to Templates
Mentor: Teodor Duțu
Spec | Details |
---|---|
Difficulty Level | hard |
Project Duration | 350 hours |
Number of Contributors | 2 |
Prerequisites | Familiarity with compilers, build systems; low-level programming |
Description
High-level language constructs are often compiled into lower-level function calls that implement the same functionality. This process is called lowering It simplifies compiler design by offloading complex code into simpler function calls. These functions are implemented in D’s runtime library (called DRuntime) and are commonly called runtime hooks.
Below is an example of such a runtime hook:
struct S { ... }
S[3] a, b;
// Original code
b = a; // b is a copy of a
// Resulting DRuntime hook
_d_arrayassign_l(b, a) // copies a into b
_d_arrayassign_l
handles array (re)allocation and assignment operators according to the types of a
and b
(S[3]
) to simplify the compiler’s work.
As shown in the example above, runtime hooks require type information, such as which assignment operator to call and the arrays’ sizes. D supports templates, but runtime hooks pre-date the introduction of templates to D. Therefore, they retrieve the necessary type information at runtime by receiving an extra argument, whose type is TypeInfo
. This object contains the size, constructors, destructors and overloaded operators of a given type. However, because this information is processed at run time, this approach is slower than the alternative of having the compiler send it to the runtime hook via template arguments. Due to D’s high flexibility regarding metaprogramming, translating each hook to a template function would allow its code to specialise according to the types of its arguments at compile time.
In general, these are the steps required to convert a runtime hook to a template:
- Implement a new template version of the hook in DRuntime.
- Change the lowering in the compiler to use the new hook.
- Run a benchmark to measure the increase in performance generated by using the new hook.
- Remove the old hook from DRuntime.
The hooks that are yet to be templated can be split into 2 categories:
rt/aa.d
implements associative arrays as language builtins. This module is made up of multiple hooks, all of them usingTypeInfo
. Therefore, this module is to be reimplemented using templates. Associative arrays are defined as an opaque structure calledImpl
. It receives aTypeInfo
argument in its constructor. This structure is accessed via the runtime hooks listed below. The plan for this direction is the following:- Template the hooks below and temporarily extract the
TypeInfo
structure required byImpl
from the template arguments usingtypeid
. Each hook will require changes to DRuntime and the compiler. - Template the
Impl
structure and modify the previously templated hooks to use the template arguments itself instead ofTypeInfo
.
The list of hooks for associative arrays is:
_aaApply _aaApply2 _aaDelX _aaEqual _aaGetRvalueX _aaGetX _aaInX _aaLen _d_assocarrayliteralTX
- Template the hooks below and temporarily extract the
-
Somewhat more independent hooks, which still have interdependencies. They are mostly implemented in
rt/lifetime.d
. A goal of this project is to remove this file and replace all its code with templated implementations. Each hook can be handled individually and separately. There was previous work on_d_arrayliteralTX
so it might be a good starting point. Another promising starting point are_d_arrayset{capacity,lengthT,lengthiT}
or_d_arrayappendcTX
. The latter three already have wrapper template hooks that call the functions fromrt/lifetime.d
. What is needed in their case is to fully move the underlying implementation to the template hooks. The full list of hooks is below:_d_arraysetcapacity _d_arraysetlengthiT _d_arraysetlengthT _d_arrayshrinkfit _d_arrayappendcTX _d_arrayliteralTX _d_interface_cast _d_isbaseof _d_isbaseof2 _adEq2
Resources
- Initial project proposal and discussion
- PRs converting some of the DRuntime hooks to templates
- Weekly reports regarding the earlier work
- DConf presentations from 2022 and 2024 on the work on DRuntime hooks
- Instructions on how to build the reference compiler - DMD