Files
carbon-lang/toolchain/check/import_ref.h
T
Richard Smith e8cd229e74 When performing an impl lookup, only import impls for related interfaces. (#6040)
This avoids impl lookups involving, say, `Core.Int` pulling in all ~65
impls in "prelude/types/int", which resulted in a lot of unnecessary
importing work, followed by a lot of unnecessary inst namer and inst
formatter work.

Before:
```
Ran 1335 tests in 6186 ms wall time, 146818 ms across threads
  Slowest tests:
  - toolchain/check/testdata/interop/cpp/function/arithmetic_types_bridged.carbon: 5611 ms, 5532 ms in Run
  - toolchain/check/testdata/interop/cpp/function/operators.carbon: 2034 ms, 1981 ms in Run
  - toolchain/check/testdata/primitives/import_symbolic.carbon: 1796 ms, 1786 ms in Run
  - toolchain/lower/testdata/operators/arithmetic.carbon: 1729 ms, 1728 ms in Run
  - toolchain/lower/testdata/function/generic/call_recursive_sccs_deep.carbon: 1700 ms, 1697 ms in Run
[==========] 1335 tests from 1 test suite ran. (682 ms total)
```

After:
```
Ran 1335 tests in 2419 ms wall time, 109587 ms across threads
  Slowest tests:
  - toolchain/check/testdata/interop/cpp/function/arithmetic_types_bridged.carbon: 1748 ms, 1665 ms in Run
  - toolchain/check/testdata/interop/cpp/function/operators.carbon: 1106 ms, 1057 ms in Run
  - toolchain/lower/testdata/function/generic/call_recursive_diamond.carbon: 1044 ms, 1041 ms in Run
  - toolchain/lower/testdata/function/generic/call_recursive_sccs_deep.carbon: 1015 ms, 1012 ms in Run
  - toolchain/lower/testdata/operators/arithmetic.carbon: 998 ms, 997 ms in Run
[==========] 1335 tests from 1 test suite ran. (652 ms total)
```

That's still slower than it should be, but a large improvement
nonetheless.

Fixes #6029
2025-09-10 21:40:27 +00:00

62 lines
2.7 KiB
C++

// 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
#ifndef CARBON_TOOLCHAIN_CHECK_IMPORT_REF_H_
#define CARBON_TOOLCHAIN_CHECK_IMPORT_REF_H_
#include "toolchain/check/context.h"
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
// Sets the IRs for `ImportIRId::ApiForImpl` from `import_ir`, and
// `ImportIRId::Cpp` from defaults. Should be called before `AddImportIR` in
// order to ensure the correct IDs are assigned.
auto SetSpecialImportIRs(Context& context, SemIR::ImportIR import_ir) -> void;
// Adds an ImportIR, returning the ID. May use an existing ID if already added.
auto AddImportIR(Context& context, SemIR::ImportIR import_ir)
-> SemIR::ImportIRId;
// Adds an import_ref instruction for the specified instruction in the
// specified IR. The import_ref is initially marked as unused.
auto AddImportRef(Context& context, SemIR::ImportIRInst import_ir_inst,
SemIR::EntityNameId entity_name_id) -> SemIR::InstId;
// Returns the canonical IR inst for an entity. Returns an `ImportIRInst` with
// a `None` ir_id for an entity that was not imported. This may add an
// `ImportIR` if it finds an indirectly referenced `File` that hasn't previously
// been found.
auto GetCanonicalImportIRInst(Context& context, SemIR::InstId inst_id)
-> SemIR::ImportIRInst;
// Verifies a new instruction is the same as a previous instruction.
// prev_import_ir_inst should come from GetCanonicalImportIRInst.
auto VerifySameCanonicalImportIRInst(Context& context, SemIR::NameId name_id,
SemIR::InstId prev_id,
SemIR::ImportIRInst prev_import_ir_inst,
SemIR::ImportIRId new_ir_id,
const SemIR::File* new_import_ir,
SemIR::InstId new_inst_id) -> void;
// If the passed in instruction ID is an ImportRefUnloaded, turns it into an
// ImportRefLoaded for use.
auto LoadImportRef(Context& context, SemIR::InstId inst_id) -> void;
// Load all impls declared in the api file corresponding to this impl file.
auto ImportImplsFromApiFile(Context& context) -> void;
// Load an impl that is declared in an imported IR.
auto ImportImpl(Context& context, SemIR::ImportIRId import_ir_id,
SemIR::ImplId impl_id) -> void;
// Load an interface that is declared in an imported IR.
auto ImportInterface(Context& context, SemIR::ImportIRId import_ir_id,
SemIR::InterfaceId interface_id) -> SemIR::InterfaceId;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_IMPORT_REF_H_