Skip to content

Commit c0cdc71

Browse files
authored
Add docs for dump-sem-ir-ranges (#5598)
1 parent 2609365 commit c0cdc71

File tree

1 file changed

+99
-8
lines changed

1 file changed

+99
-8
lines changed

toolchain/docs/adding_features.md

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2121
- [Running tests](#running-tests)
2222
- [Updating tests](#updating-tests)
2323
- [Reviewing test deltas](#reviewing-test-deltas)
24-
- [Minimal Core prelude](#minimal-core-prelude)
25-
- [Verbose output](#verbose-output)
26-
- [Stack traces](#stack-traces)
27-
- [Dumping objects in interactive debuggers](#dumping-objects-in-interactive-debuggers)
24+
- [Test patterns](#test-patterns)
25+
- [Minimal Core prelude](#minimal-core-prelude)
26+
- [SemIR dumps and ranges](#semir-dumps-and-ranges)
27+
- [Example uses](#example-uses)
28+
- [Debugging errors](#debugging-errors)
29+
- [Verbose output](#verbose-output)
30+
- [Stack traces](#stack-traces)
31+
- [Dumping objects in interactive debuggers](#dumping-objects-in-interactive-debuggers)
2832

2933
<!-- tocstop -->
3034

@@ -487,7 +491,9 @@ Using `autoupdate_testdata.py` can be useful to produce deltas during the
487491
development process because it allows `git status` and `git diff` to be used to
488492
examine what changed.
489493

490-
### Minimal Core prelude
494+
### Test patterns
495+
496+
#### Minimal Core prelude
491497

492498
For most file tests in `check/`, very little of the `Core` package is used, and
493499
the test is not intentionally testing the `Core` package itself. Compiling the
@@ -508,20 +514,105 @@ We have a set of minimal `Core` preludes for testing different compiler feature
508514
areas in `//toolchain/testing/min_prelude/`. Each file begins with the line
509515
`package Core library "prelude";` to make it provide a prelude.
510516

511-
### Verbose output
517+
#### SemIR dumps and ranges
518+
519+
In `check/` tests, we use ranges to help reduce SemIR output in golden files and
520+
focus on the most interesting parts. This is done with special
521+
`//@dump-sem-ir-begin` and `//@dump-sem-ir-end` markers; note these are not
522+
valid comments because they have no space after `//`. As rules of thumb:
523+
524+
- Put ranges around code relevant to the feature under test.
525+
- When testing similar code in multiple different ways, focus on what's
526+
expected to be unique.
527+
- Try to keep ranges small.
528+
- We try to structure tests to use type checking to validate correctness;
529+
extra code for type checking might be possible to put outside a range.
530+
- For example, when passing an argument to a function call, think about
531+
whether the function call needs to be included, or if a range can focus
532+
on the argument.
533+
- Don't put markers around most failures.
534+
- A lot of failures will print similar SemIR; we're trying to balance
535+
review costs with the value of SemIR in golden files.
536+
537+
When a range is printed, referenced constants and import_refs will be
538+
automatically included as well. A small amount of SemIR may include a number of
539+
related instructions, such as an in-range instruction referencing an import_ref
540+
referencing a constant referencing another constant.
541+
542+
> NOTE: In a test, if full SemIR is desired for files, add
543+
> `// EXTRA-ARGS: --dump-sem-ir-ranges=if-present` with an explanation why.
544+
545+
##### Example uses
546+
547+
The range markers can be placed anywhere in code, and formatting will try to
548+
print only the relevant SemIR. In the example below, the `Foo` entity will be
549+
printed because it contains a range; its body will include `LogicUnderTest()`,
550+
but `SetUp()` and `TearDown()` will be omitted.
551+
552+
```carbon
553+
fn Foo() {
554+
SetUp();
555+
//@dump-sem-ir-begin
556+
LogicUnderTest();
557+
//@dump-sem-ir-end
558+
TearDown();
559+
}
560+
```
561+
562+
The ranges are token-based, and entity declarations with an overlapping range
563+
should be included. Ranges can span entity declarations in order to have only
564+
part of an entity included in output. In the example below, `Bar` and
565+
`Bar::ImportantCall` will be printed, but `Bar::UnimportantCall` will be
566+
omitted.
567+
568+
```
569+
//@dump-sem-ir-begin
570+
class Bar {
571+
fn ImportantCall[self: Self] { ... }
572+
//@dump-sem-ir-end
573+
574+
fn UnimportantCall[self: Self]() { ... }
575+
}
576+
```
577+
578+
Out-of-line definitions can be used to print a nested entity without printing
579+
the entity that contains it. In the example below, `Baz::Interesting` will be
580+
printed because of the range in its body; `Baz` will be omitted because its
581+
definition doesn't contain a range.
582+
583+
```
584+
class Baz {
585+
fn Interesting();
586+
}
587+
588+
fn Baz::Interesting() {
589+
//@dump-sem-ir-begin
590+
...
591+
//@dump-sem-ir-end
592+
}
593+
```
594+
595+
Normally, files with no range markers will be excluded from output. For example,
596+
we'll often exclude failing tests from output: passing SemIR is typically more
597+
interesting, and failing tests only need to fail gracefully. However, sometimes
598+
output can help check that an error is correctly propagated in SemIR.
599+
600+
### Debugging errors
601+
602+
#### Verbose output
512603

513604
The `-v` flag can be passed to trace state, and should be specified before the
514605
subcommand name: `carbon -v compile ...`. `CARBON_VLOG` is used to print output
515606
in this mode. There is currently no control over the degree of verbosity.
516607

517-
### Stack traces
608+
#### Stack traces
518609

519610
While the iterative processing pattern means function stack traces will have
520611
minimal context for how the current function is reached, we use LLVM's
521612
`PrettyStackTrace` to include details about the state stack. The state stack
522613
will be above the function stack in crash output.
523614

524-
### Dumping objects in interactive debuggers
615+
#### Dumping objects in interactive debuggers
525616

526617
We provide namespace-scoped `Dump` functions in several components, such as
527618
[check/dump.cpp](/toolchain/check/dump.cpp). These `Dump` functions will print

0 commit comments

Comments
 (0)