Skip to content

Commit

Permalink
[clang][codegen] Hoist parameter attribute setting in function prolog.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darkbuck authored and mhbliao committed May 15, 2020
1 parent 9df9777 commit 6c08b90
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
25 changes: 19 additions & 6 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2424,15 +2424,18 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,

case ABIArgInfo::Extend:
case ABIArgInfo::Direct: {

// If we have the trivial case, handle it with no muss and fuss.
if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
ArgI.getCoerceToType() == ConvertType(Ty) &&
ArgI.getDirectOffset() == 0) {
auto AI = Fn->getArg(FirstIRArg);
llvm::Type *LTy = ConvertType(Arg->getType());

// Prepare parameter attributes. So far, only attributes for pointer
// parameters are prepared. See
// http://llvm.org/docs/LangRef.html#paramattrs.
if (ArgI.getDirectOffset() == 0 && LTy->isPointerTy() &&
ArgI.getCoerceToType()->isPointerTy()) {
assert(NumIRArgs == 1);
auto AI = Fn->getArg(FirstIRArg);

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

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

// Set 'noalias' if an argument type has the `restrict` qualifier.
if (Arg->getType().isRestrictQualified())
AI->addAttr(llvm::Attribute::NoAlias);
}

// Prepare the argument value. If we have the trivial case, handle it
// with no muss and fuss.
if (!isa<llvm::StructType>(ArgI.getCoerceToType()) &&
ArgI.getCoerceToType() == ConvertType(Ty) &&
ArgI.getDirectOffset() == 0) {
assert(NumIRArgs == 1);

// LLVM expects swifterror parameters to be used in very restricted
// ways. Copy the value into a less-restricted temporary.
Expand Down
7 changes: 7 additions & 0 deletions clang/test/CodeGenCUDA/amdgpu-kernel-arg-pointer-type.cu
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ __global__ void kernel6(struct T t) {
t.x[0][0] += 1.f;
t.x[1][0] += 2.f;
}

// Check that coerced pointers retain the noalias attribute when qualified with __restrict.
// CHECK: define amdgpu_kernel void @_Z7kernel7Pi(i32 addrspace(1)* noalias %x.coerce)
// HOST: define void @_Z22__device_stub__kernel7Pi(i32* noalias %x)
__global__ void kernel7(int *__restrict x) {
x[0]++;
}

0 comments on commit 6c08b90

Please sign in to comment.