From 8d08e774fccbbe7fcf82aac00bf6ef9230bbb912 Mon Sep 17 00:00:00 2001 From: Jon Ross-Perkins Date: Fri, 15 Aug 2025 11:53:43 -0700 Subject: [PATCH] Add a feature to explicitly include a file's SemIR (#5961) Trying to figure out an easy way to debug semir in the prelude, #5703 removed an option to set `--exclude-dump-file-prefix` to empty. But, this is probably an improvement over that flow... With this change, it's possible to add `//@dump-sem-ir-file` to a specific prelude file, and its full IR will be printed. Additionally, it becomes an option with the default `--dump-sem-ir-ranges=only` to add `//@dump-sem-ir-file` and get the full file's IR. --- toolchain/check/check.cpp | 6 +- .../testdata/basics/dump_sem_ir_ranges.carbon | 22 ++ .../basics/dump_sem_ir_ranges_ignore.carbon | 12 + .../basics/dump_sem_ir_ranges_only.carbon | 12 + .../testdata/basics/include_in_dumps.carbon | 361 ++++++++++++++++++ toolchain/docs/adding_features.md | 17 +- toolchain/driver/compile_subcommand.cpp | 28 +- toolchain/lex/lex.cpp | 6 +- .../lex/testdata/include_in_dumps.carbon | 17 + toolchain/lex/tokenized_buffer.cpp | 4 + toolchain/lex/tokenized_buffer.h | 9 +- 11 files changed, 484 insertions(+), 10 deletions(-) create mode 100644 toolchain/check/testdata/basics/include_in_dumps.carbon create mode 100644 toolchain/lex/testdata/include_in_dumps.carbon diff --git a/toolchain/check/check.cpp b/toolchain/check/check.cpp index f312b784f1b5..d90477684270 100644 --- a/toolchain/check/check.cpp +++ b/toolchain/check/check.cpp @@ -340,17 +340,17 @@ static auto MaybeDumpFormattedSemIR( return; } - bool has_ranges = sem_ir.parse_tree().tokens().has_dump_sem_ir_ranges(); + const auto& tokens = sem_ir.parse_tree().tokens(); if (options.dump_sem_ir_ranges == CheckParseTreesOptions::DumpSemIRRanges::Only && - !has_ranges) { + !tokens.has_dump_sem_ir_ranges() && !tokens.has_include_in_dumps()) { return; } bool use_dump_sem_ir_ranges = options.dump_sem_ir_ranges != CheckParseTreesOptions::DumpSemIRRanges::Ignore && - has_ranges; + tokens.has_dump_sem_ir_ranges(); SemIR::Formatter formatter(&sem_ir, tree_and_subtrees_getter, options.include_in_dumps, use_dump_sem_ir_ranges); formatter.Format(); diff --git a/toolchain/check/testdata/basics/dump_sem_ir_ranges.carbon b/toolchain/check/testdata/basics/dump_sem_ir_ranges.carbon index 83650b7bbad3..754645656fac 100644 --- a/toolchain/check/testdata/basics/dump_sem_ir_ranges.carbon +++ b/toolchain/check/testdata/basics/dump_sem_ir_ranges.carbon @@ -91,6 +91,12 @@ library "[[@TEST_NAME]]"; fn F(); +// --- explicit_dump_file.carbon + +library "[[@TEST_NAME]]"; + +//@include-in-dumps + // CHECK:STDOUT: --- function.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { @@ -320,3 +326,19 @@ fn F(); // CHECK:STDOUT: // CHECK:STDOUT: fn @F(); // CHECK:STDOUT: +// CHECK:STDOUT: --- explicit_dump_file.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Core: = namespace file.%Core.import, [concrete] { +// CHECK:STDOUT: import Core//prelude +// CHECK:STDOUT: import Core//prelude/... +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [concrete] { +// CHECK:STDOUT: .Core = imports.%Core +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core.import = import Core +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/basics/dump_sem_ir_ranges_ignore.carbon b/toolchain/check/testdata/basics/dump_sem_ir_ranges_ignore.carbon index 428cc9deefc4..fab0ddd49aec 100644 --- a/toolchain/check/testdata/basics/dump_sem_ir_ranges_ignore.carbon +++ b/toolchain/check/testdata/basics/dump_sem_ir_ranges_ignore.carbon @@ -25,6 +25,12 @@ library "[[@TEST_NAME]]"; fn F(); +// --- explicit_dump_file.carbon + +library "[[@TEST_NAME]]"; + +//@include-in-dumps + // CHECK:STDOUT: --- with-range.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { @@ -57,3 +63,9 @@ fn F(); // CHECK:STDOUT: // CHECK:STDOUT: fn @F(); // CHECK:STDOUT: +// CHECK:STDOUT: --- explicit_dump_file.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [concrete] {} +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/basics/dump_sem_ir_ranges_only.carbon b/toolchain/check/testdata/basics/dump_sem_ir_ranges_only.carbon index ebb20babead3..1637bae5a0c6 100644 --- a/toolchain/check/testdata/basics/dump_sem_ir_ranges_only.carbon +++ b/toolchain/check/testdata/basics/dump_sem_ir_ranges_only.carbon @@ -27,6 +27,12 @@ library "[[@TEST_NAME]]"; fn F(); +// --- explicit_dump_file.carbon + +library "[[@TEST_NAME]]"; + +//@include-in-dumps + // CHECK:STDOUT: --- with-range.carbon // CHECK:STDOUT: // CHECK:STDOUT: constants { @@ -40,3 +46,9 @@ fn F(); // CHECK:STDOUT: // CHECK:STDOUT: fn @F(); // CHECK:STDOUT: +// CHECK:STDOUT: --- explicit_dump_file.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [concrete] {} +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/check/testdata/basics/include_in_dumps.carbon b/toolchain/check/testdata/basics/include_in_dumps.carbon new file mode 100644 index 000000000000..e9768b983e64 --- /dev/null +++ b/toolchain/check/testdata/basics/include_in_dumps.carbon @@ -0,0 +1,361 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/convert.carbon +// +// This explicitly excludes some files, then set ranges to `if-present` so that +// we can see imports (particularly of `impl`s) from files that would be excluded without +// `//@include-in-dumps`. +// EXTRA-ARGS: --exclude-dump-file-prefix=exclude/ --dump-sem-ir-ranges=if-present +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/basics/include_in_dumps.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/basics/include_in_dumps.carbon + +// --- exclude/included.carbon + +library "[[@TEST_NAME]]"; + +//@include-in-dumps + +interface I {} + +// --- exclude/included_with_range.carbon + +library "[[@TEST_NAME]]"; + +//@include-in-dumps + +//@dump-sem-ir-begin +interface I { + fn Op[self: Self](); +} +//@dump-sem-ir-end + +class C { + impl C as I { + fn Op[self: Self]() {} + } +} + +// --- import_included.carbon + +library "[[@TEST_NAME]]"; + +import library "included_with_range"; + +fn F(c: C) { c.(I.Op)(); } + +// --- exclude/excluded.carbon + +library "[[@TEST_NAME]]"; + +interface I {} + +// --- exclude/excluded_with_range.carbon + +library "[[@TEST_NAME]]"; + +//@dump-sem-ir-begin +interface I { + fn Op[self: Self](); +} +//@dump-sem-ir-end + +class C { + impl C as I { + fn Op[self: Self]() {} + } +} + +// --- import_excluded.carbon + +library "[[@TEST_NAME]]"; + +import library "excluded_with_range"; + +fn F(c: C) { c.(I.Op)(); } + +// CHECK:STDOUT: --- exclude/included.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete] +// CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Core: = namespace file.%Core.import, [concrete] { +// CHECK:STDOUT: import Core//prelude +// CHECK:STDOUT: import Core//prelude/... +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [concrete] { +// CHECK:STDOUT: .Core = imports.%Core +// CHECK:STDOUT: .I = %I.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core.import = import Core +// CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: interface @I { +// CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self] +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: witness = () +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- exclude/included_with_range.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete] +// CHECK:STDOUT: %Self.826: %I.type = bind_symbolic_name Self, 0 [symbolic] +// CHECK:STDOUT: %Self.as_type.b70: type = facet_access_type %Self.826 [symbolic] +// CHECK:STDOUT: %pattern_type.6de: type = pattern_type %Self.as_type.b70 [symbolic] +// CHECK:STDOUT: %I.Op.type: type = fn_type @I.Op [concrete] +// CHECK:STDOUT: %I.Op: %I.Op.type = struct_value () [concrete] +// CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete] +// CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%I.Op.decl [concrete] +// CHECK:STDOUT: %C: type = class_type @C [concrete] +// CHECK:STDOUT: %I.impl_witness: = impl_witness @C.%I.impl_witness_table [concrete] +// CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete] +// CHECK:STDOUT: %I.facet: %I.type = facet_value %C, (%I.impl_witness) [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: %I.decl: type = interface_decl @I [concrete = constants.%I.type] {} {} +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: interface @I { +// CHECK:STDOUT: +// CHECK:STDOUT: %I.Op.decl: %I.Op.type = fn_decl @I.Op [concrete = constants.%I.Op] { +// CHECK:STDOUT: %self.patt: @I.Op.%pattern_type (%pattern_type.6de) = binding_pattern self [concrete] +// CHECK:STDOUT: %self.param_patt: @I.Op.%pattern_type (%pattern_type.6de) = value_param_pattern %self.patt, call_param0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: %self.param: @I.Op.%Self.as_type.loc8_15.1 (%Self.as_type.b70) = value_param call_param0 +// CHECK:STDOUT: %.loc8_15.1: type = splice_block %.loc8_15.2 [symbolic = %Self.as_type.loc8_15.1 (constants.%Self.as_type.b70)] { +// CHECK:STDOUT: %Self.ref: %I.type = name_ref Self, @I.%Self [symbolic = %Self (constants.%Self.826)] +// CHECK:STDOUT: %Self.as_type.loc8_15.2: type = facet_access_type %Self.ref [symbolic = %Self.as_type.loc8_15.1 (constants.%Self.as_type.b70)] +// CHECK:STDOUT: %.loc8_15.2: type = converted %Self.ref, %Self.as_type.loc8_15.2 [symbolic = %Self.as_type.loc8_15.1 (constants.%Self.as_type.b70)] +// CHECK:STDOUT: } +// CHECK:STDOUT: %self: @I.Op.%Self.as_type.loc8_15.1 (%Self.as_type.b70) = bind_name self, %self.param +// CHECK:STDOUT: } +// CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, %I.Op.decl [concrete = constants.%assoc0] +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = %Self +// CHECK:STDOUT: .Op = %assoc0 +// CHECK:STDOUT: witness = (%I.Op.decl) +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic fn @I.Op(@I.%Self: %I.type) { +// CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self.826)] +// CHECK:STDOUT: %Self.as_type.loc8_15.1: type = facet_access_type %Self [symbolic = %Self.as_type.loc8_15.1 (constants.%Self.as_type.b70)] +// CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type.loc8_15.1 [symbolic = %pattern_type (constants.%pattern_type.6de)] +// CHECK:STDOUT: +// CHECK:STDOUT: fn(%self.param: @I.Op.%Self.as_type.loc8_15.1 (%Self.as_type.b70)); +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @I.Op(constants.%Self.826) { +// CHECK:STDOUT: %Self => constants.%Self.826 +// CHECK:STDOUT: %Self.as_type.loc8_15.1 => constants.%Self.as_type.b70 +// CHECK:STDOUT: %pattern_type => constants.%pattern_type.6de +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: specific @I.Op(constants.%I.facet) { +// CHECK:STDOUT: %Self => constants.%I.facet +// CHECK:STDOUT: %Self.as_type.loc8_15.1 => constants.%C +// CHECK:STDOUT: %pattern_type => constants.%pattern_type.c48 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- import_included.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %C: type = class_type @C [concrete] +// CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete] +// CHECK:STDOUT: %complete_type: = complete_type_witness %empty_struct_type [concrete] +// CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete] +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete] +// CHECK:STDOUT: %Self.826: %I.type = bind_symbolic_name Self, 0 [symbolic] +// CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete] +// CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, imports.%Main.import_ref.7d9 [concrete] +// CHECK:STDOUT: %I.Op.type: type = fn_type @I.Op [concrete] +// CHECK:STDOUT: %I.Op: %I.Op.type = struct_value () [concrete] +// CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self.826 [symbolic] +// CHECK:STDOUT: %pattern_type.6de: type = pattern_type %Self.as_type [symbolic] +// CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete] +// CHECK:STDOUT: %I.impl_witness: = impl_witness imports.%I.impl_witness_table [concrete] +// CHECK:STDOUT: %I.facet: %I.type = facet_value %C, (%I.impl_witness) [concrete] +// CHECK:STDOUT: %.36a: type = fn_type_with_self_type %I.Op.type, %I.facet [concrete] +// CHECK:STDOUT: %C.as.I.impl.Op.type: type = fn_type @C.as.I.impl.Op [concrete] +// CHECK:STDOUT: %C.as.I.impl.Op: %C.as.I.impl.Op.type = struct_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Main.I: type = import_ref Main//included_with_range, I, loaded [concrete = constants.%I.type] +// CHECK:STDOUT: %Main.C: type = import_ref Main//included_with_range, C, loaded [concrete = constants.%C] +// CHECK:STDOUT: %Core.ece: = namespace file.%Core.import, [concrete] { +// CHECK:STDOUT: import Core//prelude +// CHECK:STDOUT: import Core//prelude/... +// CHECK:STDOUT: } +// CHECK:STDOUT: %Main.import_ref.8f2: = import_ref Main//included_with_range, loc16_1, loaded [concrete = constants.%complete_type] +// CHECK:STDOUT: %Main.import_ref.2c4 = import_ref Main//included_with_range, inst44 [no loc], unloaded +// CHECK:STDOUT: %Main.import_ref.e5d = import_ref Main//included_with_range, inst20 [no loc], unloaded +// CHECK:STDOUT: %Main.import_ref.9cd: %I.assoc_type = import_ref Main//included_with_range, loc8_22, loaded [concrete = constants.%assoc0] +// CHECK:STDOUT: %Main.Op.ca5 = import_ref Main//included_with_range, Op, unloaded +// CHECK:STDOUT: %Main.import_ref.7d9: %I.Op.type = import_ref Main//included_with_range, loc8_22, loaded [concrete = constants.%I.Op] +// CHECK:STDOUT: %Main.import_ref.5dd: %I.type = import_ref Main//included_with_range, inst20 [no loc], loaded [symbolic = constants.%Self.826] +// CHECK:STDOUT: %Main.import_ref.d7a: = import_ref Main//included_with_range, loc13_15, loaded [concrete = constants.%I.impl_witness] +// CHECK:STDOUT: %Main.import_ref.29a: type = import_ref Main//included_with_range, loc13_8, loaded [concrete = constants.%C] +// CHECK:STDOUT: %Main.import_ref.301: type = import_ref Main//included_with_range, loc13_13, loaded [concrete = constants.%I.type] +// CHECK:STDOUT: %Main.import_ref.34c = import_ref Main//included_with_range, loc12_9, unloaded +// CHECK:STDOUT: %Main.import_ref.0ed: type = import_ref Main//included_with_range, loc12_9, loaded [concrete = constants.%C] +// CHECK:STDOUT: %Main.import_ref.cb9: type = import_ref Main//included_with_range, inst63 [no loc], loaded [concrete = constants.%Destroy.type] +// CHECK:STDOUT: %Main.import_ref.003: %C.as.I.impl.Op.type = import_ref Main//included_with_range, loc14_25, loaded [concrete = constants.%C.as.I.impl.Op] +// CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (%Main.import_ref.003), @C.as.I.impl [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [concrete] { +// CHECK:STDOUT: .I = imports.%Main.I +// CHECK:STDOUT: .C = imports.%Main.C +// CHECK:STDOUT: .Core = imports.%Core.ece +// CHECK:STDOUT: .F = %F.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core.import = import Core +// CHECK:STDOUT: %default.import = import +// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] { +// CHECK:STDOUT: %c.patt: %pattern_type.c48 = binding_pattern c [concrete] +// CHECK:STDOUT: %c.param_patt: %pattern_type.c48 = value_param_pattern %c.patt, call_param0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: %c.param: %C = value_param call_param0 +// CHECK:STDOUT: %C.ref: type = name_ref C, imports.%Main.C [concrete = constants.%C] +// CHECK:STDOUT: %c: %C = bind_name c, %c.param +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: interface @I [from "exclude/included_with_range.carbon"] { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = imports.%Main.import_ref.e5d +// CHECK:STDOUT: .Op = imports.%Main.import_ref.9cd +// CHECK:STDOUT: witness = (imports.%Main.Op.ca5) +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: impl @C.as.I.impl: imports.%Main.import_ref.29a as imports.%Main.import_ref.301 [from "exclude/included_with_range.carbon"] { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: witness = imports.%Main.import_ref.d7a +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: impl @C.as.Destroy.impl: imports.%Main.import_ref.0ed as imports.%Main.import_ref.cb9 [from "exclude/included_with_range.carbon"] { +// CHECK:STDOUT: !members: +// CHECK:STDOUT: witness = imports.%Main.import_ref.34c +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: class @C [from "exclude/included_with_range.carbon"] { +// CHECK:STDOUT: complete_type_witness = imports.%Main.import_ref.8f2 +// CHECK:STDOUT: +// CHECK:STDOUT: !members: +// CHECK:STDOUT: .Self = imports.%Main.import_ref.2c4 +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%c.param: %C) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %c.ref: %C = name_ref c, %c +// CHECK:STDOUT: %I.ref: type = name_ref I, imports.%Main.I [concrete = constants.%I.type] +// CHECK:STDOUT: %Op.ref: %I.assoc_type = name_ref Op, imports.%Main.import_ref.9cd [concrete = constants.%assoc0] +// CHECK:STDOUT: %impl.elem0: %.36a = impl_witness_access constants.%I.impl_witness, element0 [concrete = constants.%C.as.I.impl.Op] +// CHECK:STDOUT: %bound_method: = bound_method %c.ref, %impl.elem0 +// CHECK:STDOUT: %C.as.I.impl.Op.call: init %empty_tuple.type = call %bound_method(%c.ref) +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: generic fn @I.Op(imports.%Main.import_ref.5dd: %I.type) [from "exclude/included_with_range.carbon"] { +// CHECK:STDOUT: %Self: %I.type = bind_symbolic_name Self, 0 [symbolic = %Self (constants.%Self.826)] +// CHECK:STDOUT: %Self.as_type: type = facet_access_type %Self [symbolic = %Self.as_type (constants.%Self.as_type)] +// CHECK:STDOUT: %pattern_type: type = pattern_type %Self.as_type [symbolic = %pattern_type (constants.%pattern_type.6de)] +// CHECK:STDOUT: +// CHECK:STDOUT: fn; +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @C.as.I.impl.Op [from "exclude/included_with_range.carbon"]; +// CHECK:STDOUT: +// CHECK:STDOUT: specific @I.Op(constants.%Self.826) { +// CHECK:STDOUT: %Self => constants.%Self.826 +// CHECK:STDOUT: %Self.as_type => constants.%Self.as_type +// CHECK:STDOUT: %pattern_type => constants.%pattern_type.6de +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: --- import_excluded.carbon +// CHECK:STDOUT: +// CHECK:STDOUT: constants { +// CHECK:STDOUT: %C: type = class_type @C [concrete] +// CHECK:STDOUT: %pattern_type.c48: type = pattern_type %C [concrete] +// CHECK:STDOUT: %F.type: type = fn_type @F [concrete] +// CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete] +// CHECK:STDOUT: %F: %F.type = struct_value () [concrete] +// CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete] +// CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete] +// CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, imports.%Main.import_ref.7d9 [concrete] +// CHECK:STDOUT: %I.Op.type: type = fn_type @I.Op [concrete] +// CHECK:STDOUT: %I.Op: %I.Op.type = struct_value () [concrete] +// CHECK:STDOUT: %I.impl_witness: = impl_witness imports.%I.impl_witness_table [concrete] +// CHECK:STDOUT: %I.facet: %I.type = facet_value %C, (%I.impl_witness) [concrete] +// CHECK:STDOUT: %.36a: type = fn_type_with_self_type %I.Op.type, %I.facet [concrete] +// CHECK:STDOUT: %C.as.I.impl.Op.type: type = fn_type @C.as.I.impl.Op [concrete] +// CHECK:STDOUT: %C.as.I.impl.Op: %C.as.I.impl.Op.type = struct_value () [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: imports { +// CHECK:STDOUT: %Main.I: type = import_ref Main//excluded_with_range, I, loaded [concrete = constants.%I.type] +// CHECK:STDOUT: %Main.C: type = import_ref Main//excluded_with_range, C, loaded [concrete = constants.%C] +// CHECK:STDOUT: %Core.ece: = namespace file.%Core.import, [concrete] { +// CHECK:STDOUT: import Core//prelude +// CHECK:STDOUT: import Core//prelude/... +// CHECK:STDOUT: } +// CHECK:STDOUT: %Main.import_ref.9cd: %I.assoc_type = import_ref Main//excluded_with_range, loc6_22, loaded [concrete = constants.%assoc0] +// CHECK:STDOUT: %Main.import_ref.7d9: %I.Op.type = import_ref Main//excluded_with_range, loc6_22, loaded [concrete = constants.%I.Op] +// CHECK:STDOUT: %Main.import_ref.003: %C.as.I.impl.Op.type = import_ref Main//excluded_with_range, loc12_25, loaded [concrete = constants.%C.as.I.impl.Op] +// CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (%Main.import_ref.003), @C.as.I.impl [concrete] +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: file { +// CHECK:STDOUT: package: = namespace [concrete] { +// CHECK:STDOUT: .I = imports.%Main.I +// CHECK:STDOUT: .C = imports.%Main.C +// CHECK:STDOUT: .Core = imports.%Core.ece +// CHECK:STDOUT: .F = %F.decl +// CHECK:STDOUT: } +// CHECK:STDOUT: %Core.import = import Core +// CHECK:STDOUT: %default.import = import +// CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] { +// CHECK:STDOUT: %c.patt: %pattern_type.c48 = binding_pattern c [concrete] +// CHECK:STDOUT: %c.param_patt: %pattern_type.c48 = value_param_pattern %c.patt, call_param0 [concrete] +// CHECK:STDOUT: } { +// CHECK:STDOUT: %c.param: %C = value_param call_param0 +// CHECK:STDOUT: %C.ref: type = name_ref C, imports.%Main.C [concrete = constants.%C] +// CHECK:STDOUT: %c: %C = bind_name c, %c.param +// CHECK:STDOUT: } +// CHECK:STDOUT: } +// CHECK:STDOUT: +// CHECK:STDOUT: fn @F(%c.param: %C) { +// CHECK:STDOUT: !entry: +// CHECK:STDOUT: %c.ref: %C = name_ref c, %c +// CHECK:STDOUT: %I.ref: type = name_ref I, imports.%Main.I [concrete = constants.%I.type] +// CHECK:STDOUT: %Op.ref: %I.assoc_type = name_ref Op, imports.%Main.import_ref.9cd [concrete = constants.%assoc0] +// CHECK:STDOUT: %impl.elem0: %.36a = impl_witness_access constants.%I.impl_witness, element0 [concrete = constants.%C.as.I.impl.Op] +// CHECK:STDOUT: %bound_method: = bound_method %c.ref, %impl.elem0 +// CHECK:STDOUT: %C.as.I.impl.Op.call: init %empty_tuple.type = call %bound_method(%c.ref) +// CHECK:STDOUT: return +// CHECK:STDOUT: } +// CHECK:STDOUT: diff --git a/toolchain/docs/adding_features.md b/toolchain/docs/adding_features.md index 0f4dae746d3b..aa67a3fe57e3 100644 --- a/toolchain/docs/adding_features.md +++ b/toolchain/docs/adding_features.md @@ -29,6 +29,7 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - [Verbose output](#verbose-output) - [Stack traces](#stack-traces) - [Dumping objects in interactive debuggers](#dumping-objects-in-interactive-debuggers) + - [Dumping prelude files](#dumping-prelude-files) @@ -539,8 +540,10 @@ automatically included as well. A small amount of SemIR may include a number of related instructions, such as an in-range instruction referencing an import_ref referencing a constant referencing another constant. -> NOTE: In a test, if full SemIR is desired for files, add -> `// EXTRA-ARGS: --dump-sem-ir-ranges=if-present` with an explanation why. +SemIR dumps for files that don't have explicit ranges can be enabled through +either `//@include-in-dumps` (per-file) or +`// EXTRA-ARGS: --dump-sem-ir-ranges=if-present`. These should be rare, and are +worth comments when they're used. ##### Example uses @@ -634,3 +637,13 @@ regarding support. Objects which inherit from `Printable` also have `Dump` member functions, but these will lack contextual information. + +#### Dumping prelude files + +By default, prelude files are excluded from dumps by +`--exclude-dump-file-prefix`. To enable dumps for specific files, add +`//@include-in-dumps`. This works for every phase after lex, but may be most +helpful to debug check and lower output. This can also be used to view +cross-file SemIR, such as imports from a prelude, by adding +`//@include-in-dumps` to the prelude file and looking at the SemIR of the +importing file. diff --git a/toolchain/driver/compile_subcommand.cpp b/toolchain/driver/compile_subcommand.cpp index f0414b49247d..2b78c37ea83d 100644 --- a/toolchain/driver/compile_subcommand.cpp +++ b/toolchain/driver/compile_subcommand.cpp @@ -470,6 +470,9 @@ class CompilationUnit { auto FlushForStackTrace() -> void { consumer_->Flush(); } auto input_filename() -> llvm::StringRef { return input_filename_; } + auto has_include_in_dumps() -> bool { + return tokens_ && tokens_->has_include_in_dumps(); + } auto success() -> bool { return success_; } auto has_source() -> bool { return source_.has_value(); } auto get_trees_and_subtrees() -> Parse::GetTreeAndSubtreesFn { @@ -551,17 +554,35 @@ class MultiUnitCache { llvm::ArrayRef> units) : options_(options), units_(units) {} + // If `include_in_dumps` is in use, we need to apply per-file include + // settings. + auto ApplyPerFileIncludeInDumps() -> void { + if (!include_in_dumps_) { + // No cached value to update. + return; + } + for (const auto& [i, unit] : llvm::enumerate(units_)) { + if (unit->has_include_in_dumps()) { + include_in_dumps_->Set(SemIR::CheckIRId(i), true); + } + } + } + auto include_in_dumps() -> const IncludeInDumpsStore& { if (!include_in_dumps_) { include_in_dumps_.emplace( IncludeInDumpsStore::MakeWithExplicitSize(units_.size(), false)); for (const auto& [i, unit] : llvm::enumerate(units_)) { - include_in_dumps_->Set( - SemIR::CheckIRId(i), + // If this is first accessed after lexing is complete, we need to apply + // per-file includes. Otherwise, this is based only on the exclude + // option. + bool include = + unit->has_include_in_dumps() || llvm::none_of(options_->exclude_dump_file_prefixes, [&](auto prefix) { return unit->input_filename().starts_with(prefix); - })); + }); + include_in_dumps_->Set(SemIR::CheckIRId(i), include); } } return *include_in_dumps_; @@ -998,6 +1019,7 @@ auto CompileSubcommand::Run(DriverEnv& driver_env) -> DriverResult { if (options_.phase == CompileOptions::Phase::Lex) { return make_result(); } + cache.ApplyPerFileIncludeInDumps(); // Parse and check phases examine `has_source` because they want to proceed if // lex failed, but not if source doesn't exist. Later steps are skipped if // anything failed, so don't need this. diff --git a/toolchain/lex/lex.cpp b/toolchain/lex/lex.cpp index 427e54dac38a..28bffb6b9f11 100644 --- a/toolchain/lex/lex.cpp +++ b/toolchain/lex/lex.cpp @@ -974,6 +974,11 @@ auto Lexer::LexComment(llvm::StringRef source_text, ssize_t& position) -> void { if (position + 2 < static_cast(source_text.size()) && LLVM_UNLIKELY(!IsSpace(source_text[position + 2]))) { llvm::StringRef comment_text = source_text.substr(position); + if (comment_text.starts_with("//@include-in-dumps\n")) { + buffer_.has_include_in_dumps_ = true; + AdvanceToLine(source_text, position, next_line()); + return; + } if (comment_text.starts_with("//@dump-sem-ir-begin\n")) { BeginDumpSemIRRange(comment_text.begin()); AdvanceToLine(source_text, position, next_line()); @@ -984,7 +989,6 @@ auto Lexer::LexComment(llvm::StringRef source_text, ssize_t& position) -> void { AdvanceToLine(source_text, position, next_line()); return; } - CARBON_DIAGNOSTIC(NoWhitespaceAfterCommentIntroducer, Error, "whitespace is required after '//'"); emitter_.Emit(comment_text.begin() + 2, NoWhitespaceAfterCommentIntroducer); diff --git a/toolchain/lex/testdata/include_in_dumps.carbon b/toolchain/lex/testdata/include_in_dumps.carbon new file mode 100644 index 000000000000..1a49c5c52ab5 --- /dev/null +++ b/toolchain/lex/testdata/include_in_dumps.carbon @@ -0,0 +1,17 @@ +// Part of the Carbon Language project, under the Apache License v2.0 with LLVM +// Exceptions. See /LICENSE for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// EXTRA-ARGS: --exclude-dump-file-prefix= +// +// AUTOUPDATE +// TIP: To test this file alone, run: +// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lex/testdata/include_in_dumps.carbon +// TIP: To dump output, run: +// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lex/testdata/include_in_dumps.carbon + +// --- enable.carbon + +//@include-in-dumps + +// --- not_enabled.carbon diff --git a/toolchain/lex/tokenized_buffer.cpp b/toolchain/lex/tokenized_buffer.cpp index d9bd91d61d19..b1beb62135d6 100644 --- a/toolchain/lex/tokenized_buffer.cpp +++ b/toolchain/lex/tokenized_buffer.cpp @@ -236,6 +236,10 @@ auto TokenizedBuffer::Print(llvm::raw_ostream& output_stream, output_stream << "\n"; } + if (has_include_in_dumps_) { + output_stream << " has_include_in_dumps: true\n"; + } + if (!dump_sem_ir_ranges_.empty()) { output_stream << " dump_sem_ir_ranges:\n"; for (auto range : dump_sem_ir_ranges_) { diff --git a/toolchain/lex/tokenized_buffer.h b/toolchain/lex/tokenized_buffer.h index cdf57fc482be..75a8d5188d71 100644 --- a/toolchain/lex/tokenized_buffer.h +++ b/toolchain/lex/tokenized_buffer.h @@ -232,6 +232,8 @@ class TokenizedBuffer : public Printable { auto comments_size() const -> size_t { return comments_.size(); } + auto has_include_in_dumps() const -> bool { return has_include_in_dumps_; } + // Returns true if any `DumpSemIRRange`s were provided. auto has_dump_sem_ir_ranges() const -> bool { return !dump_sem_ir_ranges_.empty(); @@ -333,7 +335,12 @@ class TokenizedBuffer : public Printable { // Comments in the file. ValueStore comments_; - // A range of tokens marked by `//@dump-semir-[begin|end]`. + // Whether SemIR dumping is explicitly enabled for this file. This is marked + // by `//@include-in-dumps`, and overrides other file-inclusion selection + // choices. It can be combined with ranges. + bool has_include_in_dumps_ = false; + + // A range of tokens marked by `//@dump-sem-ir-[begin|end]`. // // The particular syntax was chosen because it can be lexed efficiently. It // only occurs in invalid comment strings, so shouldn't slow down lexing of