Skip to content

Commit 2de33d4

Browse files
committed
Handle passing an allocaed sret arg directly to a callee that expects a pointer to the default AS.
1 parent f6cff66 commit 2de33d4

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5391,11 +5391,19 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
53915391
V->getType()->isIntegerTy())
53925392
V = Builder.CreateZExt(V, ArgInfo.getCoerceToType());
53935393

5394-
// If the argument doesn't match, perform a bitcast to coerce it. This
5395-
// can happen due to trivial type mismatches.
5394+
// If the argument doesn't match, we are either trying to pass an
5395+
// alloca-ed sret argument directly, and the alloca AS does not match
5396+
// the default AS, case in which we AS cast it, or we have a trivial
5397+
// type mismatch, and thus perform a bitcast to coerce it.
53965398
if (FirstIRArg < IRFuncTy->getNumParams() &&
5397-
V->getType() != IRFuncTy->getParamType(FirstIRArg))
5398-
V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
5399+
V->getType() != IRFuncTy->getParamType(FirstIRArg)) {
5400+
auto IRTy = IRFuncTy->getParamType(FirstIRArg);
5401+
auto MaybeSRetArg = dyn_cast_or_null<llvm::Argument>(V);
5402+
if (MaybeSRetArg && MaybeSRetArg->hasStructRetAttr())
5403+
V = Builder.CreateAddrSpaceCast(V, IRTy);
5404+
else
5405+
V = Builder.CreateBitCast(V, IRTy);
5406+
}
53995407

54005408
if (ArgHasMaybeUndefAttr)
54015409
V = Builder.CreateFreeze(V);

clang/test/CodeGenCXX/no-elide-constructors.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// RUN: %clang_cc1 -std=c++98 -triple i386-unknown-unknown -fno-elide-constructors -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-CXX98
22
// RUN: %clang_cc1 -std=c++11 -triple i386-unknown-unknown -fno-elide-constructors -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-CXX11
3+
// RUN: %clang_cc1 -std=c++11 -triple amdgcn-amd-amdhsa -fno-elide-constructors -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK --check-prefix=CHECK-CXX11-NONZEROALLOCAAS
34
// RUN: %clang_cc1 -std=c++98 -triple i386-unknown-unknown -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-CXX98-ELIDE
45
// RUN: %clang_cc1 -std=c++11 -triple i386-unknown-unknown -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-CXX11-ELIDE
6+
// RUN: %clang_cc1 -std=c++11 -triple amdgcn-amd-amdhsa -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-CXX11-NONZEROALLOCAAS-ELIDE
57

68
// Reduced from PR12208
79
class X {
@@ -23,8 +25,10 @@ X Test()
2325
// sret argument.
2426
// CHECK-CXX98: call void @_ZN1XC1ERKS_(
2527
// CHECK-CXX11: call void @_ZN1XC1EOS_(
28+
// CHECK-CXX11-NONZEROALLOCAAS: call void @_ZN1XC1EOS_(
2629
// CHECK-CXX98-ELIDE-NOT: call void @_ZN1XC1ERKS_(
2730
// CHECK-CXX11-ELIDE-NOT: call void @_ZN1XC1EOS_(
31+
// CHECK-CXX11-NONZEROALLOCAAS-ELIDE-NOT: call void @_ZN1XC1EOS_(
2832

2933
// Make sure that the destructor for X is called.
3034
// FIXME: This call is present even in the -ELIDE runs, but is guarded by a

0 commit comments

Comments
 (0)