Skip to content

[mlir][EmitC] Add pass that combines all available emitc conversions #117549

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 14 commits into from
May 1, 2025
3 changes: 3 additions & 0 deletions mlir/include/mlir/Conversion/ArithToEmitC/ArithToEmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
#define MLIR_CONVERSION_ARITHTOEMITC_ARITHTOEMITC_H

namespace mlir {
class DialectRegistry;
class RewritePatternSet;
class TypeConverter;

void populateArithToEmitCPatterns(TypeConverter &typeConverter,
RewritePatternSet &patterns);

void registerConvertArithToEmitCInterface(DialectRegistry &registry);
} // namespace mlir

#endif // MLIR_CONVERSION_ARITHTOEMITC_ARITHTOEMITC_H
24 changes: 24 additions & 0 deletions mlir/include/mlir/Conversion/ConvertToEmitC/ConvertToEmitCPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- ConvertToEmitCPass.h - Conversion to EmitC pass ----------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_CONVERSION_CONVERTTOEMITC_CONVERTTOEMITCPASS_H
#define MLIR_CONVERSION_CONVERTTOEMITC_CONVERTTOEMITCPASS_H

#include "llvm/ADT/SmallVector.h"

#include <memory>

namespace mlir {
class Pass;

#define GEN_PASS_DECL_CONVERTTOEMITC
#include "mlir/Conversion/Passes.h.inc"

} // namespace mlir

#endif // MLIR_CONVERSION_CONVERTTOEMITC_CONVERTTOEMITCPASS_H
45 changes: 45 additions & 0 deletions mlir/include/mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===- ToEmitCInterface.h - Conversion to EmitC iface ---*- C++ -*-===========//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef MLIR_CONVERSION_CONVERTTOEMITC_TOEMITCINTERFACE_H
#define MLIR_CONVERSION_CONVERTTOEMITC_TOEMITCINTERFACE_H

#include "mlir/IR/DialectInterface.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/IR/OpDefinition.h"

namespace mlir {
class ConversionTarget;
class TypeConverter;
class MLIRContext;
class Operation;
class RewritePatternSet;
class AnalysisManager;

class ConvertToEmitCPatternInterface
: public DialectInterface::Base<ConvertToEmitCPatternInterface> {
public:
ConvertToEmitCPatternInterface(Dialect *dialect) : Base(dialect) {}

/// Hook for derived dialect interface to provide conversion patterns
/// and mark dialect legal for the conversion target.
virtual void populateConvertToEmitCConversionPatterns(
ConversionTarget &target, TypeConverter &typeConverter,
RewritePatternSet &patterns) const = 0;
};

/// Recursively walk the IR and collect all dialects implementing the interface,
/// and populate the conversion patterns.
void populateConversionTargetFromOperation(Operation *op,
ConversionTarget &target,
TypeConverter &typeConverter,
RewritePatternSet &patterns);

} // namespace mlir

#endif // MLIR_CONVERSION_CONVERTTOEMITC_TOEMITCINTERFACE_H
7 changes: 6 additions & 1 deletion mlir/include/mlir/Conversion/FuncToEmitC/FuncToEmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@
#define MLIR_CONVERSION_FUNCTOEMITC_FUNCTOEMITC_H

namespace mlir {
class DialectRegistry;
class RewritePatternSet;
class TypeConverter;

void populateFuncToEmitCPatterns(RewritePatternSet &patterns);
void populateFuncToEmitCPatterns(const TypeConverter &typeConverter,
RewritePatternSet &patterns);

void registerConvertFuncToEmitCInterface(DialectRegistry &registry);
} // namespace mlir

#endif // MLIR_CONVERSION_FUNCTOEMITC_FUNCTOEMITC_H
3 changes: 3 additions & 0 deletions mlir/include/mlir/Conversion/MemRefToEmitC/MemRefToEmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
#define MLIR_CONVERSION_MEMREFTOEMITC_MEMREFTOEMITC_H

namespace mlir {
class DialectRegistry;
class RewritePatternSet;
class TypeConverter;

void populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter);

void populateMemRefToEmitCConversionPatterns(RewritePatternSet &patterns,
const TypeConverter &converter);

void registerConvertMemRefToEmitCInterface(DialectRegistry &registry);
} // namespace mlir

#endif // MLIR_CONVERSION_MEMREFTOEMITC_MEMREFTOEMITC_H
1 change: 1 addition & 0 deletions mlir/include/mlir/Conversion/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "mlir/Conversion/ControlFlowToSCF/ControlFlowToSCF.h"
#include "mlir/Conversion/ControlFlowToSPIRV/ControlFlowToSPIRV.h"
#include "mlir/Conversion/ControlFlowToSPIRV/ControlFlowToSPIRVPass.h"
#include "mlir/Conversion/ConvertToEmitC/ConvertToEmitCPass.h"
#include "mlir/Conversion/ConvertToLLVM/ToLLVMPass.h"
#include "mlir/Conversion/FuncToEmitC/FuncToEmitCPass.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
Expand Down
17 changes: 17 additions & 0 deletions mlir/include/mlir/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@
include "mlir/Pass/PassBase.td"
include "mlir/Dialect/Vector/Transforms/VectorTransformsBase.td"

//===----------------------------------------------------------------------===//
// ToEmitC
//===----------------------------------------------------------------------===//

def ConvertToEmitC : Pass<"convert-to-emitc"> {
let summary = "Convert to EmitC dialect via dialect interfaces";
let description = [{
This is a generic pass to convert to the EmitC dialect, it uses the
`ConvertToEmitCPatternInterface` dialect interface to delegate to dialects
the injection of conversion patterns.
}];
let options = [
ListOption<"filterDialects", "filter-dialects", "std::string",
"Test conversion patterns of only the specified dialects">,
];
}

