|
| 1 | +//===- IntToPtrPtrToIntFolding.cpp - IntToPtr/PtrToInt folding ------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file implements a pass that folds inttoptr/ptrtoint operation sequences. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "mlir/Dialect/LLVMIR/Transforms/IntToPtrPtrToIntFolding.h" |
| 14 | + |
| 15 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 16 | +#include "mlir/IR/PatternMatch.h" |
| 17 | +#include "mlir/Support/LogicalResult.h" |
| 18 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 19 | + |
| 20 | +#define DEBUG_TYPE "fold-llvm-inttoptr-ptrtoint" |
| 21 | + |
| 22 | +namespace mlir { |
| 23 | +namespace LLVM { |
| 24 | + |
| 25 | +#define GEN_PASS_DEF_FOLDINTTOPTRPTRTOINTPASS |
| 26 | +#include "mlir/Dialect/LLVMIR/Transforms/Passes.h.inc" |
| 27 | + |
| 28 | +} // namespace LLVM |
| 29 | +} // namespace mlir |
| 30 | + |
| 31 | +using namespace mlir; |
| 32 | + |
| 33 | +namespace { |
| 34 | + |
| 35 | +/// Return the bitwidth of a pointer or integer type. If the type is a pointer, |
| 36 | +/// return the bitwidth of the address space from `addrSpaceBWs`, if available. |
| 37 | +/// Return failure if the address space bitwidth is not available. |
| 38 | +static FailureOr<unsigned> getIntOrPtrBW(Type type, |
| 39 | + ArrayRef<unsigned> addrSpaceBWs) { |
| 40 | + if (auto ptrType = dyn_cast<LLVM::LLVMPointerType>(type)) { |
| 41 | + unsigned addrSpace = ptrType.getAddressSpace(); |
| 42 | + if (addrSpace < addrSpaceBWs.size() && addrSpaceBWs[addrSpace] != 0) |
| 43 | + return addrSpaceBWs[addrSpace]; |
| 44 | + return failure(); |
| 45 | + } |
| 46 | + |
| 47 | + auto integerType = cast<IntegerType>(type); |
| 48 | + return integerType.getWidth(); |
| 49 | +} |
| 50 | + |
| 51 | +/// Check if folding inttoptr/ptrtoint is valid. Check that the original type |
| 52 | +/// matches the result type of the end-to-end conversion and that the input |
| 53 | +/// value is not truncated along the conversion chain. |
| 54 | +static LogicalResult canFoldIntToPtrPtrToInt(Type originalType, |
| 55 | + Type intermediateType, |
| 56 | + Type resultType, |
| 57 | + ArrayRef<unsigned> addrSpaceBWs) { |
| 58 | + // Check if the original type matches the result type. |
| 59 | + // TODO: Support address space conversions? |
| 60 | + // TODO: Support int trunc/ext? |
| 61 | + if (originalType != resultType) |
| 62 | + return failure(); |
| 63 | + |
| 64 | + // Make sure there is no data truncation with respect to the original type at |
| 65 | + // any point during the conversion. Truncating the intermediate data is fine |
| 66 | + // as long as the original data is not truncated. |
| 67 | + auto originalBW = getIntOrPtrBW(originalType, addrSpaceBWs); |
| 68 | + if (failed(originalBW)) |
| 69 | + return failure(); |
| 70 | + |
| 71 | + auto intermediateBW = getIntOrPtrBW(intermediateType, addrSpaceBWs); |
| 72 | + if (failed(intermediateBW)) |
| 73 | + return failure(); |
| 74 | + |
| 75 | + if (*originalBW > *intermediateBW) |
| 76 | + return failure(); |
| 77 | + return success(); |
| 78 | +} |
| 79 | + |
| 80 | +/// Folds inttoptr(ptrtoint(x)) -> x or ptrtoint(inttoptr(x)) -> x. |
| 81 | +template <typename SrcConvOp, typename DstConvOp> |
| 82 | +class FoldIntToPtrPtrToInt : public OpRewritePattern<DstConvOp> { |
| 83 | +public: |
| 84 | + FoldIntToPtrPtrToInt(MLIRContext *context, ArrayRef<unsigned> addrSpaceBWs) |
| 85 | + : OpRewritePattern<DstConvOp>(context), addrSpaceBWs(addrSpaceBWs) {} |
| 86 | + |
| 87 | + LogicalResult matchAndRewrite(DstConvOp dstConvOp, |
| 88 | + PatternRewriter &rewriter) const override { |
| 89 | + auto srcConvOp = dstConvOp.getArg().template getDefiningOp<SrcConvOp>(); |
| 90 | + if (!srcConvOp) |
| 91 | + return failure(); |
| 92 | + |
| 93 | + // Check if folding is valid based on type matching and bitwidth |
| 94 | + // information. |
| 95 | + if (failed(canFoldIntToPtrPtrToInt(srcConvOp.getArg().getType(), |
| 96 | + srcConvOp.getType(), dstConvOp.getType(), |
| 97 | + addrSpaceBWs))) { |
| 98 | + return failure(); |
| 99 | + } |
| 100 | + |
| 101 | + rewriter.replaceOp(dstConvOp, srcConvOp.getArg()); |
| 102 | + return success(); |
| 103 | + } |
| 104 | + |
| 105 | +private: |
| 106 | + SmallVector<unsigned> addrSpaceBWs; |
| 107 | +}; |
| 108 | + |
| 109 | +/// Pass that folds inttoptr/ptrtoint operation sequences. |
| 110 | +struct FoldIntToPtrPtrToIntPass |
| 111 | + : public LLVM::impl::FoldIntToPtrPtrToIntPassBase< |
| 112 | + FoldIntToPtrPtrToIntPass> { |
| 113 | + using Base = |
| 114 | + LLVM::impl::FoldIntToPtrPtrToIntPassBase<FoldIntToPtrPtrToIntPass>; |
| 115 | + using Base::FoldIntToPtrPtrToIntPassBase; |
| 116 | + |
| 117 | + void runOnOperation() override { |
| 118 | + RewritePatternSet patterns(&getContext()); |
| 119 | + LLVM::populateIntToPtrPtrToIntFoldingPatterns(patterns, addrSpaceBWs); |
| 120 | + |
| 121 | + if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) |
| 122 | + signalPassFailure(); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +} // namespace |
| 127 | + |
| 128 | +void mlir::LLVM::populateIntToPtrPtrToIntFoldingPatterns( |
| 129 | + RewritePatternSet &patterns, ArrayRef<unsigned> addrSpaceBWs) { |
| 130 | + patterns.add<FoldIntToPtrPtrToInt<LLVM::PtrToIntOp, LLVM::IntToPtrOp>, |
| 131 | + FoldIntToPtrPtrToInt<LLVM::IntToPtrOp, LLVM::PtrToIntOp>>( |
| 132 | + patterns.getContext(), addrSpaceBWs); |
| 133 | +} |
0 commit comments