Skip to content

Commit c0f0d50

Browse files
committed
[HIP] emit macro __HIP_NO_IMAGE_SUPPORT
HIP texture/image support is optional as some devices do not have image instructions. A macro __HIP_NO_IMAGE_SUPPORT is defined for device not supporting images (https://github.com/ROCm-Developer-Tools/HIP/blob/d0448aa4c4dd0f4b29ccf6a663b7f5ad9f5183e0/docs/reference/kernel_language.md?plain=1#L426 ) Currently the macro is defined by HIP header based on predefined macros for GPU, e.g __gfx*__ , which is error prone. This patch let clang emit the predefined macro. Reviewed by: Matt Arsenault, Artem Belevich Differential Revision: https://reviews.llvm.org/D151349
1 parent dc895d0 commit c0f0d50

File tree

9 files changed

+79
-2
lines changed

9 files changed

+79
-2
lines changed

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ def warn_fe_backend_unsupported_fp_exceptions : Warning<
5555
def warn_fe_backend_invalid_feature_flag : Warning<
5656
"feature flag '%0' must start with either '+' to enable the feature or '-'"
5757
" to disable it; flag ignored">, InGroup<InvalidCommandLineArgument>;
58+
def warn_fe_backend_readonly_feature_flag : Warning<
59+
"feature flag '%0' is ignored since the feature is read only">,
60+
InGroup<InvalidCommandLineArgument>;
5861

5962
def err_incompatible_fp_eval_method_options : Error<
6063
"option 'ffp-eval-method' cannot be used with option "

clang/include/clang/Basic/TargetInfo.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/ADT/SmallSet.h"
3030
#include "llvm/ADT/StringMap.h"
3131
#include "llvm/ADT/StringRef.h"
32+
#include "llvm/ADT/StringSet.h"
3233
#include "llvm/Frontend/OpenMP/OMPGridValues.h"
3334
#include "llvm/IR/DerivedTypes.h"
3435
#include "llvm/Support/DataTypes.h"
@@ -267,6 +268,12 @@ class TargetInfo : public TransferrableTargetInfo,
267268
// as a DataLayout object.
268269
void resetDataLayout(StringRef DL, const char *UserLabelPrefix = "");
269270

271+
// Target features that are read-only and should not be disabled/enabled
272+
// by command line options. Such features are for emitting predefined
273+
// macros or checking availability of builtin functions and can be omitted
274+
// in function attributes in IR.
275+
llvm::StringSet<> ReadOnlyFeatures;
276+
270277
public:
271278
/// Construct a target for the given options.
272279
///
@@ -1394,6 +1401,11 @@ class TargetInfo : public TransferrableTargetInfo,
13941401
return false;
13951402
}
13961403

1404+
/// Determine whether the given target feature is read only.
1405+
bool isReadOnlyFeature(StringRef Feature) const {
1406+
return ReadOnlyFeatures.count(Feature);
1407+
}
1408+
13971409
/// Identify whether this target supports multiversioning of functions,
13981410
/// which requires support for cpu_supports and cpu_is functionality.
13991411
bool supportsMultiVersioning() const {
@@ -1711,6 +1723,9 @@ class TargetInfo : public TransferrableTargetInfo,
17111723
: std::optional<VersionTuple>();
17121724
}
17131725

1726+
/// Whether to support HIP image/texture API's.
1727+
virtual bool hasHIPImageSupport() const { return true; }
1728+
17141729
protected:
17151730
/// Copy type and layout related info.
17161731
void copyAuxTarget(const TargetInfo *Aux);

clang/lib/Basic/TargetInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ bool TargetInfo::initFeatureMap(
528528
// Apply the feature via the target.
529529
if (Name[0] != '+' && Name[0] != '-')
530530
Diags.Report(diag::warn_fe_backend_invalid_feature_flag) << Name;
531+
else if (isReadOnlyFeature(Name.substr(1)))
532+
Diags.Report(diag::warn_fe_backend_readonly_feature_flag) << Name;
531533
else
532534
setFeatureEnabled(Features, Name.substr(1), Name[0] == '+');
533535
}

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ AMDGPUTargetInfo::AMDGPUTargetInfo(const llvm::Triple &Triple,
244244

245245
MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
246246
CUMode = !(GPUFeatures & llvm::AMDGPU::FEATURE_WGP);
247+
ReadOnlyFeatures.insert("image-insts");
247248
}
248249

