[Mlir-commits] [mlir] fb9596b - [MLIR][LLVM] Produce canonical const GEP in convertGEPOp (#226699)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Sep 26 20:22:55 PDT 2026


Author: Letu Ren
Date: 2026-09-27T11:22:48+08:00
New Revision: fb9596b7117dac3a1bc680b1041464944d0caa24

URL: https://github.com/llvm/llvm-project/commit/fb9596b7117dac3a1bc680b1041464944d0caa24
DIFF: https://github.com/llvm/llvm-project/commit/fb9596b7117dac3a1bc680b1041464944d0caa24.diff

LOG: [MLIR][LLVM] Produce canonical const GEP in convertGEPOp (#226699)

The DataLayout overload of ConstantExpr::getGetElementPtr is the form
that replaces typed constant GEPs. Use it for inrange GEPs and fail
translation when the indices cannot be reduced to a byte offset.

This resolves
https://github.com/llvm/llvm-project/pull/220424#discussion_r4092337505

Assisted-by: grok-4.7
Signed-off-by: Letu Ren <fantasquex at gmail.com>

Added: 
    

Modified: 
    mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
    mlir/test/Target/LLVMIR/llvmir.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
index 98ddf0a7c51ea..3f83a5f7f22ce 100644
--- a/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
+++ b/mlir/lib/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp
@@ -267,10 +267,15 @@ static LogicalResult convertGEPOp(GEPOp op, llvm::IRBuilderBase &builder,
     constIndices.reserve(indices.size());
     for (llvm::Value *value : indices)
       constIndices.push_back(cast<llvm::Constant>(value));
+    const llvm::DataLayout &dataLayout =
+        moduleTranslation.getLLVMModule()->getDataLayout();
     res = llvm::ConstantExpr::getGetElementPtr(
-        elementType, baseConst, constIndices, nwFlags,
+        dataLayout, elementType, baseConst, constIndices, nwFlags,
         llvm::ConstantRange::getNonEmpty(inrangeAttr.getLower(),
                                          inrangeAttr.getUpper()));
+    if (!res)
+      return op.emitError(
+          "failed to lower 'inrange' GEP to a constant byte offset");
   } else {
     res = builder.CreateGEP(elementType, base, indices, "", nwFlags);
   }

diff  --git a/mlir/test/Target/LLVMIR/llvmir.mlir b/mlir/test/Target/LLVMIR/llvmir.mlir
index fb0cf9b493af4..5557788b039f7 100644
--- a/mlir/test/Target/LLVMIR/llvmir.mlir
+++ b/mlir/test/Target/LLVMIR/llvmir.mlir
@@ -124,7 +124,7 @@ llvm.mlir.global internal constant @int_gep() : !llvm.ptr {
 
 // CHECK: @vt = external constant { [3 x ptr] }
 llvm.mlir.global external constant @vt() : !llvm.struct<(array<3 x ptr>)>
-// CHECK: @int_gep_inrange = internal constant ptr getelementptr inbounds inrange(-16, 8) ({ [3 x ptr] }, ptr @vt, i32 0, i32 0, i32 2)
+// CHECK: @int_gep_inrange = internal constant ptr getelementptr inbounds inrange(-16, 8) (i8, ptr @vt, i64 16)
 llvm.mlir.global internal constant @int_gep_inrange() : !llvm.ptr {
   %addr = llvm.mlir.addressof @vt : !llvm.ptr
   %gepinit = llvm.getelementptr inbounds inrange <i64, -16, 8> %addr[0, 0, 2] : (!llvm.ptr) -> !llvm.ptr, !llvm.struct<(array<3 x ptr>)>


        


More information about the Mlir-commits mailing list