No support for `each` yet, but this should be enough that we can try out
an implementation approach supporting expanding a statement across
elements of a tuple without building "real" variadics.
Assisted-by: Claude via Antigravity
C++ interop used `GetFormatted` to turn a Carbon name into a C++ name.
For an identifier that is a Carbon keyword, such as `base`,
`GetFormatted` adds an `r#` prefix, so the C++ name came out as `r#base`
instead of `base`. This showed up in three places:
- A failed call to the C++ function `base` was reported as "no matching
function for call to 'r#base'".
- An exported Carbon function was mangled as `_ZN6CarbonL6r#baseEv`
instead of `_ZN6CarbonL4baseEv`.
- Its thunk was named `r#base__carbon_thunk` instead of
`base__carbon_thunk`.
All three now use `GetClangIdentifierInfo`, which `export.cpp` already
uses for other C++ names. The name is always an identifier in these
places: an overload set is only imported by looking up an identifier in
C++, and only functions declared in Carbon are exported, which are
always named by an identifier. So they check for an identifier, as the
export of a class name already does, rather than falling back to
`GetFormatted`. This removes a TODO about naming a `NameId::CppOperator`
overload set, which can't happen: C++ operators are resolved in
`operators.cpp` without an overload set.
Assisted-by: Claude Code
Update tree-sitter grammar to match toolchain, plus a couple of features
the toolchain doesn't yet implement: `like`, `each`, `expand`, `friend`.
Assisted-by: Claude via Antigravity
`CXXRecordDecl` and `RecordDecl` are functionally interchangeable in our
context, and other than here we only use `RecordDecl` when APIs outside
our control force us to.
C++ pointers to Carbon methods are supported using the same mechanisms
as for Carbon functions: we export the method to C++ and then form a
pointer to the exported method. This change does not support invoking a
C++ method pointer from Carbon, which will be more complicated because
we need to model instance binding on the Carbon side.
Clang implements the `"+r,m"` constraint in `benchmark::DoNotOptimize`
by always choosing memory, so each call stores the value to the stack
and loads it back. Most of our benchmarks call it on a loop counter or
another value carried to the next iteration, which puts that store and
reload on the loop's critical path.
On an Apple M1, the cost of that round trip depends on which register
the compiler uses to address the stack slot, and unrelated code changes
move it. Changing only that register from `x29` to `sp`, with the same
address, made `BM_SetLookupHitPtr<Set<int>>` 41.5% to 49.1% slower.
Add `Carbon::Testing::DoNotOptimize` in
`//testing/base:benchmark_helpers`, and switch every benchmark to it. It
only accepts types it can keep in registers: integers, enums, and
pointers go into a `"+r"` constraint with a `"memory"` clobber, and
containers pass their `data()` pointer to that form, so the compiler
must assume the call reads and writes their contents. Any other type is
a compile error. The container form doesn't block optimizing based on a
container's size; passing the container's address does.
Benchmarks that pass a loop counter or carried value no longer measure a
store and reload per iteration, so their numbers aren't comparable with
earlier runs. For example, the hashing latency benchmarks no longer
include one in each hash's latency.
Assisted-by: Claude Code
Base classes will be destroyed in a dedicated change, so we can
trivially confirm that the base is being destroyed.
This is a partial implementation of #7362.
---------
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
- Incorporates member binding interfaces from proposal #3720
- Includes updates to #3720 and new `x.impl(m)` and `typeof(x)` syntaxes
from #7697
- The concept of member resolution was replaced with just its name
lookup component, to reflect the new rules from #7697
- Updated member binding interfaces use of `extend` according to
proposal #5337
- `BindToRef` interface updated to use `ref` following proposal #5434
Assisted-by: Gemini via Antigravity
---------
Co-authored-by: Josh L <josh11b@users.noreply.github.com>
A specific's definition block may contain `InstId::None`, indicating it
has no constant value. Display this as `<not constant>` instead of
`invalid`, to avoid it sounding like an error.
If a test would still fail post-autoupdate due to either mismatched
`fail_` prefix or due to being NOAUTOUPDATE, the test is reported as `?`
instead of `.` in the incremental progress output, and a message is
listed after the progress output describing the problem. For example:
```console
Problems that require manual fixes:
- `toolchain/check/testdata/basics/zz_prefix_probe.carbon` split `bad_missing_prefix.carbon` failed; if failure is expected, add the `fail_` prefix.
- `toolchain/driver/testdata/fail_config.carbon` succeeded; if success is expected, remove the `fail_` prefix.
```
This change caused the `NOAUTOUPDATE` test
`toolchain/driver/testdata/fail_config.carbon` to fail, because when run
under autoupdate (or with `bazel run`), the install manifest would be
present, and the test is checking that it's not. Fixed by using the VFS
to look for the manifest, which then required fixing
toolchain/driver:driver_test to set up a real filesystem VFS so that it
*could* find the manifest.
With this change, there should no longer be a reason to manually run
file_test in addition to running autoupdate.
Assisted-by: Claude via Antigravity
Noticed these issues when trying to query raw semir with
[yq](https://github.com/mikefarah/yq)
- Removed additional "}" within PrintClassFields
- Removed additional "}" after is_frozen_period_self
- Surround `int(value: 123)` in quotes to avoid issues parsing the
additional ':'
- Updated DeclaredFacetType::Print to have valid YAML structure
- Constraints are added to inline list (i.e. wrapping in `[ ... ]`)
- Switched Outer seperator to Comma.
The last two may be up for debate, maybe there's no intention for raw
semir to be true valid yaml but I think its quite useful, for example I
can now do the following:
```sh
carbon compile examples/sieve.carbon --phase=check --dump-raw-sem-ir | yq '
.sem_ir |
select(.names as $n | .classes[] | .name as $k | $n[$k] == "Sieve") |
. as $ir |
$ir.classes[] |
select(.name as $k | $ir.names[$k] == "Sieve" and .body_block_id != "inst_block_empty") |
. as $c |
($ir.inst_blocks[$c.body_block_id][] as $i | $ir.insts[$i])
'
```
outputs:
```yaml
{kind: ImplDecl, arg0: impl41000000, arg1: inst_block41000008}
{kind: ImplSelfWitness, arg0: inst41000014, arg1: specific_interface41000000, type: type(inst(WitnessType))}
{kind: FunctionDecl, arg0: function41000000, arg1: inst_block41000013, type: type(inst41000030)}
{kind: FunctionDecl, arg0: function41000001, arg1: inst_block41000023, type: type(inst4100005B)}
{kind: FieldDecl, arg0: field41000000, arg1: region41000002, type: type(inst41000065)}
{kind: CompleteTypeWitness, arg0: inst41000067, type: type(inst(WitnessType))}
```
On Zen 5, a hashtable benchmark's result depends on where its timed loop
lands in memory. With a fixed hash seed, `BM_MapContainsMiss<Map<int,
int>>/64` takes 5.5-6.3 cycles per lookup at 8 of the 16 possible loop
positions within 256 bytes, and 6.7-7.1 at the other 8, through effects
in the op cache, the BTB, and the branch predictor. Which position a
build gets is an accident of the surrounding code, so comparing two
builds conflates a change's effect with where its code landed.
Each timed loop now runs from one of 16 out-of-line copies, aligned to
256 bytes and padded in 16-byte steps, which place the loop at each of
those 16 positions. A process picks a copy at random, or the one named
in `CARBON_BENCH_LAYOUT`, and reports it in the `Layout` counter.
Running `bench_runner.py` with enough runs to sample every position, 48
for example, makes its medians and confidence intervals account for
layout.
In a sweep over 1024 bytes, performance depended only on the loop's
address modulo 256, and loop heads are 16-byte aligned, so finer or
wider padding found nothing more on this CPU.
`CARBON_BENCH_LAYOUT_PAD_STEP` and `CARBON_BENCH_LAYOUT_PAD_RANGE`
adjust it. The copies grow `map_benchmark` from 4.8 MB to 7.6 MB.
On AArch64, LLVM doesn't align loop heads for generic or Apple CPUs, so
they land on any 4-byte boundary and 16-byte steps only reach a quarter
of the positions. AArch64 builds pad in 4-byte steps instead, giving 64
layouts and growing `map_benchmark` there to 16.6 MB. Every copy,
including the unpadded one, also emits the padding `asm` statement,
because its presence changes how the surrounding code is compiled.
Assisted-by: Claude Code
---------
Co-authored-by: Nicholas Bishop <nbishop@nbishop.net>
Extend the design's keyword list with the keywords from all the approved
proposals to date. Update our four syntax highlighters to have matching
keyword lists.
There are a few other stale things in some of the syntax highlighters;
those are not fixed here.
Assisted-by: Claude via Antigravity
`InstNamer` takes O(N^2) time to resolve N colliding inst names, so it
should be limited to textual dumps (where we can presumably prioritize
readability over performance). Without this change, compiling 200,000
destructor calls took hours. With this change, compiling 1,000,000
destructor calls takes about 30 seconds.
- Add TODOs indicating places where design doc updates are needed for
accepted proposals.
- Remove TODOs about unicode which were already accurately reflected in
the text and references. Issue is that proposal #142 left some work to
do, but any such work would have to be in another proposal.
- Update design docs where only a little more was needed, like the
"lambdas" section in the main README.
- Add references sections to clarify where proposals have already been
incorporated.
- Add to TODO that was missing which proposals made the decision.
- Small fixes found when creating this PR.
Fixes#5398
Assisted-by: Gemini via Antigravity
Co-authored-by: Josh L <josh11b@users.noreply.github.com>
This allows us to collect rewrites from named constraints and use them
to initialize the witness table for an `impl as` statement.
Only rewrites from extend constraints are tracked, as other constraints
should turn into equality constraints, as they don't modify the witness
table.
This change introduces a new inst kind `CppFunctionPointerType`, which
represents an imported C++ function pointer that can be invoked from
Carbon (support for forming such a pointer from a Carbon function is in
a follow-up PR). This is implemented by treating the operation of
invoking a function pointer in C++ as if it were a call to an `__invoke`
method on the function pointer type, and extending the existing
function-import logic to support importing this fictitious method.
---------
Co-authored-by: Nicholas Bishop <nbishop@nbishop.net>
This is a step toward supporting C++ function pointer types, which need
to be
imported and thunked in much the same way as C++ functions, but have a
different
underlying representation. `CalleeFunctionInfo` gives us a way to
abstract
away the representation differences, so expressing import and thunking
in terms
of `CalleeFunctionInfo` lets us reuse that code for function pointers.
Actual support for function pointers will come in a follow-up PR, but
the
API design choices I've made here are driven by that use case.
Destruction doesn't care about constness, so we remove the
`const`-qualifier and destroy the object using its non-const type.
This is a partial implementation of #7362.
Implements `typeof(expr)`, as described in #7697. `expr` is treated as
an unevaluated operand, and is kept in an `ExprRegion` separate from the
enclosing scope.
Assisted-by: Claude via Antigravity
The AST doesn't really matter here, since it's NRVO, there's no actual
code to generate to return the value. But having a correct AST does
address at least one clang false-positive diagnostic:
```
error: stack memory associated with local variable 'return_storage' is returned
3 | fn F() -> bool {
| ^
```
Check that every use of a non-constant instruction is dominated by a
definition of that instruction. Remove the fake (instruction creation
order based) dominance checks in convert; these start spuriously failing
during template instantiation of initializers.
Assisted-by: Claude Opus 5 via Antigravity
The `self` parameter block is presumably a relic from when `self` was in
the implicit parameter list; now it's just SemIR bloat.
This also factors out some common code between `self` and the other
parameters.
Includes changes from accepted proposals:
- #3762
- #3763
- #3980
- #5366
Examples from the proposals has been updated to reflect changes made in
later proposals, such as using `ref` instead of `addr`. Links from #3762
have been changed to point to the version of the code from when the PR
was merged.
Assisted-by: Gemini via Antigravity
---------
Co-authored-by: Josh L <josh11b@users.noreply.github.com>
Co-authored-by: Geoff Romer <gromer@google.com>
`render_fixed_width_float` formatted the whole part with `int(whole)`,
which is `0` with no sign for any value in (-1, 0). A -0.669% delta
rendered as `0.669%`, identical to a +0.669% one, and only the
improvement or regression marker told them apart. Render the sign
separately. Every value outside (-1, 0) renders exactly as before.
Assisted-by: Claude Code
The `Support/LLVMDriver.h` header was renamed to `Driver.h`, and the API
of `llvm::ToolContext` changed such that a constructor call is needed
instead of aggregate initialization.
A couple new bazel deps were added (libxml2 and xz), and rules_cc
required an updated version. The addition of libxml2 and xz required
some additions to check_non_test_cc_deps.py. libxml2 also required
allow-listing an additional URL in CI jobs.
Dropped `0011_Temporarily_remove_reference_to_hermetic_toolchain.patch`,
no longer needed.
Added `0012_Drop_dependencies_on_linux_uapi_and_pyyaml.patch` to remove
a couple dependencies from llvm that Carbon doesn't currently require.
These were causing bazel queries in check_deps and
forbid_llvm_googletest.py to fail.
We use `LocId`s to refer to physical locations in source code. Those
don't exist for toolchain-generated entities, so we instead choose a
related location that can stand in for a physical location. This inlines
the generated entities' constants into their points of use, and reduces
the total amount of generated SemIR.
This is especially important for calls to `Destroy.SelfDestruct` because
these are automatically generated when any destroyable object reaches
the end its lifetime.
We've made quite a few changes to how associated constants are
implemented since this was written; update the doc to match.
Assisted-by: Claude via Antigravity
Reconstruct the call syntax from the callee's explicit parameter
patterns, the callee specific, and the call arguments.
Assisted-by: Claude via Antigravity.
* Adds function begin/end logic for non-trivial `SubobjectDestroy.Op`
* Reorganises cases in `MakeSubobjectDestroyOpBody` to use `CARBON_KIND`
so each case can be made as an independent change.
As I was starting to try to play around with carbon, I wanted nvim
integration. Unfortunately, a lot of the code in the integration had
bitrotted, but not unbearably so, so I updated it 😄
It works now! Syntax highlighting and the LSP work out of the box with
nvim 0.12 (and should also work on nvim 0.11)
Hopefully all good that I did All The Commits, I come from the school of
"every commit should do one thing"
Closes#7821
---------
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>