//===----------------------------------------------------------------------===//
// ToLLVM
//===----------------------------------------------------------------------===//
Expand Down
3 changes: 3 additions & 0 deletions mlir/include/mlir/Conversion/SCFToEmitC/SCFToEmitC.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <memory>

namespace mlir {
class DialectRegistry;
class Pass;
class RewritePatternSet;

Expand All @@ -22,6 +23,8 @@ class RewritePatternSet;
/// Collect a set of patterns to convert SCF operations to the EmitC dialect.
void populateSCFToEmitCConversionPatterns(RewritePatternSet &patterns,
TypeConverter &typeConverter);

void registerConvertSCFToEmitCInterface(DialectRegistry &registry);
} // namespace mlir

#endif // MLIR_CONVERSION_SCFTOEMITC_SCFTOEMITC_H
8 changes: 8 additions & 0 deletions mlir/include/mlir/InitAllExtensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,22 @@
#ifndef MLIR_INITALLEXTENSIONS_H_
#define MLIR_INITALLEXTENSIONS_H_

#include "mlir/Conversion/ArithToEmitC/ArithToEmitC.h"
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
#include "mlir/Conversion/ComplexToLLVM/ComplexToLLVM.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/FuncToEmitC/FuncToEmitC.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
#include "mlir/Conversion/GPUCommon/GPUToLLVM.h"
#include "mlir/Conversion/GPUToNVVM/GPUToNVVM.h"
#include "mlir/Conversion/IndexToLLVM/IndexToLLVM.h"
#include "mlir/Conversion/MPIToLLVM/MPIToLLVM.h"
#include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
#include "mlir/Conversion/MemRefToEmitC/MemRefToEmitC.h"
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
#include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h"
#include "mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
#include "mlir/Conversion/SCFToEmitC/SCFToEmitC.h"
#include "mlir/Conversion/UBToLLVM/UBToLLVM.h"
#include "mlir/Conversion/VectorToLLVM/ConvertVectorToLLVM.h"
#include "mlir/Dialect/AMX/Transforms.h"
Expand Down Expand Up @@ -63,18 +67,22 @@ namespace mlir {
/// pipelines and transformations you are using.
inline void registerAllExtensions(DialectRegistry &registry) {
// Register all conversions to LLVM extensions.
registerConvertArithToEmitCInterface(registry);
arith::registerConvertArithToLLVMInterface(registry);
registerConvertComplexToLLVMInterface(registry);
cf::registerConvertControlFlowToLLVMInterface(registry);
func::registerAllExtensions(registry);
tensor::registerAllExtensions(registry);
registerConvertFuncToEmitCInterface(registry);
registerConvertFuncToLLVMInterface(registry);
index::registerConvertIndexToLLVMInterface(registry);
registerConvertMathToLLVMInterface(registry);
mpi::registerConvertMPIToLLVMInterface(registry);
registerConvertMemRefToEmitCInterface(registry);
registerConvertMemRefToLLVMInterface(registry);
registerConvertNVVMToLLVMInterface(registry);
registerConvertOpenMPToLLVMInterface(registry);
registerConvertSCFToEmitCInterface(registry);
ub::registerConvertUBToLLVMInterface(registry);
registerConvertAMXToLLVMInterface(registry);
gpu::registerConvertGpuToLLVMInterface(registry);
Expand Down
22 changes: 22 additions & 0 deletions mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "mlir/Conversion/ArithToEmitC/ArithToEmitC.h"

#include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/EmitC/Transforms/TypeConversions.h"
Expand All @@ -22,6 +23,27 @@

using namespace mlir;

namespace {
/// Implement the interface to convert Arith to EmitC.
struct ArithToEmitCDialectInterface : public ConvertToEmitCPatternInterface {
using ConvertToEmitCPatternInterface::ConvertToEmitCPatternInterface;

/// Hook for derived dialect interface to provide conversion patterns
/// and mark dialect legal for the conversion target.
void populateConvertToEmitCConversionPatterns(
ConversionTarget &target, TypeConverter &typeConverter,
RewritePatternSet &patterns) const final {
populateArithToEmitCPatterns(typeConverter, patterns);
}
};
} // namespace

void mlir::registerConvertArithToEmitCInterface(DialectRegistry &registry) {
registry.addExtension(+[](MLIRContext *ctx, arith::ArithDialect *dialect) {
dialect->addInterfaces<ArithToEmitCDialectInterface>();
});
}

//===----------------------------------------------------------------------===//
// Conversion Patterns
//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_subdirectory(ComplexToStandard)
add_subdirectory(ControlFlowToLLVM)
add_subdirectory(ControlFlowToSCF)
add_subdirectory(ControlFlowToSPIRV)
add_subdirectory(ConvertToEmitC)
add_subdirectory(ConvertToLLVM)
add_subdirectory(FuncToEmitC)
add_subdirectory(FuncToLLVM)
Expand Down
17 changes: 17 additions & 0 deletions mlir/lib/Conversion/ConvertToEmitC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_mlir_conversion_library(MLIRConvertToEmitC
ConvertToEmitCPass.cpp

ADDITIONAL_HEADER_DIRS
${MLIR_MAIN_INCLUDE_DIR}/mlir/Conversion/ConvertToEmitC

DEPENDS
MLIRConversionPassIncGen

LINK_LIBS PUBLIC
MLIRArithToEmitC
MLIRFuncToEmitC
MLIRMemRefToEmitC
MLIRPass
MLIRSCFToEmitC
MLIRTransformUtils
)
Loading
Loading