[llvm] [CHERI][RISCV][AsmPrinter] Use pointer index size rather than pointer size in AsmPrinter constant lowering. (PR #220100)
Owen Anderson via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 05:52:38 PDT 2026
https://github.com/resistor updated https://github.com/llvm/llvm-project/pull/220100
>From c23acdf64f00a6e146fb7ed42a01b8ca7c88d25c Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Mon, 31 Aug 2026 16:48:56 -0500
Subject: [PATCH] [CHERI][RISCV][AsmPrinter] Use pointer index size rather than
pointer size in AsmPrinter constant lowering.
This prevents a crash due to APInt width mismatches during accumulateConstantOffset for CHERI targets.
---
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 2 +-
llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll | 14 ++++++++++++++
2 files changed, 15 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index c0348a2dad159..fe034ac8c97c5 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3977,7 +3977,7 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV,
}
case Instruction::GetElementPtr: {
// Generate a symbolic expression for the byte address
- APInt OffsetAI(getDataLayout().getPointerTypeSizeInBits(CE->getType()), 0);
+ APInt OffsetAI(getDataLayout().getIndexTypeSizeInBits(CE->getType()), 0);
cast<GEPOperator>(CE)->accumulateConstantOffset(getDataLayout(), OffsetAI);
const MCExpr *Base = lowerConstant(CE->getOperand(0));
diff --git a/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll b/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
new file mode 100644
index 0000000000000..3d3e45020bd8a
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
@@ -0,0 +1,14 @@
+; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 -target-abi=il32pc64 -mattr=+experimental-y | FileCheck %s --check-prefixes=CHECK,RV32
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -target-abi=l64pc128 -mattr=+experimental-y | FileCheck %s --check-prefixes=CHECK,RV64
+
+;; Previously crashed due to using pointer size rather than index size when computing the initializer of @b
+
+ at a = external addrspace(200) global i32
+ at b = addrspace(200) global iXLen ptrtoint (ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 8) to iXLen)
+
+; CHECK: .globl b
+; CHECK: b:
+; RV32-NEXT: .word a+8
+; RV32-NEXT: .size b, 4
+; RV64-NEXT: .quad a+8
+; RV64-NEXT: .size b, 8
More information about the llvm-commits
mailing list