[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);
+ return llvm::success();
----------------
fabianmcg wrote:
```suggestion
return success();
```
https://github.com/llvm/llvm-project/pull/204846
More information about the Mlir-commits
mailing list