llvm-project/clang/test/CodeGenCXX/inalloca-overaligned.cpp
Aiden Grossman e2d7cd685d
[IR] Make dead_on_return attribute optionally sized
This patch makes the dead_on_return parameter attribute optionally require a number
of bytes to be passed in to specify the number of bytes known to be dead
upon function return/unwind. This is aimed at enabling annotating the
this pointer in C++ destructors with dead_on_return in clang. We need
this to handle cases like the following:

```
struct X {
  int n;
  ~X() {
    this[n].n = 0;
  }
};
void f() {
  X xs[] = {42, -1};
}
```

Where we only certain that sizeof(X) bytes are dead upon return of ~X.
Otherwise DSE would be able to eliminate the store in ~X which would not
be correct.

This patch only does the wiring within IR. Future patches will make
clang emit correct sizing information and update DSE to only delete
stores to objects marked dead_on_return that are provably in bounds of
the number of bytes specified to be dead_on_return.

Reviewers: nikic, alinas, antoniofrighetto

Pull Request: https://github.com/llvm/llvm-project/pull/171712
2026-01-21 08:22:05 -08:00

112 lines
3.7 KiB
C++

// RUN: %clang_cc1 -fms-extensions -w -triple i386-pc-win32 -emit-llvm -o - %s | FileCheck %s
// PR44395
// MSVC passes overaligned types indirectly since MSVC 2015. Make sure that
// works with inalloca.
struct NonTrivial {
NonTrivial();
NonTrivial(const NonTrivial &o);
int x;
};
struct __declspec(align(64)) OverAligned {
OverAligned();
int buf[16];
};
struct __declspec(align(8)) Both {
Both();
Both(const Both &o);
int x, y;
};
extern int gvi32;
int receive_inalloca_overaligned(NonTrivial nt, OverAligned o) {
return nt.x + o.buf[0];
}
// CHECK-LABEL: define dso_local noundef i32 @"?receive_inalloca_overaligned@@Y{{.*}}"
// CHECK-SAME: (ptr inalloca(<{ %struct.NonTrivial, ptr }>) %0)
int pass_inalloca_overaligned() {
gvi32 = receive_inalloca_overaligned(NonTrivial(), OverAligned());
return gvi32;
}
// CHECK-LABEL: define dso_local noundef i32 @"?pass_inalloca_overaligned@@Y{{.*}}"
// CHECK: [[TMP:%[^ ]*]] = alloca %struct.OverAligned, align 64
// CHECK: call ptr @llvm.stacksave.p0()
// CHECK: alloca inalloca <{ %struct.NonTrivial, ptr }>
// Construct OverAligned into TMP.
// CHECK: call x86_thiscallcc noundef ptr @"??0OverAligned@@QAE@XZ"(ptr {{[^,]*}} [[TMP]])
// Construct NonTrivial into the GEP.
// CHECK: [[GEP:%[^ ]*]] = getelementptr inbounds nuw <{ %struct.NonTrivial, ptr }>, ptr %{{.*}}, i32 0, i32 0
// CHECK: call x86_thiscallcc noundef ptr @"??0NonTrivial@@QAE@XZ"(ptr {{[^,]*}} [[GEP]])
// Store the address of an OverAligned temporary into the struct.
// CHECK: getelementptr inbounds nuw <{ %struct.NonTrivial, ptr }>, ptr %{{.*}}, i32 0, i32 1
// CHECK: store ptr [[TMP]], ptr %{{.*}}, align 4
// CHECK: call noundef i32 @"?receive_inalloca_overaligned@@Y{{.*}}"(ptr inalloca(<{ %struct.NonTrivial, ptr }>) %argmem)
int receive_both(Both o) {
return o.x + o.y;
}
// CHECK-LABEL: define dso_local noundef i32 @"?receive_both@@Y{{.*}}"
// CHECK-SAME: (ptr noundef dead_on_return %o)
int pass_both() {
gvi32 = receive_both(Both());
return gvi32;
}
// CHECK-LABEL: define dso_local noundef i32 @"?pass_both@@Y{{.*}}"
// CHECK: [[TMP:%[^ ]*]] = alloca %struct.Both, align 8
// CHECK: call x86_thiscallcc noundef ptr @"??0Both@@QAE@XZ"(ptr {{[^,]*}} [[TMP]])
// CHECK: call noundef i32 @"?receive_both@@Y{{.*}}"(ptr noundef dead_on_return [[TMP]])
int receive_inalloca_both(NonTrivial nt, Both o) {
return nt.x + o.x + o.y;
}
// CHECK-LABEL: define dso_local noundef i32 @"?receive_inalloca_both@@Y{{.*}}"
// CHECK-SAME: (ptr inalloca(<{ %struct.NonTrivial, ptr }>) %0)
int pass_inalloca_both() {
gvi32 = receive_inalloca_both(NonTrivial(), Both());
return gvi32;
}
// CHECK-LABEL: define dso_local noundef i32 @"?pass_inalloca_both@@Y{{.*}}"
// CHECK: [[TMP:%[^ ]*]] = alloca %struct.Both, align 8
// CHECK: call x86_thiscallcc noundef ptr @"??0Both@@QAE@XZ"(ptr {{[^,]*}} [[TMP]])
// CHECK: call noundef i32 @"?receive_inalloca_both@@Y{{.*}}"(ptr inalloca(<{ %struct.NonTrivial, ptr }>) %argmem)
// Here we have a type that is:
// - overaligned
// - not trivially copyable
// - can be "passed in registers" due to [[trivial_abi]]
// Clang should pass it directly.
struct [[trivial_abi]] alignas(8) MyPtr {
MyPtr();
MyPtr(const MyPtr &o);
~MyPtr();
int *ptr;
};
int receiveMyPtr(MyPtr o) { return *o.ptr; }
// CHECK-LABEL: define dso_local noundef i32 @"?receiveMyPtr@@Y{{.*}}"
// CHECK-SAME: (ptr noundef dead_on_return %o)
int passMyPtr() { return receiveMyPtr(MyPtr()); }
// CHECK-LABEL: define dso_local noundef i32 @"?passMyPtr@@Y{{.*}}"
// CHECK: [[TMP:%[^ ]*]] = alloca %struct.MyPtr, align 8
// CHECK: call x86_thiscallcc noundef ptr @"??0MyPtr@@QAE@XZ"(ptr {{[^,]*}} [[TMP]])
// CHECK: call noundef i32 @"?receiveMyPtr@@Y{{.*}}"(ptr noundef dead_on_return [[TMP]])