public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Question about implicit template instantiation in C++ modules.
       [not found] <CA+wTq=X=+J==vmP7h-XGBPfdHEhNqBRbTQJrYU_MTeAyHXuZ_A@mail.gmail.com>
@ 2020-05-25 14:35 ` Nathan Sidwell
  2020-05-26  6:39   ` Takafumi Kubota
  0 siblings, 1 reply; 2+ messages in thread
From: Nathan Sidwell @ 2020-05-25 14:35 UTC (permalink / raw)
  To: Kubota Takafumi, gcc-help

[redirected to gcc-help]

On 5/25/20 12:27 AM, Kubota Takafumi wrote:
> Hi all,
> 
> I am studying C++ modules and, especially, trying to understand how implicitly 
> instantiated template specializations are exported.

Thanks for trying things out!

> I encounter an example where GCC and Clang behave differently.
> 
> For example:
> 
> |/* mod1.cpp */ export module mod1; export { // function template definition 
> template <typename T> T tmpl (T t) { return t; } // explicit instantiation of 
> "char tmpl<char>" template char tmpl (char t); // implicit instantiation of "int 
> tmpl<int>" in exported section int f(int i) { return tmpl(i); } } // implicit 
> instantiation of "double tmpl<double>" in not-exported section double g(double 
> d) { return tmpl(d); } /* main1.cpp */ import mod1; int main() { tmpl('c'); // 
> #1 tmpl(1); // #2 tmpl(1.0); // #3 return 0; } |
> 
> Here, GCC reuses the explicitly instantiated specialization from mod1 at #1.
> 
> But, it implicitly re-instantiates at #2 and #3.
> 
> On the other hand, Clang reuses all specializations from module mod1 for each point.

Neither compiler is wrong.   It is an implementation choice.  All such implicit 
instantiations, regardless of origin are required to be ODR-same.  I chose not 
to emit implicit instantiations, unless they are referenced by something else in 
the emitted graph. (it is not at all obvious which strategy is better)

> So, I read the sections about modules and template instantiations in the working 
> draft of the technical specification [0][1][2].
> 
> However, as far as my understanding, there is no explanation about how 
> implicitly instantiated template specializations are exported.

Instantiations (explicit, implicit or partial) are not exported.  Only named 
entities may be exported. (using the language's definition of 'export', not the 
closely related implementation concept of 'emitted into a Compiled Module 
Interface'.

> According to the comment of gcc/cp/module.c, there seem to be two design choices 
> for exporting implicit specializations: re-instantiating them on-demand (GCC's 
> style) and streaming them (Clang's style) in modules.
> 
> So my questions are:
> 
> *Does how implicit specializations are exported depend on compiler implementations?*

Correct.

> if not,
> 
> *Does anyone know the expected behavior based on the technical specification?*

The std requires that a well formed program cannot tell the difference :)

> 
> Best Regards,
> 
> Takafumi.
> 
> [0] 
> <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf
> 
> [1] <http://eel.is/c++draft/>http://eel.is/c++draft/
> 
> [2] 
> <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1103r3.pdf>http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1103r3.pdf
> 
> 
> -- 
> Takafumi Kubota
> Ph.D. student at Keio University
> http://tk1012.github.io/


-- 
Nathan Sidwell

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Question about implicit template instantiation in C++ modules.
  2020-05-25 14:35 ` Question about implicit template instantiation in C++ modules Nathan Sidwell
@ 2020-05-26  6:39   ` Takafumi Kubota
  0 siblings, 0 replies; 2+ messages in thread
From: Takafumi Kubota @ 2020-05-26  6:39 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: gcc-help

Thank you for the clarification!

Takafumi

2020年5月25日(月) 23:35 Nathan Sidwell <nathan@acm.org>:

> [redirected to gcc-help]
>
> On 5/25/20 12:27 AM, Kubota Takafumi wrote:
> > Hi all,
> >
> > I am studying C++ modules and, especially, trying to understand how
> implicitly
> > instantiated template specializations are exported.
>
> Thanks for trying things out!
>
> > I encounter an example where GCC and Clang behave differently.
> >
> > For example:
> >
> > |/* mod1.cpp */ export module mod1; export { // function template
> definition
> > template <typename T> T tmpl (T t) { return t; } // explicit
> instantiation of
> > "char tmpl<char>" template char tmpl (char t); // implicit instantiation
> of "int
> > tmpl<int>" in exported section int f(int i) { return tmpl(i); } } //
> implicit
> > instantiation of "double tmpl<double>" in not-exported section double
> g(double
> > d) { return tmpl(d); } /* main1.cpp */ import mod1; int main() {
> tmpl('c'); //
> > #1 tmpl(1); // #2 tmpl(1.0); // #3 return 0; } |
> >
> > Here, GCC reuses the explicitly instantiated specialization from mod1 at
> #1.
> >
> > But, it implicitly re-instantiates at #2 and #3.
> >
> > On the other hand, Clang reuses all specializations from module mod1 for
> each point.
>
> Neither compiler is wrong.   It is an implementation choice.  All such
> implicit
> instantiations, regardless of origin are required to be ODR-same.  I chose
> not
> to emit implicit instantiations, unless they are referenced by something
> else in
> the emitted graph. (it is not at all obvious which strategy is better)
>
> > So, I read the sections about modules and template instantiations in the
> working
> > draft of the technical specification [0][1][2].
> >
> > However, as far as my understanding, there is no explanation about how
> > implicitly instantiated template specializations are exported.
>
> Instantiations (explicit, implicit or partial) are not exported.  Only
> named
> entities may be exported. (using the language's definition of 'export',
> not the
> closely related implementation concept of 'emitted into a Compiled Module
> Interface'.
>
> > According to the comment of gcc/cp/module.c, there seem to be two design
> choices
> > for exporting implicit specializations: re-instantiating them on-demand
> (GCC's
> > style) and streaming them (Clang's style) in modules.
> >
> > So my questions are:
> >
> > *Does how implicit specializations are exported depend on compiler
> implementations?*
>
> Correct.
>
> > if not,
> >
> > *Does anyone know the expected behavior based on the technical
> specification?*
>
> The std requires that a well formed program cannot tell the difference :)
>
> >
> > Best Regards,
> >
> > Takafumi.
> >
> > [0]
> > <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/n4849.pdf
> >
> > [1] <http://eel.is/c++draft/>http://eel.is/c++draft/
> >
> > [2]
> > <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1103r3.pdf>
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1103r3.pdf
> >
> >
> > --
> > Takafumi Kubota
> > Ph.D. student at Keio University
> > http://tk1012.github.io/
>
>
> --
> Nathan Sidwell
>


-- 
Takafumi Kubota
Ph.D. student at Keio University
http://tk1012.github.io/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-05-26  6:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CA+wTq=X=+J==vmP7h-XGBPfdHEhNqBRbTQJrYU_MTeAyHXuZ_A@mail.gmail.com>
2020-05-25 14:35 ` Question about implicit template instantiation in C++ modules Nathan Sidwell
2020-05-26  6:39   ` Takafumi Kubota

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).