[Mlir-commits] [mlir] [mlir][arith] Add infer exact from dlti (PR #184631)

Erick Ochoa Lopez llvmlistbot at llvm.org
Wed Mar 4 08:07:21 PST 2026


================
@@ -0,0 +1,83 @@
+//===- InferExactFromDLTI.cpp - Infer exact flags from DLTI ------*- C++
+//-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir/Dialect/Arith/Transforms/Passes.h"
+
+#include "mlir/Dialect/Arith/IR/Arith.h"
+#include "mlir/IR/PatternMatch.h"
+#include "mlir/IR/TypeUtilities.h"
+#include "mlir/Interfaces/DataLayoutInterfaces.h"
+#include "mlir/Transforms/WalkPatternRewriteDriver.h"
+
+namespace mlir {
+namespace arith {
+#define GEN_PASS_DEF_ARITHINFEREXACTFROMDLTI
+#include "mlir/Dialect/Arith/Transforms/Passes.h.inc"
+} // namespace arith
+} // namespace mlir
+
+using namespace mlir;
+using namespace mlir::arith;
+
+static unsigned getBitwidth(Type type, unsigned indexBitwidth) {
+  Type elemType = getElementTypeOrSelf(type);
+  if (isa<IndexType>(elemType))
+    return indexBitwidth;
+  return elemType.getIntOrFloatBitWidth();
+}
+
+namespace {
+template <typename CastOp>
+struct InferExactOnIndexCast final : OpRewritePattern<CastOp> {
+  InferExactOnIndexCast(MLIRContext *context, unsigned indexBitwidth)
+      : OpRewritePattern<CastOp>(context), indexBitwidth(indexBitwidth) {}
+
+  LogicalResult matchAndRewrite(CastOp op,
+                                PatternRewriter &rewriter) const override {
+    if (op.getExact())
+      return failure();
+
+    unsigned srcBW = getBitwidth(op.getIn().getType(), indexBitwidth);
+    unsigned dstBW = getBitwidth(op.getType(), indexBitwidth);
+    if (srcBW > dstBW)
+      return rewriter.notifyMatchFailure(op, "source is wider than dest");
+
+    rewriter.modifyOpInPlace(op, [&] { op.setExact(true); });
+    return success();
+  }
+
+private:
+  unsigned indexBitwidth;
+};
+
+struct ArithInferExactFromDLTIPass
+    : public arith::impl::ArithInferExactFromDLTIBase<
+          ArithInferExactFromDLTIPass> {
+
+  void runOnOperation() override {
+    Operation *op = getOperation();
+    MLIRContext *ctx = op->getContext();
+
+    DataLayout layout = DataLayout::closest(op);
----------------
amd-eochoalo wrote:

We need to to make sure this doesn't break if no layout info is found.

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


More information about the Mlir-commits mailing list