Files
carbon-lang/toolchain/lowering/testdata/array/base.carbon
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

115 lines
7.1 KiB
Plaintext

// 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
//
// AUTOUPDATE
fn Run() {
var a: [i32; 1] = (1,);
var b: [f64; 2] = (11.1, 2.2,);
var c: [(); 5] = ((), (), (), (), (),);
var d: (i32, i32, i32) = (1, 2, 3);
var e: [i32; 3] = d;
}
// CHECK:STDOUT: ; ModuleID = 'base.carbon'
// CHECK:STDOUT: source_filename = "base.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: %type = type {}
// CHECK:STDOUT:
// CHECK:STDOUT: define void @Run() {
// CHECK:STDOUT: %a = alloca [1 x i32], align 4
// CHECK:STDOUT: %tuple = alloca { i32 }, align 8
// CHECK:STDOUT: %1 = getelementptr inbounds { i32 }, ptr %tuple, i32 0, i32 0
// CHECK:STDOUT: store i32 1, ptr %1, align 4
// CHECK:STDOUT: %array = alloca [1 x i32], align 4
// CHECK:STDOUT: %array.element = getelementptr inbounds { i32 }, ptr %tuple, i32 0, i32 0
// CHECK:STDOUT: %2 = load i32, ptr %array.element, align 4
// CHECK:STDOUT: %3 = getelementptr inbounds [1 x i32], ptr %array, i32 0, i32 0
// CHECK:STDOUT: store i32 %2, ptr %3, align 4
// CHECK:STDOUT: %b = alloca [2 x double], align 8
// CHECK:STDOUT: %tuple1 = alloca { double, double }, align 8
// CHECK:STDOUT: %4 = getelementptr inbounds { double, double }, ptr %tuple1, i32 0, i32 0
// CHECK:STDOUT: store double 0x4026333333333334, ptr %4, align 8
// CHECK:STDOUT: %5 = getelementptr inbounds { double, double }, ptr %tuple1, i32 0, i32 1
// CHECK:STDOUT: store double 2.200000e+00, ptr %5, align 8
// CHECK:STDOUT: %array2 = alloca [2 x double], align 8
// CHECK:STDOUT: %array.element3 = getelementptr inbounds { double, double }, ptr %tuple1, i32 0, i32 0
// CHECK:STDOUT: %6 = load double, ptr %array.element3, align 8
// CHECK:STDOUT: %7 = getelementptr inbounds [2 x double], ptr %array2, i32 0, i32 0
// CHECK:STDOUT: store double %6, ptr %7, align 8
// CHECK:STDOUT: %array.element4 = getelementptr inbounds { double, double }, ptr %tuple1, i32 0, i32 1
// CHECK:STDOUT: %8 = load double, ptr %array.element4, align 8
// CHECK:STDOUT: %9 = getelementptr inbounds [2 x double], ptr %array2, i32 0, i32 1
// CHECK:STDOUT: store double %8, ptr %9, align 8
// CHECK:STDOUT: %tuple5 = alloca {}, align 8
// CHECK:STDOUT: %c = alloca [5 x {}], align 8
// CHECK:STDOUT: %tuple6 = alloca {}, align 8
// CHECK:STDOUT: %tuple7 = alloca {}, align 8
// CHECK:STDOUT: %tuple8 = alloca {}, align 8
// CHECK:STDOUT: %tuple9 = alloca {}, align 8
// CHECK:STDOUT: %tuple10 = alloca {}, align 8
// CHECK:STDOUT: %tuple11 = alloca { {}, {}, {}, {}, {} }, align 8
// CHECK:STDOUT: %10 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 0
// CHECK:STDOUT: store ptr %tuple6, ptr %10, align 8
// CHECK:STDOUT: %11 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 1
// CHECK:STDOUT: store ptr %tuple7, ptr %11, align 8
// CHECK:STDOUT: %12 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 2
// CHECK:STDOUT: store ptr %tuple8, ptr %12, align 8
// CHECK:STDOUT: %13 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 3
// CHECK:STDOUT: store ptr %tuple9, ptr %13, align 8
// CHECK:STDOUT: %14 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 4
// CHECK:STDOUT: store ptr %tuple10, ptr %14, align 8
// CHECK:STDOUT: %array12 = alloca [5 x {}], align 8
// CHECK:STDOUT: %array.element13 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 0
// CHECK:STDOUT: %15 = load {}, ptr %array.element13, align 1
// CHECK:STDOUT: %16 = getelementptr inbounds [5 x {}], ptr %array12, i32 0, i32 0
// CHECK:STDOUT: store {} %15, ptr %16, align 1
// CHECK:STDOUT: %array.element14 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 1
// CHECK:STDOUT: %17 = load {}, ptr %array.element14, align 1
// CHECK:STDOUT: %18 = getelementptr inbounds [5 x {}], ptr %array12, i32 0, i32 1
// CHECK:STDOUT: store {} %17, ptr %18, align 1
// CHECK:STDOUT: %array.element15 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 2
// CHECK:STDOUT: %19 = load {}, ptr %array.element15, align 1
// CHECK:STDOUT: %20 = getelementptr inbounds [5 x {}], ptr %array12, i32 0, i32 2
// CHECK:STDOUT: store {} %19, ptr %20, align 1
// CHECK:STDOUT: %array.element16 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 3
// CHECK:STDOUT: %21 = load {}, ptr %array.element16, align 1
// CHECK:STDOUT: %22 = getelementptr inbounds [5 x {}], ptr %array12, i32 0, i32 3
// CHECK:STDOUT: store {} %21, ptr %22, align 1
// CHECK:STDOUT: %array.element17 = getelementptr inbounds { {}, {}, {}, {}, {} }, ptr %tuple11, i32 0, i32 4
// CHECK:STDOUT: %23 = load {}, ptr %array.element17, align 1
// CHECK:STDOUT: %24 = getelementptr inbounds [5 x {}], ptr %array12, i32 0, i32 4
// CHECK:STDOUT: store {} %23, ptr %24, align 1
// CHECK:STDOUT: %tuple18 = alloca { %type, %type, %type }, align 8
// CHECK:STDOUT: %25 = getelementptr inbounds { %type, %type, %type }, ptr %tuple18, i32 0, i32 0
// CHECK:STDOUT: store %type zeroinitializer, ptr %25, align 1
// CHECK:STDOUT: %26 = getelementptr inbounds { %type, %type, %type }, ptr %tuple18, i32 0, i32 1
// CHECK:STDOUT: store %type zeroinitializer, ptr %26, align 1
// CHECK:STDOUT: %27 = getelementptr inbounds { %type, %type, %type }, ptr %tuple18, i32 0, i32 2
// CHECK:STDOUT: store %type zeroinitializer, ptr %27, align 1
// CHECK:STDOUT: %d = alloca { i32, i32, i32 }, align 8
// CHECK:STDOUT: %tuple19 = alloca { i32, i32, i32 }, align 8
// CHECK:STDOUT: %28 = getelementptr inbounds { i32, i32, i32 }, ptr %tuple19, i32 0, i32 0
// CHECK:STDOUT: store i32 1, ptr %28, align 4
// CHECK:STDOUT: %29 = getelementptr inbounds { i32, i32, i32 }, ptr %tuple19, i32 0, i32 1
// CHECK:STDOUT: store i32 2, ptr %29, align 4
// CHECK:STDOUT: %30 = getelementptr inbounds { i32, i32, i32 }, ptr %tuple19, i32 0, i32 2
// CHECK:STDOUT: store i32 3, ptr %30, align 4
// CHECK:STDOUT: %e = alloca [3 x i32], align 4
// CHECK:STDOUT: %array20 = alloca [3 x i32], align 4
// CHECK:STDOUT: %array.element21 = getelementptr inbounds { i32, i32, i32 }, ptr %d, i32 0, i32 0
// CHECK:STDOUT: %31 = load i32, ptr %array.element21, align 4
// CHECK:STDOUT: %32 = getelementptr inbounds [3 x i32], ptr %array20, i32 0, i32 0
// CHECK:STDOUT: store i32 %31, ptr %32, align 4
// CHECK:STDOUT: %array.element22 = getelementptr inbounds { i32, i32, i32 }, ptr %d, i32 0, i32 1
// CHECK:STDOUT: %33 = load i32, ptr %array.element22, align 4
// CHECK:STDOUT: %34 = getelementptr inbounds [3 x i32], ptr %array20, i32 0, i32 1
// CHECK:STDOUT: store i32 %33, ptr %34, align 4
// CHECK:STDOUT: %array.element23 = getelementptr inbounds { i32, i32, i32 }, ptr %d, i32 0, i32 2
// CHECK:STDOUT: %35 = load i32, ptr %array.element23, align 4
// CHECK:STDOUT: %36 = getelementptr inbounds [3 x i32], ptr %array20, i32 0, i32 2
// CHECK:STDOUT: store i32 %35, ptr %36, align 4
// CHECK:STDOUT: ret void
// CHECK:STDOUT: }