MLIR 24.0.0git
NVVMAttributes.cpp
Go to the documentation of this file.
1//===- NVVMAttributes.cpp - NVVM enums and attributes ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the NVVM dialect enums and attributes.
10//
11//===----------------------------------------------------------------------===//
12
14
18#include "llvm/ADT/STLExtras.h"
19#include <optional>
20
21using namespace mlir;
22using namespace NVVM;
23
24#include "mlir/Dialect/LLVMIR/NVVMOpsEnums.cpp.inc"
25
26//===----------------------------------------------------------------------===//
27// NVVM Address Space Attr
28//===----------------------------------------------------------------------===//
29
30unsigned NVVMMemorySpaceAttr::getAddressSpace() const {
31 return static_cast<unsigned>(getValue());
32}
33
34bool NVVMMemorySpaceAttr::isValidLoad(
35 Type type, ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
36 const ::mlir::DataLayout *dataLayout,
38 return LLVM::detail::isValidLoadStoreImpl(type, ordering, alignment,
39 dataLayout, emitError);
40}
41
42bool NVVMMemorySpaceAttr::isValidStore(
43 Type type, ptr::AtomicOrdering ordering, std::optional<int64_t> alignment,
44 const ::mlir::DataLayout *dataLayout,
46 return LLVM::detail::isValidLoadStoreImpl(type, ordering, alignment,
47 dataLayout, emitError);
48}
49
50bool NVVMMemorySpaceAttr::isValidAtomicOp(
51 ptr::AtomicBinOp op, Type type, ptr::AtomicOrdering ordering,
52 std::optional<int64_t> alignment, const ::mlir::DataLayout *dataLayout,
54 // TODO: update this method once `ptr.atomic_rmw` is implemented.
55 assert(false && "unimplemented, see TODO in the source.");
56 return false;
57}
58
59bool NVVMMemorySpaceAttr::isValidAtomicXchg(
60 Type type, ptr::AtomicOrdering successOrdering,
61 ptr::AtomicOrdering failureOrdering, std::optional<int64_t> alignment,
62 const ::mlir::DataLayout *dataLayout,
64 // TODO: update this method once `ptr.atomic_cmpxchg` is implemented.
65 assert(false && "unimplemented, see TODO in the source.");
66 return false;
67}
68
69bool NVVMMemorySpaceAttr::isValidAddrSpaceCast(
71 // TODO: update this method once the `ptr.addrspace_cast` op is added to the
72 // dialect.
73 assert(false && "unimplemented, see TODO in the source.");
74 return false;
75}
76
77bool NVVMMemorySpaceAttr::isValidPtrIntCast(
78 Type intLikeTy, Type ptrLikeTy,
80 // TODO: update this method once the int-cast ops are added to the `ptr`
81 // dialect.
82 assert(false && "unimplemented, see TODO in the source.");
83 return false;
84}
85
86//===----------------------------------------------------------------------===//
87// NVVM target attribute.
88//===----------------------------------------------------------------------===//
89LogicalResult
90NVVMTargetAttr::verify(function_ref<InFlightDiagnostic()> emitError,
91 int optLevel, StringRef triple, StringRef chip,
92 StringRef features, DictionaryAttr flags,
93 ArrayAttr files, bool verifyTarget) {
94 if (optLevel < 0 || optLevel > 3) {
95 emitError() << "The optimization level must be a number between 0 and 3.";
96 return failure();
97 }
98 if (triple.empty()) {
99 emitError() << "The target triple cannot be empty.";
100 return failure();
101 }
102 if (chip.empty()) {
103 emitError() << "The target chip cannot be empty.";
104 return failure();
105 }
106 if (files && !llvm::all_of(files, [](::mlir::Attribute attr) {
107 return mlir::isa_and_nonnull<StringAttr>(attr);
108 })) {
109 emitError() << "All the elements in the `link` array must be strings.";
110 return failure();
111 }
112 return success();
113}
114
115LogicalResult NVVMTargetAttr::verifyTarget(Operation *gpuModule) {
116 if (!getVerifyTarget())
117 return success();
118
119 auto gpuModuleOp = llvm::dyn_cast<gpu::GPUModuleOp>(gpuModule);
120 if (!gpuModuleOp) {
121 return emitError(gpuModule->getLoc(),
122 "NVVM target attribute must be attached to a GPU module");
123 }
124
125 std::optional<unsigned> targetFullSmVersion =
127 if (!targetFullSmVersion)
128 return emitError(gpuModule->getLoc())
129 << "invalid NVVM target chip \"" << getChip()
130 << "\", expected sm_<version>[a|f]";
131
132 if (!NVVMCheckSMVersion::isMinimumSMVersion(*targetFullSmVersion)) {
133 return emitError(gpuModule->getLoc(),
134 "Minimum NVVM target SM version is sm_20");
135 }
136
137 if (gpuModuleOp
138 ->walk([&](Operation *op) {
139 if (auto reqOp = llvm::dyn_cast<NVVM::RequiresSMInterface>(op)) {
140 const NVVMCheckSMVersion requirement =
141 reqOp.getRequiredMinSMVersion();
142 if (!requirement.isCompatibleWith(*targetFullSmVersion)) {
143 op->emitOpError() << "is not supported on " << getChip();
144 return WalkResult::interrupt();
145 }
146 }
147 return WalkResult::advance();
148 })
149 .wasInterrupted())
150 return failure();
151
152 return success();
153}
154
155#define GET_ATTRDEF_CLASSES
156#include "mlir/Dialect/LLVMIR/NVVMOpsAttributes.cpp.inc"
157
158//===----------------------------------------------------------------------===//
159// NVVMDialect attribute registration
160//===----------------------------------------------------------------------===//
161
162void NVVMDialect::registerAttributes() {
163 addAttributes<
164#define GET_ATTRDEF_LIST
165#include "mlir/Dialect/LLVMIR/NVVMOpsAttributes.cpp.inc"
166 >();
167}
return success()
ArrayAttr()
This class represents a diagnostic that is inflight and set to be reported.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:240
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
static WalkResult advance()
Definition WalkResult.h:47
static WalkResult interrupt()
Definition WalkResult.h:46
bool isValidLoadStoreImpl(Type type, ptr::AtomicOrdering ordering, std::optional< int64_t > alignment, const ::mlir::DataLayout *dataLayout, function_ref< InFlightDiagnostic()> emitError)
Checks whether the given type is an LLVM type that can be loaded or stored.
Definition LLVMAttrs.cpp:71
void walk(Operation *op, function_ref< void(Region *)> callback, WalkOrder order)
Walk all of the regions, blocks, or operations nested under (and including) the given operation.
Definition Visitors.h:102
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155
static bool isMinimumSMVersion(unsigned fullSmVersion)
static std::optional< unsigned > getTargetFullSmVersionFromStr(StringRef smVersionString)
bool isCompatibleWith(const unsigned &targetFullSmVersion) const