Skip to content

Commit b65fc7e

Browse files
authored
[flang][fir] allow fir.convert from and to !llvm.ptr type (#106590)
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: https://github.com/llvm/llvm-project/blob/0a00d32c5f88fce89006dcde6e235bc77d7b495e/flang/lib/Optimizer/Transforms/StackReclaim.cpp#L57
1 parent 24e791b commit b65fc7e

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

flang/include/flang/Optimizer/Dialect/FIROps.td

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,7 @@ def fir_AddrOfOp : fir_OneResultOp<"address_of", [NoMemoryEffect]> {
27132713
let assemblyFormat = "`(` $symbol `)` attr-dict `:` type($resTy)";
27142714
}
27152715

2716-
def fir_ConvertOp : fir_OneResultOp<"convert", [NoMemoryEffect]> {
2716+
def fir_ConvertOp : fir_SimpleOneResultOp<"convert", [NoMemoryEffect]> {
27172717
let summary = "encapsulates all Fortran entity type conversions";
27182718

27192719
let description = [{
@@ -2722,6 +2722,9 @@ def fir_ConvertOp : fir_OneResultOp<"convert", [NoMemoryEffect]> {
27222722
type, this instruction is a NOP and may be folded away. This also supports
27232723
integer to pointer conversion and pointer to integer conversion.
27242724

2725+
This operation also allows limited interaction between FIR and LLVM
2726+
dialects by allowing conversion between FIR pointer types and llvm.ptr type.
2727+
27252728
```
27262729
%v = ... : i64
27272730
%w = fir.convert %v : (i64) -> i32
@@ -2731,6 +2734,7 @@ def fir_ConvertOp : fir_OneResultOp<"convert", [NoMemoryEffect]> {
27312734
}];
27322735

27332736
let arguments = (ins AnyType:$value);
2737+
let results = (outs AnyType:$res);
27342738

27352739
let assemblyFormat = [{
27362740
$value attr-dict `:` functional-type($value, results)

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) {
13871387
bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) {
13881388
return mlir::isa<fir::ReferenceType, fir::PointerType, fir::HeapType,
13891389
fir::LLVMPointerType, mlir::MemRefType, mlir::FunctionType,
1390-
fir::TypeDescType>(ty);
1390+
fir::TypeDescType, mlir::LLVM::LLVMPointerType>(ty);
13911391
}
13921392

13931393
static std::optional<mlir::Type> getVectorElementType(mlir::Type ty) {

flang/test/Fir/convert.fir

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,19 @@ func.func @c(%x : !fir.complex<4>) -> !fir.complex<8> {
1111
%1 = fir.convert %x : (!fir.complex<4>) -> !fir.complex<8>
1212
return %1 : !fir.complex<8>
1313
}
14+
15+
func.func @convert_to_llvm_pointer(%x : !fir.llvm_ptr<i32>) -> !llvm.ptr {
16+
%0 = fir.convert %x : (!fir.llvm_ptr<i32>) -> !llvm.ptr
17+
return %0 : !llvm.ptr
18+
}
19+
// CHECK-LABEL: convert_to_llvm_pointer(
20+
// CHECK-SAME: ptr %[[X:.*]])
21+
// CHECK-NEXT: ret ptr %[[X]]
22+
23+
func.func @convert_from_llvm_pointer(%x : !llvm.ptr) -> !fir.llvm_ptr<i32> {
24+
%0 = fir.convert %x : (!llvm.ptr) -> !fir.llvm_ptr<i32>
25+
return %0 : !fir.llvm_ptr<i32>
26+
}
27+
// CHECK-LABEL: convert_from_llvm_pointer(
28+
// CHECK-SAME: ptr %[[X:.*]])
29+
// CHECK-NEXT: ret ptr %[[X]]

0 commit comments

Comments
 (0)