Skip to content

Commit 21a81c7

Browse files
[mlir][Transforms] Add ApplyConversionAction for profiling purposes
1 parent e34e021 commit 21a81c7

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

mlir/include/mlir/IR/Unit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Region;
2222
class Block;
2323
class Value;
2424

25-
/// IRUnit is a union of the different types of IR objects that consistute the
25+
/// IRUnit is a union of the different types of IR objects that constitute the
2626
/// IR structure (other than Type and Attribute), that is Operation, Region, and
2727
/// Block.
2828
class IRUnit : public PointerUnion<Operation *, Region *, Block *, Value> {

mlir/lib/Transforms/Utils/DialectConversion.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2713,8 +2713,7 @@ legalizeUnresolvedMaterialization(RewriterBase &rewriter,
27132713
}
27142714

27152715
LogicalResult OperationConverter::convertOperations(ArrayRef<Operation *> ops) {
2716-
if (ops.empty())
2717-
return success();
2716+
assert(!ops.empty() && "expected at least one operation");
27182717
const ConversionTarget &target = opLegalizer.getTarget();
27192718

27202719
// Compute the set of operations and blocks to convert.
@@ -3417,16 +3416,47 @@ void mlir::registerConversionPDLFunctions(RewritePatternSet &patterns) {
34173416
// Op Conversion Entry Points
34183417
//===----------------------------------------------------------------------===//
34193418

3419+
/// This is the type of Action that is dispatched when a conversion is applied.
3420+
class ApplyConversionAction
3421+
: public tracing::ActionImpl<ApplyConversionAction> {
3422+
public:
3423+
using Base = tracing::ActionImpl<ApplyConversionAction>;
3424+
ApplyConversionAction(ArrayRef<IRUnit> irUnits) : Base(irUnits) {}
3425+
static constexpr StringLiteral tag = "apply-conversion";
3426+
static constexpr StringLiteral desc =
3427+
"Encapsulate the application of a dialect conversion";
3428+
3429+
void print(raw_ostream &os) const override { os << tag; }
3430+
};
3431+
3432+
static LogicalResult applyConversion(ArrayRef<Operation *> ops,
3433+
const ConversionTarget &target,
3434+
const FrozenRewritePatternSet &patterns,
3435+
ConversionConfig config,
3436+
OpConversionMode mode) {
3437+
if (ops.empty())
3438+
return success();
3439+
MLIRContext *ctx = ops.front()->getContext();
3440+
LogicalResult status = success();
3441+
SmallVector<IRUnit> irUnits(ops.begin(), ops.end());
3442+
ctx->executeAction<ApplyConversionAction>(
3443+
[&] {
3444+
OperationConverter opConverter(target, patterns, config, mode);
3445+
status = opConverter.convertOperations(ops);
3446+
},
3447+
irUnits);
3448+
return status;
3449+
}
3450+
34203451
//===----------------------------------------------------------------------===//
34213452
// Partial Conversion
34223453
//===----------------------------------------------------------------------===//
34233454

34243455
LogicalResult mlir::applyPartialConversion(
34253456
ArrayRef<Operation *> ops, const ConversionTarget &target,
34263457
const FrozenRewritePatternSet &patterns, ConversionConfig config) {
3427-
OperationConverter opConverter(target, patterns, config,
3428-
OpConversionMode::Partial);
3429-
return opConverter.convertOperations(ops);
3458+
return applyConversion(ops, target, patterns, config,
3459+
OpConversionMode::Partial);
34303460
}
34313461
LogicalResult
34323462
mlir::applyPartialConversion(Operation *op, const ConversionTarget &target,
@@ -3443,9 +3473,7 @@ LogicalResult mlir::applyFullConversion(ArrayRef<Operation *> ops,
34433473
const ConversionTarget &target,
34443474
const FrozenRewritePatternSet &patterns,
34453475
ConversionConfig config) {
3446-
OperationConverter opConverter(target, patterns, config,
3447-
OpConversionMode::Full);
3448-
return opConverter.convertOperations(ops);
3476+
return applyConversion(ops, target, patterns, config, OpConversionMode::Full);
34493477
}
34503478
LogicalResult mlir::applyFullConversion(Operation *op,
34513479
const ConversionTarget &target,
@@ -3512,9 +3540,8 @@ LogicalResult mlir::applyAnalysisConversion(
35123540
// Convert the cloned operations. The original IR will remain unchanged.
35133541
SmallVector<Operation *> opsToConvert = llvm::map_to_vector(
35143542
ops, [&](Operation *op) { return mapping.lookup(op); });
3515-
OperationConverter opConverter(target, patterns, config,
3516-
OpConversionMode::Analysis);
3517-
LogicalResult status = opConverter.convertOperations(opsToConvert);
3543+
LogicalResult status = applyConversion(opsToConvert, target, patterns, config,
3544+
OpConversionMode::Analysis);
35183545

35193546
// Remap `legalizableOps`, so that they point to the original ops and not the
35203547
// cloned ops.

0 commit comments

Comments
 (0)