[clang] [clang-tools-extra] [llvm] [Clang] Add __builtin_type_pack_dedup template to deduplicate types in template arguments (PR #106730)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 18 02:09:40 PDT 2024


================
@@ -3158,6 +3161,33 @@ checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD,
     int64_t N = Index.getExtValue();
     return Ts.getPackAsArray()[N].getAsType();
   }
+  case BTK__type_pack_dedup: {
+    assert(Converted.size() == 2 && "__builtin_type_pack_dedup should be given "
+                                    "a template and a parameter pack");
+    TemplateArgument Template = Converted[0];
+    TemplateArgument Ts = Converted[1];
+    if (Template.isDependent() || Ts.isDependent())
----------------
ilya-biryukov wrote:

We could remove the dependency on the template, but I don't think we can ignore the dependency inside the types themselves.
Consider
```cpp
template <class T, class U, template <class...> class TL = TypeList>
struct Foo {
  // 1. the substitution has to be postponed until T and U are known.
  using result = __dedup<TypeList, T, U>;
  // 2. this is already computed right away.
  using result2 = __dedup<TypeList, int, int>; 
  // 3. this **could** be computed right away, but it is not now.
  using result3 = __dedup<TL, int, int>
};

static_assert(__is_same(TypeList<int>, Foo<int, int>::result));
static_assert(__is_same(TypeList<int, doubel>, Foo<int, double>::result));
```

- Is there something I'm missing that would allow (1) to be substituted earlier?
- Do you feel strongly that we need to do (3) ?

> Be aware that holding this specialization will make this builtin appear in mangling, so I'd avoid doing that unless necessary.

I do not think we can avoid this completely (see above) and it's not unprecedented: other builtin templates can already appear in mangling too when the arguments are dependent. So I do think it's necessary and I'm not sure which problems it carries.

https://github.com/llvm/llvm-project/pull/106730


More information about the cfe-commits mailing list