mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-17 14:20:37 +00:00
Switch from a fully lowering-oriented approach to initializing
expressions in semantics IR to an approach that retains more information
about the high-level semantics of the program. Specifically:
- Always form an `Assign` node for a variable initialization or
assignment, even for empty types and when the actual initialization is
performed in-place by evaluating the right-hand side.
- Always form a `ReturnExpression` node for a return statement with an
expression, even when the actual initialization is performed in-place
into the return slot and the function doesn't return a value.
This is intended to permit us to more easily write semantic checks over
the IR, and to preserve a use edge from the use of an initializing
expression and its initializer in all cases.
To better support this, `MaterializeTemporary` is also split into two
nodes:
- A `TemporaryStorage` node allocates and provides the storage for a
temporary, and is emitted prior to emitting the initialization of the
temporary.
- A `Temporary` node connects the temporary storage to the initializer,
and is emitted after emitting the initialization.
Example testcase:
```carbon
fn F() -> (i32, i32);
fn G() -> (i32, i32) {
var v: (i32, i32) = F();
v = F();
return F();
}
fn H() -> i32 {
return G()[0];
}
```
Before:
```carbon-semir
fn @G() -> %return: (i32, i32) {
// ...
%v: (i32, i32) = var "v"
%.loc4_24: (i32, i32) = call @F() to %v
%.loc5: (i32, i32) = call @F() to %v
%.loc6: (i32, i32) = call @F() to %return
return
}
fn @H() -> i32 {
!entry:
%.loc10_11.1: (i32, i32) = materialize_temporary
%.loc10_11.2: (i32, i32) = call @G() to %.loc10_11.1
%.loc10_14: i32 = int_literal 0
%.loc10_15.1: i32 = tuple_index %.loc10_11.1, %.loc10_14
%.loc10_15.2: i32 = bind_value %.loc10_15.1
return %.loc10_15.2
}
```
After:
```carbon-semir
fn @G() -> %return: (i32, i32) {
// ...
%v: (i32, i32) = var "v"
%.loc4_24: (i32, i32) = call @F() to %v
assign %v, %.loc4_24
%.loc5: (i32, i32) = call @F() to %v
assign %v, %.loc5
%.loc6: (i32, i32) = call @F() to %return
return %.loc6
}
fn @H() -> i32 {
!entry:
%.loc10_11.1: (i32, i32) = temporary_storage
%.loc10_11.2: (i32, i32) = call @G() to %.loc10_11.1
%.loc10_14: i32 = int_literal 0
%.loc10_11.3: (i32, i32) = temporary %.loc10_11.1, %.loc10_11.2
%.loc10_15.1: i32 = tuple_index %.loc10_11.3, %.loc10_14
%.loc10_15.2: i32 = bind_value %.loc10_15.1
return %.loc10_15.2
}
```
Note that in the "Before" code, there is no indication in `G()` that we
assigned to `v`, and no path through "use" edges from the tuple indexing
in `H()` to the call to `G()`.
This change results in our not performing `memcpy`s into the destination
for tuple and struct initializers. This is a consequence of those
initializers not yet performing in-place initialization, and will be
fixed in a subsequent change.
---------
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
150 lines
5.7 KiB
C++
150 lines
5.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_LOWERING_LOWERING_FUNCTION_CONTEXT_H_
|
|
#define CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "toolchain/lowering/lowering_context.h"
|
|
#include "toolchain/semantics/semantics_ir.h"
|
|
#include "toolchain/semantics/semantics_node.h"
|
|
|
|
namespace Carbon::Lower {
|
|
|
|
// Context and shared functionality for lowering handlers that produce an
|
|
// `llvm::Function` definition.
|
|
class FunctionContext {
|
|
public:
|
|
explicit FunctionContext(FileContext& file_context, llvm::Function* function);
|
|
|
|
// Returns a basic block corresponding to the start of the given semantics
|
|
// block, and enqueues it for emission.
|
|
auto GetBlock(SemIR::NodeBlockId block_id) -> llvm::BasicBlock*;
|
|
|
|
// If we have not yet allocated a `BasicBlock` for this `block_id`, set it to
|
|
// `block`, and enqueue `block_id` for emission. Returns whether we set the
|
|
// block.
|
|
auto TryToReuseBlock(SemIR::NodeBlockId block_id, llvm::BasicBlock* block)
|
|
-> bool;
|
|
|
|
// Returns a phi node corresponding to the block argument of the given basic
|
|
// block.
|
|
auto GetBlockArg(SemIR::NodeBlockId block_id, SemIR::TypeId type_id)
|
|
-> llvm::PHINode*;
|
|
|
|
// Returns a local (versus global) value for the given node.
|
|
auto GetLocal(SemIR::NodeId node_id) -> llvm::Value* {
|
|
// All builtins are types, with the same empty lowered value.
|
|
if (node_id.index < SemIR::BuiltinKind::ValidCount) {
|
|
return GetTypeAsValue();
|
|
}
|
|
|
|
auto it = locals_.find(node_id);
|
|
CARBON_CHECK(it != locals_.end()) << "Missing local: " << node_id;
|
|
return it->second;
|
|
}
|
|
|
|
// Returns a local (versus global) value for the given node in loaded state.
|
|
// Loads will only be inserted on an as-needed basis.
|
|
auto GetLocalLoaded(SemIR::NodeId node_id) -> llvm::Value*;
|
|
|
|
// Sets the value for the given node.
|
|
auto SetLocal(SemIR::NodeId node_id, llvm::Value* value) {
|
|
bool added = locals_.insert({node_id, value}).second;
|
|
CARBON_CHECK(added) << "Duplicate local insert: " << node_id;
|
|
}
|
|
|
|
// Returns the requested index into val based on whether val is a pointer
|
|
// type.
|
|
auto GetIndexFromStructOrArray(llvm::Type* llvm_type, llvm::Value* val,
|
|
unsigned idx, const llvm::Twine& name)
|
|
-> llvm::Value* {
|
|
return val->getType()->isPointerTy()
|
|
? builder().CreateStructGEP(llvm_type, val, idx, name)
|
|
: builder().CreateExtractValue(val, idx, name);
|
|
}
|
|
|
|
// Gets a callable's function.
|
|
auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
|
|
return file_context_->GetFunction(function_id);
|
|
}
|
|
|
|
// Returns a lowered type for the given type_id.
|
|
auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
|
|
return file_context_->GetType(type_id);
|
|
}
|
|
|
|
// Returns a lowered value to use for a value of type `type`.
|
|
auto GetTypeAsValue() -> llvm::Value* {
|
|
return file_context_->GetTypeAsValue();
|
|
}
|
|
|
|
// Create a synthetic block that corresponds to no SemIR::NodeBlockId. Such
|
|
// a block should only ever have a single predecessor, and is used when we
|
|
// need multiple `llvm::BasicBlock`s to model the linear control flow in a
|
|
// single SemIR::File block.
|
|
auto CreateSyntheticBlock() -> llvm::BasicBlock*;
|
|
|
|
// Determine whether block is the most recently created synthetic block.
|
|
auto IsCurrentSyntheticBlock(llvm::BasicBlock* block) -> bool {
|
|
return synthetic_block_ == block;
|
|
}
|
|
|
|
// After emitting an initializer `init_id`, finishes performing the
|
|
// initialization of `dest_id` from that initializer. This is a no-op if the
|
|
// initialization was performed in-place, and otherwise performs a store or a
|
|
// copy.
|
|
auto FinishInitialization(SemIR::TypeId type_id, SemIR::NodeId dest_id,
|
|
SemIR::NodeId init_id) -> void;
|
|
|
|
auto llvm_context() -> llvm::LLVMContext& {
|
|
return file_context_->llvm_context();
|
|
}
|
|
auto llvm_module() -> llvm::Module& { return file_context_->llvm_module(); }
|
|
auto builder() -> llvm::IRBuilder<>& { return builder_; }
|
|
auto semantics_ir() -> const SemIR::File& {
|
|
return file_context_->semantics_ir();
|
|
}
|
|
|
|
private:
|
|
// Emits a value copy for type `type_id` from `source_id` to `dest_id`.
|
|
// `source_id` must produce a value representation for `type_id`, and
|
|
// `dest_id` must be a pointer to a `type_id` object.
|
|
auto CopyValue(SemIR::TypeId type_id, SemIR::NodeId source_id,
|
|
SemIR::NodeId dest_id) -> void;
|
|
|
|
// Context for the overall lowering process.
|
|
FileContext* file_context_;
|
|
|
|
// The IR function we're generating.
|
|
llvm::Function* function_;
|
|
|
|
llvm::IRBuilder<> builder_;
|
|
|
|
// Maps a function's SemIR::File blocks to lowered blocks.
|
|
llvm::DenseMap<SemIR::NodeBlockId, llvm::BasicBlock*> blocks_;
|
|
|
|
// The synthetic block we most recently created. May be null if there is no
|
|
// such block.
|
|
llvm::BasicBlock* synthetic_block_ = nullptr;
|
|
|
|
// Maps a function's SemIR::File nodes to lowered values.
|
|
// TODO: Handle nested scopes. Right now this is just cleared at the end of
|
|
// every block.
|
|
llvm::DenseMap<SemIR::NodeId, llvm::Value*> locals_;
|
|
};
|
|
|
|
// Declare handlers for each SemIR::File node.
|
|
#define CARBON_SEMANTICS_NODE_KIND(Name) \
|
|
auto Handle##Name(FunctionContext& context, SemIR::NodeId node_id, \
|
|
SemIR::Node node) \
|
|
->void;
|
|
#include "toolchain/semantics/semantics_node_kind.def"
|
|
|
|
} // namespace Carbon::Lower
|
|
|
|
#endif // CARBON_TOOLCHAIN_LOWERING_LOWERING_FUNCTION_CONTEXT_H_
|