29#include "llvm/ADT/StringExtras.h"
30#include "llvm/ADT/StringRef.h"
31#include "llvm/ADT/TypeSwitch.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/raw_ostream.h"
38#include "mlir/Dialect/LLVMIR/ROCDLOpsDialect.cpp.inc"
39#include "mlir/Dialect/LLVMIR/ROCDLOpsEnums.cpp.inc"
46struct ROCDLInlinerInterface final : DialectInlinerInterface {
47 using DialectInlinerInterface::DialectInlinerInterface;
48 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const final {
55void ROCDLDialect::initialize() {
56 registerROCDLDialectOperations(
this);
59#define GET_ATTRDEF_LIST
60#include "mlir/Dialect/LLVMIR/ROCDLOpsAttributes.cpp.inc"
64 allowUnknownOperations();
65 addInterfaces<ROCDLInlinerInterface>();
66 declarePromisedInterface<gpu::TargetAttrInterface, ROCDLTargetAttr>();
70BufferOOBModeModuleFlagAttr::getModuleFlagBehavior()
const {
71 return LLVM::ModFlagBehavior::Max;
74StringAttr BufferOOBModeModuleFlagAttr::getModuleFlagKey()
const {
76 ROCDLDialect::getModuleFlagKeyBufferOOBModeName());
79Attribute BufferOOBModeModuleFlagAttr::getModuleFlagValue()
const {
80 return BufferOOBModeAttr::get(
getContext(), getValue());
84TBufferOOBModeModuleFlagAttr::getModuleFlagBehavior()
const {
85 return LLVM::ModFlagBehavior::Max;
88StringAttr TBufferOOBModeModuleFlagAttr::getModuleFlagKey()
const {
90 ROCDLDialect::getModuleFlagKeyTBufferOOBModeName());
93Attribute TBufferOOBModeModuleFlagAttr::getModuleFlagValue()
const {
94 return BufferOOBModeAttr::get(
getContext(), getValue());
97LogicalResult ROCDLDialect::verifyOperationAttribute(
Operation *op,
100 if (kernelAttrName.getName() == attr.
getName()) {
101 if (!isa<LLVM::LLVMFuncOp>(op)) {
102 return op->
emitError() <<
"'" << kernelAttrName.getName()
103 <<
"' attribute attached to unexpected op";
107 if (attr.
getName() == xnackAttrName.getName() ||
108 attr.
getName() == srameccAttrName.getName()) {
111 << attr.
getName() <<
" is only supported on modules";
112 if (!isa<BoolAttr>(attr.
getValue()))
122template <
typename EnumAttrT,
typename EnumT>
132 cachePolicy = EnumAttrT::get(parser.
getContext(), *parsed);
142 if (failed(*rawValueParseResult))
145 IntegerAttr::get(IntegerType::get(parser.
getContext(), 32), rawValue);
149 StringRef policyFamily;
152 &policyFamily, {
"pre_gfx12",
"gfx942",
"gfx12",
"gfx12_atomic"}))) {
154 <<
"expected cache policy family 'pre_gfx12', 'gfx942', 'gfx12', "
155 "'gfx12_atomic', or a 32-bit integer";
158 if (policyFamily ==
"pre_gfx12")
160 parser, cachePolicy);
161 if (policyFamily ==
"gfx942")
163 parser, cachePolicy);
164 if (policyFamily ==
"gfx12")
166 parser, cachePolicy);
168 Gfx12AtomicCachePolicy>(parser, cachePolicy);
171template <
typename EnumAttrT>
174 printer << family <<
"<" << cachePolicy.getValue() <<
">";
180 .Case<IntegerAttr>([&](IntegerAttr rawPolicy) {
181 printer << rawPolicy.getValue().getZExtValue();
183 .Case<PreGfx12CachePolicyAttr>([&](PreGfx12CachePolicyAttr policy) {
186 .Case<Gfx942CachePolicyAttr>([&](Gfx942CachePolicyAttr policy) {
189 .Case<Gfx12CachePolicyAttr>([&](Gfx12CachePolicyAttr policy) {
192 .Case<Gfx12AtomicCachePolicyAttr>([&](Gfx12AtomicCachePolicyAttr policy) {
195 .DefaultUnreachable(
"unknown ROCDL cache policy attribute");
203 int optLevel, StringRef triple, StringRef chip,
204 StringRef features, StringRef abiVersion,
205 DictionaryAttr flags, ArrayAttr files) {
206 if (optLevel < 0 || optLevel > 3) {
207 emitError() <<
"The optimization level must be a number between 0 and 3.";
210 if (triple.empty()) {
211 emitError() <<
"The target triple cannot be empty.";
215 emitError() <<
"The target chip cannot be empty.";
218 if (abiVersion !=
"400" && abiVersion !=
"500" && abiVersion !=
"600") {
219 emitError() <<
"Invalid ABI version, it must be `400`, `500` or '600'.";
222 if (files && !llvm::all_of(files, [](::mlir::Attribute attr) {
223 return mlir::isa_and_nonnull<StringAttr>(attr);
225 emitError() <<
"All the elements in the `link` array must be strings.";
231#define GET_ATTRDEF_CLASSES
232#include "mlir/Dialect/LLVMIR/ROCDLOpsAttributes.cpp.inc"
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.
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.
This class represents a diagnostic that is inflight and set to be reported.
NamedAttribute represents a combination of a name and an Attribute value.
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
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.
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
Provide a template class that can be specialized by users to dispatch to parsers.