Skip to content

Commit 9142c0b

Browse files
committed
[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
1 parent 276c8dd commit 9142c0b

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
@@ -2425,15 +2425,18 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI,
24252425

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

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

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

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

24942507
// LLVM expects swifterror parameters to be used in very restricted
24952508
// 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)