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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion flang/include/flang/Optimizer/Dialect/FIROps.td
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [{
Expand All @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Dialect/FIROps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions flang/test/Fir/convert.fir
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Loading