126 lines · cpp
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-exception6//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/LLVMDialect.h"16 17using namespace mlir;18using namespace mlir::LLVM;19 20/// Verifies that all elements of `array` are instances of `Attr`.21template <class AttrT>22static LogicalResult isArrayOf(Operation *op, ArrayAttr array) {23 for (Attribute iter : array)24 if (!isa<AttrT>(iter))25 return op->emitOpError("expected op to return array of ")26 << AttrT::getMnemonic() << " attributes";27 return success();28}29 30//===----------------------------------------------------------------------===//31// AccessGroupOpInterface32//===----------------------------------------------------------------------===//33 34LogicalResult mlir::LLVM::detail::verifyAccessGroupOpInterface(Operation *op) {35 auto iface = cast<AccessGroupOpInterface>(op);36 ArrayAttr accessGroups = iface.getAccessGroupsOrNull();37 if (!accessGroups)38 return success();39 40 return isArrayOf<AccessGroupAttr>(op, accessGroups);41}42 43//===----------------------------------------------------------------------===//44// AliasAnalysisOpInterface45//===----------------------------------------------------------------------===//46 47LogicalResult48mlir::LLVM::detail::verifyAliasAnalysisOpInterface(Operation *op) {49 auto iface = cast<AliasAnalysisOpInterface>(op);50 51 if (auto aliasScopes = iface.getAliasScopesOrNull())52 if (failed(isArrayOf<AliasScopeAttr>(op, aliasScopes)))53 return failure();54 55 if (auto noAliasScopes = iface.getNoAliasScopesOrNull())56 if (failed(isArrayOf<AliasScopeAttr>(op, noAliasScopes)))57 return failure();58 59 ArrayAttr tags = iface.getTBAATagsOrNull();60 if (!tags)61 return success();62 63 return isArrayOf<TBAATagAttr>(op, tags);64}65 66//===----------------------------------------------------------------------===//67// DereferenceableOpInterface68//===----------------------------------------------------------------------===//69 70LogicalResult71mlir::LLVM::detail::verifyDereferenceableOpInterface(Operation *op) {72 auto iface = cast<DereferenceableOpInterface>(op);73 74 if (auto derefAttr = iface.getDereferenceableOrNull())75 if (op->getNumResults() != 1 ||76 !mlir::isa<LLVMPointerType>(op->getResult(0).getType()))77 return op->emitOpError(78 "expected op to return a single LLVM pointer type");79 80 return success();81}82 83SmallVector<Value> mlir::LLVM::AtomicCmpXchgOp::getAccessedOperands() {84 return {getPtr()};85}86 87SmallVector<Value> mlir::LLVM::AtomicRMWOp::getAccessedOperands() {88 return {getPtr()};89}90 91SmallVector<Value> mlir::LLVM::LoadOp::getAccessedOperands() {92 return {getAddr()};93}94 95SmallVector<Value> mlir::LLVM::StoreOp::getAccessedOperands() {96 return {getAddr()};97}98 99SmallVector<Value> mlir::LLVM::MemcpyOp::getAccessedOperands() {100 return {getDst(), getSrc()};101}102 103SmallVector<Value> mlir::LLVM::MemcpyInlineOp::getAccessedOperands() {104 return {getDst(), getSrc()};105}106 107SmallVector<Value> mlir::LLVM::MemmoveOp::getAccessedOperands() {108 return {getDst(), getSrc()};109}110 111SmallVector<Value> mlir::LLVM::MemsetOp::getAccessedOperands() {112 return {getDst()};113}114 115SmallVector<Value> mlir::LLVM::MemsetInlineOp::getAccessedOperands() {116 return {getDst()};117}118 119SmallVector<Value> mlir::LLVM::CallOp::getAccessedOperands() {120 return llvm::filter_to_vector(getArgOperands(), [](Value arg) {121 return isa<LLVMPointerType>(arg.getType());122 });123}124 125#include "mlir/Dialect/LLVMIR/LLVMInterfaces.cpp.inc"126