[clang] [CIR] Implement non-scalar lvalue return values (PR #190795)
Erich Keane via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 7 11:02:36 PDT 2026
https://github.com/erichkeane updated https://github.com/llvm/llvm-project/pull/190795
>From dfca63c351b90fcca7ba2a6c72606a1199d677d8 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Tue, 7 Apr 2026 06:46:53 -0700
Subject: [PATCH 1/2] [CIR] Implement non-scalar lvalue return values
I could only get these to happen in C++03 (as we do a
materialize-temporary-expr in later standards), but this does appear in
a number of benchmarks. The implementation ends up being pretty trivial,
as we just have to lower the aggregate correctly.
---
clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 7 ++--
.../CIR/CodeGen/non-scalar-lval-return.cpp | 42 +++++++++++++++++++
2 files changed, 45 insertions(+), 4 deletions(-)
create mode 100644 clang/test/CIR/CodeGen/non-scalar-lval-return.cpp
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index 81737aeb4c847..a306cc68dff8e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -1895,10 +1895,9 @@ LValue CIRGenFunction::emitCompoundLiteralLValue(const CompoundLiteralExpr *e) {
LValue CIRGenFunction::emitCallExprLValue(const CallExpr *e) {
RValue rv = emitCallExpr(e);
- if (!rv.isScalar()) {
- cgm.errorNYI(e->getSourceRange(), "emitCallExprLValue: non-scalar return");
- return {};
- }
+ if (!rv.isScalar())
+ return makeAddrLValue(rv.getAggregateAddress(), e->getType(),
+ AlignmentSource::Decl);
assert(e->getCallReturnType(getContext())->isReferenceType() &&
"Can't have a scalar return unless the return type is a "
diff --git a/clang/test/CIR/CodeGen/non-scalar-lval-return.cpp b/clang/test/CIR/CodeGen/non-scalar-lval-return.cpp
new file mode 100644
index 0000000000000..e3598c0c5574c
--- /dev/null
+++ b/clang/test/CIR/CodeGen/non-scalar-lval-return.cpp
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++03 -fclangir -emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++03 -fclangir -emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM,LLVMCIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++03 -emit-llvm %s -o %t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM,OGCG
+
+struct Struct {
+ int member;
+ Struct(int);
+};
+
+extern "C" Struct getStruct(int i) { return i; }
+
+extern "C" void use() {
+ int g = getStruct(0).member;
+
+ // CIR-LABEL: @use()
+ // CIR: %[[G_ALLOCA:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["g", init]
+ // CIR: %[[TEMP_ALLOCA:.*]] = cir.alloca !rec_Struct, !cir.ptr<!rec_Struct>
+ // CIR: %[[ZERO:.*]] = cir.const #cir.int<0> : !s32i
+ // CIR: %[[GET_STRUCT_CALL:.*]] = cir.call @getStruct(%[[ZERO]])
+ // CIR: cir.store{{.*}} %[[GET_STRUCT_CALL]], %[[TEMP_ALLOCA]]
+ // CIR: %[[GET_MEMBER:.*]] = cir.get_member %[[TEMP_ALLOCA]][0] {name = "member"}
+ // CIR: %[[LOAD_MEM:.*]] = cir.load{{.*}}%[[GET_MEMBER]]
+ // CIR: cir.store{{.*}} %[[LOAD_MEM]], %[[G_ALLOCA]] : !s32i, !cir.ptr<!s32i>
+ //
+ // LLVM-LABEL: @use()
+ // LLVM: %[[G_ALLOCA:.*]] = alloca i32
+ // LLVM: %[[TEMP_ALLOCA:.*]] = alloca %struct.Struct
+ //
+ // LLVMCIR: %[[GET_STRUCT_CALL:.*]] = call %struct.Struct @getStruct(i32 noundef 0)
+ // LLVMCIR: store %struct.Struct %[[GET_STRUCT_CALL]], ptr %[[TEMP_ALLOCA]]
+ // OGCG: %[[GET_STRUCT_CALL_BEFORE:.*]] = call i32 @getStruct(i32 noundef 0)
+ // OGCG: %[[GET_STRUCT_CALL:.*]] = getelementptr{{.*}}%struct.Struct, ptr %[[TEMP_ALLOCA]]
+ // OGCG: store i32 %[[GET_STRUCT_CALL_BEFORE]], ptr %[[GET_STRUCT_CALL]]
+ //
+ // LLVM: %[[GET_MEMBER:.*]] = getelementptr {{.*}}%struct.Struct, ptr %[[TEMP_ALLOCA]], i32 0, i32 0
+ // LLVM: %[[LOAD_MEM:.*]] = load i32, ptr %[[GET_MEMBER]]
+ // LLVM: store i32 %[[LOAD_MEM]], ptr %[[G_ALLOCA]]
+}
+
>From 880e5149e461ea48d0ce34a5adf0af63d01ded39 Mon Sep 17 00:00:00 2001
From: erichkeane <ekeane at nvidia.com>
Date: Tue, 7 Apr 2026 11:02:22 -0700
Subject: [PATCH 2/2] Add offset to the GEPs in the test
---
clang/test/CIR/CodeGen/non-scalar-lval-return.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/test/CIR/CodeGen/non-scalar-lval-return.cpp b/clang/test/CIR/CodeGen/non-scalar-lval-return.cpp
index e3598c0c5574c..df8186fc1f6ff 100644
--- a/clang/test/CIR/CodeGen/non-scalar-lval-return.cpp
+++ b/clang/test/CIR/CodeGen/non-scalar-lval-return.cpp
@@ -32,7 +32,7 @@ extern "C" void use() {
// LLVMCIR: %[[GET_STRUCT_CALL:.*]] = call %struct.Struct @getStruct(i32 noundef 0)
// LLVMCIR: store %struct.Struct %[[GET_STRUCT_CALL]], ptr %[[TEMP_ALLOCA]]
// OGCG: %[[GET_STRUCT_CALL_BEFORE:.*]] = call i32 @getStruct(i32 noundef 0)
- // OGCG: %[[GET_STRUCT_CALL:.*]] = getelementptr{{.*}}%struct.Struct, ptr %[[TEMP_ALLOCA]]
+ // OGCG: %[[GET_STRUCT_CALL:.*]] = getelementptr{{.*}}%struct.Struct, ptr %[[TEMP_ALLOCA]], i32 0, i32 0
// OGCG: store i32 %[[GET_STRUCT_CALL_BEFORE]], ptr %[[GET_STRUCT_CALL]]
//
// LLVM: %[[GET_MEMBER:.*]] = getelementptr {{.*}}%struct.Struct, ptr %[[TEMP_ALLOCA]], i32 0, i32 0
More information about the cfe-commits
mailing list