Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ interoperability goals #175

Merged
merged 32 commits into from
Nov 25, 2020
Merged

C++ interoperability goals #175

merged 32 commits into from
Nov 25, 2020

Conversation

jonmeow
Copy link
Contributor

@jonmeow jonmeow commented Oct 15, 2020

@jonmeow jonmeow added WIP proposal A proposal labels Oct 15, 2020
@google-cla google-cla bot added the cla: yes PR meets CLA requirements according to bot. label Oct 15, 2020
proposals/p0175.md Outdated Show resolved Hide resolved
@jonmeow jonmeow added proposal rfc Proposal with request-for-comment sent out and removed WIP labels Oct 21, 2020
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
ability to understand code rewrites. It should be reasonable to have these
instead rewritten to use Carbon's metaprogramming model.

## Open questions to be resolved later
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might mention common language extensions here as well. They seem like something that could go through a case-by-case cost-benefit analysis like you suggest for CRTP.

Attributes seem like a good example.

I imagine that Carbon will have some way of expressing that a function doesn't return, or the type of a function that does not return. Clang knows about several such ways to express this in C and C++. And being able to pass a Carbon no-return function to a C++ function with a _Noreturn or [[noreturn]] parameter could be worthwhile. Similarly, non-null C++ attributes may be worth the effort.

Whereas attributes for Renderscript or Objective-C garbage collection may not be, and not be something Carbon provides any support for, let alone interoperability.

Interactions with the linker (specifying sections and so on) and inline assembly may fall in between. Carbon may need to provide functionality here, but maybe not interoperability with C++.

My point isn't so much the specific examples (though they are of general interest to the kind of programming I'm representing), but more that I'd suggest that this section is not complete, and not intended to be complete, and suggest that you leave room for evaluating particular extensions in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that "most idiomatic usage of advanced C++ features" could be considered to cover this. I've added attributes explicitly there. Enumerating specific attributes is definitely too detailed for a "goals" doc.

both Carbon and C/C++. Pragmatically, Carbon will likely be the limiting factor
here.

#### Interoperability with and migration from existing C++ code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be overly pedantic or covered in some other context I missed; sorry if so.

Sometimes this document says C/C++; sometimes it says C++.

In particular, right here it says "C++ migration". I wanted to know if this means really only migration from C++17, or if migration from C is something you are planning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes this document says C/C++; sometimes it says C++.

That's a little deliberate in this doc: I've tried to be a little precise to avoid bikesheds of "oh, you plan C++ interop, but are ignoring C". I try to talk about C++ without C when talking about C++-specific features, like templates.

In particular, right here it says "C++ migration".

Please note, this is the phrasing of the language goal, not specific to this proposal.

I wanted to know if this means really only migration from C++17, or if migration from C is something you are planning.

Rephrased the first sentence to specify C++17.

should be provided where possible. However, native Carbon code will be easier to
make safe than Carbon code that invokes C/C++. C/C++ code will not benefit from
the same set of safety mechanisms that Carbon offers. As a result, safety
guarantees will be weaker around interoperability layers.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/will be/may be/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer stronger phrasing, and I'm wondering if you're misunderstanding why "will" is appropriate -- I've elaborated a little to try to improve this point without softening it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may for some parts of safety and may not for others, so the stronger phrasing feels misleading

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make sure we're discussing the same text, the phrasing now is:
C/C++ code will not benefit from the same set of safety
mechanisms that Carbon offers, so Carbon code calling into C/C++ will accept
higher safety risks.

Are you saying that you feel this is still misleading? I think "same set" should address your concern regarding "some parts", shouldn't it?


