Skip to content

[Clang][Sema] Reject unsupported opencl address space attributes in SYCL and HLSL #152528

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ Bug Fixes in This Version
targets that treat ``_Float16``/``__fp16`` as native scalar types. Previously
the warning was silently lost because the operands differed only by an implicit
cast chain. (#GH149967).
- Fix a crash when using opencl address space attributes in HLSL or SYCL device code.
(#GH152460).

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -4660,7 +4660,7 @@ def err_attribute_regparm_wrong_platform : Error<
def err_attribute_regparm_invalid_number : Error<
"'regparm' parameter must be between 0 and %0 inclusive">;
def err_attribute_not_supported_in_lang : Error<
"%0 attribute is not supported in %select{C|C++|Objective-C}1">;
"%0 attribute is not supported in %select{C|C++|HLSL|Objective-C|SYCL when compiling for the device}1">;
def err_attribute_not_supported_on_arch
: Error<"%0 attribute is not supported on '%1'">;
def warn_gcc_ignores_type_attr : Warning<
Expand Down
31 changes: 31 additions & 0 deletions clang/lib/Sema/AttributeLangSupport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===- AttributeLangSupport.h -----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the declaration of AttributeLangSupport, which is used in
/// diagnostics to indicate the language in which an attribute is (not)
/// supported.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H
#define LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H

// NOTE: The order should match the order of the %select in
// err_attribute_not_supported_in_lang in DiagnosticSemaKinds.td
namespace clang::AttributeLangSupport {
enum LANG {
C,
Cpp,
HLSL,
ObjC,
SYCLDevice,
};
} // end namespace clang::AttributeLangSupport

#endif // LLVM_CLANG_SEMA_ATTRIBUTE_LANG_SUPPORT_H
9 changes: 1 addition & 8 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "AttributeLangSupport.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTMutationListener.h"
Expand Down Expand Up @@ -73,14 +74,6 @@
using namespace clang;
using namespace sema;

namespace AttributeLangSupport {
enum LANG {
C,
Cpp,
ObjC
};
} // end namespace AttributeLangSupport

static unsigned getNumAttributeArgs(const ParsedAttr &AL) {
// FIXME: Include the type in the argument list.
return AL.getNumArgs() + AL.hasParsedType();
Expand Down
13 changes: 11 additions & 2 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "AttributeLangSupport.h"
#include "TypeLocBuilder.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
Expand Down Expand Up @@ -6573,8 +6574,16 @@ static void HandleAddressSpaceTypeAttribute(QualType &Type,
if (S.getLangOpts().HLSL)
ASIdx = Attr.asHLSLLangAS();

if (ASIdx == LangAS::Default)
llvm_unreachable("Invalid address space");
if (ASIdx == LangAS::Default) {
assert((S.getLangOpts().SYCLIsDevice || S.getLangOpts().HLSL) &&
"Unexpected language mode");
S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang)
<< Attr
<< (S.getLangOpts().SYCLIsDevice ? AttributeLangSupport::SYCLDevice
: AttributeLangSupport::HLSL);
Attr.setInvalid();
return;
}

if (DiagnoseMultipleAddrSpaceAttributes(S, Type.getAddressSpace(), ASIdx,
Attr.getLoc())) {
Expand Down
25 changes: 25 additions & 0 deletions clang/test/Sema/named_addrspace_lang.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fsyntax-only -x c++ -fsycl-is-host -verify %s
// RUN: %clang_cc1 -fsyntax-only -x c++ -fsycl-is-device -verify=sycl %s
// RUN: %clang_cc1 -fsyntax-only -x hlsl -triple dxil-pc-shadermodel6.3-library -verify=hlsl %s

// hlsl-error@#constant{{'opencl_constant' attribute is not supported in HLSL}}
// hlsl-error@#generic{{'opencl_generic' attribute is not supported in HLSL}}
// hlsl-error@#global{{'opencl_global' attribute is not supported in HLSL}}
// hlsl-error@#global_host{{'opencl_global_host' attribute is not supported in HLSL}}
// hlsl-error@#global_device{{'opencl_global_device' attribute is not supported in HLSL}}
// hlsl-error@#local{{'opencl_local' attribute is not supported in HLSL}}
// hlsl-error@#private{{'opencl_private' attribute is not supported in HLSL}}

// sycl-error@#constant{{'opencl_constant' attribute is not supported in SYCL when compiling for the device}}
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder, why only SYCL device? IMO address space attributes don't make sense for host compilation at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because I just kept the current behavior, turning llvm_unreachable into a the "attribute not supported" error.
Mechanically it's only on device because of this piece of code SemaType.cpp:6572, which was added by @bader in D89909, he might remember still why it's only device :).

Copy link
Contributor Author

@Maetveis Maetveis Aug 8, 2025

Choose a reason for hiding this comment

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

TBH I'm not entirely sure why __attribute__((opencl_*)) is supported for C++/Obj-C/CUDA etc (why isn't this guarded by getLangOpts().Opencl || getLangOpts().OpenCLCPlusPlus) either, but I also didn't want to change that.

// sycl-error@#generic{{'opencl_generic' attribute is not supported in SYCL when compiling for the device}}

int __attribute__((opencl_constant)) glob = 1; // #constant
int __attribute__((opencl_generic)) gen; // #generic
int __attribute__((opencl_global)) c; // #global
int __attribute__((opencl_global_host)) h; // #global_host
int __attribute__((opencl_global_device)) d; // #global_device
int __attribute__((opencl_local)) l; // #local
int __attribute__((opencl_private)) p; // #private

// expected-no-diagnostics