Files
carbon-lang/toolchain/semantics/semantics_node_kind.def
T
Richard SmithandJon Ross-Perkins e41a165359 Retain information about how an initializing expression is used in semantics IR (#3173)
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>
2023-08-30 23:29:23 +00:00

102 lines
5.9 KiB
Modula-2

// 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
//
// This is an X-macro header. It does not use `#include` guards, and instead is
// designed to be `#include`ed after the x-macro is defined in order for its
// inclusion to expand to the desired output. Macro definitions are cleaned up
// at the end of this file.
//
// Exactly one of these macros should be defined before including this header:
// - CARBON_SEMANTICS_NODE_KIND(Name)
// Invoked for each kind of semantic node.
// - CARBON_SEMANTICS_NODE_KIND_WITH_VALUE_KIND(Name, TypeFieldKind)
// Invoked for each kind of semantic node, along with information about
// whether the node produces a value, and if so, what kind of value.
// - CARBON_SEMANTICS_NODE_KIND_WITH_TERMINATOR_KIND(Name, TerminatorKind)
// Invoked for each kind of semantic node, along with information about
// whether the node is a terminator node.
// - CARBON_SEMANTICS_NODE_KIND_WITH_IR_NAME(Name, IRName)
// Invoked for each kind of semantic node, along with the name that is used
// to denote this node in textual Semantics IR.
#if defined(CARBON_SEMANTICS_NODE_KIND)
#define CARBON_SEMANTICS_NODE_KIND_IMPL(Name, IRName, ValueKind, \
TerminatorKind) \
CARBON_SEMANTICS_NODE_KIND(Name)
#elif defined(CARBON_SEMANTICS_NODE_KIND_WITH_VALUE_KIND)
#define CARBON_SEMANTICS_NODE_KIND_IMPL(Name, IRName, ValueKind, \
TerminatorKind) \
CARBON_SEMANTICS_NODE_KIND_WITH_VALUE_KIND(Name, ValueKind)
#elif defined(CARBON_SEMANTICS_NODE_KIND_WITH_TERMINATOR_KIND)
#define CARBON_SEMANTICS_NODE_KIND_IMPL(Name, IRName, ValueKind, \
TerminatorKind) \
CARBON_SEMANTICS_NODE_KIND_WITH_TERMINATOR_KIND(Name, TerminatorKind)
#elif defined(CARBON_SEMANTICS_NODE_KIND_WITH_IR_NAME)
#define CARBON_SEMANTICS_NODE_KIND_IMPL(Name, IRName, ValueKind, \
TerminatorKind) \
CARBON_SEMANTICS_NODE_KIND_WITH_IR_NAME(Name, IRName)
#else
#error "Must define the x-macro to use this file."
#endif
CARBON_SEMANTICS_NODE_KIND_IMPL(Invalid, "invalid", None, NotTerminator)
// A cross-reference between IRs.
CARBON_SEMANTICS_NODE_KIND_IMPL(CrossReference, "xref", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(AddressOf, "address_of", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(ArrayIndex, "array_index", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(ArrayType, "array_type", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(ArrayValue, "array_value", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Assign, "assign", None, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(BinaryOperatorAdd, "add", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(BindValue, "bind_value", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(BlockArg, "block_arg", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(BoolLiteral, "bool_literal", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Branch, "br", None, Terminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(BranchIf, "br", None, TerminatorSequence)
CARBON_SEMANTICS_NODE_KIND_IMPL(BranchWithArg, "br", None, Terminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Builtin, "builtin", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Call, "call", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(ConstType, "const_type", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Dereference, "dereference", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(FunctionDeclaration, "fn_decl", Untyped,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(IntegerLiteral, "int_literal", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Namespace, "namespace", Untyped, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(NoOp, "no_op", None, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Parameter, "parameter", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(PointerType, "ptr_type", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(RealLiteral, "real_literal", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Return, "return", None, Terminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(ReturnExpression, "return", None, Terminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(StringLiteral, "string_literal", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(StructAccess, "struct_access", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(StructType, "struct_type", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(StructTypeField, "struct_type_field", None,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(StructValue, "struct_value", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(StubReference, "stub_reference", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(Temporary, "temporary", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(TemporaryStorage, "temporary_storage", Typed,
NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(TupleIndex, "tuple_index", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(TupleType, "tuple_type", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(TupleValue, "tuple_value", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(UnaryOperatorNot, "not", Typed, NotTerminator)
CARBON_SEMANTICS_NODE_KIND_IMPL(VarStorage, "var", Typed, NotTerminator)
#undef CARBON_SEMANTICS_NODE_KIND
#undef CARBON_SEMANTICS_NODE_KIND_WITH_VALUE_KIND
#undef CARBON_SEMANTICS_NODE_KIND_WITH_TERMINATOR_KIND
#undef CARBON_SEMANTICS_NODE_KIND_WITH_IR_NAME
#undef CARBON_SEMANTICS_NODE_KIND_IMPL