Skip to content

[mlir] Speed up FuncToLLVM using a SymbolTable #68082

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

Merged
merged 1 commit into from
Oct 5, 2023
Merged
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
15 changes: 13 additions & 2 deletions mlir/include/mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace mlir {
class DialectRegistry;
class LLVMTypeConverter;
class RewritePatternSet;
class SymbolTable;

/// Collect the default pattern to convert a FuncOp to the LLVM dialect. If
/// `emitCWrappers` is set, the pattern will also produce functions
Expand All @@ -31,8 +32,18 @@ void populateFuncToLLVMFuncOpConversionPattern(LLVMTypeConverter &converter,
/// conversion patterns capture the LLVMTypeConverter and the LowerToLLVMOptions
/// by reference meaning the references have to remain alive during the entire
/// pattern lifetime.
void populateFuncToLLVMConversionPatterns(LLVMTypeConverter &converter,
RewritePatternSet &patterns);
///
/// The `symbolTable` parameter can be used to speed up function lookups in the
/// module. It's good to provide it, but only if we know that the patterns will
/// be applied to a single module and the symbols referenced by the symbol table
/// will not be removed and new symbols will not be added during the usage of
/// the patterns. If provided, the lookups will have O(calls) cumulative
/// runtime, otherwise O(calls * functions). The symbol table is currently not
/// needed if `converter.getOptions().useBarePtrCallConv` is `true`, but it's
/// not an error to provide it anyway.
void populateFuncToLLVMConversionPatterns(
LLVMTypeConverter &converter, RewritePatternSet &patterns,
const SymbolTable *symbolTable = nullptr);

void registerConvertFuncToLLVMInterface(DialectRegistry &registry);

Expand Down
56 changes: 41 additions & 15 deletions mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Support/MathExtras.h"
Expand All @@ -48,6 +49,7 @@
#include "llvm/Support/FormatVariadic.h"
#include <algorithm>
#include <functional>
#include <optional>

namespace mlir {
#define GEN_PASS_DEF_CONVERTFUNCTOLLVMPASS
Expand Down Expand Up @@ -601,19 +603,38 @@ struct CallOpInterfaceLowering : public ConvertOpToLLVMPattern<CallOpType> {
}
};

struct CallOpLowering : public CallOpInterfaceLowering<func::CallOp> {
using Super::Super;
class CallOpLowering : public CallOpInterfaceLowering<func::CallOp> {
public:
CallOpLowering(const LLVMTypeConverter &typeConverter,
// Can be nullptr.
const SymbolTable *symbolTable, PatternBenefit benefit = 1)
: CallOpInterfaceLowering<func::CallOp>(typeConverter, benefit),
symbolTable(symbolTable) {}

LogicalResult
matchAndRewrite(func::CallOp callOp, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
bool useBarePtrCallConv = false;
if (Operation *callee = SymbolTable::lookupNearestSymbolFrom(
callOp, callOp.getCalleeAttr())) {
useBarePtrCallConv = shouldUseBarePtrCallConv(callee, getTypeConverter());
if (getTypeConverter()->getOptions().useBarePtrCallConv) {
useBarePtrCallConv = true;
} else if (symbolTable != nullptr) {
// Fast lookup.
Operation *callee =
symbolTable->lookup(callOp.getCalleeAttr().getValue());
useBarePtrCallConv =
callee != nullptr && callee->hasAttr(barePtrAttrName);
} else {
// Warning: This is a linear lookup.
Operation *callee =
SymbolTable::lookupNearestSymbolFrom(callOp, callOp.getCalleeAttr());
useBarePtrCallConv =
callee != nullptr && callee->hasAttr(barePtrAttrName);
}
return matchAndRewriteImpl(callOp, adaptor, rewriter, useBarePtrCallConv);
}

private:
const SymbolTable *symbolTable = nullptr;
};

struct CallIndirectOpLowering
Expand Down Expand Up @@ -728,16 +749,14 @@ void mlir::populateFuncToLLVMFuncOpConversionPattern(
patterns.add<FuncOpConversion>(converter);
}

void mlir::populateFuncToLLVMConversionPatterns(LLVMTypeConverter &converter,
RewritePatternSet &patterns) {
void mlir::populateFuncToLLVMConversionPatterns(
LLVMTypeConverter &converter, RewritePatternSet &patterns,
const SymbolTable *symbolTable) {
populateFuncToLLVMFuncOpConversionPattern(converter, patterns);
// clang-format off
patterns.add<
CallIndirectOpLowering,
CallOpLowering,
ConstantOpLowering,
ReturnOpLowering>(converter);
// clang-format on
patterns.add<CallIndirectOpLowering>(converter);
patterns.add<CallOpLowering>(converter, symbolTable);
patterns.add<ConstantOpLowering>(converter);
patterns.add<ReturnOpLowering>(converter);
}

namespace {
Expand Down Expand Up @@ -776,8 +795,15 @@ struct ConvertFuncToLLVMPass
LLVMTypeConverter typeConverter(&getContext(), options,
&dataLayoutAnalysis);

std::optional<SymbolTable> optSymbolTable = std::nullopt;
const SymbolTable *symbolTable = nullptr;
if (!options.useBarePtrCallConv) {
optSymbolTable.emplace(m);
symbolTable = &optSymbolTable.value();
}

RewritePatternSet patterns(&getContext());
populateFuncToLLVMConversionPatterns(typeConverter, patterns);
populateFuncToLLVMConversionPatterns(typeConverter, patterns, symbolTable);

// TODO: Remove these in favor of their dedicated conversion passes.
arith::populateArithToLLVMConversionPatterns(typeConverter, patterns);
Expand Down