Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[CUDA][HIP] Fix host/device attribute of builtin #138162

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 1 commit into from
May 8, 2025
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
8 changes: 8 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6357,6 +6357,14 @@ static FunctionDecl *rewriteBuiltinFunctionDecl(Sema *Sema, ASTContext &Context,
Params.push_back(Parm);
}
OverloadDecl->setParams(Params);
// We cannot merge host/device attributes of redeclarations. They have to
// be consistent when created.
if (Sema->LangOpts.CUDA) {
if (FDecl->hasAttr<CUDAHostAttr>())
OverloadDecl->addAttr(CUDAHostAttr::CreateImplicit(Context));
if (FDecl->hasAttr<CUDADeviceAttr>())
OverloadDecl->addAttr(CUDADeviceAttr::CreateImplicit(Context));
}
Sema->mergeDeclAttributes(OverloadDecl, FDecl);
return OverloadDecl;
}
Expand Down
36 changes: 36 additions & 0 deletions clang/test/SemaCUDA/overloaded-builtin.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -aux-triple amdgcn-amd-amdhsa -fsyntax-only -verify=host -xhip %s
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fsyntax-only -fcuda-is-device -verify=dev -xhip %s

// dev-no-diagnostics

#include "Inputs/cuda.h"

__global__ void kernel() {
__attribute__((address_space(0))) void *mem_ptr;
(void)__builtin_amdgcn_is_shared(mem_ptr);
}

template<typename T>
__global__ void template_kernel(T *p) {
__attribute__((address_space(0))) void *mem_ptr;
(void)__builtin_amdgcn_is_shared(mem_ptr);
}

void hfun() {
__attribute__((address_space(0))) void *mem_ptr;
(void)__builtin_amdgcn_is_shared(mem_ptr); // host-error {{reference to __device__ function '__builtin_amdgcn_is_shared' in __host__ function}}
}

template<typename T>
void template_hfun(T *p) {
__attribute__((address_space(0))) void *mem_ptr;
(void)__builtin_amdgcn_is_shared(mem_ptr); // host-error {{reference to __device__ function '__builtin_amdgcn_is_shared' in __host__ function}}
}


int main() {
int *p;
kernel<<<1,1>>>();
template_kernel<<<1,1>>>(p);
template_hfun(p); // host-note {{called by 'main'}}
}