Index: mlir/examples/toy/Ch6/CMakeLists.txt =================================================================== --- mlir/examples/toy/Ch6/CMakeLists.txt +++ mlir/examples/toy/Ch6/CMakeLists.txt @@ -35,6 +35,8 @@ MLIRExecutionEngine MLIRIR MLIRLLVMIR + MLIROpenMP + LLVMFrontendOpenMP MLIRLoopToStandard MLIRParser MLIRPass @@ -42,4 +44,3 @@ MLIRTargetLLVMIR MLIRTransforms ) - Index: mlir/examples/toy/Ch7/CMakeLists.txt =================================================================== --- mlir/examples/toy/Ch7/CMakeLists.txt +++ mlir/examples/toy/Ch7/CMakeLists.txt @@ -34,6 +34,8 @@ MLIRAnalysis MLIRExecutionEngine MLIRIR + MLIROpenMP + LLVMFrontendOpenMP MLIRLoopToStandard MLIRParser MLIRPass Index: mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h =================================================================== --- mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h +++ mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h @@ -19,6 +19,7 @@ #include "mlir/IR/Module.h" #include "mlir/IR/Value.h" +#include "llvm/Frontend/OpenMP/OMPIRBuilder.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Function.h" #include "llvm/IR/IRBuilder.h" @@ -102,6 +103,9 @@ /// A converter for translating debug information. std::unique_ptr debugTranslation; + /// Builder for LLVM IR generation of OpenMP constructs. + std::unique_ptr OMPBuilder; + /// Mappings between llvm.mlir.global definitions and corresponding globals. DenseMap globalsMapping; Index: mlir/lib/Target/LLVMIR/ModuleTranslation.cpp =================================================================== --- mlir/lib/Target/LLVMIR/ModuleTranslation.cpp +++ mlir/lib/Target/LLVMIR/ModuleTranslation.cpp @@ -15,12 +15,14 @@ #include "DebugTranslation.h" #include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/Dialect/OpenMP/OpenMPDialect.h" #include "mlir/IR/Attributes.h" #include "mlir/IR/Module.h" #include "mlir/IR/StandardTypes.h" #include "mlir/Support/LLVM.h" #include "llvm/ADT/SetVector.h" +#include "llvm/Frontend/OpenMP/OMPIRBuilder.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" @@ -374,6 +376,22 @@ return success(); } + if (opInst.getDialect() == + opInst.getContext()->getRegisteredDialect()) { + if (!OMPBuilder) { + OMPBuilder = + std::move(std::make_unique(*llvmModule)); + OMPBuilder->initialize(); + } + + if (isa(opInst)) { + OMPBuilder->CreateBarrier(builder.saveIP(), llvm::omp::OMPD_barrier); + return success(); + } + return opInst.emitError("unsupported OpenMP operation: ") + << opInst.getName(); + } + return opInst.emitError("unsupported or non-LLVM operation: ") << opInst.getName(); } Index: mlir/test/Target/openmp-llvm.mlir =================================================================== --- /dev/null +++ mlir/test/Target/openmp-llvm.mlir @@ -0,0 +1,10 @@ +// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s + +// CHECK-LABEL: define void @empty() +// CHECK: [[OMP_THREAD:%.*]] = call i32 @__kmpc_global_thread_num(%struct.ident_t* @{{[0-9]+}}) +// CHECK-NEXT: call void @__kmpc_barrier(%struct.ident_t* @{{[0-9]+}}, i32 [[OMP_THREAD]]) +// CHECK-NEXT: ret void +llvm.func @empty() { + omp.barrier + llvm.return +} Index: mlir/tools/mlir-cpu-runner/CMakeLists.txt =================================================================== --- mlir/tools/mlir-cpu-runner/CMakeLists.txt +++ mlir/tools/mlir-cpu-runner/CMakeLists.txt @@ -10,6 +10,8 @@ MLIRIR MLIRJitRunner MLIRLLVMIR + MLIROpenMP + LLVMFrontendOpenMP MLIRParser MLIRTargetLLVMIR MLIRSupport Index: mlir/tools/mlir-translate/CMakeLists.txt =================================================================== --- mlir/tools/mlir-translate/CMakeLists.txt +++ mlir/tools/mlir-translate/CMakeLists.txt @@ -9,12 +9,15 @@ MLIRTargetROCDLIR MLIRTranslation MLIRSupport + MLIROpenMP + LLVMFrontendOpenMP ) set(FULL_LIBS MLIRSPIRVSerialization MLIRTargetLLVMIR MLIRTargetNVVMIR MLIRTargetROCDLIR + LLVMFrontendOpenMP ) add_llvm_tool(mlir-translate mlir-translate.cpp