Skip to content

[draft] Dialect Conversion without Rollback #93412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions mlir/include/mlir/Transforms/DialectConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ class TypeConverter {
/// Attempts a 1-1 type conversion, expecting the result type to be
/// `TargetType`. Returns the converted type cast to `TargetType` on success,
/// and a null type on conversion or cast failure.
template <typename TargetType> TargetType convertType(Type t) const {
template <typename TargetType>
TargetType convertType(Type t) const {
return dyn_cast_or_null<TargetType>(convertType(t));
}

Expand Down Expand Up @@ -657,7 +658,7 @@ struct ConversionPatternRewriterImpl;
/// This class implements a pattern rewriter for use with ConversionPatterns. It
/// extends the base PatternRewriter and provides special conversion specific
/// hooks.
class ConversionPatternRewriter final : public PatternRewriter {
class ConversionPatternRewriter : public PatternRewriter {
public:
~ConversionPatternRewriter() override;

Expand Down Expand Up @@ -708,8 +709,18 @@ class ConversionPatternRewriter final : public PatternRewriter {
/// Return the converted values that replace 'keys' with types defined by the
/// type converter of the currently executing pattern. Returns failure if the
/// remap failed, success otherwise.
LogicalResult getRemappedValues(ValueRange keys,
SmallVectorImpl<Value> &results);
LogicalResult getRemappedValues(ValueRange keys, SmallVector<Value> &results);

virtual void setCurrentTypeConverter(const TypeConverter *converter);

virtual const TypeConverter *getCurrentTypeConverter() const;

/// Populate the operands that are used for constructing the adapter into
/// `remapped`.
virtual LogicalResult getAdapterOperands(StringRef valueDiagTag,
std::optional<Location> inputLoc,
ValueRange values,
SmallVector<Value> &remapped);

//===--------------------------------------------------------------------===//
// PatternRewriter Hooks
Expand Down Expand Up @@ -755,6 +766,14 @@ class ConversionPatternRewriter final : public PatternRewriter {
/// Return a reference to the internal implementation.
detail::ConversionPatternRewriterImpl &getImpl();

protected:
/// Protected constructor for `OneShotConversionPatternRewriter`. Does not
/// initialize `impl`.
explicit ConversionPatternRewriter(MLIRContext *ctx);

// Hide unsupported pattern rewriter API.
using OpBuilder::setListener;

private:
// Allow OperationConverter to construct new rewriters.
friend struct OperationConverter;
Expand All @@ -765,9 +784,6 @@ class ConversionPatternRewriter final : public PatternRewriter {
explicit ConversionPatternRewriter(MLIRContext *ctx,
const ConversionConfig &config);

// Hide unsupported pattern rewriter API.
using OpBuilder::setListener;

std::unique_ptr<detail::ConversionPatternRewriterImpl> impl;
};

Expand Down
8 changes: 8 additions & 0 deletions mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace mlir {

class ConversionTarget;

/// This enum controls which ops are put on the worklist during a greedy
/// pattern rewrite.
enum class GreedyRewriteStrictness {
Expand Down Expand Up @@ -78,6 +80,8 @@ class GreedyRewriteConfig {
/// excluded.
GreedyRewriteStrictness strictMode = GreedyRewriteStrictness::AnyOp;

bool enableOperationDce = true;

/// An optional listener that should be notified about IR modifications.
RewriterBase::Listener *listener = nullptr;
};
Expand Down Expand Up @@ -188,6 +192,10 @@ applyOpPatternsAndFold(ArrayRef<Operation *> ops,
GreedyRewriteConfig config = GreedyRewriteConfig(),
bool *changed = nullptr, bool *allErased = nullptr);

LogicalResult
applyPartialOneShotConversion(Operation *op, const ConversionTarget &target,
const FrozenRewritePatternSet &patterns);

} // namespace mlir

#endif // MLIR_TRANSFORMS_GREEDYPATTERNREWRITEDRIVER_H_
5 changes: 3 additions & 2 deletions mlir/lib/Conversion/AffineToStandard/AffineToStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mlir/IR/IntegerSet.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"

namespace mlir {
Expand Down Expand Up @@ -563,8 +564,8 @@ class LowerAffinePass
ConversionTarget target(getContext());
target.addLegalDialect<arith::ArithDialect, memref::MemRefDialect,
scf::SCFDialect, VectorDialect>();
if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
if (failed(applyPartialOneShotConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
}
};
Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include <type_traits>

namespace mlir {
Expand Down Expand Up @@ -479,8 +480,8 @@ struct ArithToLLVMConversionPass
LLVMTypeConverter converter(&getContext(), options);
mlir::arith::populateArithToLLVMConversionPatterns(converter, patterns);

if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
if (failed(applyPartialOneShotConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
}
};
Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Conversion/ComplexToStandard/ComplexToStandard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mlir/IR/PatternMatch.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include <memory>
#include <type_traits>

Expand Down Expand Up @@ -1346,8 +1347,8 @@ void ConvertComplexToStandardPass::runOnOperation() {
ConversionTarget target(getContext());
target.addLegalDialect<arith::ArithDialect, math::MathDialect>();
target.addLegalOp<complex::CreateOp, complex::ImOp, complex::ReOp>();
if (failed(
applyPartialConversion(getOperation(), target, std::move(patterns))))
if (failed(applyPartialOneShotConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
}
} // namespace
Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "mlir/IR/PatternMatch.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/ADT/StringRef.h"
#include <functional>

Expand Down Expand Up @@ -240,8 +241,8 @@ struct ConvertControlFlowToLLVM
LLVMTypeConverter converter(&getContext(), options);
mlir::cf::populateControlFlowToLLVMConversionPatterns(converter, patterns);

if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
if (failed(applyPartialOneShotConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
}
};
Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Conversion/MathToLLVM/MathToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mlir/Dialect/Math/IR/Math.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"

namespace mlir {
#define GEN_PASS_DEF_CONVERTMATHTOLLVMPASS
Expand Down Expand Up @@ -291,8 +292,8 @@ struct ConvertMathToLLVMPass
LLVMTypeConverter converter(&getContext());
populateMathToLLVMConversionPatterns(converter, patterns, approximateLog1p);
LLVMConversionTarget target(getContext());
if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
if (failed(applyPartialOneShotConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
}
};
Expand Down
6 changes: 4 additions & 2 deletions mlir/lib/Conversion/NVGPUToNVVM/NVGPUToNVVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "mlir/IR/TypeUtilities.h"
#include "mlir/IR/Value.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -475,9 +476,10 @@ struct ConvertNVGPUToNVVMPass
target.addLegalDialect<::mlir::NVVM::NVVMDialect>();
mlir::scf::populateSCFStructuralTypeConversionsAndLegality(
converter, patterns, target);
if (failed(applyPartialConversion(getOperation(), target,
std::move(patterns))))
if (failed(applyPartialOneShotConversion(getOperation(), target,
std::move(patterns))))
signalPassFailure();
// applyPartialConversion
}
};

Expand Down
41 changes: 30 additions & 11 deletions mlir/lib/Transforms/Utils/DialectConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,9 @@ ConversionPatternRewriter::ConversionPatternRewriter(
setListener(impl.get());
}

ConversionPatternRewriter::ConversionPatternRewriter(MLIRContext *ctx)
: PatternRewriter(ctx), impl(nullptr) {}

ConversionPatternRewriter::~ConversionPatternRewriter() = default;

void ConversionPatternRewriter::replaceOp(Operation *op, Operation *newOp) {
Expand Down Expand Up @@ -1717,19 +1720,17 @@ void ConversionPatternRewriter::replaceUsesOfBlockArgument(BlockArgument from,

Value ConversionPatternRewriter::getRemappedValue(Value key) {
SmallVector<Value> remappedValues;
if (failed(impl->remapValues("value", /*inputLoc=*/std::nullopt, *this, key,
remappedValues)))
if (failed(getRemappedValues(key, remappedValues)))
return nullptr;
return remappedValues.front();
}

LogicalResult
ConversionPatternRewriter::getRemappedValues(ValueRange keys,
SmallVectorImpl<Value> &results) {
SmallVector<Value> &results) {
if (keys.empty())
return success();
return impl->remapValues("value", /*inputLoc=*/std::nullopt, *this, keys,
results);
return getAdapterOperands("value", /*inputLoc=*/std::nullopt, keys, results);
}

void ConversionPatternRewriter::inlineBlockBefore(Block *source, Block *dest,
Expand Down Expand Up @@ -1819,6 +1820,22 @@ detail::ConversionPatternRewriterImpl &ConversionPatternRewriter::getImpl() {
return *impl;
}

void ConversionPatternRewriter::setCurrentTypeConverter(
const TypeConverter *converter) {
impl->currentTypeConverter = converter;
}

const TypeConverter *
ConversionPatternRewriter::getCurrentTypeConverter() const {
return impl->currentTypeConverter;
}

LogicalResult ConversionPatternRewriter::getAdapterOperands(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can we agree on Adapter/Adaptor spelling? E.g., we already have OpAdaptor.

StringRef valueDiagTag, std::optional<Location> inputLoc, ValueRange values,
SmallVector<Value> &remapped) {
return impl->remapValues(valueDiagTag, inputLoc, *this, values, remapped);
}

//===----------------------------------------------------------------------===//
// ConversionPattern
//===----------------------------------------------------------------------===//
Expand All @@ -1827,16 +1844,18 @@ LogicalResult
ConversionPattern::matchAndRewrite(Operation *op,
PatternRewriter &rewriter) const {
auto &dialectRewriter = static_cast<ConversionPatternRewriter &>(rewriter);
auto &rewriterImpl = dialectRewriter.getImpl();

// Track the current conversion pattern type converter in the rewriter.
llvm::SaveAndRestore currentConverterGuard(rewriterImpl.currentTypeConverter,
getTypeConverter());
const TypeConverter *currentTypeConverter =
dialectRewriter.getCurrentTypeConverter();
auto resetTypeConverter = llvm::make_scope_exit(
[&] { dialectRewriter.setCurrentTypeConverter(currentTypeConverter); });
dialectRewriter.setCurrentTypeConverter(getTypeConverter());

// Remap the operands of the operation.
SmallVector<Value, 4> operands;
if (failed(rewriterImpl.remapValues("operand", op->getLoc(), rewriter,
op->getOperands(), operands))) {
SmallVector<Value> operands;
if (failed(dialectRewriter.getAdapterOperands("operand", op->getLoc(),
op->getOperands(), operands))) {
return failure();
}
return matchAndRewrite(op, operands, dialectRewriter);
Expand Down
Loading