|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 | 8 |
|
| 9 | +#include "mlir/Dialect/Transform/IR/TransformOps.h" |
9 | 10 | #include "mlir/Dialect/Transform/Interfaces/TransformInterfaces.h"
|
| 11 | +#include "mlir/IR/OpImplementation.h" |
10 | 12 | #include "llvm/Support/Debug.h"
|
11 | 13 |
|
12 | 14 | #include "mlir/Dialect/Transform/TuneExtension/TuneExtensionOps.h"
|
13 | 15 |
|
14 | 16 | using namespace mlir;
|
15 | 17 |
|
| 18 | +static ParseResult parseAlternativesOpSelectedRegion( |
| 19 | + OpAsmParser &parser, IntegerAttr &selectedRegionAttr, |
| 20 | + std::optional<OpAsmParser::UnresolvedOperand> &selectedRegionParam); |
| 21 | + |
| 22 | +static void printAlternativesOpSelectedRegion(OpAsmPrinter &printer, |
| 23 | + Operation *op, |
| 24 | + IntegerAttr selectedRegionAttr, |
| 25 | + Value selectedRegionParam); |
| 26 | + |
16 | 27 | #define GET_OP_CLASSES
|
17 | 28 | #include "mlir/Dialect/Transform/TuneExtension/TuneExtensionOps.cpp.inc"
|
18 | 29 |
|
@@ -57,3 +68,176 @@ LogicalResult transform::tune::KnobOp::verify() {
|
57 | 68 |
|
58 | 69 | return success();
|
59 | 70 | }
|
| 71 | + |
| 72 | +//===----------------------------------------------------------------------===// |
| 73 | +// AlternativesOp |
| 74 | +//===----------------------------------------------------------------------===// |
| 75 | + |
| 76 | +static ParseResult parseAlternativesOpSelectedRegion( |
| 77 | + OpAsmParser &parser, IntegerAttr &selectedRegionAttr, |
| 78 | + std::optional<OpAsmParser::UnresolvedOperand> &selectedRegionParam) { |
| 79 | + size_t selectedRegionIdx; |
| 80 | + OptionalParseResult attrParseRes = |
| 81 | + parser.parseOptionalInteger(selectedRegionIdx); |
| 82 | + if (attrParseRes.has_value()) { |
| 83 | + if (failed(*attrParseRes)) |
| 84 | + return failure(); |
| 85 | + |
| 86 | + selectedRegionAttr = parser.getBuilder().getIndexAttr(selectedRegionIdx); |
| 87 | + return success(); |
| 88 | + } |
| 89 | + |
| 90 | + OpAsmParser::UnresolvedOperand param; |
| 91 | + auto paramParseRes = parser.parseOptionalOperand(param); |
| 92 | + if (paramParseRes.has_value()) { |
| 93 | + if (failed(*paramParseRes)) |
| 94 | + return failure(); |
| 95 | + |
| 96 | + selectedRegionParam = param; |
| 97 | + return success(); |
| 98 | + } |
| 99 | + |
| 100 | + return parser.emitError(parser.getCurrentLocation()) |
| 101 | + << "expected either an integer attribute or a transform.param operand"; |
| 102 | +} |
| 103 | + |
| 104 | +static void printAlternativesOpSelectedRegion(OpAsmPrinter &printer, |
| 105 | + Operation *op, |
| 106 | + IntegerAttr selectedRegionAttr, |
| 107 | + Value selectedRegionParam) { |
| 108 | + if (selectedRegionAttr) |
| 109 | + printer << selectedRegionAttr.getValue(); |
| 110 | + if (selectedRegionParam) |
| 111 | + printer << selectedRegionParam; |
| 112 | +} |
| 113 | + |
| 114 | +OperandRange transform::tune::AlternativesOp::getEntrySuccessorOperands( |
| 115 | + RegionBranchPoint point) { |
| 116 | + // No operands will be forwarded to the region(s). |
| 117 | + return getOperands().slice(0, 0); |
| 118 | +} |
| 119 | + |
| 120 | +void transform::tune::AlternativesOp::getSuccessorRegions( |
| 121 | + RegionBranchPoint point, SmallVectorImpl<RegionSuccessor> ®ions) { |
| 122 | + if (point.isParent()) |
| 123 | + if (auto selectedRegionIdx = getSelectedRegionAttr()) |
| 124 | + regions.emplace_back( |
| 125 | + &getAlternatives()[selectedRegionIdx->getSExtValue()], |
| 126 | + Block::BlockArgListType()); |
| 127 | + else |
| 128 | + for (Region &alternative : getAlternatives()) |
| 129 | + regions.emplace_back(&alternative, Block::BlockArgListType()); |
| 130 | + else |
| 131 | + regions.emplace_back(getOperation()->getResults()); |
| 132 | +} |
| 133 | + |
| 134 | +void transform::tune::AlternativesOp::getRegionInvocationBounds( |
| 135 | + ArrayRef<Attribute> operands, SmallVectorImpl<InvocationBounds> &bounds) { |
| 136 | + (void)operands; |
| 137 | + bounds.reserve(getNumRegions()); |
| 138 | + |
| 139 | + if (auto selectedRegionIdx = getSelectedRegionAttr()) { |
| 140 | + bounds.resize(getNumRegions(), InvocationBounds(0, 0)); |
| 141 | + bounds[selectedRegionIdx->getSExtValue()] = InvocationBounds(1, 1); |
| 142 | + } else { |
| 143 | + bounds.resize(getNumRegions(), InvocationBounds(0, 1)); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +void transform::tune::AlternativesOp::getEffects( |
| 148 | + SmallVectorImpl<MemoryEffects::EffectInstance> &effects) { |
| 149 | + onlyReadsHandle(getSelectedRegionParamMutable(), effects); |
| 150 | + producesHandle(getOperation()->getOpResults(), effects); |
| 151 | + // TODO: should effects from regions be forwarded? |
| 152 | +} |
| 153 | + |
| 154 | +DiagnosedSilenceableFailure |
| 155 | +transform::tune::AlternativesOp::apply(transform::TransformRewriter &rewriter, |
| 156 | + transform::TransformResults &results, |
| 157 | + transform::TransformState &state) { |
| 158 | + std::optional<size_t> selectedRegionIdx; |
| 159 | + |
| 160 | + if (auto selectedRegionAttr = getSelectedRegionAttr()) |
| 161 | + selectedRegionIdx = selectedRegionAttr->getSExtValue(); |
| 162 | + |
| 163 | + if (Value selectedRegionParam = getSelectedRegionParam()) { |
| 164 | + ArrayRef<Attribute> associatedAttrs = state.getParams(selectedRegionParam); |
| 165 | + IntegerAttr selectedRegionAttr; |
| 166 | + if (associatedAttrs.size() != 1 || |
| 167 | + !(selectedRegionAttr = dyn_cast<IntegerAttr>(associatedAttrs[0]))) |
| 168 | + return emitDefiniteFailure() |
| 169 | + << "param should hold exactly one integer attribute, got: " |
| 170 | + << associatedAttrs[0]; |
| 171 | + selectedRegionIdx = selectedRegionAttr.getValue().getSExtValue(); |
| 172 | + } |
| 173 | + |
| 174 | + if (!selectedRegionIdx) |
| 175 | + return emitDefiniteFailure() << "non-deterministic choice " << getName() |
| 176 | + << " is only resolved through providing a " |
| 177 | + "`selected_region` attr/param"; |
| 178 | + |
| 179 | + if (*selectedRegionIdx < 0 || *selectedRegionIdx >= getNumRegions()) |
| 180 | + return emitDefiniteFailure() |
| 181 | + << "'selected_region' attribute/param specifies region at index " |
| 182 | + << *selectedRegionIdx << " while op has only " << getNumRegions() |
| 183 | + << " regions"; |
| 184 | + |
| 185 | + Region &selectedRegion = getRegion(*selectedRegionIdx); |
| 186 | + auto scope = state.make_region_scope(selectedRegion); |
| 187 | + Block &block = selectedRegion.front(); |
| 188 | + // Apply the region's ops one by one. |
| 189 | + for (Operation &transform : block.without_terminator()) { |
| 190 | + DiagnosedSilenceableFailure result = |
| 191 | + state.applyTransform(cast<transform::TransformOpInterface>(transform)); |
| 192 | + if (result.isDefiniteFailure()) |
| 193 | + return result; |
| 194 | + |
| 195 | + if (result.isSilenceableFailure()) { |
| 196 | + for (const auto &res : getResults()) |
| 197 | + results.set(res, {}); |
| 198 | + return result; |
| 199 | + } |
| 200 | + } |
| 201 | + // Forward the operation mapping for values yielded from the region to the |
| 202 | + // values produced by the alternatives op. |
| 203 | + transform::detail::forwardTerminatorOperands(&block, state, results); |
| 204 | + return DiagnosedSilenceableFailure::success(); |
| 205 | +} |
| 206 | + |
| 207 | +LogicalResult transform::tune::AlternativesOp::verify() { |
| 208 | + for (auto *region : getRegions()) { |
| 209 | + auto yieldTerminator = |
| 210 | + llvm::dyn_cast_if_present<transform::YieldOp>(region->front().back()); |
| 211 | + if (!yieldTerminator) |
| 212 | + return emitOpError() << "expected '" |
| 213 | + << transform::YieldOp::getOperationName() |
| 214 | + << "' as terminator"; |
| 215 | + |
| 216 | + if (yieldTerminator->getNumOperands() != getNumResults()) |
| 217 | + return yieldTerminator.emitOpError() |
| 218 | + << "expected terminator to have as many operands as the parent op " |
| 219 | + "has results"; |
| 220 | + |
| 221 | + for (auto [i, operandType, resultType] : llvm::zip_equal( |
| 222 | + llvm::seq<unsigned>(0, yieldTerminator->getNumOperands()), |
| 223 | + yieldTerminator->getOperands().getType(), getResultTypes())) { |
| 224 | + if (operandType == resultType) |
| 225 | + continue; |
| 226 | + return yieldTerminator.emitOpError() |
| 227 | + << "the type of the terminator operand #" << i |
| 228 | + << " must match the type of the corresponding parent op result (" |
| 229 | + << operandType << " vs " << resultType << ")"; |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + if (auto selectedRegionAttr = getSelectedRegionAttr()) { |
| 234 | + size_t regionIdx = selectedRegionAttr->getSExtValue(); |
| 235 | + if (regionIdx < 0 || regionIdx >= getNumRegions()) |
| 236 | + return emitOpError() |
| 237 | + << "'selected_region' attribute specifies region at index " |
| 238 | + << regionIdx << " while op has only " << getNumRegions() |
| 239 | + << " regions"; |
| 240 | + } |
| 241 | + |
| 242 | + return success(); |
| 243 | +} |
0 commit comments