MLIR 24.0.0git
LLVMInterfaces.cpp
Go to the documentation of this file.
1//===- LLVMInterfaces.cpp - LLVM Interfaces ---------------------*- C++ -*-===//
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 op interfaces for the LLVM dialect in MLIR.
10//
11//===----------------------------------------------------------------------===//
12
14
17
18using namespace mlir;
19using namespace mlir::LLVM;
20
21/// Verifies that all elements of `array` are instances of `Attr`.
22template <class AttrT>
23static LogicalResult isArrayOf(Operation *op, ArrayAttr array) {
24 for (Attribute iter : array)
25 if (!isa<AttrT>(iter))
26 return op->emitOpError("expected op to return array of ")
27 << AttrT::getMnemonic() << " attributes";
28 return success();
29}
30
31//===----------------------------------------------------------------------===//
32// AccessGroupOpInterface
33//===----------------------------------------------------------------------===//
34
36 auto iface = cast<AccessGroupOpInterface>(op);
37 ArrayAttr accessGroups = iface.getAccessGroupsOrNull();
38 if (!accessGroups)
39 return success();
40
41 return isArrayOf<AccessGroupAttr>(op, accessGroups);
42}
43
44//===----------------------------------------------------------------------===//
45// AliasAnalysisOpInterface
46//===----------------------------------------------------------------------===//
47
48LogicalResult
50 auto iface = cast<AliasAnalysisOpInterface>(op);
51
52 if (auto aliasScopes = iface.getAliasScopesOrNull())
53 if (failed(isArrayOf<AliasScopeAttr>(op, aliasScopes)))
54 return failure();
55
56 if (auto noAliasScopes = iface.getNoAliasScopesOrNull())
57 if (failed(isArrayOf<AliasScopeAttr>(op, noAliasScopes)))
58 return failure();
59
60 ArrayAttr tags = iface.getTBAATagsOrNull();
61 if (!tags)
62 return success();
63
64 return isArrayOf<TBAATagAttr>(op, tags);
65}
66
67//===----------------------------------------------------------------------===//
68// DereferenceableOpInterface
69//===----------------------------------------------------------------------===//
70
71LogicalResult
73 auto iface = cast<DereferenceableOpInterface>(op);
74
75 if (auto derefAttr = iface.getDereferenceableOrNull())
76 if (op->getNumResults() != 1 ||
77 !mlir::isa<LLVMPointerType>(op->getResult(0).getType()))
78 return op->emitOpError(
79 "expected op to return a single LLVM pointer type");
80
81 return success();
82}
83
84//===----------------------------------------------------------------------===//
85// RangeOpInterface
86//===----------------------------------------------------------------------===//
87
89 auto iface = cast<RangeOpInterface>(op);
90
91 ConstantRangeAttr range = iface.getRangeOrNull();
92 if (!range)
93 return success();
94 if (op->getNumResults() != 1)
95 return op->emitOpError("expected op to have a single result");
96 auto intType =
97 dyn_cast<IntegerType>(getElementTypeOrSelf(op->getResult(0).getType()));
98 if (!intType || intType.getWidth() != range.getLower().getBitWidth())
99 return op->emitOpError(
100 "expected range bitwidth to match the integer (element) result type");
101 if (range.getLower() == range.getUpper())
102 return op->emitOpError("expected range to be neither empty nor full");
103 return success();
104}
105
106SmallVector<Value> mlir::LLVM::AtomicCmpXchgOp::getAccessedOperands() {
107 return {getPtr()};
108}
109
110SmallVector<Value> mlir::LLVM::AtomicRMWOp::getAccessedOperands() {
111 return {getPtr()};
112}
113
114SmallVector<Value> mlir::LLVM::LoadOp::getAccessedOperands() {
115 return {getAddr()};
116}
117
118SmallVector<Value> mlir::LLVM::StoreOp::getAccessedOperands() {
119 return {getAddr()};
120}
121
122SmallVector<Value> mlir::LLVM::MemcpyOp::getAccessedOperands() {
123 return {getDst(), getSrc()};
124}
125
126SmallVector<Value> mlir::LLVM::MemcpyInlineOp::getAccessedOperands() {
127 return {getDst(), getSrc()};
128}
129
130SmallVector<Value> mlir::LLVM::MemmoveOp::getAccessedOperands() {
131 return {getDst(), getSrc()};
132}
133
134SmallVector<Value> mlir::LLVM::MemsetOp::getAccessedOperands() {
135 return {getDst()};
136}
137
138SmallVector<Value> mlir::LLVM::MemsetInlineOp::getAccessedOperands() {
139 return {getDst()};
140}
141
142SmallVector<Value> mlir::LLVM::MaskedLoadOp::getAccessedOperands() {
143 return {getData()};
144}
145
146SmallVector<Value> mlir::LLVM::MaskedStoreOp::getAccessedOperands() {
147 return {getData()};
148}
149
150SmallVector<Value> mlir::LLVM::masked_gather::getAccessedOperands() {
151 return {getPtrs()};
152}
153
154SmallVector<Value> mlir::LLVM::masked_scatter::getAccessedOperands() {
155 return {getPtrs()};
156}
157
158SmallVector<Value> mlir::LLVM::CallOp::getAccessedOperands() {
159 // Note: This must not use `getArgOperands`, which excludes the variadic
160 // arguments of a call to a variadic callee. Those are passed to the callee
161 // and may well be accessed by it.
162 Operation::operand_range operands = getCalleeOperands();
163 // In an indirect call, the first callee operand is the callee itself.
164 if (!getCallee().has_value() && !operands.empty())
165 operands = operands.drop_front();
166 return llvm::filter_to_vector(
167 operands, [](Value arg) { return isa<LLVMPointerType>(arg.getType()); });
168}
169
170#include "mlir/Dialect/LLVMIR/LLVMInterfaces.cpp.inc"
return success()
static LogicalResult isArrayOf(Operation *op, ArrayAttr array)
Verifies that all elements of array are instances of Attr.
ArrayAttr()
Attributes are known-constant values of operations.
Definition Attributes.h:25
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:432
OperandRange operand_range
Definition Operation.h:396
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
unsigned getNumResults()
Return the number of results held by this operation.
Definition Operation.h:429
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
LogicalResult verifyAliasAnalysisOpInterface(Operation *op)
Verifies the alias analysis attributes of memory operations that implement the alias analysis interfa...
LogicalResult verifyRangeOpInterface(Operation *op)
Verifies that the range of an operation implementing the range interface matches the bitwidth of its ...
LogicalResult verifyAccessGroupOpInterface(Operation *op)
Verifies the access groups attribute of memory operations that implement the access group interface.
LogicalResult verifyDereferenceableOpInterface(Operation *op)
Verifies that the operation implementing the dereferenceable interface has exactly one result of LLVM...
Include the generated interface declarations.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.