blob: 7d5a86d6740af515f6107a1e8a498fa9aeb98913 [file] [edit]
//===- LLVMInterfaces.cpp - LLVM Interfaces ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines op interfaces for the LLVM dialect in MLIR.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/TypeUtilities.h"
using namespace mlir;
using namespace mlir::LLVM;
/// Verifies that all elements of `array` are instances of `Attr`.
template <class AttrT>
static LogicalResult isArrayOf(Operation *op, ArrayAttr array) {
for (Attribute iter : array)
if (!isa<AttrT>(iter))
return op->emitOpError("expected op to return array of ")
<< AttrT::getMnemonic() << " attributes";
return success();
}
//===----------------------------------------------------------------------===//
// AccessGroupOpInterface
//===----------------------------------------------------------------------===//
LogicalResult mlir::LLVM::detail::verifyAccessGroupOpInterface(Operation *op) {
auto iface = cast<AccessGroupOpInterface>(op);
ArrayAttr accessGroups = iface.getAccessGroupsOrNull();
if (!accessGroups)
return success();
return isArrayOf<AccessGroupAttr>(op, accessGroups);
}
//===----------------------------------------------------------------------===//
// AliasAnalysisOpInterface
//===----------------------------------------------------------------------===//
LogicalResult
mlir::LLVM::detail::verifyAliasAnalysisOpInterface(Operation *op) {
auto iface = cast<AliasAnalysisOpInterface>(op);
if (auto aliasScopes = iface.getAliasScopesOrNull())
if (failed(isArrayOf<AliasScopeAttr>(op, aliasScopes)))
return failure();
if (auto noAliasScopes = iface.getNoAliasScopesOrNull())
if (failed(isArrayOf<AliasScopeAttr>(op, noAliasScopes)))
return failure();
ArrayAttr tags = iface.getTBAATagsOrNull();
if (!tags)
return success();
return isArrayOf<TBAATagAttr>(op, tags);
}
//===----------------------------------------------------------------------===//
// DereferenceableOpInterface
//===----------------------------------------------------------------------===//
LogicalResult
mlir::LLVM::detail::verifyDereferenceableOpInterface(Operation *op) {
auto iface = cast<DereferenceableOpInterface>(op);
if (auto derefAttr = iface.getDereferenceableOrNull())
if (op->getNumResults() != 1 ||
!mlir::isa<LLVMPointerType>(op->getResult(0).getType()))
return op->emitOpError(
"expected op to return a single LLVM pointer type");
return success();
}
//===----------------------------------------------------------------------===//
// RangeOpInterface
//===----------------------------------------------------------------------===//
LogicalResult mlir::LLVM::detail::verifyRangeOpInterface(Operation *op) {
auto iface = cast<RangeOpInterface>(op);
ConstantRangeAttr range = iface.getRangeOrNull();
if (!range)
return success();
if (op->getNumResults() != 1)
return op->emitOpError("expected op to have a single result");
auto intType =
dyn_cast<IntegerType>(getElementTypeOrSelf(op->getResult(0).getType()));
if (!intType || intType.getWidth() != range.getLower().getBitWidth())
return op->emitOpError(
"expected range bitwidth to match the integer (element) result type");
if (range.getLower() == range.getUpper())
return op->emitOpError("expected range to be neither empty nor full");
return success();
}
SmallVector<Value> mlir::LLVM::AtomicCmpXchgOp::getAccessedOperands() {
return {getPtr()};
}
SmallVector<Value> mlir::LLVM::AtomicRMWOp::getAccessedOperands() {
return {getPtr()};
}
SmallVector<Value> mlir::LLVM::LoadOp::getAccessedOperands() {
return {getAddr()};
}
SmallVector<Value> mlir::LLVM::StoreOp::getAccessedOperands() {
return {getAddr()};
}
SmallVector<Value> mlir::LLVM::MemcpyOp::getAccessedOperands() {
return {getDst(), getSrc()};
}
SmallVector<Value> mlir::LLVM::MemcpyInlineOp::getAccessedOperands() {
return {getDst(), getSrc()};
}
SmallVector<Value> mlir::LLVM::MemmoveOp::getAccessedOperands() {
return {getDst(), getSrc()};
}
SmallVector<Value> mlir::LLVM::MemsetOp::getAccessedOperands() {
return {getDst()};
}
SmallVector<Value> mlir::LLVM::MemsetInlineOp::getAccessedOperands() {
return {getDst()};
}
SmallVector<Value> mlir::LLVM::MaskedLoadOp::getAccessedOperands() {
return {getData()};
}
SmallVector<Value> mlir::LLVM::MaskedStoreOp::getAccessedOperands() {
return {getData()};
}
SmallVector<Value> mlir::LLVM::masked_gather::getAccessedOperands() {
return {getPtrs()};
}
SmallVector<Value> mlir::LLVM::masked_scatter::getAccessedOperands() {
return {getPtrs()};
}
SmallVector<Value> mlir::LLVM::CallOp::getAccessedOperands() {
// Note: This must not use `getArgOperands`, which excludes the variadic
// arguments of a call to a variadic callee. Those are passed to the callee
// and may well be accessed by it.
Operation::operand_range operands = getCalleeOperands();
// In an indirect call, the first callee operand is the callee itself.
if (!getCallee().has_value() && !operands.empty())
operands = operands.drop_front();
return llvm::filter_to_vector(
operands, [](Value arg) { return isa<LLVMPointerType>(arg.getType()); });
}
#include "mlir/Dialect/LLVMIR/LLVMInterfaces.cpp.inc"