Clang Template Specialization Resugaring

GSoC 2022

Roadmap Part 2

Matheus Izvekov <mizvekov@gmail.com>

Accomplishments

We submitted the RFC at https://discourse.llvm.org/t/rfc-improving-diagnostics-with-template-specialization-resugaring/64294.

  • We have positive feedback, people want to see this implemented!
  • We got one extra volunteer for reviewing.
  • We got feedback that this work might influence debug info.
  • We linked to the WIP patch in phabricator.
    However, the patch is too big and we must work on splitting it up.

Splitting the patch

The main patch of this effort is submitted to phab, can be found at https://reviews.llvm.org/D127695.
There are several things going on at the same time there though, and we can split it up to facilitate review:

  • There is a part where we change the AST representation of type substitution.
    This has already been split up, submitted to https://reviews.llvm.org/D131858
    Already in review, and there are some changes needed to ASTImporter.
  • In another part, we change CheckTemplateArguments to produce an additional sugared converted template argument list, in addition to the canonical one it already did. We then modify TemplateSpecializationType to carry this new list, in addition to the specified template arguments it already did.
  • In another part, we modify TypedefTypes and UsingTypes to be able to carry an underlying type different from the declaration. This is a fairly small change, but we still need to implement uniquing for TypedefTypes.

Other pre-requisite patches

  • We got the ElaboratedType changes merged.
  • Not much progress on the patches which implement merging of type sugar. New reviewer started looking at it, but will probably still need Richard to finish it.
  • One of the bug fix patches got approved and merged.

Performance

We need to start tracking performance changes caused by the main patch and some of the pre-requisite patches.
This will inform us of the need for switch to turn on / off, doing the resugaring eagerly vs lazily, and other
configurable limitations that we might need to introduce.

  • One reviewer asked for feedback on performance impact of D131858. I expect it to be fairly minor, but we can take a look.
  • The changes to CheckTemplateArguments might have bigger impact, will need to monitor that once we split it up.

Performance

The main resugaring patch has a fairly efficient implementation, but there are some minor tweaks needed:

  • Doing it in all language modes. Good for testing, but we have to limit this somewhere, hopefully at the driver.
  • There are some fairly simple changes needed to detect that the current naming context won't produce any results for resugaring, such as being empty or having only canonical arguments.
  • We should implement uniquing of TemplateSpecializationTypes to avoid rebuilding them unnecessarily.

Performance

  • We should implement some caching such that we don't have to repeat work that we already performed.
    Some upstream tests even present worst cases here which will make this mandatory.
  • We can create a new dependency, substitution dependency, to avoid recursing into types which don't contain any substitution sugar nodes.
  • Possible minor tweak to the TreeTransform to eliminate work on TypeLocs when we don't need them.

Bugs and missing functionality

  • Resugaring is not working well in libstdc++, but does in libc++.
  • We still can't resugar over not-specified, default template arguments. This is likely one of the issues that affects libstdc++.
  • D131858 breaks building libc++, we have to investigate but is likely related to a missing change to the ASTIMporter.
  • One of the reviwers pointed out that we are not resugaring over chained member access expressions. This is likely a fairly easy to implement improvement.

The End