Skip to content

[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

Merged
merged 1 commit into from
Aug 30, 2024

Conversation

jeanPerier
Copy link
Contributor

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:

auto stackSaveOp = builder.create<LLVM::StackSaveOp>(loc, voidPtr);

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Aug 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 29, 2024

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (jeanPerier)

Changes

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:

auto stackSaveOp = builder.create<LLVM::StackSaveOp>(loc, voidPtr);


Full diff: https://github.com/llvm/llvm-project/pull/106590.diff

3 Files Affected:

  • (modified) flang/include/flang/Optimizer/Dialect/FIROps.td (+5-1)
  • (modified) flang/lib/Optimizer/Dialect/FIROps.cpp (+1-1)
  • (modified) flang/test/Fir/convert.fir (+16)
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]]

Copy link
Contributor

@razvanlupusoru razvanlupusoru left a 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!

Copy link
Contributor

@clementval clementval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jeanPerier jeanPerier merged commit b65fc7e into llvm:main Aug 30, 2024
12 checks passed
@jeanPerier jeanPerier deleted the llvm-ptr-convert branch August 30, 2024 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants