MLIR 24.0.0git
ROCDLDialect.cpp
Go to the documentation of this file.
1//===- ROCDLDialect.cpp - ROCDL IR dialect registration -------------------===//
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 ROCDL IR dialect in MLIR, its attributes and the
10// custom parsers and printers shared by its ops, and registers the dialect.
11//
12// The ROCDL dialect only contains GPU specific additions on top of the general
13// LLVM dialect.
14//
15//===----------------------------------------------------------------------===//
16
18
19#include "IR/ROCDLOps.h"
20
24#include "mlir/IR/Builders.h"
27#include "mlir/IR/Operation.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/ADT/TypeSwitch.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/raw_ostream.h"
33
34// We can get the op registration hook from any partition: initial benchmarking
35// showed `Sched` was cheapest.
37
38using namespace mlir;
39using namespace ROCDL;
40
41#include "mlir/Dialect/LLVMIR/ROCDLOpsDialect.cpp.inc"
42#include "mlir/Dialect/LLVMIR/ROCDLOpsEnums.cpp.inc"
43
44//===----------------------------------------------------------------------===//
45// ROCDLDialect initialization, type parsing, and registration.
46//===----------------------------------------------------------------------===//
47
48namespace {
49struct ROCDLInlinerInterface final : DialectInlinerInterface {
50 using DialectInlinerInterface::DialectInlinerInterface;
51 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
52 return true;
53 }
54};
55} // namespace
56
57// TODO: This should be the llvm.rocdl dialect once this is supported.
58void ROCDLDialect::initialize() {
59 // Support unknown operations because not all ROCDL operations are registered.
60 allowUnknownOperations();
61 addInterfaces<ROCDLInlinerInterface>();
62 declarePromisedInterface<gpu::TargetAttrInterface, ROCDLTargetAttr>();
63}
64
65LLVM::ModFlagBehavior
66BufferOOBModeModuleFlagAttr::getModuleFlagBehavior() const {
67 return LLVM::ModFlagBehavior::Max;
68}
69
70StringAttr BufferOOBModeModuleFlagAttr::getModuleFlagKey() const {
71 return StringAttr::get(getContext(),
72 ROCDLDialect::getModuleFlagKeyBufferOOBModeName());
73}
74
75Attribute BufferOOBModeModuleFlagAttr::getModuleFlagValue() const {
76 return BufferOOBModeAttr::get(getContext(), getValue());
77}
78
79LLVM::ModFlagBehavior
80TBufferOOBModeModuleFlagAttr::getModuleFlagBehavior() const {
81 return LLVM::ModFlagBehavior::Max;
82}
83
84StringAttr TBufferOOBModeModuleFlagAttr::getModuleFlagKey() const {
85 return StringAttr::get(getContext(),
86 ROCDLDialect::getModuleFlagKeyTBufferOOBModeName());
87}
88
89Attribute TBufferOOBModeModuleFlagAttr::getModuleFlagValue() const {
90 return BufferOOBModeAttr::get(getContext(), getValue());
91}
92
93LogicalResult ROCDLDialect::verifyOperationAttribute(Operation *op,
94 NamedAttribute attr) {
95 // Kernel function attribute should be attached to functions.
96 if (kernelAttrName.getName() == attr.getName()) {
97 if (!isa<LLVM::LLVMFuncOp>(op)) {
98 return op->emitError() << "'" << kernelAttrName.getName()
99 << "' attribute attached to unexpected op";
100 }
101 }
102 // xnack/sramecc describe the whole code object.
103 if (attr.getName() == xnackAttrName.getName() ||
104 attr.getName() == srameccAttrName.getName()) {
106 return op->emitError()
107 << attr.getName() << " is only supported on modules";
108 if (!isa<BoolAttr>(attr.getValue()))
109 return op->emitError() << attr.getName() << " must be a boolean";
110 }
111 return success();
112}
113
114//===----------------------------------------------------------------------===//
115// ROCDL op custom parsers/printers.
116//===----------------------------------------------------------------------===//
117
118template <typename EnumAttrT, typename EnumT>
119static ParseResult parseCachePolicyEnum(OpAsmParser &parser,
120 Attribute &cachePolicy) {
121 if (parser.parseLess())
122 return failure();
123 FailureOr<EnumT> parsed = FieldParser<EnumT>::parse(parser);
124 if (failed(parsed))
125 return failure();
126 if (parser.parseGreater())
127 return failure();
128 cachePolicy = EnumAttrT::get(parser.getContext(), *parsed);
129 return success();
130}
131
133 Attribute &cachePolicy) {
134 uint32_t rawValue;
135 OptionalParseResult rawValueParseResult =
136 parser.parseOptionalInteger(rawValue);
137 if (rawValueParseResult.has_value()) {
138 if (failed(*rawValueParseResult))
139 return failure();
140 cachePolicy =
141 IntegerAttr::get(IntegerType::get(parser.getContext(), 32), rawValue);
142 return success();
143 }
144
145 StringRef policyFamily;
146 auto loc = parser.getCurrentLocation();
147 if (failed(parser.parseOptionalKeyword(
148 &policyFamily, {"pre_gfx12", "gfx942", "gfx12", "gfx12_atomic"}))) {
149 return parser.emitError(loc)
150 << "expected cache policy family 'pre_gfx12', 'gfx942', 'gfx12', "
151 "'gfx12_atomic', or a 32-bit integer";
152 }
153
154 if (policyFamily == "pre_gfx12")
156 parser, cachePolicy);
157 if (policyFamily == "gfx942")
159 parser, cachePolicy);
160 if (policyFamily == "gfx12")
162 parser, cachePolicy);
163 return parseCachePolicyEnum<Gfx12AtomicCachePolicyAttr,
164 Gfx12AtomicCachePolicy>(parser, cachePolicy);
165}
166
167template <typename EnumAttrT>
168static void printCachePolicyEnum(OpAsmPrinter &printer, EnumAttrT cachePolicy,
169 StringRef family) {
170 printer << family << "<" << cachePolicy.getValue() << ">";
171}
172
174 Attribute cachePolicy) {
175 llvm::TypeSwitch<Attribute>(cachePolicy)
176 .Case<IntegerAttr>([&](IntegerAttr rawPolicy) {
177 printer << rawPolicy.getValue().getZExtValue();
178 })
179 .Case<PreGfx12CachePolicyAttr>([&](PreGfx12CachePolicyAttr policy) {
180 printCachePolicyEnum(printer, policy, "pre_gfx12");
181 })
182 .Case<Gfx942CachePolicyAttr>([&](Gfx942CachePolicyAttr policy) {
183 printCachePolicyEnum(printer, policy, "gfx942");
184 })
185 .Case<Gfx12CachePolicyAttr>([&](Gfx12CachePolicyAttr policy) {
186 printCachePolicyEnum(printer, policy, "gfx12");
187 })
188 .Case<Gfx12AtomicCachePolicyAttr>([&](Gfx12AtomicCachePolicyAttr policy) {
189 printCachePolicyEnum(printer, policy, "gfx12_atomic");
190 })
191 .DefaultUnreachable("unknown ROCDL cache policy attribute");
192}
193
194//===----------------------------------------------------------------------===//
195// ROCDL target attribute.
196//===----------------------------------------------------------------------===//
197LogicalResult
198ROCDLTargetAttr::verify(function_ref<InFlightDiagnostic()> emitError,
199 int optLevel, StringRef triple, StringRef chip,
200 StringRef features, StringRef abiVersion,
201 DictionaryAttr flags, ArrayAttr files) {
202 if (optLevel < 0 || optLevel > 3) {
203 emitError() << "The optimization level must be a number between 0 and 3.";
204 return failure();
205 }
206 if (triple.empty()) {
207 emitError() << "The target triple cannot be empty.";
208 return failure();
209 }
210 if (chip.empty()) {
211 emitError() << "The target chip cannot be empty.";
212 return failure();
213 }
214 if (abiVersion != "400" && abiVersion != "500" && abiVersion != "600") {
215 emitError() << "Invalid ABI version, it must be `400`, `500` or '600'.";
216 return failure();
217 }
218 if (files && !llvm::all_of(files, [](::mlir::Attribute attr) {
219 return mlir::isa_and_nonnull<StringAttr>(attr);
220 })) {
221 emitError() << "All the elements in the `link` array must be strings.";
222 return failure();
223 }
224 return success();
225}
226
227#define GET_ATTRDEF_CLASSES
228#include "mlir/Dialect/LLVMIR/ROCDLOpsAttributes.cpp.inc"
return success()
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
b getContext())
static ParseResult parseCachePolicyEnum(OpAsmParser &parser, Attribute &cachePolicy)
static void printCachePolicyEnum(OpAsmPrinter &printer, EnumAttrT cachePolicy, StringRef family)
virtual OptionalParseResult parseOptionalInteger(APInt &result)=0
Parse an optional integer value from the stream.
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
MLIRContext * getContext() const
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseGreater()=0
Parse a '>' token.
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class represents a diagnostic that is inflight and set to be reported.
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:166
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
Definition Attributes.h:181
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
This class implements Optional functionality for ParseResult.
bool has_value() const
Returns true if we contain a valid ParseResult value.
bool satisfiesLLVMModule(Operation *op)
LLVM requires some operations to be inside of a Module operation.
ParseResult parseCachePolicy(OpAsmParser &parser, Attribute &cachePolicy)
void printCachePolicy(OpAsmPrinter &printer, Operation *, Attribute cachePolicy)
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
Provide a template class that can be specialized by users to dispatch to parsers.