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
13#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
14
15#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
16#include "mlir/IR/TypeUtilities.h"
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(message: "expected op to return array of ")
27 << AttrT::getMnemonic() << " attributes";
28 return success();
29}
30
31//===----------------------------------------------------------------------===//
32// AccessGroupOpInterface
33//===----------------------------------------------------------------------===//
34
35LogicalResult mlir::LLVM::detail::verifyAccessGroupOpInterface(Operation *op) {
36 auto iface = cast<AccessGroupOpInterface>(Val: op);
37 ArrayAttr accessGroups = iface.getAccessGroupsOrNull();
38 if (!accessGroups)
39 return success();
40
41 return isArrayOf<AccessGroupAttr>(op, array: accessGroups);
42}
43
44//===----------------------------------------------------------------------===//
45// AliasAnalysisOpInterface
46//===----------------------------------------------------------------------===//
47
48LogicalResult
49mlir::LLVM::detail::verifyAliasAnalysisOpInterface(Operation *op) {
50 auto iface = cast<AliasAnalysisOpInterface>(Val: op);
51
52 if (auto aliasScopes = iface.getAliasScopesOrNull())
53 if (failed(Result: isArrayOf<AliasScopeAttr>(op, array: aliasScopes)))
54 return failure();
55
56 if (auto noAliasScopes = iface.getNoAliasScopesOrNull())
57 if (failed(Result: isArrayOf<AliasScopeAttr>(op, array: noAliasScopes)))
58 return failure();
59
60 ArrayAttr tags = iface.getTBAATagsOrNull();
61 if (!tags)
62 return success();
63
64 return isArrayOf<TBAATagAttr>(op, array: tags);
65}
66
67//===----------------------------------------------------------------------===//
68// TBAAStructOpInterface
69//===----------------------------------------------------------------------===//
70
71LogicalResult mlir::LLVM::detail::verifyTBAAStructOpInterface(Operation *op) {
72 auto iface = cast<TBAAStructOpInterface>(Val: op);
73 ArrayAttr fields = iface.getTBAAStructOrNull();
74 if (!fields)
75 return success();
76
77 if (failed(Result: isArrayOf<TBAAStructFieldAttr>(op, array: fields)))
78 return failure();
79
80 // Field offsets must be non-decreasing, matching LLVM's verifier rule.
81 // Compare unsigned: LLVM zero-extends the offsets before comparing.
82 std::optional<uint64_t> prevOffset;
83 for (Attribute field : fields) {
84 uint64_t offset = cast<TBAAStructFieldAttr>(Val&: field).getOffset();
85 if (prevOffset && offset < *prevOffset)
86 return op->emitOpError(
87 message: "tbaa.struct field offsets must be non-decreasing");
88 prevOffset = offset;
89 }
90 return success();
91}
92
93//===----------------------------------------------------------------------===//
94// DereferenceableOpInterface
95//===----------------------------------------------------------------------===//
96
97LogicalResult
98mlir::LLVM::detail::verifyDereferenceableOpInterface(Operation *op) {
99 auto iface = cast<DereferenceableOpInterface>(Val: op);
100
101 if (auto derefAttr = iface.getDereferenceableOrNull())
102 if (op->getNumResults() != 1 ||
103 !mlir::isa<LLVMPointerType>(Val: op->getResult(idx: 0).getType()))
104 return op->emitOpError(
105 message: "expected op to return a single LLVM pointer type");
106
107 return success();
108}
109
110//===----------------------------------------------------------------------===//
111// RangeOpInterface
112//===----------------------------------------------------------------------===//
113
114LogicalResult mlir::LLVM::detail::verifyRangeOpInterface(Operation *op) {
115 auto iface = cast<RangeOpInterface>(Val: op);
116
117 ConstantRangeAttr range = iface.getRangeOrNull();
118 if (!range)
119 return success();
120 if (op->getNumResults() != 1)
121 return op->emitOpError(message: "expected op to have a single result");
122 auto intType =
123 dyn_cast<IntegerType>(Val: getElementTypeOrSelf(type: op->getResult(idx: 0).getType()));
124 if (!intType || intType.getWidth() != range.getLower().getBitWidth())
125 return op->emitOpError(
126 message: "expected range bitwidth to match the integer (element) result type");
127 if (range.getLower() == range.getUpper())
128 return op->emitOpError(message: "expected range to be neither empty nor full");
129 return success();
130}
131
132#include "mlir/Dialect/LLVMIR/LLVMInterfaces.cpp.inc"
133