Store contents of incomplete node blocks on the node block stack. (#3223)

`SemIR` now only stores complete blocks, and placeholders for blocks
where we need a block ID before we know the block contents.

This is a preparation step towards the new node block allocation design.
This commit is contained in:
Richard Smith authored and GitHub committed 2023-09-13 23:28:13 +00:00
1 parent 9ba1669472
commit a66f35e92c
9 files changed
+123 -84

No files matched your search

+31 -15
View File
@@ -39,8 +39,8 @@ class NodeBlockStack {
// Peeks at the top node block. This does not trigger lazy allocation, so the
// returned node block may be invalid.
auto Peek() -> SemIR::NodeBlockId {
CARBON_CHECK(!stack_.empty()) << "no current block";
return stack_.back();
CARBON_CHECK(!empty()) << "no current block";
return stack_[size() - 1].id;
}
// Returns the top node block, allocating one if it's still invalid. If
@@ -52,16 +52,10 @@ class NodeBlockStack {
// SemIR::NodeBlockId::Empty is returned if one wasn't allocated.
auto Pop() -> SemIR::NodeBlockId;
// Pops the top node block, ensuring that it is lazily allocated if it's
// empty. For use when more nodes will be added to the block later.
auto PopForAdd() -> SemIR::NodeBlockId {
PeekForAdd();
return Pop();
}
// Adds the given node ID to the block at the top of the stack.
auto AddNodeId(SemIR::NodeId node_id) -> void {
semantics_ir_->AddNodeIdForNodeBlockStack(PeekForAdd(), node_id);
CARBON_CHECK(!empty()) << "no current block";
stack_[size_ - 1].content.push_back(node_id);
}
// Returns whether the current block is statically reachable.
@@ -72,10 +66,30 @@ class NodeBlockStack {
// Prints the stack for a stack dump.
auto PrintForStackDump(llvm::raw_ostream& output) const -> void;
auto empty() const -> bool { return stack_.empty(); }
auto size() const -> size_t { return stack_.size(); }
auto empty() const -> bool { return size() == 0; }
auto size() const -> int { return size_; }
private:
struct StackEntry {
// Preallocate an arbitrary size for the stack entries.
// TODO: Perform measurements to pick a good starting size to avoid
// reallocation.
StackEntry() { content.reserve(32); }
auto Reset(SemIR::NodeBlockId new_id) {
id = new_id;
content.clear();
}
// The block ID, if one has been allocated, Invalid if no block has been
// allocated, or Unreachable if this block is known to be unreachable.
SemIR::NodeBlockId id = SemIR::NodeBlockId::Invalid;
// The content of the block. Stored as a vector rather than as a SmallVector
// to reduce the cost of resizing `stack_` and performing swaps.
std::vector<SemIR::NodeId> content;
};
// A name for debugging.
llvm::StringLiteral name_;
@@ -86,9 +100,11 @@ class NodeBlockStack {
llvm::raw_ostream* vlog_stream_;
// The actual stack.
// PushEntry and PopEntry control modification in order to centralize
// vlogging.
llvm::SmallVector<SemIR::NodeBlockId> stack_;
llvm::SmallVector<StackEntry> stack_;
// The size of the stack. Entries after this in `stack_` are kept around so
// that we can reuse the allocated buffer for their content.
int size_ = 0;
};
} // namespace Carbon::Check