mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-17 15:00:28 +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>
79 lines
3.8 KiB
Plaintext
79 lines
3.8 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 A() -> (i32, i32) { return (1, 2); }
|
|
|
|
fn B() -> [i32; 2] { return (1, 2); }
|
|
|
|
fn Run() {
|
|
var a: [i32; 2] = A();
|
|
var b: i32 = A()[0];
|
|
var c: i32 = a[b];
|
|
var d: i32 = B()[1];
|
|
}
|
|
|
|
// CHECK:STDOUT: ; ModuleID = 'array_element_access.carbon'
|
|
// CHECK:STDOUT: source_filename = "array_element_access.carbon"
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define void @A(ptr sret({ i32, i32 }) %return) {
|
|
// CHECK:STDOUT: %tuple = alloca { i32, i32 }, align 8
|
|
// CHECK:STDOUT: %1 = getelementptr inbounds { i32, i32 }, ptr %tuple, i32 0, i32 0
|
|
// CHECK:STDOUT: store i32 1, ptr %1, align 4
|
|
// CHECK:STDOUT: %2 = getelementptr inbounds { i32, i32 }, ptr %tuple, i32 0, i32 1
|
|
// CHECK:STDOUT: store i32 2, ptr %2, align 4
|
|
// CHECK:STDOUT: ret void
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define void @B(ptr sret([2 x i32]) %return) {
|
|
// CHECK:STDOUT: %tuple = alloca { i32, i32 }, align 8
|
|
// CHECK:STDOUT: %1 = getelementptr inbounds { i32, i32 }, ptr %tuple, i32 0, i32 0
|
|
// CHECK:STDOUT: store i32 1, ptr %1, align 4
|
|
// CHECK:STDOUT: %2 = getelementptr inbounds { i32, i32 }, ptr %tuple, i32 0, i32 1
|
|
// CHECK:STDOUT: store i32 2, ptr %2, align 4
|
|
// CHECK:STDOUT: %array = alloca [2 x i32], align 4
|
|
// CHECK:STDOUT: %array.element = getelementptr inbounds { i32, i32 }, ptr %tuple, i32 0, i32 0
|
|
// CHECK:STDOUT: %3 = load i32, ptr %array.element, align 4
|
|
// CHECK:STDOUT: %4 = getelementptr inbounds [2 x i32], ptr %array, i32 0, i32 0
|
|
// CHECK:STDOUT: store i32 %3, ptr %4, align 4
|
|
// CHECK:STDOUT: %array.element1 = getelementptr inbounds { i32, i32 }, ptr %tuple, i32 0, i32 1
|
|
// CHECK:STDOUT: %5 = load i32, ptr %array.element1, align 4
|
|
// CHECK:STDOUT: %6 = getelementptr inbounds [2 x i32], ptr %array, i32 0, i32 1
|
|
// CHECK:STDOUT: store i32 %5, ptr %6, align 4
|
|
// CHECK:STDOUT: ret void
|
|
// CHECK:STDOUT: }
|
|
// CHECK:STDOUT:
|
|
// CHECK:STDOUT: define void @Run() {
|
|
// CHECK:STDOUT: %a = alloca [2 x i32], align 4
|
|
// CHECK:STDOUT: %temp = alloca { i32, i32 }, align 8
|
|
// CHECK:STDOUT: call void @A(ptr %temp)
|
|
// CHECK:STDOUT: %array = alloca [2 x i32], align 4
|
|
// CHECK:STDOUT: %array.element = getelementptr inbounds { i32, i32 }, ptr %temp, i32 0, i32 0
|
|
// CHECK:STDOUT: %1 = load i32, ptr %array.element, align 4
|
|
// CHECK:STDOUT: %2 = getelementptr inbounds [2 x i32], ptr %array, i32 0, i32 0
|
|
// CHECK:STDOUT: store i32 %1, ptr %2, align 4
|
|
// CHECK:STDOUT: %array.element1 = getelementptr inbounds { i32, i32 }, ptr %temp, i32 0, i32 1
|
|
// CHECK:STDOUT: %3 = load i32, ptr %array.element1, align 4
|
|
// CHECK:STDOUT: %4 = getelementptr inbounds [2 x i32], ptr %array, i32 0, i32 1
|
|
// CHECK:STDOUT: store i32 %3, ptr %4, align 4
|
|
// CHECK:STDOUT: %b = alloca i32, align 4
|
|
// CHECK:STDOUT: %temp2 = alloca { i32, i32 }, align 8
|
|
// CHECK:STDOUT: call void @A(ptr %temp2)
|
|
// CHECK:STDOUT: %tuple.index = getelementptr inbounds { i32, i32 }, ptr %temp2, i32 0, i32 0
|
|
// CHECK:STDOUT: %5 = load i32, ptr %tuple.index, align 4
|
|
// CHECK:STDOUT: store i32 %5, ptr %b, align 4
|
|
// CHECK:STDOUT: %c = alloca i32, align 4
|
|
// CHECK:STDOUT: %6 = load i32, ptr %b, align 4
|
|
// CHECK:STDOUT: %array.index = getelementptr inbounds [2 x i32], ptr %a, i32 %6
|
|
// CHECK:STDOUT: %7 = load i32, ptr %array.index, align 4
|
|
// CHECK:STDOUT: store i32 %7, ptr %c, align 4
|
|
// CHECK:STDOUT: %d = alloca i32, align 4
|
|
// CHECK:STDOUT: %temp3 = alloca [2 x i32], align 4
|
|
// CHECK:STDOUT: call void @B(ptr %temp3)
|
|
// CHECK:STDOUT: %array.index4 = getelementptr inbounds [2 x i32], ptr %temp3, i32 0, i32 1
|
|
// CHECK:STDOUT: %8 = load i32, ptr %array.index4, align 4
|
|
// CHECK:STDOUT: store i32 %8, ptr %d, align 4
|
|
// CHECK:STDOUT: ret void
|
|
// CHECK:STDOUT: }
|