|
| 1 | +/** |
| 2 | + * Copyright (c) 2017-2018, Facebook, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#pragma once |
| 17 | + |
| 18 | +#include "tc/core/check.h" |
| 19 | + |
| 20 | +namespace tc { |
| 21 | +namespace polyhedral { |
| 22 | + |
| 23 | +/* |
| 24 | + * Extract a mapping from the domain elements active at "tree" |
| 25 | + * to identifiers "ids", where all branches in "tree" |
| 26 | + * are assumed to have been mapped to these identifiers. |
| 27 | + * The result lives in a space of the form "tupleId"["ids"...]. |
| 28 | + */ |
| 29 | +inline isl::multi_union_pw_aff extractDomainToIds( |
| 30 | + const detail::ScheduleTree* root, |
| 31 | + const detail::ScheduleTree* tree, |
| 32 | + const std::vector<mapping::MappingId>& ids, |
| 33 | + isl::id tupleId) { |
| 34 | + using namespace polyhedral::detail; |
| 35 | + |
| 36 | + auto space = isl::space(tree->ctx_, 0); |
| 37 | + auto empty = isl::union_set::empty(space); |
| 38 | + space = space.add_named_tuple_id_ui(tupleId, ids.size()); |
| 39 | + auto zero = isl::multi_val::zero(space); |
| 40 | + auto domainToIds = isl::multi_union_pw_aff(empty, zero); |
| 41 | + |
| 42 | + for (auto mapping : tree->collect(tree, ScheduleTreeType::Mapping)) { |
| 43 | + auto mappingNode = mapping->as<ScheduleTreeMapping>(); |
| 44 | + auto list = isl::union_pw_aff_list(tree->ctx_, ids.size()); |
| 45 | + for (auto id : ids) { |
| 46 | + if (mappingNode->mapping.count(id) == 0) { |
| 47 | + break; |
| 48 | + } |
| 49 | + auto idMap = mappingNode->mapping.at(id); |
| 50 | + list = list.add(idMap); |
| 51 | + } |
| 52 | + // Ignore this node if it does not map to all required ids. |
| 53 | + if (static_cast<size_t>(list.size()) != ids.size()) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + auto nodeToIds = isl::multi_union_pw_aff(space, list); |
| 57 | + auto active = activeDomainPoints(root, mapping); |
| 58 | + TC_CHECK(active.intersect(domainToIds.domain()).is_empty()) |
| 59 | + << "conflicting mappings; are the filters in the tree disjoint?"; |
| 60 | + nodeToIds = nodeToIds.intersect_domain(active); |
| 61 | + domainToIds = domainToIds.union_add(nodeToIds); |
| 62 | + } |
| 63 | + |
| 64 | + auto active = activeDomainPoints(root, tree); |
| 65 | + TC_CHECK(active.is_subset(domainToIds.domain())) |
| 66 | + << "not all domain points of\n" |
| 67 | + << active << "\nwere mapped to the required ids"; |
| 68 | + |
| 69 | + return domainToIds; |
| 70 | +} |
| 71 | + |
| 72 | +} // namespace polyhedral |
| 73 | +} // namespace tc |
0 commit comments