Files
carbon-lang/toolchain/sem_ir/impl.h
T
Jon Ross-Perkinsandjosh11b f1190a4792 Add basic output of where memory is stored after a compile. (#4136)
The output is really basic, I'm just adding this to help track how
memory is allocated.

```
---
filename:        'check/testdata/expr_category/in_place_tuple_init.carbon'
source_:
  used_bytes:      8057
  reserved_bytes:  8057
tokens_.allocator_:
  used_bytes:      0
  reserved_bytes:  0
tokens_.token_infos_:
  used_bytes:      1040
  reserved_bytes:  2032

(eliding)

value_stores_.string_literals_.set_:
  used_bytes:      320
  reserved_bytes:  320
Total:
  used_bytes:      20609
  reserved_bytes:  29437
...
```

---------

Co-authored-by: josh11b <15258583+josh11b@users.noreply.github.com>
2024-07-16 23:11:01 +00:00

95 lines
3.2 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_SEM_IR_IMPL_H_
#define CARBON_TOOLCHAIN_SEM_IR_IMPL_H_
#include "common/map.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::SemIR {
// An implementation of a constraint.
struct Impl : public Printable<Impl> {
auto Print(llvm::raw_ostream& out) const -> void {
out << "{self: " << self_id << ", constraint: " << constraint_id << "}";
}
// Determines whether this impl has been fully defined. This is false until we
// reach the `}` of the impl definition.
auto is_defined() const -> bool { return witness_id.is_valid(); }
// Determines whether this impl's definition has begun but not yet ended.
auto is_being_defined() const -> bool {
return definition_id.is_valid() && !is_defined();
}
// The following members always have values, and do not change throughout the
// lifetime of the interface.
// TODO: Track the generic parameters for `impl forall`.
// The type for which the impl is implementing a constraint.
TypeId self_id;
// The constraint that the impl implements.
TypeId constraint_id;
// The following members are set at the `{` of the impl definition.
// The definition of the impl. This is an ImplDecl.
InstId definition_id = InstId::Invalid;
// The impl scope.
NameScopeId scope_id = NameScopeId::Invalid;
// The first block of the impl body.
// TODO: Handle control flow in the impl body, such as if-expressions.
InstBlockId body_block_id = InstBlockId::Invalid;
// The following members are set at the `}` of the impl definition.
// The witness for the impl. This can be `BuiltinError`.
InstId witness_id = InstId::Invalid;
};
// A collection of `Impl`s, which can be accessed by the self type and
// constraint implemented.
class ImplStore {
public:
// Looks up the impl with this self type and constraint, or creates a new
// `Impl` if none exists.
// TODO: Handle parameters.
auto LookupOrAdd(TypeId self_id, TypeId constraint_id) -> ImplId {
auto result = lookup_.Insert(std::pair{self_id, constraint_id}, [&]() {
return values_.Add({.self_id = self_id, .constraint_id = constraint_id});
});
return result.value();
}
// Returns a mutable value for an ID.
auto Get(ImplId id) -> Impl& { return values_.Get(id); }
// Returns the value for an ID.
auto Get(ImplId id) const -> const Impl& { return values_.Get(id); }
auto OutputYaml() const -> Yaml::OutputMapping {
return values_.OutputYaml();
}
// Collects memory usage of members.
auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
-> void {
mem_usage.Collect(MemUsage::ConcatLabel(label, "values_"), values_);
mem_usage.Add(MemUsage::ConcatLabel(label, "lookup_"), lookup_);
}
auto array_ref() const -> llvm::ArrayRef<Impl> { return values_.array_ref(); }
auto size() const -> size_t { return values_.size(); }
private:
ValueStore<ImplId> values_;
Map<std::pair<TypeId, TypeId>, ImplId> lookup_;
};
} // namespace Carbon::SemIR
#endif // CARBON_TOOLCHAIN_SEM_IR_IMPL_H_