From 545688e9d4b7e24a9428e78e695445b5681d681a Mon Sep 17 00:00:00 2001 From: Farzana Ahmed Siddique Date: Tue, 25 Jul 2023 09:00:17 -0700 Subject: [PATCH] Lowering for tuple (#3016) Lowering for tuple with a workaround for typetype. I don't think this will generate tuple objects with correct values for the typetype issue. --------- Co-authored-by: Farzana Ahmed Siddique --- toolchain/lowering/lowering_context.cpp | 10 +++++ toolchain/lowering/lowering_context.h | 7 +++- .../lowering/lowering_function_context.cpp | 2 - toolchain/lowering/lowering_handle.cpp | 29 +++++++++++-- .../testdata/function/call/empty_tuple.carbon | 33 +++++++++++++++ .../testdata/struct/nested_struct.carbon | 21 ++++++++++ .../lowering/testdata/tuple/empty.carbon | 28 +++++++++++++ .../testdata/tuple/nested_tuple.carbon | 25 +++++++++++ .../lowering/testdata/tuple/one_entry.carbon | 35 ++++++++++++++++ .../testdata/tuple/two_entries.carbon | 41 +++++++++++++++++++ 10 files changed, 224 insertions(+), 7 deletions(-) create mode 100644 toolchain/lowering/testdata/function/call/empty_tuple.carbon create mode 100644 toolchain/lowering/testdata/struct/nested_struct.carbon create mode 100644 toolchain/lowering/testdata/tuple/empty.carbon create mode 100644 toolchain/lowering/testdata/tuple/nested_tuple.carbon create mode 100644 toolchain/lowering/testdata/tuple/one_entry.carbon create mode 100644 toolchain/lowering/testdata/tuple/two_entries.carbon diff --git a/toolchain/lowering/lowering_context.cpp b/toolchain/lowering/lowering_context.cpp index 30e337910ec1..17cdf3082e79 100644 --- a/toolchain/lowering/lowering_context.cpp +++ b/toolchain/lowering/lowering_context.cpp @@ -165,6 +165,16 @@ auto LoweringContext::BuildType(SemanticsNodeId node_id) -> llvm::Type* { return llvm::StructType::create(*llvm_context_, subtypes, "StructLiteralType"); } + case SemanticsNodeKind::TupleType: { + auto refs = semantics_ir_->GetTypeBlock(node.GetAsTupleType()); + llvm::SmallVector subtypes; + subtypes.reserve(refs.size()); + for (auto ref_id : refs) { + subtypes.push_back(GetType(ref_id)); + } + return llvm::StructType::create(*llvm_context_, subtypes, + "TupleLiteralType"); + } default: { CARBON_FATAL() << "Cannot use node as type: " << node_id; } diff --git a/toolchain/lowering/lowering_context.h b/toolchain/lowering/lowering_context.h index 5b5c8ddb175d..164bdd2daffa 100644 --- a/toolchain/lowering/lowering_context.h +++ b/toolchain/lowering/lowering_context.h @@ -32,7 +32,12 @@ class LoweringContext { // Returns a lowered type for the given type_id. auto GetType(SemanticsTypeId type_id) -> llvm::Type* { - // Neither TypeType nor InvalidType should be passed in. + // InvalidType should not be passed in. + if (type_id == SemanticsTypeId::TypeType) { + // TODO: Handle TypeType. Currently using a place holder + // as a workaround. + return llvm::Type::getInt1Ty(llvm_context()); + } CARBON_CHECK(type_id.index >= 0) << type_id; return types_[type_id.index]; } diff --git a/toolchain/lowering/lowering_function_context.cpp b/toolchain/lowering/lowering_function_context.cpp index d863b642fa3e..b79fc2a24a61 100644 --- a/toolchain/lowering/lowering_function_context.cpp +++ b/toolchain/lowering/lowering_function_context.cpp @@ -4,9 +4,7 @@ #include "toolchain/lowering/lowering_function_context.h" -#include "common/vlog.h" #include "toolchain/semantics/semantics_ir.h" -#include "toolchain/semantics/semantics_node_kind.h" namespace Carbon { diff --git a/toolchain/lowering/lowering_handle.cpp b/toolchain/lowering/lowering_handle.cpp index 38793926f773..a698e77c8c45 100644 --- a/toolchain/lowering/lowering_handle.cpp +++ b/toolchain/lowering/lowering_handle.cpp @@ -215,10 +215,24 @@ auto LoweringHandleStructType(LoweringFunctionContext& /*context*/, // No action to take. } -auto LoweringHandleTupleValue(LoweringFunctionContext& /*context*/, - SemanticsNodeId /*node_id*/, SemanticsNode node) +auto LoweringHandleTupleValue(LoweringFunctionContext& context, + SemanticsNodeId node_id, SemanticsNode node) -> void { - CARBON_FATAL() << "TODO: Add support: " << node; + auto* llvm_type = context.GetType(node.type_id()); + auto* alloca = context.builder().CreateAlloca( + llvm_type, /*ArraySize=*/nullptr, "TupleLiteralValue"); + context.SetLocal(node_id, alloca); + + auto refs = context.semantics_ir().GetNodeBlock(node.GetAsTupleValue()); + auto type_refs = context.semantics_ir().GetTypeBlock( + context.semantics_ir() + .GetNode(context.semantics_ir().GetType(node.type_id())) + .GetAsTupleType()); + + for (int i = 0; i < static_cast(type_refs.size()); ++i) { + auto* gep = context.builder().CreateStructGEP(llvm_type, alloca, i); + context.builder().CreateStore(context.GetLocal(refs[i]), gep); + } } auto LoweringHandleTupleType(LoweringFunctionContext& /*context*/, @@ -259,7 +273,14 @@ auto LoweringHandleStructValue(LoweringFunctionContext& context, auto LoweringHandleStubReference(LoweringFunctionContext& context, SemanticsNodeId node_id, SemanticsNode node) -> void { - context.SetLocal(node_id, context.GetLocal(node.GetAsStubReference())); + // TODO: Handle TypeType. Currently using a place holder + // but this is just a workaround. + if (node.type_id() == SemanticsTypeId::TypeType) { + llvm::Value* v = llvm::ConstantInt::get(context.builder().getInt1Ty(), 0); + context.SetLocal(node_id, v); + } else { + context.SetLocal(node_id, context.GetLocal(node.GetAsStubReference())); + } } auto LoweringHandleUnaryOperatorNot(LoweringFunctionContext& context, diff --git a/toolchain/lowering/testdata/function/call/empty_tuple.carbon b/toolchain/lowering/testdata/function/call/empty_tuple.carbon new file mode 100644 index 000000000000..c33a0bf2ab25 --- /dev/null +++ b/toolchain/lowering/testdata/function/call/empty_tuple.carbon @@ -0,0 +1,33 @@ +// 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 +// CHECK:STDOUT: ; ModuleID = 'empty_tuple.carbon' +// CHECK:STDOUT: source_filename = "empty_tuple.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: %TupleLiteralType = type {} +// CHECK:STDOUT: %EmptyTupleType = type {} +// CHECK:STDOUT: +// CHECK:STDOUT: define %TupleLiteralType @Echo(%TupleLiteralType %a) { +// CHECK:STDOUT: ret %TupleLiteralType %a +// CHECK:STDOUT: } + +// CHECK:STDOUT: +// CHECK:STDOUT: define %EmptyTupleType @Main() { +// CHECK:STDOUT: %TupleLiteralValue = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %var = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %TupleLiteralValue1 = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %1 = load %TupleLiteralType, ptr %TupleLiteralValue1, align 1 +// CHECK:STDOUT: %Echo = call %TupleLiteralType @Echo(%TupleLiteralType %1) +// CHECK:STDOUT: store %TupleLiteralType %Echo, ptr %var, align 1 +// CHECK:STDOUT: ret void +// CHECK:STDOUT: } + +fn Echo(a: ()) -> () { + return a; +} + +fn Main() { + var b: () = Echo(()); +} diff --git a/toolchain/lowering/testdata/struct/nested_struct.carbon b/toolchain/lowering/testdata/struct/nested_struct.carbon new file mode 100644 index 000000000000..d006a9ea9353 --- /dev/null +++ b/toolchain/lowering/testdata/struct/nested_struct.carbon @@ -0,0 +1,21 @@ +// 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 +// CHECK:STDOUT: ; ModuleID = 'nested_struct.carbon' +// CHECK:STDOUT: source_filename = "nested_struct.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: %StructLiteralType.0 = type { i1 } +// CHECK:STDOUT: +// CHECK:STDOUT: define i32 @Run() { +// CHECK:STDOUT: %StructLiteralValue = alloca %StructLiteralType.0, align 8 +// CHECK:STDOUT: %b = getelementptr inbounds %StructLiteralType.0, ptr %StructLiteralValue, i32 0, i32 0 +// CHECK:STDOUT: store i1 false, ptr %b, align 1 +// CHECK:STDOUT: ret i32 0 +// CHECK:STDOUT: } + +fn Run() -> i32 { + {.b = {.a: i32}}; + return 0; +} diff --git a/toolchain/lowering/testdata/tuple/empty.carbon b/toolchain/lowering/testdata/tuple/empty.carbon new file mode 100644 index 000000000000..715ea0beb5be --- /dev/null +++ b/toolchain/lowering/testdata/tuple/empty.carbon @@ -0,0 +1,28 @@ +// 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 +// CHECK:STDOUT: ; ModuleID = 'empty.carbon' +// CHECK:STDOUT: source_filename = "empty.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: %TupleLiteralType = type {} +// CHECK:STDOUT: +// CHECK:STDOUT: define i32 @Run() { +// CHECK:STDOUT: %TupleLiteralValue = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %var = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %TupleLiteralValue1 = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %1 = load %TupleLiteralType, ptr %TupleLiteralValue1, align 1 +// CHECK:STDOUT: store %TupleLiteralType %1, ptr %var, align 1 +// CHECK:STDOUT: %TupleLiteralValue2 = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %var3 = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %2 = load %TupleLiteralType, ptr %var, align 1 +// CHECK:STDOUT: store %TupleLiteralType %2, ptr %var3, align 1 +// CHECK:STDOUT: ret i32 0 +// CHECK:STDOUT: } + +fn Run() -> i32 { + var x: () = (); + var y: () = x; + return 0; +} diff --git a/toolchain/lowering/testdata/tuple/nested_tuple.carbon b/toolchain/lowering/testdata/tuple/nested_tuple.carbon new file mode 100644 index 000000000000..1e6959ff85fc --- /dev/null +++ b/toolchain/lowering/testdata/tuple/nested_tuple.carbon @@ -0,0 +1,25 @@ +// 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 +// CHECK:STDOUT: ; ModuleID = 'nested_tuple.carbon' +// CHECK:STDOUT: source_filename = "nested_tuple.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: %TupleLiteralType = type { i1 } +// CHECK:STDOUT: %TupleLiteralType.0 = type { %TupleLiteralType } +// CHECK:STDOUT: +// CHECK:STDOUT: define i32 @Run() { +// CHECK:STDOUT: %TupleLiteralValue = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %1 = getelementptr inbounds %TupleLiteralType, ptr %TupleLiteralValue, i32 0, i32 0 +// CHECK:STDOUT: store i1 false, ptr %1, align 1 +// CHECK:STDOUT: %TupleLiteralValue1 = alloca %TupleLiteralType.0, align 8 +// CHECK:STDOUT: %2 = getelementptr inbounds %TupleLiteralType.0, ptr %TupleLiteralValue1, i32 0, i32 0 +// CHECK:STDOUT: store ptr %TupleLiteralValue, ptr %2, align 8 +// CHECK:STDOUT: ret i32 0 +// CHECK:STDOUT: } + +fn Run() -> i32 { + ((i32,), ); + return 0; +} diff --git a/toolchain/lowering/testdata/tuple/one_entry.carbon b/toolchain/lowering/testdata/tuple/one_entry.carbon new file mode 100644 index 000000000000..59570f26c62d --- /dev/null +++ b/toolchain/lowering/testdata/tuple/one_entry.carbon @@ -0,0 +1,35 @@ +// 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 +// CHECK:STDOUT: ; ModuleID = 'one_entry.carbon' +// CHECK:STDOUT: source_filename = "one_entry.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: %TupleLiteralType = type { i1 } +// CHECK:STDOUT: %TupleLiteralType.0 = type { i32 } +// CHECK:STDOUT: +// CHECK:STDOUT: define i32 @Run() { +// CHECK:STDOUT: %TupleLiteralValue = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %1 = getelementptr inbounds %TupleLiteralType, ptr %TupleLiteralValue, i32 0, i32 0 +// CHECK:STDOUT: store i1 false, ptr %1, align 1 +// CHECK:STDOUT: %var = alloca %TupleLiteralType.0, align 8 +// CHECK:STDOUT: %TupleLiteralValue1 = alloca %TupleLiteralType.0, align 8 +// CHECK:STDOUT: %2 = getelementptr inbounds %TupleLiteralType.0, ptr %TupleLiteralValue1, i32 0, i32 0 +// CHECK:STDOUT: store i32 1, ptr %2, align 4 +// CHECK:STDOUT: %3 = load %TupleLiteralType.0, ptr %TupleLiteralValue1, align 4 +// CHECK:STDOUT: store %TupleLiteralType.0 %3, ptr %var, align 4 +// CHECK:STDOUT: %TupleLiteralValue2 = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %4 = getelementptr inbounds %TupleLiteralType, ptr %TupleLiteralValue2, i32 0, i32 0 +// CHECK:STDOUT: store i1 false, ptr %4, align 1 +// CHECK:STDOUT: %var3 = alloca %TupleLiteralType.0, align 8 +// CHECK:STDOUT: %5 = load %TupleLiteralType.0, ptr %var, align 4 +// CHECK:STDOUT: store %TupleLiteralType.0 %5, ptr %var3, align 4 +// CHECK:STDOUT: ret i32 0 +// CHECK:STDOUT: } + +fn Run() -> i32 { + var x: (i32, ) = (1, ); + var y: (i32, ) = x; + return 0; +} diff --git a/toolchain/lowering/testdata/tuple/two_entries.carbon b/toolchain/lowering/testdata/tuple/two_entries.carbon new file mode 100644 index 000000000000..15a668a30284 --- /dev/null +++ b/toolchain/lowering/testdata/tuple/two_entries.carbon @@ -0,0 +1,41 @@ +// 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 +// CHECK:STDOUT: ; ModuleID = 'two_entries.carbon' +// CHECK:STDOUT: source_filename = "two_entries.carbon" +// CHECK:STDOUT: +// CHECK:STDOUT: %TupleLiteralType = type { i1, i1 } +// CHECK:STDOUT: %TupleLiteralType.0 = type { i32, i32 } +// CHECK:STDOUT: +// CHECK:STDOUT: define i32 @Run() { +// CHECK:STDOUT: %TupleLiteralValue = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %1 = getelementptr inbounds %TupleLiteralType, ptr %TupleLiteralValue, i32 0, i32 0 +// CHECK:STDOUT: store i1 false, ptr %1, align 1 +// CHECK:STDOUT: %2 = getelementptr inbounds %TupleLiteralType, ptr %TupleLiteralValue, i32 0, i32 1 +// CHECK:STDOUT: store i1 false, ptr %2, align 1 +// CHECK:STDOUT: %var = alloca %TupleLiteralType.0, align 8 +// CHECK:STDOUT: %TupleLiteralValue1 = alloca %TupleLiteralType.0, align 8 +// CHECK:STDOUT: %3 = getelementptr inbounds %TupleLiteralType.0, ptr %TupleLiteralValue1, i32 0, i32 0 +// CHECK:STDOUT: store i32 12, ptr %3, align 4 +// CHECK:STDOUT: %4 = getelementptr inbounds %TupleLiteralType.0, ptr %TupleLiteralValue1, i32 0, i32 1 +// CHECK:STDOUT: store i32 7, ptr %4, align 4 +// CHECK:STDOUT: %5 = load %TupleLiteralType.0, ptr %TupleLiteralValue1, align 4 +// CHECK:STDOUT: store %TupleLiteralType.0 %5, ptr %var, align 4 +// CHECK:STDOUT: %TupleLiteralValue2 = alloca %TupleLiteralType, align 8 +// CHECK:STDOUT: %6 = getelementptr inbounds %TupleLiteralType, ptr %TupleLiteralValue2, i32 0, i32 0 +// CHECK:STDOUT: store i1 false, ptr %6, align 1 +// CHECK:STDOUT: %7 = getelementptr inbounds %TupleLiteralType, ptr %TupleLiteralValue2, i32 0, i32 1 +// CHECK:STDOUT: store i1 false, ptr %7, align 1 +// CHECK:STDOUT: %var3 = alloca %TupleLiteralType.0, align 8 +// CHECK:STDOUT: %8 = load %TupleLiteralType.0, ptr %var, align 4 +// CHECK:STDOUT: store %TupleLiteralType.0 %8, ptr %var3, align 4 +// CHECK:STDOUT: ret i32 0 +// CHECK:STDOUT: } + +fn Run() -> i32 { + var x: (i32, i32) = (12, 7); + var y: (i32, i32) = x; + return 0; +}