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"
36#include "mlir/Dialect/LLVMIR/ROCDLOpsDialect.cpp.inc"
37#include "mlir/Dialect/LLVMIR/ROCDLOpsEnums.cpp.inc"
44struct ROCDLInlinerInterface final : DialectInlinerInterface {
45 using DialectInlinerInterface::DialectInlinerInterface;
46 bool isLegalToInline(Operation *, Region *,
bool, IRMapping &)
const final {
53void ROCDLDialect::initialize() {
54 registerROCDLDialectOperations(
this);
57#define GET_ATTRDEF_LIST
58#include "mlir/Dialect/LLVMIR/ROCDLOpsAttributes.cpp.inc"
62 allowUnknownOperations();
63 addInterfaces<ROCDLInlinerInterface>();
64 declarePromisedInterface<gpu::TargetAttrInterface, ROCDLTargetAttr>();
68BufferOOBModeModuleFlagAttr::getModuleFlagBehavior()
const {
69 return LLVM::ModFlagBehavior::Max;
72StringAttr BufferOOBModeModuleFlagAttr::getModuleFlagKey()
const {
74 ROCDLDialect::getModuleFlagKeyBufferOOBModeName());
77Attribute BufferOOBModeModuleFlagAttr::getModuleFlagValue()
const {
78 return BufferOOBModeAttr::get(
getContext(), getValue());
82TBufferOOBModeModuleFlagAttr::getModuleFlagBehavior()
const {
83 return LLVM::ModFlagBehavior::Max;
86StringAttr TBufferOOBModeModuleFlagAttr::getModuleFlagKey()
const {
88 ROCDLDialect::getModuleFlagKeyTBufferOOBModeName());
91Attribute TBufferOOBModeModuleFlagAttr::getModuleFlagValue()
const {
92 return BufferOOBModeAttr::get(
getContext(), getValue());
95LogicalResult ROCDLDialect::verifyOperationAttribute(
Operation *op,
98 if (kernelAttrName.getName() == attr.
getName()) {
99 if (!isa<LLVM::LLVMFuncOp>(op)) {
100 return op->
emitError() <<
"'" << kernelAttrName.getName()
101 <<
"' attribute attached to unexpected op";
105 if (attr.
getName() == xnackAttrName.getName() ||
106 attr.
getName() == srameccAttrName.getName()) {
109 << attr.
getName() <<
" is only supported on modules";
110 if (!isa<BoolAttr>(attr.
getValue()))
120template <
typename EnumAttrT,
typename EnumT>
130 cachePolicy = EnumAttrT::get(parser.
getContext(), *parsed);
140 if (failed(*rawValueParseResult))
143 IntegerAttr::get(IntegerType::get(parser.
getContext(), 32), rawValue);
147 StringRef policyFamily;
150 &policyFamily, {
"pre_gfx12",
"gfx942",
"gfx12",
"gfx12_atomic"}))) {
152 <<
"expected cache policy family 'pre_gfx12', 'gfx942', 'gfx12', "
153 "'gfx12_atomic', or a 32-bit integer";
156 if (policyFamily ==
"pre_gfx12")
158 parser, cachePolicy);
159 if (policyFamily ==
"gfx942")
161 parser, cachePolicy);
162 if (policyFamily ==
"gfx12")
164 parser, cachePolicy);
166 Gfx12AtomicCachePolicy>(parser, cachePolicy);
169template <
typename EnumAttrT>
172 printer << family <<
"<" << cachePolicy.getValue() <<
">";
178 .Case<IntegerAttr>([&](IntegerAttr rawPolicy) {
179 printer << rawPolicy.getValue().getZExtValue();
181 .Case<PreGfx12CachePolicyAttr>([&](PreGfx12CachePolicyAttr policy) {
184 .Case<Gfx942CachePolicyAttr>([&](Gfx942CachePolicyAttr policy) {
187 .Case<Gfx12CachePolicyAttr>([&](Gfx12CachePolicyAttr policy) {
190 .Case<Gfx12AtomicCachePolicyAttr>([&](Gfx12AtomicCachePolicyAttr policy) {
193 .DefaultUnreachable(
"unknown ROCDL cache policy attribute");
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.";
208 if (triple.empty()) {
209 emitError() <<
"The target triple cannot be empty.";
213 emitError() <<
"The target chip cannot be empty.";
216 if (abiVersion !=
"400" && abiVersion !=
"500" && abiVersion !=
"600") {
217 emitError() <<
"Invalid ABI version, it must be `400`, `500` or '600'.";
220 if (files && !llvm::all_of(files, [](::mlir::Attribute attr) {
221 return mlir::isa_and_nonnull<StringAttr>(attr);
223 emitError() <<
"All the elements in the `link` array must be strings.";
229#define GET_ATTRDEF_CLASSES
230#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.