Lowering bug fix for tuple indexing. (#3110)

The existing tuple index lowering was crashing while accessing the
element from return value. This PR fixes the bug.

Co-authored-by: Farzana Ahmed Siddique <fasiddique@google.com>
This commit is contained in:
Farzana Ahmed Siddique
2023-08-16 20:59:56 +00:00
committed by GitHub
co-authored by Farzana Ahmed Siddique
parent 6af1c435e0
commit 14f489dff1
3 changed files with 47 additions and 7 deletions
+13 -5
View File
@@ -273,15 +273,23 @@ auto LoweringHandleTupleIndex(LoweringFunctionContext& context,
SemanticsNodeId node_id, SemanticsNode node)
-> void {
auto [tuple_node_id, index_node_id] = node.GetAsTupleIndex();
auto* llvm_type =
context.GetType(context.semantics_ir().GetNode(tuple_node_id).type_id());
auto* tuple_value = context.GetLocal(tuple_node_id);
auto index_node = context.semantics_ir().GetNode(index_node_id);
const auto index = context.semantics_ir()
.GetIntegerLiteral(index_node.GetAsIntegerLiteral())
.getZExtValue();
auto* gep = context.builder().CreateStructGEP(
llvm_type, context.GetLocal(tuple_node_id), index, "tuple.index");
context.SetLocal(node_id, gep);
llvm::Value* value;
if (tuple_value->getType()->isPointerTy()) {
auto* llvm_type = context.GetType(
context.semantics_ir().GetNode(tuple_node_id).type_id());
value = context.builder().CreateStructGEP(llvm_type, tuple_value, index,
"tuple.index");
} else {
// For non pointer types such as call or return, using extract value as
// gep cannot be used.
value = context.builder().CreateExtractValue(tuple_value, index);
}
context.SetLocal(node_id, value);
}
auto LoweringHandleTupleValue(LoweringFunctionContext& context,
@@ -11,8 +11,8 @@ fn Run() -> i32 {
return 0;
}
// CHECK:STDOUT: ; ModuleID = 'member_access.carbon'
// CHECK:STDOUT: source_filename = "member_access.carbon"
// CHECK:STDOUT: ; ModuleID = 'tuple_element_access.carbon'
// CHECK:STDOUT: source_filename = "tuple_element_access.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: %type = type {}
// CHECK:STDOUT:
@@ -0,0 +1,32 @@
// 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 F() -> (i32, i32) { return (12, 24); }
fn Run() {
var t: i32 = F()[1];
}
// CHECK:STDOUT: ; ModuleID = 'tuple_return_value_access.carbon'
// CHECK:STDOUT: source_filename = "tuple_return_value_access.carbon"
// CHECK:STDOUT:
// CHECK:STDOUT: define { i32, i32 } @F() {
// 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 12, ptr %1, align 4
// CHECK:STDOUT: %2 = getelementptr inbounds { i32, i32 }, ptr %tuple, i32 0, i32 1
// CHECK:STDOUT: store i32 24, ptr %2, align 4
// CHECK:STDOUT: %3 = load { i32, i32 }, ptr %tuple, align 4
// CHECK:STDOUT: ret { i32, i32 } %3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: define void @Run() {
// CHECK:STDOUT: %var = alloca i32, align 4
// CHECK:STDOUT: %F = call { i32, i32 } @F()
// CHECK:STDOUT: %1 = extractvalue { i32, i32 } %F, 1
// CHECK:STDOUT: store i32 %1, ptr %var, align 4
// CHECK:STDOUT: ret void
// CHECK:STDOUT: }