[CIR][OpenMP] Add OpenMP-to-LLVM type conversion for CIR-to-LLVM lowering#190063
[CIR][OpenMP] Add OpenMP-to-LLVM type conversion for CIR-to-LLVM lowering#190063
Conversation
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clangir Author: Jan Leyonberg (jsjodin) ChangesRegister OpenMP conversion legality and patterns in the CIR-to-LLVM pass so that OpenMP operations (e.g. omp.map.info, omp.target) have their CIR types properly converted to LLVM types during lowering. Without this, the conversion leaves behind unrealized_conversion_cast ops that cause translation to LLVM IR to fail. Also registers omp::PointerLikeType on cir::PointerType so that CIR pointers are accepted as operands in OpenMP map operations. Assised-by: Cursor / Claude Opus 4.6 Full diff: https://github.com/llvm/llvm-project/pull/190063.diff 4 Files Affected:
diff --git a/clang/lib/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.cpp b/clang/lib/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.cpp
index b5129202e66c4..3a66f93238808 100644
--- a/clang/lib/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.cpp
+++ b/clang/lib/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.cpp
@@ -11,8 +11,20 @@
//===----------------------------------------------------------------------===//
#include "clang/CIR/Dialect/OpenMP/RegisterOpenMPExtensions.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/Dialect/OpenMP/OpenMPInterfaces.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
+#include "clang/CIR/Dialect/IR/CIRTypes.h"
+
+namespace {
+struct OpenMPPointerLikeModel
+ : public mlir::omp::PointerLikeType::ExternalModel<OpenMPPointerLikeModel,
+ cir::PointerType> {
+ mlir::Type getElementType(mlir::Type pointer) const {
+ return mlir::cast<cir::PointerType>(pointer).getPointee();
+ }
+};
+} // namespace
namespace cir::omp {
@@ -20,6 +32,7 @@ void registerOpenMPExtensions(mlir::DialectRegistry ®istry) {
registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) {
cir::FuncOp::attachInterface<
mlir::omp::DeclareTargetDefaultModel<cir::FuncOp>>(*ctx);
+ cir::PointerType::attachInterface<OpenMPPointerLikeModel>(*ctx);
});
}
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
index 021397fee992b..00c6102f039f1 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/CMakeLists.txt
@@ -21,6 +21,7 @@ add_clang_library(clangCIRLoweringDirectToLLVM
MLIRCIRTargetLowering
MLIRBuiltinToLLVMIRTranslation
MLIRLLVMToLLVMIRTranslation
+ MLIROpenMPToLLVM
MLIROpenMPToLLVMIRTranslation
MLIROpenMPTransforms
MLIRIR
diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
index 149cd90b813ec..d8af7e22e0587 100644
--- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
+++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
@@ -17,10 +17,12 @@
#include <optional>
#include "mlir/Conversion/LLVMCommon/TypeConverter.h"
+#include "mlir/Conversion/OpenMPToLLVM/ConvertOpenMPToLLVM.h"
#include "mlir/Dialect/DLTI/DLTI.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
+#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/Dialect/OpenMP/Transforms/Passes.h"
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
@@ -3678,6 +3680,9 @@ void ConvertCIRToLLVMPass::runOnOperation() {
mlir::ConversionTarget target(getContext());
target.addLegalOp<mlir::ModuleOp>();
target.addLegalDialect<mlir::LLVM::LLVMDialect>();
+ mlir::configureOpenMPToLLVMConversionLegality(target, converter);
+ target.addLegalDialect<mlir::omp::OpenMPDialect>();
+ mlir::populateOpenMPToLLVMConversionPatterns(converter, patterns);
target.addIllegalDialect<mlir::BuiltinDialect, cir::CIRDialect,
mlir::func::FuncDialect>();
diff --git a/clang/test/CIR/Lowering/omp-target-map.cir b/clang/test/CIR/Lowering/omp-target-map.cir
new file mode 100644
index 0000000000000..b885cffc2bf48
--- /dev/null
+++ b/clang/test/CIR/Lowering/omp-target-map.cir
@@ -0,0 +1,30 @@
+// RUN: cir-opt %s --cir-to-llvm | FileCheck %s
+
+// Verify that OpenMP operations have their CIR types properly converted to
+// LLVM types during CIR-to-LLVM lowering. Without the OpenMP-to-LLVM
+// conversion patterns, this would fail with unrealized_conversion_cast errors.
+
+!s32i = !cir.int<s, 32>
+
+module {
+ // CHECK-LABEL: llvm.func @target_map_from
+ cir.func @target_map_from(%arg0 : !s32i) {
+ %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init] {alignment = 4 : i64}
+ cir.store %arg0, %0 : !s32i, !cir.ptr<!s32i>
+
+ // CHECK: %[[ALLOCA:.*]] = llvm.alloca {{.*}} x i32
+ // CHECK: %[[MAP:.*]] = omp.map.info var_ptr(%[[ALLOCA]] : !llvm.ptr, i32) map_clauses(from) capture(ByRef) -> !llvm.ptr {name = "x"}
+ %1 = omp.map.info var_ptr(%0 : !cir.ptr<!s32i>, !s32i) map_clauses(from) capture(ByRef) -> !cir.ptr<!s32i> {name = "x"}
+
+ // CHECK: omp.target map_entries(%[[MAP]] -> %[[ARG:.*]] : !llvm.ptr)
+ omp.target map_entries(%1 -> %arg1 : !cir.ptr<!s32i>) {
+ // CHECK: %[[C10:.*]] = llvm.mlir.constant(10 : i32) : i32
+ // CHECK: llvm.store %[[C10]], %[[ARG]]
+ %c10 = cir.const #cir.int<10> : !s32i
+ cir.store %c10, %arg1 : !s32i, !cir.ptr<!s32i>
+ // CHECK: omp.terminator
+ omp.terminator
+ }
+ cir.return
+ }
+}
|
andykaylor
left a comment
There was a problem hiding this comment.
This looks good.
I don't see any equivalent code in the incubator. Was OpenMP just not working there?
I know that Luca Parigi was working on workshare loops here in PR #181841, and he was seeing the same issues, so likely this never worked in the incubator. |
7f12f72 to
f569f3c
Compare
…ring Register OpenMP conversion legality and patterns in the CIR-to-LLVM pass so that OpenMP operations (e.g. omp.map.info, omp.target) have their CIR types properly converted to LLVM types during lowering. Without this, the conversion leaves behind unrealized_conversion_cast ops that cause translation to LLVM IR to fail. Also registers omp::PointerLikeType on cir::PointerType so that CIR pointers are accepted as operands in OpenMP map operations. Assised-by: Cursor / Claude Opus 4.6
f569f3c to
bcad090
Compare
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/88/builds/22463 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/128/builds/12416 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/66/builds/29442 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/169/builds/21929 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/20/builds/15816 Here is the relevant piece of the build log for the reference |
Register OpenMP conversion legality and patterns in the CIR-to-LLVM pass so that OpenMP operations (e.g. omp.map.info, omp.target) have their CIR types properly converted to LLVM types during lowering. Without this, the conversion leaves behind unrealized_conversion_cast ops that cause translation to LLVM IR to fail.
Also registers omp::PointerLikeType on cir::PointerType so that CIR pointers are accepted as operands in OpenMP map operations.
Assised-by: Cursor / Claude Opus 4.6