[Mlir-commits] [mlir] [mlir][LLVMIR] Add folders for `llvm.inttoptr` and `llvm.ptrtoint` (PR #141891)
Tobias Gysi
llvmlistbot at llvm.org
Fri May 30 11:26:31 PDT 2025
================
@@ -134,6 +134,32 @@ llvm.func @fold_extract_splat() -> f64 {
// -----
+// CHECK-LABEL: fold_inttoptr_ptrtoint
+// CHECK-SAME: %[[ARG:.+]]: i32) -> i32
+// CHECK-NOT: inttoptr
+// CHECK-NOT: ptrtoint
+// CHECK-NEXT: llvm.return %[[ARG]]
+llvm.func @fold_inttoptr_ptrtoint(%x : i32) -> i32 {
+ %c = llvm.inttoptr %x : i32 to !llvm.ptr
+ %d = llvm.ptrtoint %c : !llvm.ptr to i32
+ llvm.return %d : i32
+}
+
+// -----
+
+// CHECK-LABEL: fold_ptrtoint_inttoptr
+// CHECK-SAME: %[[ARG:.+]]: !llvm.ptr<3>) -> !llvm.ptr<3>
+// CHECK-NOT: inttoptr
+// CHECK-NOT: ptrtoint
+// CHECK-NEXT: llvm.return %[[ARG]]
+llvm.func @fold_ptrtoint_inttoptr(%x : !llvm.ptr<3>) -> !llvm.ptr<3> {
+ %c = llvm.ptrtoint %x : !llvm.ptr<3> to i32
+ %d = llvm.inttoptr %c : i32 to !llvm.ptr<3>
+ llvm.return %d : !llvm.ptr<3>
----------------
gysit wrote:
> Is retrieving DL info allowed withing a folder?
I am not aware of a specific rule but I would not recommend to query the data layout from a folder. For such a transformation, a separate optimization pass sounds like the better choice? That way the data layout doesn't need to queried for every fold.
> Have we agreed upon how to represent pointer information in the DL for the LLVM dialect already (@rengolin)?
AFAIK the pointer bit width is available today but I don't think non-integral pointer info that @krzysz00 mentioned is available.
https://github.com/llvm/llvm-project/pull/141891
More information about the Mlir-commits
mailing list