[clang] [clang][CodeGen] Use the pointee type for sret on swift_indirect_result (PR #249135)
Jiaye Fang via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 4 17:52:38 PST 2026
https://github.com/cjsrxzdyzds created https://github.com/llvm/llvm-project/pull/249135
Clang gives the first `swift_indirect_result` parameter of a `void` function an `sret` attribute whose type is the parameter's own type, `ptr`, while `dereferenceable` and `align` come from the pointee. LLVM takes the size of the `sret` type as the size of the object the argument points to, so for a result larger than a pointer, DSE treats each pointer-sized store as overwriting the whole result and deletes the stores before it. For a `void *` result, which gets no `dereferenceable`, LLVM instead takes the `sret` type as 8 dereferenceable, non-null, 8-aligned bytes, and SimplifyCFG and LICM move guarded loads above their guard.
Use the pointee's in-memory type when its size is known, the same condition under which `dereferenceable` and `align` are added. Otherwise use an opaque type, which has no size, named `swift.opaque` like the type Swift's own IR uses in `sret` for results of unknown layout. The pointee's memory type would be wrong there: `i8` for `void` still claims one dereferenceable byte, and an incomplete array would get a zero-sized array.
Fixes #249128.
Fixes #249134.
Assisted-by: Claude Code
>From df85279078f16b8cfd00257957c3bc69be7fc05d Mon Sep 17 00:00:00 2001
From: cjsrxzdyzds <jiayefang at hotmail.com>
Date: Fri, 4 Dec 2026 19:59:01 -0500
Subject: [PATCH 1/2] [clang][CodeGen] Test the sret type of
swift_indirect_result (NFC)
Record the sret type Clang gives a swift_indirect_result parameter for
complete, void, incomplete and incomplete-array pointees, at the
definition and at a call. It is currently the parameter's own pointer
type.
Assisted-by: Claude Code
---
.../test/CodeGen/swiftcall-indirect-result.c | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
create mode 100644 clang/test/CodeGen/swiftcall-indirect-result.c
diff --git a/clang/test/CodeGen/swiftcall-indirect-result.c b/clang/test/CodeGen/swiftcall-indirect-result.c
new file mode 100644
index 0000000000000..053a64c7b8d41
--- /dev/null
+++ b/clang/test/CodeGen/swiftcall-indirect-result.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm -o - %s | FileCheck %s
+
+// The sret type of a swift_indirect_result parameter. LLVM takes its size as
+// the size of the object the argument points to.
+
+#define SWIFTCALL __attribute__((swiftcall))
+#define OUT __attribute__((swift_indirect_result))
+
+struct big { long a, b, c, d; };
+struct incomplete;
+
+// CHECK-LABEL: define {{.*}} void @struct_result(ptr noalias noundef sret(ptr) align 8 dereferenceable(32) %out)
+SWIFTCALL void struct_result(OUT struct big *out) {}
+
+// CHECK-LABEL: define {{.*}} void @int_result(ptr noalias noundef sret(ptr) align 4 dereferenceable(4) %out)
+SWIFTCALL void int_result(OUT int *out) {}
+
+// CHECK-LABEL: define {{.*}} void @void_result(ptr noalias noundef sret(ptr) %out)
+SWIFTCALL void void_result(OUT void *out) {}
+
+// CHECK-LABEL: define {{.*}} void @incomplete_result(ptr noalias noundef sret(ptr) %out)
+SWIFTCALL void incomplete_result(OUT struct incomplete *out) {}
+
+// CHECK-LABEL: define {{.*}} void @incomplete_array_result(ptr noalias noundef sret(ptr) %out)
+SWIFTCALL void incomplete_array_result(OUT int (*out)[]) {}
+
+// CHECK-LABEL: define {{.*}} void @call_struct_result(
+// CHECK: call swiftcc void @struct_result(ptr noalias noundef sret(ptr) align 8 dereferenceable(32) %{{.*}})
+void call_struct_result(struct big *b) { struct_result(b); }
+
+// CHECK-LABEL: define {{.*}} void @call_void_result(
+// CHECK: call swiftcc void @void_result(ptr noalias noundef sret(ptr) %{{.*}})
+void call_void_result(void *p) { void_result(p); }
>From 3088bab57cfc3fc388efa782d40d7244251ee122 Mon Sep 17 00:00:00 2001
From: cjsrxzdyzds <jiayefang at hotmail.com>
Date: Fri, 4 Dec 2026 20:06:59 -0500
Subject: [PATCH 2/2] [clang][CodeGen] Use the pointee type for sret on
swift_indirect_result
Clang gives the first swift_indirect_result parameter of a void
function an sret attribute whose type is the parameter's own type, ptr,
while dereferenceable and align come from the pointee. LLVM takes the
size of the sret type as the size of the object the argument points
to, so for a result larger than a pointer, DSE treats each
pointer-sized store as overwriting the whole result and deletes the
stores before it. For a void * result, which gets no dereferenceable,
LLVM instead takes the sret type as 8 dereferenceable, non-null,
8-aligned bytes, and SimplifyCFG and LICM move guarded loads above
their guard.
Use the pointee's in-memory type when its size is known, the same
condition under which dereferenceable and align are added. Otherwise
use an opaque type, which has no size, named swift.opaque like the type
Swift's own IR uses in sret for results of unknown layout. The
pointee's memory type would be wrong there: i8 for void still claims
one dereferenceable byte, and an incomplete array would get a
zero-sized array.
Fixes #249128.
Fixes #249134.
Assisted-by: Claude Code
---
clang/docs/ReleaseNotes.md | 6 ++++++
clang/lib/CodeGen/CGCall.cpp | 21 ++++++++++++++++---
clang/test/CodeGen/64bit-swiftcall.c | 2 +-
clang/test/CodeGen/arm-swiftcall.c | 2 +-
.../test/CodeGen/swiftcall-indirect-result.c | 17 ++++++++-------
clang/test/CodeGen/windows-swiftcall.c | 2 +-
6 files changed, 37 insertions(+), 13 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index a08b457ffcf51..d9ed712ab5e32 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1145,6 +1145,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
`interrupt` attribute whose first parameter uses a typedef for a pointer
type. (#GH204756)
+- Fixed miscompilations of `swiftcall` functions with a `swift_indirect_result`
+ parameter. Clang gave the result the parameter's pointer type as its
+ in-memory type, so stores to a result larger than a pointer could be
+ deleted, and loads through a `void *` result could be executed even when
+ the code guarded them. (#GH249128, #GH249134)
+
#### Bug Fixes to C++ Support
- Clang now recovers from invalid single pointer declarators in structured
bindings (such as ``auto *[x, y] = s;``) by removing the pointer declarator.
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index cd48e495d656f..3e6e5fc64fc76 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -3396,10 +3396,26 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
break;
case ParameterABI::SwiftIndirectResult: {
+ auto PTy = ParamType->getPointeeType();
+ bool HasKnownSize = !PTy->isIncompleteType() && PTy->isConstantSizeType();
+
// Add 'sret' if we haven't already used it for something, but
// only if the result is void.
if (!hasUsedSRet && RetTy->isVoidType()) {
- Attrs.addStructRetAttr(getTypes().ConvertTypeForMem(ParamType));
+ // The sret type is the in-memory type of the result, and LLVM takes
+ // its size as the size of the object the argument points to. If the
+ // result has no known size, use an opaque type, which has no size.
+ llvm::Type *SRetTy;
+ if (HasKnownSize) {
+ SRetTy = getTypes().ConvertTypeForMem(PTy);
+ } else {
+ auto *Opaque =
+ llvm::StructType::getTypeByName(getLLVMContext(), "swift.opaque");
+ if (!Opaque || !Opaque->isOpaque())
+ Opaque = llvm::StructType::create(getLLVMContext(), "swift.opaque");
+ SRetTy = Opaque;
+ }
+ Attrs.addStructRetAttr(SRetTy);
hasUsedSRet = true;
}
@@ -3407,8 +3423,7 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
Attrs.addAttribute(llvm::Attribute::NoAlias);
// Add 'dereferenceable' and 'alignment'.
- auto PTy = ParamType->getPointeeType();
- if (!PTy->isIncompleteType() && PTy->isConstantSizeType()) {
+ if (HasKnownSize) {
auto info = getContext().getTypeInfoInChars(PTy);
Attrs.addDereferenceableAttr(info.Width.getQuantity());
Attrs.addAlignmentAttr(info.Align.getAsAlign());
diff --git a/clang/test/CodeGen/64bit-swiftcall.c b/clang/test/CodeGen/64bit-swiftcall.c
index e60e09b201c0a..5c6758513e3e7 100644
--- a/clang/test/CodeGen/64bit-swiftcall.c
+++ b/clang/test/CodeGen/64bit-swiftcall.c
@@ -20,7 +20,7 @@
/*****************************************************************************/
SWIFTCALL void indirect_result_1(OUT int *arg0, OUT float *arg1) {}
-// CHECK-LABEL: define {{.*}} void @indirect_result_1(ptr noalias sret(ptr) align 4 dereferenceable(4){{.*}}, ptr noalias align 4 dereferenceable(4){{.*}})
+// CHECK-LABEL: define {{.*}} void @indirect_result_1(ptr noalias sret(i32) align 4 dereferenceable(4){{.*}}, ptr noalias align 4 dereferenceable(4){{.*}})
// TODO: maybe this shouldn't suppress sret.
SWIFTCALL int indirect_result_2(OUT int *arg0, OUT float *arg1) { __builtin_unreachable(); }
diff --git a/clang/test/CodeGen/arm-swiftcall.c b/clang/test/CodeGen/arm-swiftcall.c
index 7f0b4f6d518ed..ac33fc36be708 100644
--- a/clang/test/CodeGen/arm-swiftcall.c
+++ b/clang/test/CodeGen/arm-swiftcall.c
@@ -14,7 +14,7 @@
/*****************************************************************************/
SWIFTCALL void indirect_result_1(OUT int *arg0, OUT float *arg1) {}
-// CHECK-LABEL: define{{.*}} void @indirect_result_1(ptr noalias sret(ptr) align 4 dereferenceable(4){{.*}}, ptr noalias align 4 dereferenceable(4){{.*}})
+// CHECK-LABEL: define{{.*}} void @indirect_result_1(ptr noalias sret(i32) align 4 dereferenceable(4){{.*}}, ptr noalias align 4 dereferenceable(4){{.*}})
// TODO: maybe this shouldn't suppress sret.
SWIFTCALL int indirect_result_2(OUT int *arg0, OUT float *arg1) { __builtin_unreachable(); }
diff --git a/clang/test/CodeGen/swiftcall-indirect-result.c b/clang/test/CodeGen/swiftcall-indirect-result.c
index 053a64c7b8d41..ecf0d285c3281 100644
--- a/clang/test/CodeGen/swiftcall-indirect-result.c
+++ b/clang/test/CodeGen/swiftcall-indirect-result.c
@@ -9,25 +9,28 @@
struct big { long a, b, c, d; };
struct incomplete;
-// CHECK-LABEL: define {{.*}} void @struct_result(ptr noalias noundef sret(ptr) align 8 dereferenceable(32) %out)
+// CHECK-DAG: %struct.big = type { i64, i64, i64, i64 }
+// CHECK-DAG: %swift.opaque = type opaque
+
+// CHECK-LABEL: define {{.*}} void @struct_result(ptr noalias noundef sret(%struct.big) align 8 dereferenceable(32) %out)
SWIFTCALL void struct_result(OUT struct big *out) {}
-// CHECK-LABEL: define {{.*}} void @int_result(ptr noalias noundef sret(ptr) align 4 dereferenceable(4) %out)
+// CHECK-LABEL: define {{.*}} void @int_result(ptr noalias noundef sret(i32) align 4 dereferenceable(4) %out)
SWIFTCALL void int_result(OUT int *out) {}
-// CHECK-LABEL: define {{.*}} void @void_result(ptr noalias noundef sret(ptr) %out)
+// CHECK-LABEL: define {{.*}} void @void_result(ptr noalias noundef sret(%swift.opaque) %out)
SWIFTCALL void void_result(OUT void *out) {}
-// CHECK-LABEL: define {{.*}} void @incomplete_result(ptr noalias noundef sret(ptr) %out)
+// CHECK-LABEL: define {{.*}} void @incomplete_result(ptr noalias noundef sret(%swift.opaque) %out)
SWIFTCALL void incomplete_result(OUT struct incomplete *out) {}
-// CHECK-LABEL: define {{.*}} void @incomplete_array_result(ptr noalias noundef sret(ptr) %out)
+// CHECK-LABEL: define {{.*}} void @incomplete_array_result(ptr noalias noundef sret(%swift.opaque) %out)
SWIFTCALL void incomplete_array_result(OUT int (*out)[]) {}
// CHECK-LABEL: define {{.*}} void @call_struct_result(
-// CHECK: call swiftcc void @struct_result(ptr noalias noundef sret(ptr) align 8 dereferenceable(32) %{{.*}})
+// CHECK: call swiftcc void @struct_result(ptr noalias noundef sret(%struct.big) align 8 dereferenceable(32) %{{.*}})
void call_struct_result(struct big *b) { struct_result(b); }
// CHECK-LABEL: define {{.*}} void @call_void_result(
-// CHECK: call swiftcc void @void_result(ptr noalias noundef sret(ptr) %{{.*}})
+// CHECK: call swiftcc void @void_result(ptr noalias noundef sret(%swift.opaque) %{{.*}})
void call_void_result(void *p) { void_result(p); }
diff --git a/clang/test/CodeGen/windows-swiftcall.c b/clang/test/CodeGen/windows-swiftcall.c
index 2ef022854b7b7..6ccf84094dbb8 100644
--- a/clang/test/CodeGen/windows-swiftcall.c
+++ b/clang/test/CodeGen/windows-swiftcall.c
@@ -10,7 +10,7 @@
/*****************************************************************************/
SWIFTCALL void indirect_result_1(OUT int *arg0, OUT float *arg1) {}
-// CHECK-LABEL: define {{.*}} void @indirect_result_1(ptr noalias noundef sret(ptr) align 4 dereferenceable(4){{.*}}, ptr noalias noundef align 4 dereferenceable(4){{.*}})
+// CHECK-LABEL: define {{.*}} void @indirect_result_1(ptr noalias noundef sret(i32) align 4 dereferenceable(4){{.*}}, ptr noalias noundef align 4 dereferenceable(4){{.*}})
// TODO: maybe this shouldn't suppress sret.
SWIFTCALL int indirect_result_2(OUT int *arg0, OUT float *arg1) { __builtin_unreachable(); }
More information about the cfe-commits
mailing list