**Incomplete types** must have a mapping with similar semantics, similar to
primitive types.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should state that the Carbon memory model for concurrency will also be compatible with C++ (possibly excepting memory_order_consume). And that a release in Carbon will establish a happens before relationship with an acquire in C++ and vice versa

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be part of the goals? It's important, yes, but is this detail really something for the high-level goals, versus a design about the memory model?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are enumerating a bunch of "unsurprising mappings", it seems to make sense that this would be a good place to call out both the important of memory model and the fact that it will be unsurprising.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I think having a goal around the fundamentals of the memory model makes a lot of sense. It is a question that has come up in multiple introductions to Carbon I've done so far...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a "Compatibility with the C/C++ memory model" goal under "mixing toolchains", because it feels like a better fit there.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition there. Flows well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


- An interface which can hide the difference between whether `std::vector<T>`
or the equivalent, idiomatic Carbon type is in use may also be offered,
although this could end up similarly adding performance overhead.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that a wrapper type + specialization would be sufficient to get nearly zero overhead access to std::vector<T> as an idiomatic carbon type. If we cannot provide this for unordered_map, string, and vector I would be quite worried about being able to incrementally migrate to idiomatic carbon.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, we should aim to make generics efficient (from a runtime perspective, with some potential compile-time cost) for this use case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rephrased to avoid a performance argument. I'm not convinced that you can guarantee zero-overhead in every possible case with generics wrapping C++ types vs native idiomatic Carbon, but I don't think it's important here.

To be clear though, "nearly zero" and "zero" are important to differentiate if we're positioning Carbon as a performance-first language. If it's not zero, then we should anticipate that somebody will ask for a better option.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by "nearly zero" I think of the level of overhead that unique_ptr introduces. Not zero, but close enough to it that most everyone is satisfied most of the time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged, with my noted concern -- actually captured under "Performance-critical software" already. However, I don't think that affects this section as written.

proposals/p0175.md Outdated Show resolved Hide resolved
#### Support use of advanced C++ features

There should be support for most idiomatic usage of advanced C++ features. For
example, templates, overload sets, and ADL.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a sentence.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"A few examples are"


- An interface which can hide the difference between whether `std::vector<T>`
or the equivalent, idiomatic Carbon type is in use may also be offered,
although this could end up similarly adding performance overhead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, we should aim to make generics efficient (from a runtime perspective, with some potential compile-time cost) for this use case.

Comment on lines 276 to 278
With a Carbon toolchain compiling both Carbon and C++ code, Carbon templates can
be handled by adding C++ compiler code paths that use portions of the Carbon AST
generated by the Carbon template code. While this approach is still complex and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Carbon templates can
be handled by adding C++ compiler code paths that use portions of the Carbon AST
generated by the Carbon template code.

This sentence is pretty central to understanding what your saying, but I didn't succeed in gathering what it was saying. Are you talking about C++ templates with Carbon types as parameters, or maybe Carbon templates with C++ types? These aren't what I think of if you say "Carbon templates". I don't know what "C++ compiler code paths" are -- "paths" reminds me of files, but I don't think that is what you mean.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rephrased a little:
exposing the Carbon compiler's AST to the C++ compiler directly,
as a compiler extension

proposals/p0175.md Outdated Show resolved Hide resolved
able to use the generated C++ source. However, generating that source is
complex: whereas mixed toolchain interoperability assumes Carbon can generate
declarations for C++ headers, that's a simple task compared to translating
arbitrary Carbon template code to C++. Supporting Carbon to C++ code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might point out that the operators will be different between Carbon and C++. For example, C++ uses << both for streamed output and binary shift left

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already talk a bit more about mixing toolchains than I'd like -- the section is pretty long. My sense is that it's better to avoid talking about operator<<, because of the amount of discussion that doing so would require, and it's not clear to me that it would add much:

  • There's a design decision to be made about whether operator<< should really be that different.
  • I'd expect any decision to address the C++ incompatibility, given the prevalence of use.
  • There should still be some interoperable method for using C++'s operator<<, and likely even overloading it from Carbon, in order to support calling standard C++ code from Carbon.
  • In context here, a template could rely on using whatever interoperable method is chosen.

