Skip to content

Commit 6c08b90

Browse files
darkbuckmhbliao
authored andcommitted
[clang][codegen] Hoist parameter attribute setting in function prolog.
Summary: - If the coerced type is still a pointer, it should be set with proper parameter attributes, such as `noalias`, `nonnull`, and etc. Hoist that (pointer) parameter attribute setting so that the coerced pointer parameter could be marked properly. Depends on D79394 Reviewers: rjmccall, kerbowa, yaxunl Subscribers: jvesely, nhaehnle, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D79395 Change-Id: Ic86876ba8c724541d6c6071e8a8bc9dd8a61b51e
1 parent 9df9777 commit 6c08b90

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,15 +2424,18 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
24242424

24252425
case ABIArgInfo::Extend:
24262426
case ABIArgInfo::Direct: {
2427-
2428-
// If we have the trivial case, handle it with no muss and fuss.
2429-
if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
2430-
ArgI.getCoerceToType() == ConvertType(Ty) &&
2431-
ArgI.getDirectOffset() == 0) {
2427+
auto AI = Fn->getArg(FirstIRArg);
2428+
llvm::Type *LTy = ConvertType(Arg->getType());
2429+
2430+
// Prepare parameter attributes. So far, only attributes for pointer
2431+
// parameters are prepared. See
2432+
// http://llvm.org/docs/LangRef.html#paramattrs.
2433+
if (ArgI.getDirectOffset() == 0 && LTy->isPointerTy() &&
2434+
ArgI.getCoerceToType()->isPointerTy()) {
24322435
assert(NumIRArgs == 1);
2433-
auto AI = Fn->getArg(FirstIRArg);
24342436

24352437
if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(Arg)) {
2438+
// Set `nonnull` attribute if any.
24362439
if (getNonNullAttr(CurCodeDecl, PVD, PVD->getType(),
24372440
PVD->getFunctionScopeIndex()) &&
24382441
!CGM.getCodeGenOpts().NullPointerIsValid)
@@ -2470,6 +2473,7 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
24702473
AI->addAttr(llvm::Attribute::NonNull);
24712474
}
24722475

2476+
// Set `align` attribute if any.
24732477
const auto *AVAttr = PVD->getAttr<AlignValueAttr>();
24742478
if (!AVAttr)
24752479
if (const auto *TOTy = dyn_cast<TypedefType>(OTy))
@@ -2487,8 +2491,17 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
24872491
}
24882492
}
24892493

2494+
// Set 'noalias' if an argument type has the `restrict` qualifier.
24902495
if (Arg->getType().isRestrictQualified())
24912496
AI->addAttr(llvm::Attribute::NoAlias);
2497+
}
2498+
2499+
// Prepare the argument value. If we have the trivial case, handle it
2500+
// with no muss and fuss.
2501+
if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
2502+
ArgI.getCoerceToType() == ConvertType(Ty) &&
2503+
ArgI.getDirectOffset() == 0) {
2504+
assert(NumIRArgs == 1);
24922505

24932506
// LLVM expects swifterror parameters to be used in very restricted
24942507
// ways. Copy the value into a less-restricted temporary.

clang/test/CodeGenCUDA/amdgpu-kernel-arg-pointer-type.cu

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,10 @@ __global__ void kernel6(struct T t) {
6767
t.x[0][0] += 1.f;
6868
t.x[1][0] += 2.f;
6969
}
70+
71+
// Check that coerced pointers retain the noalias attribute when qualified with __restrict.
72+
// CHECK: define amdgpu_kernel void @_Z7kernel7Pi(i32 addrspace(1)* noalias %x.coerce)
73+
// HOST: define void @_Z22__device_stub__kernel7Pi(i32* noalias %x)
74+
__global__ void kernel7(int *__restrict x) {
75+
x[0]++;
76+
}

0 commit comments

Comments
 (0)