[Mlir-commits] [mlir] [MLIR][LLVM] Support inrange in GEP (PR #220424)

Letu Ren llvmlistbot at llvm.org
Sat Sep 12 20:07:53 PDT 2026


================
@@ -352,13 +365,41 @@ def LLVM_GEPOp : LLVM_Op<"getelementptr", [Pure,
     }
     Type baseElementType = op.getElemType();
     llvm::Type *elementType = moduleTranslation.convertType(baseElementType);
-    $res = builder.CreateGEP(elementType, $base, indices, "",
-                             llvm::GEPNoWrapFlags::fromRaw(
-                                 static_cast<unsigned>(
-                                     op.getNoWrapFlags())));
+    llvm::GEPNoWrapFlags nwFlags = llvm::GEPNoWrapFlags::fromRaw(
+        static_cast<unsigned>(op.getNoWrapFlags()));
+    // `inrange` is only representable on LLVM constant GEP expressions.
+    if (auto inrangeAttr = op.getInrangeAttr()) {
+      auto *baseConst = ::llvm::dyn_cast<llvm::Constant>($base);
+      if (!baseConst ||
+          !::llvm::all_of(indices, [](llvm::Value *value) {
+            return ::llvm::isa<llvm::Constant>(value);
+          }))
+        return op.emitError("'inrange' requires the base and indices to "
+                            "translate to LLVM constants");
+
+      unsigned indexBitWidth =
+          moduleTranslation.getLLVMModule()
+              ->getDataLayout()
+              .getIndexTypeSizeInBits($base->getType());
+      llvm::APInt lower = inrangeAttr.getLower().sextOrTrunc(indexBitWidth);
+      llvm::APInt upper = inrangeAttr.getUpper().sextOrTrunc(indexBitWidth);
+      if (lower.sge(upper))
+        return op.emitError("expected 'inrange' end to be larger than start "
----------------
FantasqueX wrote:

The verifier checks the bounds using the attribute’s original bit width. Translation then extends or truncates them to the target pointer-index width, which can change their signed ordering. For example, with a 32-bit pointer index, `<i64, 2147483647, 2147483648>` is ordered before conversion but reversed after truncation.

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


More information about the Mlir-commits mailing list