249250
void AMDGPUTargetInfo::adjust(DiagnosticsEngine &Diags, LangOptions &Opts) {

clang/lib/Basic/Targets/AMDGPU.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
4646
/// Whether to use cumode or WGP mode. True for cumode. False for WGP mode.
4747
bool CUMode;
4848

49+
/// Whether having image instructions.
50+
bool HasImage = false;
51+
4952
/// Target ID is device name followed by optional feature name postfixed
5053
/// by plus or minus sign delimitted by colon, e.g. gfx908:xnack+:sramecc-.
5154
/// If the target ID contains feature+, map it to true.
@@ -449,6 +452,8 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
449452
CUMode = true;
450453
else if (F == "-cumode")
451454
CUMode = false;
455+
else if (F == "+image-insts")
456+
HasImage = true;
452457
bool IsOn = F.front() == '+';
453458
StringRef Name = StringRef(F).drop_front();
454459
if (!llvm::is_contained(TargetIDFeatures, Name))
@@ -469,6 +474,8 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
469474
return getCanonicalTargetID(getArchNameAMDGCN(GPUKind),
470475
OffloadArchFeatures);
471476
}
477+
478+
bool hasHIPImageSupport() const override { return HasImage; }
472479
};
473480

474481
} // namespace targets

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,9 @@ bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
22992299
AddedAttr = true;
23002300
}
23012301
if (!Features.empty() && SetTargetFeatures) {
2302+
llvm::erase_if(Features, [&](const std::string& F) {
2303+
return getTarget().isReadOnlyFeature(F.substr(1));
2304+
});
23022305
llvm::sort(Features);
23032306
Attrs.addAttribute("target-features", llvm::join(Features, ","));
23042307
AddedAttr = true;

clang/lib/Frontend/InitPreprocessor.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,11 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI,
583583
Builder.defineMacro("__HIP_MEMORY_SCOPE_WORKGROUP", "3");
584584
Builder.defineMacro("__HIP_MEMORY_SCOPE_AGENT", "4");
585585
Builder.defineMacro("__HIP_MEMORY_SCOPE_SYSTEM", "5");
586-
if (LangOpts.CUDAIsDevice)
586+
if (LangOpts.CUDAIsDevice) {
587587
Builder.defineMacro("__HIP_DEVICE_COMPILE__");
588+
if (!TI.hasHIPImageSupport())
589+
Builder.defineMacro("__HIP_NO_IMAGE_SUPPORT", "1");
590+
}
588591
if (LangOpts.GPUDefaultStream ==
589592
LangOptions::GPUDefaultStreamKind::PerThread)
590593
Builder.defineMacro("HIP_API_PER_THREAD_DEFAULT_STREAM");

clang/test/Driver/hip-macros.hip

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,22 @@
4242
// WARN-CUMODE-NOT: warning: ignoring '-mno-cumode' option as it is not currently supported for processor 'gfx906' [-Woption-ignored]
4343
// CUMODE-ON-DAG: #define __AMDGCN_CUMODE__ 1
4444
// CUMODE-OFF-DAG: #define __AMDGCN_CUMODE__ 0
45+
46+
// RUN: %clang -E -dM --offload-arch=gfx90a --cuda-device-only -nogpuinc -nogpulib \
47+
// RUN: %s 2>&1 | FileCheck --check-prefixes=IMAGE,NOWARN %s
48+
// RUN: %clang -E -dM --offload-arch=gfx1100 --cuda-device-only -nogpuinc -nogpulib \
49+
// RUN: %s 2>&1 | FileCheck --check-prefixes=IMAGE,NOWARN %s
50+
// RUN: %clang -E -dM --offload-arch=gfx940 --cuda-device-only -nogpuinc -nogpulib \
51+
// RUN: %s 2>&1 | FileCheck --check-prefixes=NOIMAGE,NOWARN %s
52+
// RUN: %clang -E -dM --offload-arch=gfx941 --cuda-device-only -nogpuinc -nogpulib \
53+
// RUN: %s 2>&1 | FileCheck --check-prefixes=NOIMAGE,NOWARN %s
54+
// RUN: %clang -E -dM --offload-arch=gfx942 --cuda-device-only -nogpuinc -nogpulib \
55+
// RUN: %s 2>&1 | FileCheck --check-prefixes=NOIMAGE,NOWARN %s
56+
// RUN: %clang -E -dM --offload-arch=gfx1100 --cuda-device-only -nogpuinc -nogpulib \
57+
// RUN: -Xclang -target-feature -Xclang "-image-insts" %s 2>&1 | FileCheck --check-prefixes=IMAGE,WARN %s
58+
// RUN: %clang -E -dM --offload-arch=gfx940 --cuda-device-only -nogpuinc -nogpulib \
59+
// RUN: -Xclang -target-feature -Xclang "+image-insts" %s 2>&1 | FileCheck --check-prefixes=NOIMAGE,WARN %s
60+
// NOWARN-NOT: warning
61+
// WARN: warning: feature flag '{{[+|-]}}image-insts' is ignored since the feature is read only [-Winvalid-command-line-argument]
62+
// IMAGE-NOT: #define __HIP_NO_IMAGE_SUPPORT
63+
// NOIMAGE: #define __HIP_NO_IMAGE_SUPPORT 1

llvm/lib/TargetParser/TargetParser.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ void AMDGPU::fillAMDGPUFeatureMap(StringRef GPU, const Triple &T,
279279
Features["gfx10-3-insts"] = true;
280280
Features["gfx11-insts"] = true;
281281
Features["atomic-fadd-rtn-insts"] = true;
282+
Features["image-insts"] = true;
282283
break;
283284
case GK_GFX1036:
284285
case GK_GFX1035:
@@ -301,6 +302,7 @@ void AMDGPU::fillAMDGPUFeatureMap(StringRef GPU, const Triple &T,
301302
Features["gfx9-insts"] = true;
302303
Features["gfx10-insts"] = true;
303304
Features["gfx10-3-insts"] = true;
305+
Features["image-insts"] = true;
304306
Features["s-memrealtime"] = true;
305307
Features["s-memtime-inst"] = true;
306308
break;
@@ -322,6 +324,7 @@ void AMDGPU::fillAMDGPUFeatureMap(StringRef GPU, const Triple &T,
322324
Features["gfx8-insts"] = true;
323325
Features["gfx9-insts"] = true;
324326
Features["gfx10-insts"] = true;
327+
Features["image-insts"] = true;
325328
Features["s-memrealtime"] = true;
326329
Features["s-memtime-inst"] = true;
327330
break;
@@ -333,7 +336,27 @@ void AMDGPU::fillAMDGPUFeatureMap(StringRef GPU, const Triple &T,
333336
Features["atomic-ds-pk-add-16-insts"] = true;
334337
Features["atomic-flat-pk-add-16-insts"] = true;
335338
Features["atomic-global-pk-add-bf16-inst"] = true;
336-
[[fallthrough]];
339+
Features["gfx90a-insts"] = true;
340+
Features["atomic-buffer-global-pk-add-f16-insts"] = true;
341+
Features["atomic-fadd-rtn-insts"] = true;
342+
Features["dot3-insts"] = true;
343+
Features["dot4-insts"] = true;
344+
Features["dot5-insts"] = true;
345+
Features["dot6-insts"] = true;
346+
Features["mai-insts"] = true;
347+
Features["dl-insts"] = true;
348+
Features["dot1-insts"] = true;
349+
Features["dot2-insts"] = true;
350+
Features["dot7-insts"] = true;
351+
Features["dot10-insts"] = true;
352+
Features["gfx9-insts"] = true;
353+
Features["gfx8-insts"] = true;
354+
Features["16-bit-insts"] = true;
355+
Features["dpp"] = true;
356+
Features["s-memrealtime"] = true;
357+
Features["ci-insts"] = true;
358+
Features["s-memtime-inst"] = true;
359+
break;
337360
case GK_GFX90A:
338361
Features["gfx90a-insts"] = true;
339362
Features["atomic-buffer-global-pk-add-f16-insts"] = true;
@@ -381,6 +404,7 @@ void AMDGPU::fillAMDGPUFeatureMap(StringRef GPU, const Triple &T,
381404
case GK_GFX602:
382405
case GK_GFX601:
383406
case GK_GFX600:
407+
Features["image-insts"] = true;
384408
Features["s-memtime-inst"] = true;
385409
break;
386410
case GK_NONE:

0 commit comments

Comments
 (0)