CUDASecurity-DCL01ΒΆ

Do not declare an identifier with conflicting Memory attribute specifiers

Required inputs: IR

All declarations of an identifier should have the same memory specifiers (e.g. __device__ , __constant__ or __shared__).

Scope: Host, Device.
Audience: CUDA C++.
Category: Mandatory.
Hardware Applicability: All Compute Capabilities.
Rationale

Differing specifiers on different declarations of the same identifier can make it unclear which specifiers are applicable to the identifier and which are disregarded. No diagnostic is guaranteed for mismatches of memory specifiers across different declarations. This can lead to undefined behavior.

The language specification demands (see [spec.target]) that "If a CUDA target specifier appears in any declaration of a variable or function, it shall be present in all other declarations, explicit specializations and explicit instantiations of that entity; no diagnostic is required." This rule therefore also applies to the redundant __device__ specifier that may appear in addition to __constant__ or __shared__.

Example (Bad)
__constant__ int cs;

extern __shared__ int cs; // ill-formed, memory specifiers don't match previous declaration.
Excerpt from NVIDIA CUDA C++ Guidelines for robust and safety-critical programming, Version 3.0.1, Copyright (C) 2018-2023 NVIDIA Corporation.

Possible Messages

Key

Text

Severity

Disabled

memory_specifier_mismatch

Mismatch in memory attribute specifier

None

False

Options