MLIR 24.0.0git
NVVMDialect.cpp
Go to the documentation of this file.
1//===- NVVMDialect.cpp - NVVM 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 NVVM IR dialect in MLIR and registers it.
10//
11// The NVVM dialect only contains GPU specific additions on top of the general
12// LLVM dialect.
13//
14//===----------------------------------------------------------------------===//
15
17
25
26// Any op partition header declares the hook that registers the ops of all op
27// partitions. NVVMMiscSRegOps.h is the cheapest to include.
29
30using namespace mlir;
31using namespace mlir::NVVM;
32
33#include "mlir/Dialect/LLVMIR/NVVMOpsDialect.cpp.inc"
34
35//===----------------------------------------------------------------------===//
36// NVVMDialect initialization, type parsing, and registration.
37//===----------------------------------------------------------------------===//
38
39namespace {
40struct NVVMInlinerInterface final : DialectInlinerInterface {
41 using DialectInlinerInterface::DialectInlinerInterface;
42 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
43 return true;
44 }
45};
46} // namespace
47
48// TODO: This should be the llvm.nvvm dialect once this is supported.
49void NVVMDialect::initialize() {
50 // Support unknown operations because not all NVVM operations are
51 // registered.
52 allowUnknownOperations();
53 addInterfaces<NVVMInlinerInterface>();
54 declarePromisedInterface<ConvertToLLVMPatternInterface, NVVMDialect>();
55 declarePromisedInterface<gpu::TargetAttrInterface, NVVMTargetAttr>();
56}
57
58LogicalResult NVVMDialect::verifyOperationAttribute(Operation *op,
59 NamedAttribute attr) {
60 StringAttr attrName = attr.getName();
61 // Kernel function attribute should be attached to functions.
62 if (attrName == NVVMDialect::getKernelFuncAttrName()) {
63 if (!isa<LLVM::LLVMFuncOp>(op)) {
64 return op->emitError() << "'" << NVVMDialect::getKernelFuncAttrName()
65 << "' attribute attached to unexpected op";
66 }
67 }
68 // If maxntid / reqntid / cluster_dim exist, it must be an array with max 3
69 // dim
70 if (attrName == NVVMDialect::getMaxntidAttrName() ||
71 attrName == NVVMDialect::getReqntidAttrName() ||
72 attrName == NVVMDialect::getClusterDimAttrName()) {
73 auto values = llvm::dyn_cast<DenseI32ArrayAttr>(attr.getValue());
74 if (!values || values.empty() || values.size() > 3) {
75 return op->emitError()
76 << "'" << attrName
77 << "' attribute must be integer array with maximum 3 index";
78 }
79 }
80 // If minctasm / maxnreg / cluster_max_blocks exist, it must be an integer
81 // attribute
82 if (attrName == NVVMDialect::getMinctasmAttrName() ||
83 attrName == NVVMDialect::getMaxnregAttrName() ||
84 attrName == NVVMDialect::getClusterMaxBlocksAttrName()) {
85 if (!llvm::dyn_cast<IntegerAttr>(attr.getValue())) {
86 return op->emitError()
87 << "'" << attrName << "' attribute must be integer constant";
88 }
89 }
90 // blocksareclusters must be used along with reqntid and cluster_dim
91 if (attrName == NVVMDialect::getBlocksAreClustersAttrName()) {
92 if (!op->hasDiscardableAttr(NVVMDialect::getReqntidAttrName()) ||
93 !op->hasDiscardableAttr(NVVMDialect::getClusterDimAttrName())) {
94 return op->emitError()
95 << "'" << attrName << "' attribute must be used along with " << "'"
96 << NVVMDialect::getReqntidAttrName() << "' and " << "'"
97 << NVVMDialect::getClusterDimAttrName() << "'";
98 }
99 }
100
101 return success();
102}
103
104LogicalResult NVVMDialect::verifyRegionArgAttribute(Operation *op,
105 unsigned regionIndex,
106 unsigned argIndex,
107 NamedAttribute argAttr) {
108 auto funcOp = dyn_cast<FunctionOpInterface>(op);
109 if (!funcOp)
110 return success();
111
112 bool isKernel = op->hasDiscardableAttr(NVVMDialect::getKernelFuncAttrName());
113 StringAttr attrName = argAttr.getName();
114 if (attrName == NVVM::NVVMDialect::getGridConstantAttrName()) {
115 if (!isKernel) {
116 return op->emitError()
117 << "'" << attrName
118 << "' attribute must be present only on kernel arguments";
119 }
120 if (!isa<UnitAttr>(argAttr.getValue()))
121 return op->emitError() << "'" << attrName << "' must be a unit attribute";
122 if (!funcOp.getArgAttr(argIndex, LLVM::LLVMDialect::getByValAttrName())) {
123 return op->emitError()
124 << "'" << attrName
125 << "' attribute requires the argument to also have attribute '"
126 << LLVM::LLVMDialect::getByValAttrName() << "'";
127 }
128 }
129
130 return success();
131}
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.
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
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...
bool hasDiscardableAttr(StringRef name)
Return true if this operation has a discardable attribute with the provided name.
Definition Operation.h:505
Include the generated interface declarations.