[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 03:53:56 PDT 2026
https://github.com/resistor updated https://github.com/llvm/llvm-project/pull/220100
>From 478d16fe50f2785e93da079d75567972377a6c16 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 1/3] [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 +-
.../CodeGen/RISCV/cheri-global-cap-init.ll | 19 +++++++++++++++++++
2 files changed, 20 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 3d06677d1706bc..2be36a09fd0bb2 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3956,7 +3956,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 00000000000000..13fd76424a6dfa
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
@@ -0,0 +1,19 @@
+; RUN: llc -mtriple=riscv32 --relocation-model=pic -target-abi il32pc64f -mattr=+experimental-y,+f -relocation-model=static %s -o -
+; RUN: llc -mtriple=riscv32 --relocation-model=pic -target-abi il32pc64f -mattr=+experimental-y,+f %s -o -
+; RUN: llc -mtriple=riscv32 --relocation-model=pic -target-abi il32pc64f -mattr=+experimental-y,+f -filetype=obj %s -o -
+
+; Ported from CodeGen/CHERI-Generic/Inputs/cheri-global-cap-init.ll in CHERI downstreams.
+
+; FIXME: Add RV64 RUN lines and asm/disasm checks once .chericap directives are supported.
+
+ at a = common addrspace(200) global [5 x i32] zeroinitializer, align 4
+ at b = addrspace(200) global [3 x ptr addrspace(200)] [
+ ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 8),
+ ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 4),
+ ptr addrspace(200) @a
+ ], align 32
+ at c = addrspace(200) constant [3 x ptr addrspace(200)] [
+ ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 16),
+ ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 12),
+ ptr addrspace(200) @a
+ ], align 32
>From defd35af70c3e1ce40084ca4d525125c0d7e008f Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Tue, 1 Sep 2026 13:53:14 -0500
Subject: [PATCH 2/3] Minimize test to only exercise this assertion, and remove
the need for capability relocation emission so that RV64 can be included as
well.
---
.../CodeGen/RISCV/cheri-global-cap-init.ll | 24 ++++++-------------
1 file changed, 7 insertions(+), 17 deletions(-)
diff --git a/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll b/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
index 13fd76424a6dfa..69319a8164a6c5 100644
--- a/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
+++ b/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
@@ -1,19 +1,9 @@
-; RUN: llc -mtriple=riscv32 --relocation-model=pic -target-abi il32pc64f -mattr=+experimental-y,+f -relocation-model=static %s -o -
-; RUN: llc -mtriple=riscv32 --relocation-model=pic -target-abi il32pc64f -mattr=+experimental-y,+f %s -o -
-; RUN: llc -mtriple=riscv32 --relocation-model=pic -target-abi il32pc64f -mattr=+experimental-y,+f -filetype=obj %s -o -
+; RUN: llc -mtriple=riscv32 -target-abi il32pc64 -mattr=+experimental-y %s -o /dev/null
+; RUN: llc -mtriple=riscv32 -target-abi il32pc64 -mattr=+experimental-y -filetype=obj %s -o /dev/null
+; RUN: llc -mtriple=riscv64 -target-abi l64pc128 -mattr=+experimental-y %s -o /dev/null
+; RUN: llc -mtriple=riscv64 -target-abi l64pc128 -mattr=+experimental-y -filetype=obj %s -o /dev/null
-; Ported from CodeGen/CHERI-Generic/Inputs/cheri-global-cap-init.ll in CHERI downstreams.
+; Previously crashed due to using pointer size rather than index size when computing the initializer of @b
-; FIXME: Add RV64 RUN lines and asm/disasm checks once .chericap directives are supported.
-
- at a = common addrspace(200) global [5 x i32] zeroinitializer, align 4
- at b = addrspace(200) global [3 x ptr addrspace(200)] [
- ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 8),
- ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 4),
- ptr addrspace(200) @a
- ], align 32
- at c = addrspace(200) constant [3 x ptr addrspace(200)] [
- ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 16),
- ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 12),
- ptr addrspace(200) @a
- ], align 32
+ at a = external addrspace(200) global i32
+ at b = addrspace(200) global i32 ptrtoint (ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 8) to i32)
>From bd087910e867533cf90f04a8035c0765112be9a1 Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Mon, 14 Sep 2026 16:16:20 +0200
Subject: [PATCH 3/3] Update testcase based on feedback.
---
.../CodeGen/RISCV/cheri-global-cap-init.ll | 22 ++++++++++++++-----
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll b/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
index 69319a8164a6c5..a788458d964a23 100644
--- a/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
+++ b/llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll
@@ -1,9 +1,19 @@
-; RUN: llc -mtriple=riscv32 -target-abi il32pc64 -mattr=+experimental-y %s -o /dev/null
-; RUN: llc -mtriple=riscv32 -target-abi il32pc64 -mattr=+experimental-y -filetype=obj %s -o /dev/null
-; RUN: llc -mtriple=riscv64 -target-abi l64pc128 -mattr=+experimental-y %s -o /dev/null
-; RUN: llc -mtriple=riscv64 -target-abi l64pc128 -mattr=+experimental-y -filetype=obj %s -o /dev/null
+; RUN: sed 's/iXLen/i32/g' %s | llc -mtriple=riscv32 -target-abi il32pc64 -mattr=+experimental-y -o - | FileCheck %s --check-prefixes=CHECK,RV32
+; RUN: sed 's/iXLen/i64/g' %s | llc -mtriple=riscv64 -target-abi l64pc128 -mattr=+experimental-y -o - | FileCheck %s --check-prefixes=CHECK,RV64
-; Previously crashed due to using pointer size rather than index size when computing the initializer of @b
+;; Previously crashed due to using pointer size rather than index size when computing the initializer of @b
@a = external addrspace(200) global i32
- at b = addrspace(200) global i32 ptrtoint (ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 8) to i32)
+ at b = addrspace(200) global iXLen ptrtoint (ptr addrspace(200) getelementptr (i8, ptr addrspace(200) @a, i64 8) to iXLen)
+
+; CHECK: .p2align 0, 0x0
+; CHECK-NEXT: .type b, at object
+; CHECK-NEXT: .data
+; CHECK-NEXT: .globl b
+; RV32-NEXT: .p2align 2, 0x0
+; RV64-NEXT: .p2align 3, 0x0
+; CHECK-NEXT: 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