Skip to content

[Clang] Fix sema checks thinking kernels aren't kernels #104460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7123,6 +7123,13 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
}
}

static bool isKernelDecl(Decl *D) {
const FunctionType *FnTy = D->getFunctionType();
return D->hasAttr<OpenCLKernelAttr>() ||
(FnTy && FnTy->getCallConv() == CallingConv::CC_AMDGPUKernelCall) ||
D->hasAttr<CUDAGlobalAttr>() || D->getAttr<NVPTXKernelAttr>();
}

void Sema::ProcessDeclAttributeList(
Scope *S, Decl *D, const ParsedAttributesView &AttrList,
const ProcessDeclAttributeOptions &Options) {
Expand Down Expand Up @@ -7163,24 +7170,25 @@ void Sema::ProcessDeclAttributeList(
} else if (const auto *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) {
Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A;
D->setInvalidDecl();
} else if (!D->hasAttr<CUDAGlobalAttr>()) {
if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
} else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
} else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
} else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
}
}
}
if (!isKernelDecl(D)) {
if (const auto *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
} else if (const auto *A = D->getAttr<AMDGPUWavesPerEUAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
} else if (const auto *A = D->getAttr<AMDGPUNumSGPRAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
} else if (const auto *A = D->getAttr<AMDGPUNumVGPRAttr>()) {
Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
<< A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
D->setInvalidDecl();
Comment on lines +7176 to +7191
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could refactor these repetitive diags into something like this:

for(const auto A* : { D->getAttr<AMDGPUFlatWorkGroupSizeAttr>(), 
           D->getAttr<AMDGPUWavesPerEUAttr>(), ...}) {
    if (A) {
      Diag(D->getLocation(), diag::err_attribute_wrong_decl_type)
          << A << A->isRegularKeywordAttribute() << ExpectedKernelFunction;
      D->setInvalidDecl();
      break;
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, fails in practice because to put it in a uniform container we need to erase them all to Attr * which then seems to cause some unfortunate side effects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bummer. Oh, well. Thank you for trying. Keeping it all as is is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also might want to revisit the CUDA launch bounds attr. IIRC we had to duplicate a lot of the CUDA attrs since they all require the CUDA language unlike the AMD ones.

}
}

Expand Down
5 changes: 5 additions & 0 deletions clang/test/CodeGenCXX/amdgpu-kernel-arg-pointer-type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
// The original test passes the result through opt O2, but that seems to introduce invalid
// addrspace casts which are not being fixed as part of the present change.

// COMMON: define{{.*}} amdgpu_kernel void @_Z6kernelv() #[[ATTR:[0-9]+]]
__attribute__((amdgpu_kernel, amdgpu_flat_work_group_size(1, 256))) void
kernel() {}

// COMMON-LABEL: define{{.*}} amdgpu_kernel void @_Z7kernel1Pi(ptr {{.*}} %x)
// CHECK-NOT: ={{.*}} addrspacecast [[TYPE:.*]] addrspace(1)* %{{.*}} to ptr
__attribute__((amdgpu_kernel)) void kernel1(int *x) {
Expand Down Expand Up @@ -81,3 +85,4 @@ __attribute__((amdgpu_kernel)) void kernel8(struct SS a) {
*a.x += 3.f;
}

// COMMON: attributes #[[ATTR]] = { {{.*}}"amdgpu-flat-work-group-size"="1,256"{{.*}} }
Loading