Skip to content

Commit 66900b3

Browse files
committed
[mlir] Use dialect interfaces to translate OpenMP dialect to LLVM IR
Migrate the translation of the OpenMP dialect operations to LLVM IR to the new dialect-based mechanism. Depends On D96503 Reviewed By: nicolasvasilache Differential Revision: https://reviews.llvm.org/D96504
1 parent 2423a38 commit 66900b3

File tree

9 files changed

+417
-287
lines changed

9 files changed

+417
-287
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===- OpenMPToLLVMIRTranslation.h - OpenMP Dialect to LLVM IR --*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements the dialect interface for translating the OpenMP dialect
10+
// to LLVM IR.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef MLIR_TARGET_LLVMIR_DIALECT_OPENMP_OPENMPTOLLVMIRTRANSLATION_H
15+
#define MLIR_TARGET_LLVMIR_DIALECT_OPENMP_OPENMPTOLLVMIRTRANSLATION_H
16+
17+
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
18+
19+
namespace mlir {
20+
21+
/// Implementation of the dialect interface that converts operations beloning to
22+
/// the OpenMP dialect to LLVM IR.
23+
class OpenMPDialectLLVMIRTranslationInterface
24+
: public LLVMTranslationDialectInterface {
25+
public:
26+
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
27+
28+
/// Translates the given operation to LLVM IR using the provided IR builder
29+
/// and saving the state in `moduleTranslation`.
30+
LogicalResult
31+
convertOperation(Operation *op, llvm::IRBuilderBase &builder,
32+
LLVM::ModuleTranslation &moduleTranslation) const final;
33+
};
34+
35+
} // namespace mlir
36+
37+
#endif // MLIR_TARGET_LLVMIR_DIALECT_OPENMP_OPENMPTOLLVMIRTRANSLATION_H

mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
2323
#include "mlir/Target/LLVMIR/TypeTranslation.h"
2424

25+
#include "llvm/ADT/SetVector.h"
2526
#include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
2627
#include "llvm/IR/BasicBlock.h"
2728
#include "llvm/IR/Function.h"
@@ -160,6 +161,29 @@ class ModuleTranslation {
160161
return globalsMapping.lookup(op);
161162
}
162163

164+
/// Returns the OpenMP IR builder associated with the LLVM IR module being
165+
/// constructed.
166+
llvm::OpenMPIRBuilder *getOpenMPBuilder() {
167+
if (!ompBuilder) {
168+
ompBuilder = std::make_unique<llvm::OpenMPIRBuilder>(*llvmModule);
169+
ompBuilder->initialize();
170+
}
171+
return ompBuilder.get();
172+
}
173+
174+
/// Translates the given location.
175+
const llvm::DILocation *translateLoc(Location loc, llvm::DILocalScope *scope);
176+
177+
/// Translates the contents of the given block to LLVM IR using this
178+
/// translator. The LLVM IR basic block corresponding to the given block is
179+
/// expected to exist in the mapping of this translator. Uses `builder` to
180+
/// translate the IR, leaving it at the end of the block. If `ignoreArguments`
181+
/// is set, does not produce PHI nodes for the block arguments. Otherwise, the
182+
/// PHI nodes are constructed for block arguments but are _not_ connected to
183+
/// the predecessors that may not exist yet.
184+
LogicalResult convertBlock(Block &bb, bool ignoreArguments,
185+
llvm::IRBuilder<> &builder);
186+
163187
protected:
164188
/// Translate the given MLIR module expressed in MLIR LLVM IR dialect into an
165189
/// LLVM IR module. The MLIR LLVM IR dialect holds a pointer to an
@@ -170,19 +194,6 @@ class ModuleTranslation {
170194

171195
virtual LogicalResult convertOperation(Operation &op,
172196
llvm::IRBuilder<> &builder);
173-
virtual LogicalResult convertOmpOperation(Operation &op,
174-
llvm::IRBuilder<> &builder);
175-
virtual LogicalResult convertOmpParallel(Operation &op,
176-
llvm::IRBuilder<> &builder);
177-
virtual LogicalResult convertOmpMaster(Operation &op,
178-
llvm::IRBuilder<> &builder);
179-
void convertOmpOpRegions(Region &region, StringRef blockName,
180-
llvm::BasicBlock &sourceBlock,
181-
llvm::BasicBlock &continuationBlock,
182-
llvm::IRBuilder<> &builder,
183-
LogicalResult &bodyGenStatus);
184-
virtual LogicalResult convertOmpWsLoop(Operation &opInst,
185-
llvm::IRBuilder<> &builder);
186197

187198
static std::unique_ptr<llvm::Module>
188199
prepareLLVMModule(Operation *m, llvm::LLVMContext &llvmContext,
@@ -196,8 +207,6 @@ class ModuleTranslation {
196207
LogicalResult convertFunctions();
197208
LogicalResult convertGlobals();
198209
LogicalResult convertOneFunction(LLVMFuncOp func);
199-
LogicalResult convertBlock(Block &bb, bool ignoreArguments,
200-
llvm::IRBuilder<> &builder);
201210

202211
/// Original and translated module.
203212
Operation *mlirModule;
@@ -232,6 +241,16 @@ class ModuleTranslation {
232241
DenseMap<Operation *, llvm::Instruction *> branchMapping;
233242
};
234243

244+
namespace detail {
245+
/// For all blocks in the region that were converted to LLVM IR using the given
246+
/// ModuleTranslation, connect the PHI nodes of the corresponding LLVM IR blocks
247+
/// to the results of preceding blocks.
248+
void connectPHINodes(Region &region, const ModuleTranslation &state);
249+
250+
/// Get a topologically sorted list of blocks of the given region.
251+
llvm::SetVector<Block *> getTopologicallySortedBlocks(Region &region);
252+
} // namespace detail
253+
235254
} // namespace LLVM
236255
} // namespace mlir
237256

mlir/lib/Target/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ add_mlir_translation_library(MLIRTargetLLVMIR
5757

5858
LINK_LIBS PUBLIC
5959
MLIRLLVMToLLVMIRTranslation
60+
MLIROpenMPToLLVMIRTranslation
6061
MLIRTargetLLVMIRModuleTranslation
6162
)
6263

mlir/lib/Target/LLVMIR/ConvertToLLVMIR.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1616
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
17+
#include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
1718
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
1819
#include "mlir/Translation.h"
1920

@@ -70,6 +71,8 @@ void registerToLLVMIRTranslation() {
7071
},
7172
[](DialectRegistry &registry) {
7273
registry.insert<omp::OpenMPDialect>();
74+
registry.addDialectInterface<omp::OpenMPDialect,
75+
OpenMPDialectLLVMIRTranslationInterface>();
7376
registerLLVMDialectTranslation(registry);
7477
});
7578
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
add_subdirectory(LLVMIR)
2+
add_subdirectory(OpenMP)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_mlir_translation_library(MLIROpenMPToLLVMIRTranslation
2+
OpenMPToLLVMIRTranslation.cpp
3+
4+
LINK_COMPONENTS
5+
Core
6+
7+
LINK_LIBS PUBLIC
8+
MLIRIR
9+
MLIRLLVMIR
10+
MLIROpenMP
11+
MLIRSupport
12+
MLIRTargetLLVMIRModuleTranslation
13+
)

0 commit comments

Comments
 (0)