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

Tobias Gysi llvmlistbot at llvm.org
Mon Sep 14 12:02:53 PDT 2026


================
@@ -234,6 +236,58 @@ convertNamedMetadataOp(NamedMetadataOp op,
   return success();
 }
 
+/// Translate `llvm.getelementptr`. `inrange` is only representable on LLVM
+/// constant GEP expressions.
+static LogicalResult convertGEPOp(GEPOp op, llvm::IRBuilderBase &builder,
+                                  LLVM::ModuleTranslation &moduleTranslation) {
+  SmallVector<llvm::Value *> indices;
+  indices.reserve(op.getRawConstantIndices().size());
+  for (PointerUnion<IntegerAttr, Value> valueOrAttr : op.getIndices()) {
+    if (Value value = dyn_cast_if_present<Value>(valueOrAttr))
+      indices.push_back(moduleTranslation.lookupValue(value));
+    else
+      indices.push_back(
+          builder.getInt32(cast<IntegerAttr>(valueOrAttr).getInt()));
+  }
+
+  llvm::Type *elementType = moduleTranslation.convertType(op.getElemType());
+  llvm::GEPNoWrapFlags nwFlags =
+      llvm::GEPNoWrapFlags::fromRaw(static_cast<unsigned>(op.getNoWrapFlags()));
+  llvm::Value *base = moduleTranslation.lookupValue(op.getBase());
+  llvm::Value *res;
+  if (ConstantRangeAttr inrangeAttr = op.getInrangeAttr()) {
+    auto *baseConst = dyn_cast<llvm::Constant>(base);
+    if (!baseConst || !llvm::all_of(indices, [](llvm::Value *value) {
+          return 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);
----------------
gysit wrote:

Why can't we assume the ConstantRangeAttr already uses the correct index bitwidth? That would simplify the translation quite a bit? We could technically verify the ConstantRangeAttr bitwidth using the datalayout (see `verifyAtomicMemOp` for a verifier that queries the data layout). 

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


More information about the Mlir-commits mailing list