Skip to content

Commit 58d9703

Browse files
authored
[mlir][OpenMP] Implement the ConvertToLLVMPatternInterface (#101997)
This patch implements the `ConvertToLLVMPatternInterface` for the OpenMP dialect, allowing `convert-to-llvm` to act on the OpenMP dialect.
1 parent adaa603 commit 58d9703

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

mlir/include/mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memory>
1212

1313
namespace mlir {
14+
class DialectRegistry;
1415
class LLVMTypeConverter;
1516
class ConversionTarget;
1617
class MLIRContext;
@@ -28,6 +29,10 @@ void configureOpenMPToLLVMConversionLegality(
2829
/// Populate the given list with patterns that convert from OpenMP to LLVM.
2930
void populateOpenMPToLLVMConversionPatterns(LLVMTypeConverter &converter,
3031
RewritePatternSet &patterns);
32+
33+
/// Registers the `ConvertToLLVMPatternInterface` interface in the `OpenMP`
34+
/// dialect.
35+
void registerConvertOpenMPToLLVMInterface(DialectRegistry &registry);
3136
} // namespace mlir
3237

3338
#endif // MLIR_CONVERSION_OPENMPTOLLVM_CONVERTOPENMPTOLLVM_H

mlir/include/mlir/InitAllExtensions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "mlir/Conversion/MathToLLVM/MathToLLVM.h"
2323
#include "mlir/Conversion/MemRefToLLVM/MemRefToLLVM.h"
2424
#include "mlir/Conversion/NVVMToLLVM/NVVMToLLVM.h"
25+
#include "mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
2526
#include "mlir/Conversion/UBToLLVM/UBToLLVM.h"
2627
#include "mlir/Dialect/Affine/TransformOps/AffineTransformOps.h"
2728
#include "mlir/Dialect/Bufferization/TransformOps/BufferizationTransformOps.h"
@@ -67,6 +68,7 @@ inline void registerAllExtensions(DialectRegistry &registry) {
6768
registerConvertMathToLLVMInterface(registry);
6869
registerConvertMemRefToLLVMInterface(registry);
6970
registerConvertNVVMToLLVMInterface(registry);
71+
registerConvertOpenMPToLLVMInterface(registry);
7072
ub::registerConvertUBToLLVMInterface(registry);
7173

7274
// Register all transform dialect extensions.

mlir/lib/Conversion/OpenMPToLLVM/OpenMPToLLVM.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
1212
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
13+
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
1314
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
1415
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
1516
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
@@ -314,3 +315,31 @@ void ConvertOpenMPToLLVMPass::runOnOperation() {
314315
if (failed(applyPartialConversion(module, target, std::move(patterns))))
315316
signalPassFailure();
316317
}
318+
319+
//===----------------------------------------------------------------------===//
320+
// ConvertToLLVMPatternInterface implementation
321+
//===----------------------------------------------------------------------===//
322+
namespace {
323+
/// Implement the interface to convert OpenMP to LLVM.
324+
struct OpenMPToLLVMDialectInterface : public ConvertToLLVMPatternInterface {
325+
using ConvertToLLVMPatternInterface::ConvertToLLVMPatternInterface;
326+
void loadDependentDialects(MLIRContext *context) const final {
327+
context->loadDialect<LLVM::LLVMDialect>();
328+
}
329+
330+
/// Hook for derived dialect interface to provide conversion patterns
331+
/// and mark dialect legal for the conversion target.
332+
void populateConvertToLLVMConversionPatterns(
333+
ConversionTarget &target, LLVMTypeConverter &typeConverter,
334+
RewritePatternSet &patterns) const final {
335+
configureOpenMPToLLVMConversionLegality(target, typeConverter);
336+
populateOpenMPToLLVMConversionPatterns(typeConverter, patterns);
337+
}
338+
};
339+
} // namespace
340+
341+
void mlir::registerConvertOpenMPToLLVMInterface(DialectRegistry &registry) {
342+
registry.addExtension(+[](MLIRContext *ctx, omp::OpenMPDialect *dialect) {
343+
dialect->addInterfaces<OpenMPToLLVMDialectInterface>();
344+
});
345+
}

mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
14+
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
1415
#include "mlir/Dialect/Func/IR/FuncOps.h"
1516
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
1617
#include "mlir/Dialect/OpenACCMPCommon/Interfaces/AtomicInterfaces.h"
@@ -83,6 +84,8 @@ void OpenMPDialect::initialize() {
8384
#include "mlir/Dialect/OpenMP/OpenMPOpsTypes.cpp.inc"
8485
>();
8586

87+
declarePromisedInterface<ConvertToLLVMPatternInterface, OpenMPDialect>();
88+
8689
MemRefType::attachInterface<MemRefPointerLikeModel>(*getContext());
8790
LLVM::LLVMPointerType::attachInterface<LLVMPointerPointerLikeModel>(
8891
*getContext());

mlir/test/Conversion/OpenMPToLLVM/convert-to-llvmir.mlir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: mlir-opt -convert-openmp-to-llvm -split-input-file %s | FileCheck %s
2+
// RUN: mlir-opt -convert-to-llvm -split-input-file %s | FileCheck %s
23

34
// CHECK-LABEL: llvm.func @foo(i64, i64)
45
func.func private @foo(index, index)

0 commit comments

Comments
 (0)