[Mlir-commits] [mlir] [MLIR][LLVM] Produce canonical const GEP in convertGEPOp (PR #226699)
Letu Ren
llvmlistbot at llvm.org
Sat Sep 26 06:50:13 PDT 2026
https://github.com/FantasqueX created https://github.com/llvm/llvm-project/pull/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
>From 50b3c7b528d89e353d585f0994a01212d69ab328 Mon Sep 17 00:00:00 2001
From: Letu Ren <fantasquex at gmail.com>
Date: Sat, 26 Sep 2026 21:43:56 +0800
Subject: [PATCH] [MLIR][LLVM] Produce canonical const GEP in convertGEPOp
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>
---
.../LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.cpp | 7 ++++++-
mlir/test/Target/LLVMIR/llvmir.mlir | 2 +-
2 files changed, 7 insertions(+), 2 deletions(-)
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