MLIR 24.0.0git
ROCDLDialect.cpp
Go to the documentation of this file.
1//===- ROCDLDialect.cpp - ROCDL IR Ops and 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 types and operation details for the ROCDL IR dialect in
10// MLIR, and the LLVM IR dialect. It also 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
23#include "mlir/IR/Builders.h"
26#include "mlir/IR/Operation.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/TypeSwitch.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32
33using namespace mlir;
34using namespace ROCDL;
35
36#include "mlir/Dialect/LLVMIR/ROCDLOpsDialect.cpp.inc"
37#include "mlir/Dialect/LLVMIR/ROCDLOpsEnums.cpp.inc"
38
39//===----------------------------------------------------------------------===//
40// ROCDLDialect initialization, type parsing, and registration.
41//===----------------------------------------------------------------------===//
42
43namespace {
44struct ROCDLInlinerInterface final : DialectInlinerInterface {
45 using DialectInlinerInterface::DialectInlinerInterface;
46 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
47 return true;
48 }
49};
50} // namespace
51
52// TODO: This should be the llvm.rocdl dialect once this is supported.
53void ROCDLDialect::initialize() {
54 registerROCDLDialectOperations(this);
55
56 addAttributes<
57#define GET_ATTRDEF_LIST
58#include "mlir/Dialect/LLVMIR/ROCDLOpsAttributes.cpp.inc"
59 >();
60
61 // Support unknown operations because not all ROCDL operations are registered.
62 allowUnknownOperations();
63 addInterfaces<ROCDLInlinerInterface>();
64 declarePromisedInterface<gpu::TargetAttrInterface, ROCDLTargetAttr>();
65}
66
67LLVM::ModFlagBehavior
68BufferOOBModeModuleFlagAttr::getModuleFlagBehavior() const {
69 return LLVM::ModFlagBehavior::Max;
70}
71
72StringAttr BufferOOBModeModuleFlagAttr::getModuleFlagKey() const {
73 return StringAttr::get(getContext(),
74 ROCDLDialect::getModuleFlagKeyBufferOOBModeName());
75}
76
77Attribute BufferOOBModeModuleFlagAttr::getModuleFlagValue() const {
78 return BufferOOBModeAttr::get(getContext(), getValue());
79}
80
81LLVM::ModFlagBehavior
82TBufferOOBModeModuleFlagAttr::getModuleFlagBehavior() const {
83 return LLVM::ModFlagBehavior::Max;
84}
85
86StringAttr TBufferOOBModeModuleFlagAttr::getModuleFlagKey() const {
87 return StringAttr::get(getContext(),
88 ROCDLDialect::getModuleFlagKeyTBufferOOBModeName());
89}
90
91Attribute TBufferOOBModeModuleFlagAttr::getModuleFlagValue() const {
92 return BufferOOBModeAttr::get(getContext(), getValue());
93}
94
95LogicalResult ROCDLDialect::verifyOperationAttribute(Operation *op,
96 NamedAttribute attr) {
97 // Kernel function attribute should be attached to functions.
98 if (kernelAttrName.getName() == attr.getName()) {
99 if (!isa<LLVM::LLVMFuncOp>(op)) {
100 return op->emitError() << "'" << kernelAttrName.getName()
101 << "' attribute attached to unexpected op";
102 }
103 }
104 // xnack/sramecc describe the whole code object.
105 if (attr.getName() == xnackAttrName.getName() ||
106 attr.getName() == srameccAttrName.getName()) {
108 return op->emitError()
109 << attr.getName() << " is only supported on modules";
110 if (!isa<BoolAttr>(attr.getValue()))
111 return op->emitError() << attr.getName() << " must be a boolean";
112 }
113 return success();
114}
115
116//===----------------------------------------------------------------------===//
117// ROCDL op custom parsers/printers.
118//===----------------------------------------------------------------------===//
119
120template <typename EnumAttrT, typename EnumT>
121static ParseResult parseCachePolicyEnum(OpAsmParser &parser,
122 Attribute &cachePolicy) {
123 if (parser.parseLess())
124 return failure();
125 FailureOr<EnumT> parsed = FieldParser<EnumT>::parse(parser);
126 if (failed(parsed))
127 return failure();
128 if (parser.parseGreater())
129 return failure();
130 cachePolicy = EnumAttrT::get(parser.getContext(), *parsed);
131 return success();
132}
133
135 Attribute &cachePolicy) {
136 uint32_t rawValue;
137 OptionalParseResult rawValueParseResult =
138 parser.parseOptionalInteger(rawValue);
139 if (rawValueParseResult.has_value()) {
140 if (failed(*rawValueParseResult))
141 return failure();
142 cachePolicy =
143 IntegerAttr::get(IntegerType::get(parser.getContext(), 32), rawValue);
144 return success();
145 }
146
147 StringRef policyFamily;
148 auto loc = parser.getCurrentLocation();
149 if (failed(parser.parseOptionalKeyword(
150 &policyFamily, {"pre_gfx12", "gfx942", "gfx12", "gfx12_atomic"}))) {
151 return parser.emitError(loc)
152 << "expected cache policy family 'pre_gfx12', 'gfx942', 'gfx12', "
153 "'gfx12_atomic', or a 32-bit integer";
154 }
155
156 if (policyFamily == "pre_gfx12")
158 parser, cachePolicy);
159 if (policyFamily == "gfx942")
161 parser, cachePolicy);
162 if (policyFamily == "gfx12")
164 parser, cachePolicy);
165 return parseCachePolicyEnum<Gfx12AtomicCachePolicyAttr,
166 Gfx12AtomicCachePolicy>(parser, cachePolicy);
167}
168
169template <typename EnumAttrT>
170static void printCachePolicyEnum(OpAsmPrinter &printer, EnumAttrT cachePolicy,
171 StringRef family) {
172 printer << family << "<" << cachePolicy.getValue() << ">";
173}
174
176 Attribute cachePolicy) {
177 llvm::TypeSwitch<Attribute>(cachePolicy)
178 .Case<IntegerAttr>([&](IntegerAttr rawPolicy) {
179 printer << rawPolicy.getValue().getZExtValue();
180 })
181 .Case<PreGfx12CachePolicyAttr>([&](PreGfx12CachePolicyAttr policy) {
182 printCachePolicyEnum(printer, policy, "pre_gfx12");
183 })
184 .Case<Gfx942CachePolicyAttr>([&](Gfx942CachePolicyAttr policy) {
185 printCachePolicyEnum(printer, policy, "gfx942");
186 })
187 .Case<Gfx12CachePolicyAttr>([&](Gfx12CachePolicyAttr policy) {
188 printCachePolicyEnum(printer, policy, "gfx12");
189 })
190 .Case<Gfx12AtomicCachePolicyAttr>([&](Gfx12AtomicCachePolicyAttr policy) {
191 printCachePolicyEnum(printer, policy, "gfx12_atomic");
192 })
193 .DefaultUnreachable("unknown ROCDL cache policy attribute");
194}
195
196//===----------------------------------------------------------------------===//
197// ROCDL target attribute.
198//===----------------------------------------------------------------------===//
199LogicalResult
200ROCDLTargetAttr::verify(function_ref<InFlightDiagnostic()> emitError,
201 int optLevel, StringRef triple, StringRef chip,
202 StringRef features, StringRef abiVersion,
203 DictionaryAttr flags, ArrayAttr files) {
204 if (optLevel < 0 || optLevel > 3) {
205 emitError() << "The optimization level must be a number between 0 and 3.";
206 return failure();
207 }
208 if (triple.empty()) {
209 emitError() << "The target triple cannot be empty.";
210 return failure();
211 }
212 if (chip.empty()) {
213 emitError() << "The target chip cannot be empty.";
214 return failure();
215 }
216 if (abiVersion != "400" && abiVersion != "500" && abiVersion != "600") {
217 emitError() << "Invalid ABI version, it must be `400`, `500` or '600'.";
218 return failure();
219 }
220 if (files && !llvm::all_of(files, [](::mlir::Attribute attr) {
221 return mlir::isa_and_nonnull<StringAttr>(attr);
222 })) {
223 emitError() << "All the elements in the `link` array must be strings.";
224 return failure();
225 }
226 return success();
227}
228
229#define GET_ATTRDEF_CLASSES
230#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.