[Mlir-commits] [mlir] [mlir][LLVMIR] Add folders for `llvm.inttoptr` and `llvm.ptrtoint` (PR #141891)
Tobias Gysi
llvmlistbot at llvm.org
Wed May 28 23:57:21 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:
I believe this should not be folded since the ptrtoint operation truncates the pointer value to an i32, if I understand correctly? After the folding all bits of the function argument are forwarded to the result.
Do you know what folders LLVM proper implements? I suspect there are some edge cases around these operations that require special handling.
https://github.com/llvm/llvm-project/pull/141891
More information about the Mlir-commits
mailing list