1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
|
//===- ShapeToStandard.cpp - conversion from Shape to Standard dialect ----===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "mlir/Conversion/ShapeToStandard/ShapeToStandard.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Shape/IR/Shape.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/ImplicitLocOpBuilder.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include "llvm/ADT/STLExtras.h"
namespace mlir {
#define GEN_PASS_DEF_CONVERTSHAPETOSTANDARD
#include "mlir/Conversion/Passes.h.inc"
} // namespace mlir
using namespace mlir;
using namespace mlir::shape;
using namespace mlir::scf;
/// Conversion patterns.
namespace {
class AnyOpConversion : public OpConversionPattern<AnyOp> {
public:
using OpConversionPattern<AnyOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(AnyOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult
AnyOpConversion::matchAndRewrite(AnyOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// Replace `any` with its first operand.
// Any operand would be a valid substitution.
rewriter.replaceOp(op, {adaptor.getInputs().front()});
return success();
}
namespace {
template <typename SrcOpTy, typename DstOpTy>
class BinaryOpConversion : public OpConversionPattern<SrcOpTy> {
public:
using OpConversionPattern<SrcOpTy>::OpConversionPattern;
LogicalResult
matchAndRewrite(SrcOpTy op, typename SrcOpTy::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// For now, only error-free types are supported by this lowering.
if (isa<SizeType>(op.getType()))
return failure();
rewriter.replaceOpWithNewOp<DstOpTy>(op, adaptor.getLhs(),
adaptor.getRhs());
return success();
}
};
} // namespace
namespace {
struct BroadcastOpConverter : public OpConversionPattern<BroadcastOp> {
using OpConversionPattern<BroadcastOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(BroadcastOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
// Get the resulting extent in a given dimension. This is computed with any
// number of extent tensors and shifted offsets into them.
Value getBroadcastedDim(ImplicitLocOpBuilder lb, ValueRange extentTensors,
ValueRange rankDiffs, Value outputDimension) {
Value one = lb.create<arith::ConstantIndexOp>(1);
Value broadcastedDim = one;
for (auto tup : llvm::zip(extentTensors, rankDiffs)) {
Value shape = std::get<0>(tup);
Value rankDiff = std::get<1>(tup);
Value outOfBounds = lb.create<arith::CmpIOp>(arith::CmpIPredicate::ult,
outputDimension, rankDiff);
Type indexTy = lb.getIndexType();
broadcastedDim =
lb.create<IfOp>(
outOfBounds,
[&](OpBuilder &b, Location loc) {
b.create<scf::YieldOp>(loc, broadcastedDim);
},
[&](OpBuilder &b, Location loc) {
// The broadcasting logic is:
// - if one extent (here we arbitrarily choose the
// extent from the greater-rank operand) is equal to 1,
// then take the extent from the other operand
// - otherwise, take the extent as-is.
// Note that this logic remains correct in the presence
// of dimensions of zero extent.
Value lesserRankOperandDimension = b.create<arith::SubIOp>(
loc, indexTy, outputDimension, rankDiff);
Value lesserRankOperandExtent = b.create<tensor::ExtractOp>(
loc, shape, ValueRange{lesserRankOperandDimension});
Value dimIsOne =
b.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq,
lesserRankOperandExtent, one);
Value dim = b.create<arith::SelectOp>(
loc, dimIsOne, broadcastedDim, lesserRankOperandExtent);
b.create<scf::YieldOp>(loc, dim);
})
.getResult(0);
}
return broadcastedDim;
}
} // namespace
LogicalResult BroadcastOpConverter::matchAndRewrite(
BroadcastOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// For now, this lowering is only defined on `tensor<?xindex>` operands, not
// on shapes.
if (isa<ShapeType>(op.getType()))
return failure();
auto loc = op.getLoc();
ImplicitLocOpBuilder lb(loc, rewriter);
Value zero = lb.create<arith::ConstantIndexOp>(0);
Type indexTy = lb.getIndexType();
// Save all the ranks for bounds checking. Because this is a tensor
// representing the shape extents, the rank is the extent of the only
// dimension in the tensor.
SmallVector<Value> ranks, rankDiffs;
llvm::append_range(ranks, llvm::map_range(adaptor.getShapes(), [&](Value v) {
return lb.create<tensor::DimOp>(v, zero);
}));
// Find the maximum rank
Value maxRank = ranks.front();
for (Value v : llvm::drop_begin(ranks, 1)) {
Value rankIsGreater =
lb.create<arith::CmpIOp>(arith::CmpIPredicate::ugt, v, maxRank);
maxRank = lb.create<arith::SelectOp>(rankIsGreater, v, maxRank);
}
// Calculate the difference of ranks and the maximum rank for later offsets.
llvm::append_range(rankDiffs, llvm::map_range(ranks, [&](Value v) {
return lb.create<arith::SubIOp>(indexTy, maxRank, v);
}));
Value replacement = lb.create<tensor::GenerateOp>(
getExtentTensorType(lb.getContext()), ValueRange{maxRank},
[&](OpBuilder &b, Location loc, ValueRange args) {
Value broadcastedDim =
getBroadcastedDim(ImplicitLocOpBuilder(loc, b), adaptor.getShapes(),
rankDiffs, args[0]);
b.create<tensor::YieldOp>(loc, broadcastedDim);
});
if (replacement.getType() != op.getType())
replacement = lb.create<tensor::CastOp>(op.getType(), replacement);
rewriter.replaceOp(op, replacement);
return success();
}
namespace {
class ConstShapeOpConverter : public OpConversionPattern<ConstShapeOp> {
public:
using OpConversionPattern<ConstShapeOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(ConstShapeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult ConstShapeOpConverter::matchAndRewrite(
ConstShapeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// For now, this lowering supports only extent tensors, not `shape.shape`
// types.
if (isa<ShapeType>(op.getType()))
return failure();
auto loc = op.getLoc();
SmallVector<Value, 4> extentOperands;
for (auto extent : op.getShape()) {
extentOperands.push_back(
rewriter.create<arith::ConstantIndexOp>(loc, extent.getLimitedValue()));
}
Type resultTy =
RankedTensorType::get({op.getShape().size()}, rewriter.getIndexType());
Value tensor =
rewriter.create<tensor::FromElementsOp>(loc, resultTy, extentOperands);
rewriter.replaceOpWithNewOp<tensor::CastOp>(op, resultTy, tensor);
return success();
}
namespace {
class ConstSizeOpConversion : public OpConversionPattern<ConstSizeOp> {
public:
using OpConversionPattern<ConstSizeOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(ConstSizeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult ConstSizeOpConversion::matchAndRewrite(
ConstSizeOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
rewriter.replaceOpWithNewOp<arith::ConstantIndexOp>(
op, op.getValue().getSExtValue());
return success();
}
namespace {
struct IsBroadcastableOpConverter
: public OpConversionPattern<IsBroadcastableOp> {
using OpConversionPattern<IsBroadcastableOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(IsBroadcastableOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult IsBroadcastableOpConverter::matchAndRewrite(
IsBroadcastableOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// For now, this lowering is only defined on `tensor<?xindex>` operands, not
// on shapes.
if (!llvm::all_of(op.getShapes(),
[](Value v) { return !isa<ShapeType>(v.getType()); }))
return failure();
auto loc = op.getLoc();
ImplicitLocOpBuilder lb(loc, rewriter);
Value zero = lb.create<arith::ConstantIndexOp>(0);
Value one = lb.create<arith::ConstantIndexOp>(1);
Type indexTy = lb.getIndexType();
// Save all the ranks for bounds checking. Because this is a tensor
// representing the shape extents, the rank is the extent of the only
// dimension in the tensor.
SmallVector<Value> ranks, rankDiffs;
llvm::append_range(ranks, llvm::map_range(adaptor.getShapes(), [&](Value v) {
return lb.create<tensor::DimOp>(v, zero);
}));
// Find the maximum rank
Value maxRank = ranks.front();
for (Value v : llvm::drop_begin(ranks, 1)) {
Value rankIsGreater =
lb.create<arith::CmpIOp>(arith::CmpIPredicate::ugt, v, maxRank);
maxRank = lb.create<arith::SelectOp>(rankIsGreater, v, maxRank);
}
// Calculate the difference of ranks and the maximum rank for later offsets.
llvm::append_range(rankDiffs, llvm::map_range(ranks, [&](Value v) {
return lb.create<arith::SubIOp>(indexTy, maxRank, v);
}));
Type i1Ty = rewriter.getI1Type();
Value trueVal =
rewriter.create<arith::ConstantOp>(loc, i1Ty, rewriter.getBoolAttr(true));
auto reduceResult = lb.create<ForOp>(
loc, zero, maxRank, one, ValueRange{trueVal},
[&](OpBuilder &b, Location loc, Value iv, ValueRange iterArgs) {
// Find a non-1 dim, if it exists. Note that the first part of this
// could reuse the Broadcast lowering entirely, but we redo the work
// here to make optimizations easier between the two loops.
Value broadcastedDim = getBroadcastedDim(
ImplicitLocOpBuilder(loc, b), adaptor.getShapes(), rankDiffs, iv);
Value broadcastable = iterArgs[0];
for (auto tup : llvm::zip(adaptor.getShapes(), rankDiffs)) {
Value shape, rankDiff;
std::tie(shape, rankDiff) = tup;
Value outOfBounds = b.create<arith::CmpIOp>(
loc, arith::CmpIPredicate::ult, iv, rankDiff);
broadcastable =
b.create<IfOp>(
loc, outOfBounds,
[&](OpBuilder &b, Location loc) {
// Non existent dimensions are always broadcastable
b.create<scf::YieldOp>(loc, broadcastable);
},
[&](OpBuilder &b, Location loc) {
// Every value needs to be either 1, or the same non-1
// value to be broadcastable in this dim.
Value operandDimension =
b.create<arith::SubIOp>(loc, indexTy, iv, rankDiff);
Value dimensionExtent = b.create<tensor::ExtractOp>(
loc, shape, ValueRange{operandDimension});
Value equalOne = b.create<arith::CmpIOp>(
loc, arith::CmpIPredicate::eq, dimensionExtent, one);
Value equalBroadcasted = b.create<arith::CmpIOp>(
loc, arith::CmpIPredicate::eq, dimensionExtent,
broadcastedDim);
Value result = b.create<arith::AndIOp>(
loc, broadcastable,
b.create<arith::OrIOp>(loc, equalOne,
equalBroadcasted));
b.create<scf::YieldOp>(loc, result);
})
.getResult(0);
}
b.create<scf::YieldOp>(loc, broadcastable);
});
rewriter.replaceOp(op, reduceResult.getResults().front());
return success();
}
namespace {
class DimOpConverter : public OpConversionPattern<DimOp> {
using OpConversionPattern<DimOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(DimOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult
DimOpConverter::matchAndRewrite(DimOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// Lower to dim(X, i) to get_extent(shape_of(X), i) and rely on further
// lowerings. This can be further optimized if needed to avoid intermediate
// steps.
auto shapeOf = rewriter.create<shape::ShapeOfOp>(op.getLoc(), op.getValue());
rewriter.replaceOpWithNewOp<shape::GetExtentOp>(op, op.getType(), shapeOf,
op.getIndex());
return success();
}
namespace {
class GetExtentOpConverter : public OpConversionPattern<GetExtentOp> {
using OpConversionPattern<GetExtentOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(GetExtentOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult GetExtentOpConverter::matchAndRewrite(
GetExtentOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// For now, only error-free types are supported by this lowering.
if (isa<SizeType>(op.getType()))
return failure();
// Derive shape extent directly from shape origin if possible. This
// circumvents the necessity to materialize the shape in memory.
if (auto shapeOfOp = op.getShape().getDefiningOp<ShapeOfOp>()) {
if (isa<ShapedType>(shapeOfOp.getArg().getType())) {
rewriter.replaceOpWithNewOp<tensor::DimOp>(op, shapeOfOp.getArg(),
adaptor.getDim());
return success();
}
}
rewriter.replaceOpWithNewOp<tensor::ExtractOp>(op, rewriter.getIndexType(),
adaptor.getShape(),
ValueRange{adaptor.getDim()});
return success();
}
namespace {
class RankOpConverter : public OpConversionPattern<shape::RankOp> {
public:
using OpConversionPattern<shape::RankOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(shape::RankOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult
RankOpConverter::matchAndRewrite(shape::RankOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// For now, this lowering supports only error-free types.
if (isa<SizeType>(op.getType()))
return failure();
rewriter.replaceOpWithNewOp<tensor::DimOp>(op, adaptor.getShape(), 0);
return success();
}
namespace {
/// Converts `shape.reduce` to `scf.for`.
struct ReduceOpConverter : public OpConversionPattern<shape::ReduceOp> {
public:
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(shape::ReduceOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const final;
};
} // namespace
LogicalResult
ReduceOpConverter::matchAndRewrite(shape::ReduceOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// For now, this lowering is only defined on `tensor<?xindex>` operands.
if (isa<ShapeType>(op.getShape().getType()))
return failure();
auto loc = op.getLoc();
Value zero = rewriter.create<arith::ConstantIndexOp>(loc, 0);
Value one = rewriter.create<arith::ConstantIndexOp>(loc, 1);
Type indexTy = rewriter.getIndexType();
Value rank =
rewriter.create<tensor::DimOp>(loc, indexTy, adaptor.getShape(), zero);
auto loop = rewriter.create<scf::ForOp>(
loc, zero, rank, one, op.getInitVals(),
[&](OpBuilder &b, Location loc, Value iv, ValueRange args) {
Value extent = b.create<tensor::ExtractOp>(loc, adaptor.getShape(), iv);
SmallVector<Value, 2> mappedValues{iv, extent};
mappedValues.append(args.begin(), args.end());
IRMapping mapping;
Block *reduceBody = op.getBody();
mapping.map(reduceBody->getArguments(), mappedValues);
for (auto &nested : reduceBody->without_terminator())
b.clone(nested, mapping);
SmallVector<Value, 2> mappedResults;
for (auto result : reduceBody->getTerminator()->getOperands())
mappedResults.push_back(mapping.lookup(result));
b.create<scf::YieldOp>(loc, mappedResults);
});
rewriter.replaceOp(op, loop.getResults());
return success();
}
namespace {
/// Converts `shape.shape_eq` to an `scf.for` loop. For now, the lowering is
/// only defined on `tensor<?xindex>` operands. The test for equality first
/// compares their size and, if equal, checks every extent for equality.
///
/// Example:
///
/// %result = shape.shape_eq %a, %b : tensor<?xindex>, tensor<?xindex>
///
/// becomes
///
/// %c0 = arith.constant 0 : index
/// %0 = dim %arg0, %c0 : tensor<?xindex>
/// %1 = dim %arg1, %c0 : tensor<?xindex>
/// %2 = arith.cmpi "eq", %0, %1 : index
/// %result = scf.if %2 -> (i1) {
/// %c1 = arith.constant 1 : index
/// %true = arith.constant true
/// %4 = scf.for %arg2 = %c0 to %0 step %c1 iter_args(%arg3 = %true) -> (i1) {
/// %5 = tensor.extract %arg0[%arg2] : tensor<?xindex>
/// %6 = tensor.extract %arg1[%arg2] : tensor<?xindex>
/// %7 = arith.cmpi "eq", %5, %6 : index
/// %8 = arith.andi %arg3, %7 : i1
/// scf.yield %8 : i1
/// }
/// scf.yield %4 : i1
/// } else {
/// %false = arith.constant false
/// scf.yield %false : i1
/// }
///
struct ShapeEqOpConverter : public OpConversionPattern<ShapeEqOp> {
using OpConversionPattern<ShapeEqOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(ShapeEqOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult
ShapeEqOpConverter::matchAndRewrite(ShapeEqOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
if (!llvm::all_of(op.getShapes(),
[](Value v) { return !isa<ShapeType>(v.getType()); }))
return failure();
Type i1Ty = rewriter.getI1Type();
if (op.getShapes().size() <= 1) {
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, i1Ty,
rewriter.getBoolAttr(true));
return success();
}
auto loc = op.getLoc();
Type indexTy = rewriter.getIndexType();
Value zero = rewriter.create<arith::ConstantIndexOp>(loc, 0);
Value firstShape = adaptor.getShapes().front();
Value firstRank =
rewriter.create<tensor::DimOp>(loc, indexTy, firstShape, zero);
Value result = nullptr;
// Generate a linear sequence of compares, all with firstShape as lhs.
for (Value shape : adaptor.getShapes().drop_front(1)) {
Value rank = rewriter.create<tensor::DimOp>(loc, indexTy, shape, zero);
Value eqRank = rewriter.create<arith::CmpIOp>(loc, arith::CmpIPredicate::eq,
firstRank, rank);
auto same = rewriter.create<IfOp>(
loc, eqRank,
[&](OpBuilder &b, Location loc) {
Value one = b.create<arith::ConstantIndexOp>(loc, 1);
Value init =
b.create<arith::ConstantOp>(loc, i1Ty, b.getBoolAttr(true));
auto loop = b.create<scf::ForOp>(
loc, zero, firstRank, one, ValueRange{init},
[&](OpBuilder &b, Location nestedLoc, Value iv, ValueRange args) {
Value conj = args[0];
Value lhsExtent =
b.create<tensor::ExtractOp>(loc, firstShape, iv);
Value rhsExtent = b.create<tensor::ExtractOp>(loc, shape, iv);
Value eqExtent = b.create<arith::CmpIOp>(
loc, arith::CmpIPredicate::eq, lhsExtent, rhsExtent);
Value conjNext = b.create<arith::AndIOp>(loc, conj, eqExtent);
b.create<scf::YieldOp>(loc, ValueRange({conjNext}));
});
b.create<scf::YieldOp>(loc, loop.getResults());
},
[&](OpBuilder &b, Location loc) {
Value result =
b.create<arith::ConstantOp>(loc, i1Ty, b.getBoolAttr(false));
b.create<scf::YieldOp>(loc, result);
});
result = !result ? same.getResult(0)
: rewriter.create<arith::AndIOp>(loc, result,
same.getResult(0));
}
rewriter.replaceOp(op, result);
return success();
}
namespace {
class ShapeOfOpConversion : public OpConversionPattern<ShapeOfOp> {
public:
using OpConversionPattern<ShapeOfOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(ShapeOfOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult ShapeOfOpConversion::matchAndRewrite(
ShapeOfOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// For now, only error-free types are supported by this lowering.
if (isa<ShapeType>(op.getType()))
return failure();
// For ranked tensor arguments, lower to `tensor.from_elements`.
auto loc = op.getLoc();
Value tensor = adaptor.getArg();
Type tensorTy = tensor.getType();
if (isa<RankedTensorType>(tensorTy)) {
// Build values for individual extents.
SmallVector<Value, 8> extentValues;
RankedTensorType rankedTensorTy = cast<RankedTensorType>(tensorTy);
int64_t rank = rankedTensorTy.getRank();
for (int64_t i = 0; i < rank; i++) {
if (rankedTensorTy.isDynamicDim(i)) {
Value extent = rewriter.create<tensor::DimOp>(loc, tensor, i);
extentValues.push_back(extent);
} else {
Value extent = rewriter.create<arith::ConstantIndexOp>(
loc, rankedTensorTy.getDimSize(i));
extentValues.push_back(extent);
}
}
// Materialize extent tensor.
Value staticExtentTensor = rewriter.create<tensor::FromElementsOp>(
loc, RankedTensorType::get({rank}, rewriter.getIndexType()),
extentValues);
rewriter.replaceOpWithNewOp<tensor::CastOp>(op, op.getType(),
staticExtentTensor);
return success();
}
// Lower to `tensor.generate` otherwise.
auto *ctx = rewriter.getContext();
Value rank = rewriter.create<tensor::RankOp>(loc, tensor);
rewriter.replaceOpWithNewOp<tensor::GenerateOp>(
op, getExtentTensorType(ctx), ValueRange{rank},
[&](OpBuilder &b, Location loc, ValueRange args) {
Value dim = args.front();
Value extent = b.create<tensor::DimOp>(loc, tensor, dim);
b.create<tensor::YieldOp>(loc, extent);
});
return success();
}
namespace {
class SplitAtOpConversion : public OpConversionPattern<SplitAtOp> {
public:
using OpConversionPattern<SplitAtOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(SplitAtOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override;
};
} // namespace
LogicalResult SplitAtOpConversion::matchAndRewrite(
SplitAtOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const {
// Error conditions are not implemented, only lower if all operands and
// results are extent tensors.
if (llvm::any_of(ValueRange{op.getOperand(), op.getHead(), op.getTail()},
[](Value v) { return isa<ShapeType>(v.getType()); }))
return failure();
ImplicitLocOpBuilder b(op.getLoc(), rewriter);
Value zero = b.create<arith::ConstantIndexOp>(0);
Value rank = b.create<tensor::DimOp>(adaptor.getOperand(), zero);
// index < 0 ? index + rank : index
Value originalIndex = adaptor.getIndex();
Value add = b.create<arith::AddIOp>(originalIndex, rank);
Value indexIsNegative =
b.create<arith::CmpIOp>(arith::CmpIPredicate::slt, originalIndex, zero);
Value index = b.create<arith::SelectOp>(indexIsNegative, add, originalIndex);
Value one = b.create<arith::ConstantIndexOp>(1);
Value head =
b.create<tensor::ExtractSliceOp>(adaptor.getOperand(), zero, index, one);
Value tailSize = b.create<arith::SubIOp>(rank, index);
Value tail = b.create<tensor::ExtractSliceOp>(adaptor.getOperand(), index,
tailSize, one);
rewriter.replaceOp(op, {head, tail});
return success();
}
namespace {
class ToExtentTensorOpConversion
: public OpConversionPattern<ToExtentTensorOp> {
public:
using OpConversionPattern<ToExtentTensorOp>::OpConversionPattern;
LogicalResult
matchAndRewrite(ToExtentTensorOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
if (!isa<RankedTensorType>(adaptor.getInput().getType()))
return rewriter.notifyMatchFailure(op, "input needs to be a tensor");
rewriter.replaceOpWithNewOp<tensor::CastOp>(op, op.getType(),
adaptor.getInput());
return success();
}
};
} // namespace
namespace {
/// Import the Shape Ops to Std Patterns.
#include "ShapeToStandard.cpp.inc"
} // namespace
namespace {
/// Conversion pass.
class ConvertShapeToStandardPass
: public impl::ConvertShapeToStandardBase<ConvertShapeToStandardPass> {
void runOnOperation() override;
};
} // namespace
void ConvertShapeToStandardPass::runOnOperation() {
// Setup target legality.
MLIRContext &ctx = getContext();
ConversionTarget target(ctx);
target.addLegalDialect<arith::ArithDialect, SCFDialect,
tensor::TensorDialect>();
target.addLegalOp<CstrRequireOp, func::FuncOp, ModuleOp>();
// Setup conversion patterns.
RewritePatternSet patterns(&ctx);
populateShapeToStandardConversionPatterns(patterns);
// Apply conversion.
auto module = getOperation();
if (failed(applyPartialConversion(module, target, std::move(patterns))))
signalPassFailure();
}
void mlir::populateShapeToStandardConversionPatterns(
RewritePatternSet &patterns) {
// clang-format off
populateWithGenerated(patterns);
patterns.add<
AnyOpConversion,
BinaryOpConversion<AddOp, arith::AddIOp>,
BinaryOpConversion<MulOp, arith::MulIOp>,
BroadcastOpConverter,
ConstShapeOpConverter,
ConstSizeOpConversion,
DimOpConverter,
IsBroadcastableOpConverter,
GetExtentOpConverter,
RankOpConverter,
ReduceOpConverter,
ShapeEqOpConverter,
ShapeOfOpConversion,
SplitAtOpConversion,
ToExtentTensorOpConversion>(patterns.getContext());
// clang-format on
}
std::unique_ptr<OperationPass<ModuleOp>>
mlir::createConvertShapeToStandardPass() {
return std::make_unique<ConvertShapeToStandardPass>();
}
|