Explaining all of this seems pretty expensive, and also possibly bleeds operator<< design choices into this "goals" proposal. I think it's better to stay at a high level.

Copy link
Contributor Author

@jonmeow jonmeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Responding to zygoloid

proposals/p0175.md Outdated Show resolved Hide resolved

### Philosophy

The C/C++ interoperability layer of Carbon is the section wherein a specific,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine fine, replaced C/C++ with C. Added a specific C goal.

I disagree that C should not be mentioned here -- there are still many maintained systems that rely on C APIs, and I think that part of writing a successor to C++ is guaranteeing we can still interact with C APIs.

Comment on lines 210 to 215
Mixing toolchains restricts functionality to what's feasible with the C/C++ ABI.
For example, developers should expect that Carbon templates will be callable
from C++ when using the Carbon toolchain, and will not be available when mixing
toolchains because it would require a substantially different and more complex
interoperability implementation. This degraded interoperability should still be
sufficient for most developers, albeit with the potential of more bridge code.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added:
Any C++ interoperability code that works when mixing toolchains must work when
using the native Carbon toolchain. The mixed toolchain support must not have
semantic divergence. The converse is not true, and the native Carbon toolchain
may have additional language support and optimizations.


### Non-goals

#### Full parity between a Carbon-only toolchain and mixing C++/Carbon toolchains
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was trying to say this, but rephrased this non-goal.


#### Allow C++ bridge code in Carbon files

Carbon files should support inline C++ bridge code. Where bridge code is
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter, rephrased

@sidney13 sidney13 added needs decision and removed comment deadline proposal rfc Proposal with request-for-comment sent out labels Nov 18, 2020
proposals/p0175.md Outdated Show resolved Hide resolved
proposals/p0175.md Outdated Show resolved Hide resolved
jonmeow and others added 2 commits November 24, 2020 10:03
Co-authored-by: Dmitri Gribenko <[email protected]>
Co-authored-by: Chandler Carruth <[email protected]>
@jonmeow jonmeow mentioned this pull request Nov 24, 2020
@sidney13 sidney13 added proposal accepted Decision made, proposal accepted and removed needs decision labels Nov 25, 2020
Copy link
Contributor

@sidney13 sidney13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Accepted on 2020-11-24.

Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly leaving an LGTM on my part.

@jonmeow
Copy link
Contributor Author

jonmeow commented Nov 25, 2020

@jonmeow jonmeow merged commit 312987f into carbon-language:trunk Nov 25, 2020
@jonmeow jonmeow deleted the interop-skeleton branch November 25, 2020 17:06
jonmeow added a commit that referenced this pull request Dec 3, 2020
This takes #175 and publishes it as interop goals. I've made a few small editorial changes, but the main addition is the "Offer equivalent support for languages other than C++" non-goal which I thought may be useful (in particular, it can be used to clarify why this directory is "interoperability" and not "interoperability_cpp").
sidney13 added a commit that referenced this pull request Dec 9, 2020
* Decision for #175: C++ interoperability goals

* Update proposals/p0175_decision.md

Co-authored-by: Chandler Carruth <[email protected]>

* Prettified suggested changes

* Update table of contents

Co-authored-by: Chandler Carruth <[email protected]>
chandlerc pushed a commit that referenced this pull request Jun 28, 2022
This takes #175 and publishes it as interop goals. I've made a few small editorial changes, but the main addition is the "Offer equivalent support for languages other than C++" non-goal which I thought may be useful (in particular, it can be used to clarify why this directory is "interoperability" and not "interoperability_cpp").
chandlerc added a commit that referenced this pull request Jun 28, 2022
* Decision for #175: C++ interoperability goals

* Update proposals/p0175_decision.md

Co-authored-by: Chandler Carruth <[email protected]>

* Prettified suggested changes

* Update table of contents

Co-authored-by: Chandler Carruth <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes PR meets CLA requirements according to bot. proposal accepted Decision made, proposal accepted proposal A proposal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants