Commit Graph
2324 Commits
Author SHA1 Message Date
Rayyan Kandjonmeow a1d03b1f56 Fixed var keyword in Carbon code example. Incorrect keyword color. (#3600)
This PR is changing the color of the var keyword in the `PrintTotalArea`
source example which is referenced in the README. The original color was
white and changed to `#FF7B72`.

---------

Co-authored-by: jonmeow <jperkins@google.com>
2024-01-19 22:30:23 +00:00
Jon Ross-Perkins a80d13ec6c Update bazel and lock to 7.0.1 (#3621) 2024-01-19 20:49:26 +00:00
Jon Ross-Perkins 9be99cad4c Don't take a NodeId argument for insts that have no parse node. (#3623)
This is low impact because we typically support a parse node, but I was
hoping to reduce ambiguity in the cases that don't.
2024-01-19 19:59:19 +00:00
josh11b 816a48ca45 Fix typo in comment (#3622) 2024-01-19 18:03:13 +00:00
5370041af5 Improve diagnosing of name conflicts in imports. (#3619)
Name conflicts weren't previously tested, and the diagnostics were just
a TODO, so this is also adding testing for that. But handling too.

Removing AddEntry because I think it's hard to make helpful for this
use-case when we want to do a diagnostic followup (because really,
callers want the full `.insert` result of pointer + success), and unused
otherwise.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2024-01-19 16:36:19 +00:00
Richard SmithandJon Ross-Perkins 9bd5fbd25c Automatically update constant_values() when adding a constant to the constant store. (#3618)
As requested in review of #3611.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2024-01-19 16:09:36 +00:00
Jon Ross-Perkins 9e3c4a3ef1 Reduce a few cases of retrieving the parse node for conditional diagnostics. (#3620) 2024-01-19 03:01:24 +00:00
Richard Smith b138c90c9e Use constant evaluation to determine the identity of types. (#3617)
Remove the type canonicalization mechanism and instead rely on constant
canonicalization to deduplicate types.

Rename the `Canonicalize*Type` functions to reflect that they're no
longer performing canonicalization. Switch code that creates types due
to semantic checking, rather than due to source syntax, to directly
create type constants through evaluation rather than creating an
instruction and evaluating it to produce a separate constant
representation.

The mapping from `const (const T)` that was previously performed by type
canonicalization is now implemented in expression evaluation instead.

The value `<error>` is now treated as a constant value, with a special
property that an instruction involving `<error>` that could possibly be
constant evaluates to `<error>`. This helps avoid producing follow-on
errors when an error occurs as a subexpression of an expression, such as
a type, that is intended to be constant.
2024-01-19 00:47:37 +00:00
Jon Ross-Perkins d0fb4b5815 Change DiagnoseDuplicateName to expect an inst ID for the duplicate. (#3616)
This makes duplicate and previous definition handling match. While we
may want to make both point more fine-grained at the name, the necessary
logic seems likely to be equivalent.

Note, I'm looking at this mainly due to duplicate names in imports,
where it's especially helpful to take an instruction instead of a parse
node. We'll eventually want to handle parse nodes from other imports
better, and I think this is the way it would most likely work.
2024-01-18 23:23:31 +00:00
Jon Ross-Perkins af14a2586a Typo fix abokve (#3614) 2024-01-18 22:20:51 +00:00
Richard Smith 29c294880d Deduplicate and canonicalize all constants. (#3611)
Rather than producing multiple constants with the same value, fold all
instances of a given constant to the same constant instruction.

A future PR will use this to replace the current type canonicalization
system.
2024-01-18 21:42:19 +00:00
Jon Ross-Perkins 62db3e9ad0 Fix reference to package_scope. (#3613)
The reference may be invalidated by an `Import` call reallocating the
`name_scopes`.
2024-01-18 17:47:20 +00:00
Richard Smith b7c21a7fa7 Add constant evaluation for namespace expressions. (#3612) 2024-01-17 21:12:25 +00:00
Jon Ross-Perkins 785ab04ede Address a likely issue with language_server (#3610)
The `node_kind` check is on `ch`, so the `node_token` probably should be
too. Trying to address the issue reported by [skoriop on
Discord](https://discord.com/channels/655572317891461132/993958660277555301/1197201736226574420)

Note, I haven't tested this beyond building.
2024-01-17 19:52:29 +00:00
Chandler Carruth 523e1c2972 Add a native Bazel build for libpfm and use it in benchmarks. (#3601)
The `libpfm` in the Bazel central repository uses `make` to build it,
which is difficult to integrate with our toolchain. Rather than try to
fix all the issues there, it's easy to just add a native Bazel build for
the library. I don't know that any of the relevant upstream folks are
interested in this kind of build, but it seems easy for us to maintain
as a Carbon project build configuration. I've also not tried to port all
of the different configurations as a consequence, and only 64-bit x86
and Arm as that seems the only likely architectures we'll care about in
the near term.

I've kept this using the `bzlmod` stuff as best I can, and I *think* I'm
holding all of those pieces correctly, but if not, happy for suggestions
on adjustments.

The `google_benchmark` package also has an awkward way of enabling
`libpfm` support using a top-level `bazel` command line flag. I think
this is because of how brittle the Bazel build of `libpfm` is, but I'm
not sure. With the new build, it seems easy to patch `google_benchmark`
to detect the same conditions as we build `libpfm` under, and enable it
there. So I've done this to avoid folks having to pass a command line
flag on platforms where it is supported.

The result is that we now get really nice CPU counter support in our
benchmarks out-of-the-box on Linux x86-64 and AArch64. For example on my
Fedora Asahi install on a Mac Mini I get:

```console
$ bazel run -c opt --copt=-gmlt //common:hashing_benchmark --run_under="taskset -c 4" -- --benchmark_counters_tabular=true --benchmark_perf_counters=CYCLES,INSTRUCTIONS
INFO: Invocation ID: 4aaeb9e9-7df5-4f1f-b56b-c03411790268
INFO: Analyzed target //common:hashing_benchmark (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //common:hashing_benchmark up-to-date:
  bazel-bin/common/hashing_benchmark
INFO: Elapsed time: 0.360s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: /bin/bash -c 'taskset -c 4 bazel-bin/common/hashing_benchmark '\''--benchmark_counters_tabular=true'\'' '\''--benchmark_perf_counters=CYCLES,INSTRUCTIONS'\'''
2024-01-15T00:10:50-08:00
Running /home/chandlerc/.cache/bazel/_bazel_chandlerc/b686aa8910e0845b88c21d715819b076/execroot/_main/bazel-out/aarch64-opt/bin/common/hashing_benchmark
Run on (8 X 2064 MHz CPU s)
CPU Caches:
  L1 Data 64 KiB (x8)
  L1 Instruction 128 KiB (x8)
  L2 Unified 4096 KiB (x2)
Load Average: 0.01, 0.08, 0.08
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Benchmark                                                                           Time             CPU   Iterations     CYCLES INSTRUCTIONS bytes_per_second
--------------------------------------------------------------------------------------------------------------------------------------------------------------
BM_LatencyHash<RandValues<uint8_t>, CarbonHashBench>                             4.11 ns         4.11 ns    170200064    13.1321      9.00587      232.116Mi/s
BM_LatencyHash<RandValues<uint8_t>, AbseilHashBench>                             4.82 ns         4.82 ns    145643520    15.3657      12.0059      197.946Mi/s
BM_LatencyHash<RandValues<uint8_t>, LLVMHashBench>                               7.96 ns         7.95 ns     87956480    25.3737      17.0068      119.991Mi/s
BM_LatencyHash<RandValues<uint16_t>, CarbonHashBench>                            4.11 ns         4.11 ns    170365952    13.1247      9.00587      464.573Mi/s
BM_LatencyHash<RandValues<uint16_t>, AbseilHashBench>                            5.51 ns         5.51 ns    127568896    17.5578      14.0059      346.225Mi/s
BM_LatencyHash<RandValues<uint16_t>, LLVMHashBench>                              8.00 ns         7.99 ns     87085056     25.377      17.0068      238.834Mi/s
BM_LatencyHash<RandValues<std::pair<uint8_t, uint8_t>>, CarbonHashBench>         4.91 ns         4.90 ns    136013824    15.6456      14.0059      389.006Mi/s
BM_LatencyHash<RandValues<std::pair<uint8_t, uint8_t>>, AbseilHashBench>         6.85 ns         6.85 ns    102630400    21.8041      18.0059      278.637Mi/s
BM_LatencyHash<RandValues<std::pair<uint8_t, uint8_t>>, LLVMHashBench>           7.57 ns         7.56 ns     92798976    24.1437      20.0068      252.151Mi/s
BM_LatencyHash<RandValues<uint32_t>, CarbonHashBench>                            4.12 ns         4.12 ns    170229760    13.1272      9.00587      926.444Mi/s
BM_LatencyHash<RandValues<uint32_t>, AbseilHashBench>                            4.93 ns         4.92 ns    145304576    15.3738      12.0059      775.224Mi/s
BM_LatencyHash<RandValues<uint32_t>, LLVMHashBench>                              8.11 ns         8.10 ns     87127040     25.373      17.0068       470.98Mi/s
```
2024-01-17 03:39:01 +00:00
Richard Smith 906346cf35 Ensure we evaluate instructions created in uncommon ways. (#3598)
Instructions created by splices during conversion are now evaluated, as
are instructions created in cases where we first create a placeholder
instruction and later replace it by a different instruction.

This also removes the ability to set a parse node and instruction
independently after creating an `InstId`, which could lead to them
accidentally not matching.
2024-01-16 21:28:14 +00:00
Richard Smith d712bf12a6 Remove parse nodes from constants. (#3599)
These instructions are intended to be shared across all uses and so
don't have a meaningful location. So far, only type constants are
shared.
2024-01-13 04:52:25 +00:00
Jon Ross-Perkins e305c896a1 Add TODOs for implicit params to class and interface. (#3596)
Without these, the code crashes.
2024-01-12 22:40:11 +00:00
Daniel James SzelogowskiandRichard Smith 44fdcc3db8 Refactor Duplicate Code Patterns in StaticScope Class (#3550)
These changes are aimed at improving code readability and
maintainability within the `StaticScope` class of the AST folder.

- **Optimized Print and PrintID Methods:**
Introduced a template function `PrintCommon` to handle common logic in
`Print` and `PrintID`. This change simplifies the class interface and
avoids repetition, enhancing code readability.

- **Simplified TryResolveHere Method:**
Streamlined the `TryResolveHere` method by simplifying the conditional
logic. The refactored code is more readable and easier to understand,
improving overall code quality.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-12 22:35:54 +00:00
Jon Ross-Perkins f5e9158fa7 Support passing an InstId for check diagnostics. (#3597) 2024-01-12 22:09:32 +00:00
Jon Ross-PerkinsandRichard Smith f197219c10 Split parse nodes out from instructions because they're rarely used. (#3590)
The parse nodes are still tracked as part of the same value store
interface in order to ensure parity, but they're split out from Inst
itself in order to reduce the size of Inst -- the expectation is that
they don't need to be passed around quite as much.

This change doesn't actually reduce the passing very much, although
there are hints of it: AddInstAndPush doesn't typically need a separate
parse node from the one on the Inst itself, for example. In a couple
spots I changed code to rely a little more on the InstId until the
ParseNode is needed, but it's very low hanging fruit where done. I think
convert could do more to not eagerly fetch the parse node before its
use, but more cleanup felt it would be easier to handle separately. I'm
currently viewing this as making such cleanup _possible_ rather than
executing on it up-front.

But also, I want to make sure there's a consensus to head in this
direction before pulling the trigger. We speculated that this would
result in the parse node being passed around less, and I do think that's
the case, although it's a bit fuzzy in the change.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-12 19:01:51 +00:00
Richard SmithandChandler Carruth a3154356f0 Distinguish between template constants and symbolic constants. (#3595)
This is accomplished by tracking an extra bit on the ID we store in the
constant values table, and propagating that from subexpressions to the
enclosing expression. This extra bit is not yet computed correctly for
types; that will be addressed in later PRs.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2024-01-12 02:56:22 +00:00
8ba24bbe41 Roadmap for 2024 and a retrospective for 2023 (#3564)
We propose a roadmap for 2024 focused on a working Carbon toolchain that
supports Carbon ↔ C++ interop.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-12 01:14:48 +00:00
Richard Smith a2012d4777 Add evaluation for conversion instructions. (#3594)
Also some finessing of evaluation for other instruction kinds and some
additional testing.
2024-01-11 23:50:04 +00:00
Richard Smith 7553d864e1 Very basic support for constant evaluation of expressions. (#3581)
Form a side table with constant values for each instruction. Evaluation
is only supported for a few very simple kinds of instruction for now.
This is not observable outside of the SemIR output, because nothing
depends on expressions having a constant value phase yet.
2024-01-11 23:12:58 +00:00
Jon Ross-Perkins b3c82bf0d3 Remove unused default on AllocateName. (#3591) 2024-01-11 22:56:16 +00:00
Jon Ross-Perkins 3d2b4f8ae7 Switch gnu::unused to maybe_unused (#3592)
Preferring the C+11 name, [reflecting old
discussion](https://discord.com/channels/655572317891461132/655578254970716160/1171977169556230164)
2024-01-11 22:55:54 +00:00
Jon Ross-Perkins 93bd97c289 Drop yash from vscode recommendations, and add black (#3589)
yash is mainly for flex/bison formatting, which was for explorer: we're
refocusing away from that, so it doesn't make as much sense to include
now.

black I think is relatively new, it was previously part of the python
plugin. I'm hoping inclusion helps with setup.
2024-01-11 22:55:23 +00:00
Jon Ross-Perkins 7083b267d4 Switch clang::fallthrough to fallthrough (#3593)
Preferring the C+11 name, [reflecting old
discussion](https://discord.com/channels/655572317891461132/655578254970716160/1171977169556230164)
2024-01-11 22:54:54 +00:00
Jon Ross-PerkinsandRichard Smith 52037436eb Change LexicalLookup to not resize, due to lack of current need. (#3587)
Per discussion, unqualified lookup should only occur on identifiers that
existed at the time Context was initialized. As a consequence, the
resize logic in LexicalLookup may not be necessary. Even considering
metaprogramming, if metaprogramming is restricted to qualified name
lookup, it may not be necessary in the future. Support should be easy to
add if we need it. Trying to clearly document in the CHECK message what
the rationale is.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-11 18:43:31 +00:00
Jon Ross-Perkins dc75295a72 Change lexical lookup to use an array instead of hash map. (#3582)
This is primarily being done for performance reasons, removing hash
lookups. The increased memory consumption is accepted.

Refactors LexicalLookup out to its own structure.
2024-01-10 23:06:37 +00:00
josh11b 5e004b854e Delete unused NoOp instruction (#3586) 2024-01-10 22:40:34 +00:00
Jon Ross-Perkins 7c151a9b47 Split import logic to its own file. (#3576)
This is currently built on top of #3575. I'm just getting to the point
where imports have enough logic that I'm thinking they may be able to
stand on their own.
2024-01-10 22:28:35 +00:00
Jon Ross-Perkins aece16a595 Fix bazel mod deps file regex. (#3585)
Oops.
2024-01-10 18:06:10 +00:00
Jon Ross-PerkinsandRichard Smith cad4605dad Add imports of enclosing scopes. (#3575)
Namespaces are copied, which means also adding their name to the
underlying instruction. It happened not to be done previously; the name
was only in name lookup.

Since the only import supported right now is the default import,
functionality is limited; in the future I'll need to deal with namespace
vs package conflicts.

Tests of namespace imports are under "namespace" -- I figured this would
be best for scaling as more instructions get support.

This also improves some debugging-related output that I was trying to
use while trying to build the support.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-10 17:36:50 +00:00
Jon Ross-Perkins 848d4d7ec0 Add geoffromer and josh11b as toolchain reviewers (#3584) 2024-01-10 01:12:36 +00:00
Jon Ross-Perkins 5d0465d43c Adjust a few Invalid comparisons to use is_valid (#3583)
Deliberately not touching the case that came up from #3575
2024-01-10 00:21:27 +00:00
josh11b f5c34d62dd Abbreviate "address" -> "addr" (#3580)
As [requested in
Discord](https://discord.com/channels/655572317891461132/655578254970716160/1184904724483416064)
and is now documented in [the toolchain architecture
doc](https://docs.google.com/document/d/1RRYMm42osyqhI2LyjrjockYCutQ5dOf8Abu50kTrkX0/edit?resourcekey=0-kHyqOESbOHmzZphUbtLrTw&tab=t.0#heading=h.pph7i5m5un7q).
2024-01-09 22:37:48 +00:00
Geoff Romer 214d7b991d Handle missing condition in while (#3578) 2024-01-09 19:40:52 +00:00
josh11b 0b9e73ab07 Put check stage handle functions in execution order (#3573)
No changes other than moving code and adding section comments.
2024-01-09 02:46:10 +00:00
Jon Ross-Perkins a196b9840f Run clang-tidy on headers (#3572)
This patches bazel_clang_tidy handling of headers. I found an equivalent
change at https://github.com/erenon/bazel_clang_tidy/pull/13, but that
was [already
rejected](https://github.com/erenon/bazel_clang_tidy/pull/13#issuecomment-1047007424).
Per the criticism, this will result in redundant processing of headers.

The project instead uses `HeaderFilterRegex: ".*"`, but that results in
two problems:

1. When running with `-k`, errors are repeated when a header is included
more than once, which is common.
2. clang-tidy including errors from headers that are included from other
modules (e.g., abseil-cpp); filtering correctly is difficult.

Given the trade-offs and options (including forking), I thought patching
was preferable so long as it remains narrow.
2024-01-05 23:01:47 +00:00
Geoff RomerandRichard Smith 927d633762 Simplify handling of VariableInitializer (#3515)
Also stop supporting `var` with initializer inside `for`.

Resolves TODO in `handle_variable.cpp`

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-05 17:58:22 +00:00
Richard Smith a6508fcf05 Basic support for generic bindings. (#3555)
This change adds a `BindSymbolicName` instruction for generic bindings,
paralleling the existing `BindName`. A mechanism is also added to allow
both kinds of binding to be accessed uniformly, for convenience in the
case where the two different kinds of binding are treated the same.

Generic bindings of type `type` are allowed to be used as types,
although no operations are provided for such types. For now lowering
treats these types as empty structs, which seems like a reasonable
lowering for non-monomorphized unconstrained types.
2024-01-05 03:39:45 +00:00
Jon Ross-Perkins 0205645e7d Refactor BindName to support tracking the enclosing scope. (#3566)
This is a step towards adding enclosing scopes for imports. It creates
an indirection for all bind names.

We discussed specializing for bindings that are in function scope (i.e.,
not a useful enclosing scope for imports or diagnostics). However, the
thought is to go ahead with this singular approach for now, and only
change structure if it's a performance issues so that we have
incrementally fewer instructions to handle.
2024-01-05 02:56:03 +00:00
Richard Smith dfb27ded9c Update setup instructions for recent debian / ubuntu (#3571)
`pip3 install` refuses to perform a system-wide installation, but we can
install `pre-commit` with `apt` instead.
2024-01-05 00:19:09 +00:00
Richard Smith 07efa026de Support defining instruction categories with a common representation (#3569)
Add a mechanism to define instruction categories, to support inspecting
the common representation of similar kinds of instruction. Use that
mechanism to make formatting of branch instructions slightly more
type-safe.

The idea here is to use the existing `Inst` mechanism for converting to
and from structs, extended to operate on a struct representing multiple
different kinds of instruction. In this case, the concrete kind of
instruction is stored in the struct in a `kind` field, rather than being
implied by the type.

Factored out of #3555 where this mechanism is used to provide a common
interface for runtime and symbolic name bindings.
2024-01-04 23:10:25 +00:00
Jon Ross-Perkins a6fc65c40d Update LLVM to a recent commit. (#3568)
One small deprecation fix that was missed by #3519
2024-01-04 23:00:12 +00:00
josh11b e93b5c61da Suppress clang-tidy error to use auto in tree_test.cpp (#3570) 2024-01-04 22:33:56 +00:00
josh11bandRichard Smith 82179bc903 Add Tree::TryAs to cast to a typed parse node id (#3565)
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2024-01-04 21:51:19 +00:00
Jon Ross-Perkins facf6eea04 Check clang-tidy in tests.yaml (#3567)
Example success:
https://github.com/carbon-language/carbon-lang/actions/runs/7414437667/job/20175507192
Example failure:
https://github.com/carbon-language/carbon-lang/actions/runs/7414486486/job/20175656728
2024-01-04 21:45:53 +00:00