-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang][fir] allow fir.convert from and to !llvm.ptr type #106590
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
Conversation
@llvm/pr-subscribers-flang-fir-hlfir Author: None (jeanPerier) ChangesAllow some interaction between LLVM and FIR dialect by allowing conversion between FIR memory types and llvm.ptr type.
Full diff: https://github.com/llvm/llvm-project/pull/106590.diff 3 Files Affected:
diff --git a/flang/include/flang/Optimizer/Dialect/FIROps.td b/flang/include/flang/Optimizer/Dialect/FIROps.td
index 2cd202c3623250..68847cfa2cbc25 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROps.td
+++ b/flang/include/flang/Optimizer/Dialect/FIROps.td
@@ -2713,7 +2713,7 @@ def fir_AddrOfOp : fir_OneResultOp<"address_of", [NoMemoryEffect]> {
let assemblyFormat = "`(` $symbol `)` attr-dict `:` type($resTy)";
}
-def fir_ConvertOp : fir_OneResultOp<"convert", [NoMemoryEffect]> {
+def fir_ConvertOp : fir_SimpleOneResultOp<"convert", [NoMemoryEffect]> {
let summary = "encapsulates all Fortran entity type conversions";
let description = [{
@@ -2722,6 +2722,9 @@ def fir_ConvertOp : fir_OneResultOp<"convert", [NoMemoryEffect]> {
type, this instruction is a NOP and may be folded away. This also supports
integer to pointer conversion and pointer to integer conversion.
+ This operation also allows limited interaction between FIR and LLVM
+ dialects by allowing conversion between FIR pointer types and llvm.ptr type.
+
```
%v = ... : i64
%w = fir.convert %v : (i64) -> i32
@@ -2731,6 +2734,7 @@ def fir_ConvertOp : fir_OneResultOp<"convert", [NoMemoryEffect]> {
}];
let arguments = (ins AnyType:$value);
+ let results = (outs AnyType:$res);
let assemblyFormat = [{
$value attr-dict `:` functional-type($value, results)
diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index ce0ae446bb7af8..13a30a358a8660 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -1387,7 +1387,7 @@ bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) {
bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) {
return mlir::isa<fir::ReferenceType, fir::PointerType, fir::HeapType,
fir::LLVMPointerType, mlir::MemRefType, mlir::FunctionType,
- fir::TypeDescType>(ty);
+ fir::TypeDescType, mlir::LLVM::LLVMPointerType>(ty);
}
static std::optional<mlir::Type> getVectorElementType(mlir::Type ty) {
diff --git a/flang/test/Fir/convert.fir b/flang/test/Fir/convert.fir
index ef9242ef1d8b41..25a318336aa07d 100644
--- a/flang/test/Fir/convert.fir
+++ b/flang/test/Fir/convert.fir
@@ -11,3 +11,19 @@ func.func @c(%x : !fir.complex<4>) -> !fir.complex<8> {
%1 = fir.convert %x : (!fir.complex<4>) -> !fir.complex<8>
return %1 : !fir.complex<8>
}
+
+func.func @convert_to_llvm_pointer(%x : !fir.llvm_ptr<i32>) -> !llvm.ptr {
+ %0 = fir.convert %x : (!fir.llvm_ptr<i32>) -> !llvm.ptr
+ return %0 : !llvm.ptr
+}
+// CHECK-LABEL: convert_to_llvm_pointer(
+// CHECK-SAME: ptr %[[X:.*]])
+// CHECK-NEXT: ret ptr %[[X]]
+
+func.func @convert_from_llvm_pointer(%x : !llvm.ptr) -> !fir.llvm_ptr<i32> {
+ %0 = fir.convert %x : (!llvm.ptr) -> !fir.llvm_ptr<i32>
+ return %0 : !fir.llvm_ptr<i32>
+}
+// CHECK-LABEL: convert_from_llvm_pointer(
+// CHECK-SAME: ptr %[[X:.*]])
+// CHECK-NEXT: ret ptr %[[X]]
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent. Makes sense to me!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Allow some interaction between LLVM and FIR dialect by allowing conversion between FIR memory types and llvm.ptr type.
This is meant to help experimentation where FIR and LLVM dialect coexists, and is useful to deal with cases where LLVM type makes it early into the MLIR produced by flang, like when inserting LLVM stack intrinsic here:
llvm-project/flang/lib/Optimizer/Transforms/StackReclaim.cpp
Line 57 in 0a00d32