Changeset View
Changeset View
Standalone View
Standalone View
mlir/lib/IR/OpConnectivity.cpp
- This file was added.
//===- OpConnectivity.cpp ---------------------------------------*- 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 | |||||
// | |||||
//===----------------------------------------------------------------------===// | |||||
// Utility functions to get the prodcuers and consumer operations of a given | |||||
// operation | |||||
//===----------------------------------------------------------------------===// | |||||
#include "mlir/IR/OpConnectivity.h" | |||||
namespace mlir { | |||||
/* | |||||
Returns Detailed Information of the Consumers of an Value | |||||
If a Value goes to the same Operation more than once this is | |||||
treated as multiple individual connections | |||||
from the value to the operation | |||||
*/ | |||||
std::vector<connection> ExtractConsumers(Value v) { | |||||
std::vector<connection> consumers; | |||||
std::set<mlir::Operation *> unique_consumers; | |||||
for (Operation *consumer : v.getUsers()) { | |||||
unique_consumers.insert(consumer); | |||||
} | |||||
for (Operation *consumer : unique_consumers) { | |||||
unsigned external_slot_index; | |||||
for (int ope = 0; ope < consumer->getNumOperands(); ope++) { | |||||
Value v1 = consumer->getOperand(ope); | |||||
if (v1 == v) { | |||||
connection oper_val(ope, -1, nullptr, consumer, nullptr, v, | |||||
operation_2_consumer); | |||||
consumers.push_back(oper_val); | |||||
} | |||||
} | |||||
} | |||||
return consumers; | |||||
} | |||||
/* | |||||
Returns Detailed Information of the Consumers of an Operation | |||||
If a Value goes to the same Operation more than once this is | |||||
treated as multiple individual connections | |||||
from the operation to the operation | |||||
*/ | |||||
std::vector<connection> ExtractConsumers(Operation *i) { | |||||
std::vector<connection> consumers; | |||||
unsigned no_results = i->getNumResults(); | |||||
for (unsigned int r = 0; r < no_results; r++) { | |||||
OpResult result = i->getResult(r); | |||||
std::set<mlir::Operation *> unique_consumers; | |||||
for (Operation *consumer : result.getUsers()) { | |||||
unique_consumers.insert(consumer); | |||||
} | |||||
for (Operation *consumer : unique_consumers) { | |||||
for (int ope = 0; ope < consumer->getNumOperands(); ope++) { | |||||
Value v1 = consumer->getOperand(ope); | |||||
if (v1 == result) { | |||||
connection oper_val(ope, r, i, consumer, nullptr, result, | |||||
operation_2_consumer); | |||||
consumers.push_back(oper_val); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
return consumers; | |||||
} | |||||
/* | |||||
Returns Detailed Information of the Producers of an Operation | |||||
If a Value comes from the same Operation and more than once this is | |||||
treated as multiple individual connections | |||||
from the operation to the operation | |||||
*/ | |||||
std::vector<connection> ExtractProducers(Operation *i) { | |||||
std::vector<connection> producers; | |||||
for (unsigned int p = 0; p < i->getNumOperands(); p++) { | |||||
Value input = i->getOperand(p); | |||||
Operation *op_con = input.getDefiningOp(); | |||||
if (op_con != nullptr) { | |||||
/* Producer is from an Op */ | |||||
OpResult result = input.cast<OpResult>(); | |||||
Operation *producer = result.getOwner(); | |||||
unsigned external_slot_index = 9999999; | |||||
for (int ope = 0; ope < producer->getNumResults(); ope++) { | |||||
Value v1 = producer->getResult(ope); | |||||
if (v1 == result) { | |||||
external_slot_index = ope; | |||||
break; | |||||
} | |||||
} | |||||
assert(external_slot_index != 9999999 && | |||||
"ExtractProducers::Operation Operand Index not found"); | |||||
unsigned operation_slot_index = p; | |||||
connection oper_val(external_slot_index, operation_slot_index, i, | |||||
producer, nullptr, input, producer_2_operation); | |||||
producers.push_back(oper_val); | |||||
} else { | |||||
BlockArgument block_argument = input.cast<BlockArgument>(); | |||||
Block *producer = block_argument.getOwner(); | |||||
unsigned external_slot_index = 9999999; | |||||
for (int ope = 0; ope < producer->getNumArguments(); ope++) { | |||||
BlockArgument v1 = producer->getArgument(ope); | |||||
if (v1 == block_argument) { | |||||
external_slot_index = ope; | |||||
break; | |||||
} | |||||
} | |||||
assert(external_slot_index != 9999999 && | |||||
"ExtractProducers::Block Operand Index not found"); | |||||
unsigned operation_slot_index = p; | |||||
connection oper_val(external_slot_index, operation_slot_index, i, nullptr, | |||||
producer, input, producer_2_operation); | |||||
producers.push_back(oper_val); | |||||
} | |||||
} | |||||
return producers; | |||||
} | |||||
std::list<Operation *> ExtractUniqueConsumers_Operations(Operation *i) { | |||||
std::vector<connection> conn = ExtractConsumers(i); | |||||
std::list<Operation *> conn_filtered; | |||||
for (auto c : conn) { | |||||
conn_filtered.push_back(c.external_operation); | |||||
} | |||||
return conn_filtered; | |||||
} | |||||
std::list<Operation *> ExtractUniqueProducers_Operations(Operation *i) { | |||||
std::vector<connection> prod = ExtractProducers(i); | |||||
std::list<Operation *> prod_filtered; | |||||
for (auto p : prod) { | |||||
if (p.external_operation) { | |||||
prod_filtered.push_back(p.external_operation); | |||||
} | |||||
} | |||||
return prod_filtered; | |||||
} | |||||
SmallVector<int64_t, 4> arrayref_2_smallvector(ArrayRef<int64_t> s) { | |||||
SmallVector<int64_t, 4> values; | |||||
for (int i = 0; i < s.size(); i++) | |||||
values.push_back(s[i]); | |||||
return values; | |||||
} | |||||
} // namespace mlir |