[Mlir-commits] [mlir] [mlir][ptr] Add constantop convertion (PR #204846)

Fabian Mora llvmlistbot at llvm.org
Fri Jun 19 09:07:41 PDT 2026


================
@@ -370,6 +380,35 @@ LogicalResult TypeOffsetOpConversion::matchAndRewrite(
   return success();
 }
 
+//===----------------------------------------------------------------------===//
+// ConstantOpConversion
+//===----------------------------------------------------------------------===//
+
+LogicalResult ConstantOpConversion::matchAndRewrite(
+    ptr::ConstantOp op, OpAdaptor adaptor,
+    ConversionPatternRewriter &rewriter) const {
+  mlir::TypedAttr value = op.getValue();
+  Type resultType = getTypeConverter()->convertType(op.getType());
+  if (!resultType)
+    return rewriter.notifyMatchFailure(op, "Couldn't convert the result type");
+
+  if (llvm::dyn_cast<ptr::NullAttr>(value)) {
+    rewriter.replaceOpWithNewOp<LLVM::ZeroOp>(op, resultType);
+    return llvm::success();
+  }
+
+  auto llvmPtrType = cast<LLVM::LLVMPointerType>(resultType);
+  auto addrAttr = cast<ptr::AddressAttr>(value);
+  unsigned addressSpace = llvmPtrType.getAddressSpace();
+  unsigned bitwidth = getTypeConverter()->getPointerBitwidth(addressSpace);
+  Type intType = rewriter.getIntegerType(bitwidth);
+  APInt addr = addrAttr.getValue().zextOrTrunc(bitwidth);
+  Value intConst =
+      LLVM::ConstantOp::create(rewriter, op.getLoc(), intType, addr);
+  rewriter.replaceOpWithNewOp<LLVM::IntToPtrOp>(op, resultType, intConst);
----------------
fabianmcg wrote:

This seems overtly verbose, try to inline some the calls. Also, per the lang ref:
> The ‘inttoptr’ instruction converts value to type ty2 by applying either a zero extension or a truncation depending on the size of the integer value. If value is larger than the size of a pointer then a truncation is done.

This means, the constant should be created with the natural APIInt bitwidth and let intToPtr extand or trunc.

https://github.com/llvm/llvm-project/pull/204846


More information about the Mlir-commits mailing list