[Mlir-commits] [mlir] [mlir][ptr] Add constantop convertion (PR #204846)
Fabian Mora
llvmlistbot at llvm.org
Fri Jun 19 10:01:08 PDT 2026
================
@@ -370,6 +382,33 @@ LogicalResult TypeOffsetOpConversion::matchAndRewrite(
return success();
}
+//===----------------------------------------------------------------------===//
+// ConstantOpConversion
+//===----------------------------------------------------------------------===//
+
+LogicalResult ConstantOpConversion::matchAndRewrite(
+ ptr::ConstantOp op, OpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter) const {
+ TypedAttr value = op.getValue();
+ Type resultType = getTypeConverter()->convertType(op.getType());
+ if (!resultType)
+ return rewriter.notifyMatchFailure(op, "Couldn't convert the result type");
+
+ if (isa<ptr::NullAttr>(value)) {
+ rewriter.replaceOpWithNewOp<LLVM::ZeroOp>(op, resultType);
+ } else if (auto addrAttr = dyn_cast<ptr::AddressAttr>(value)) {
+ Type intType = rewriter.getIntegerType(addrAttr.getValue().getBitWidth());
+ Value intConst = LLVM::ConstantOp::create(rewriter, op.getLoc(), intType,
+ addrAttr.getValue());
+ rewriter.replaceOpWithNewOp<LLVM::IntToPtrOp>(op, resultType, intConst);
+ } else {
+ return rewriter.notifyMatchFailure(
+ op, "unsupported value attribute kind: " +
+ value.getAbstractAttribute().getName());
+ }
+ return success();
+}
----------------
fabianmcg wrote:
Always prefer code that returns early and decreases indentation, it makes it more readable.
```suggestion
if (isa<ptr::NullAttr>(value)) {
rewriter.replaceOpWithNewOp<LLVM::ZeroOp>(op, resultType);
return success();
}
auto addrAttr = dyn_cast<ptr::AddressAttr>(value);
// Early-exit if unknown attribute.
if (!addrAttr) {
return rewriter.notifyMatchFailure(
op, "unsupported value attribute kind: " +
value.getAbstractAttribute().getName());
}
Type intType = rewriter.getIntegerType(addrAttr.getValue().getBitWidth());
Value intConst = LLVM::ConstantOp::create(rewriter, op.getLoc(), intType,
addrAttr.getValue());
rewriter.replaceOpWithNewOp<LLVM::IntToPtrOp>(op, resultType, intConst);
return success();
}
```
https://github.com/llvm/llvm-project/pull/204846
More information about the Mlir-commits
mailing list