[llvm] [CHERI][RISCV][AsmPrinter] Use pointer index size rather than pointer size in AsmPrinter constant lowering. (PR #220100)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 31 14:51:39 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-risc-v

Author: Owen Anderson (resistor)

<details>
<summary>Changes</summary>

This prevents a crash due to APInt width mismatches during accumulateConstantOffset for CHERI targets.


---
Full diff: https://github.com/llvm/llvm-project/pull/220100.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+1-1) 
- (added) llvm/test/CodeGen/RISCV/cheri-global-cap-init.ll (+19) 


``````````diff
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 3d06677d1706b..2be36a09fd0bb 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 0000000000000..74972e955c5f3
--- /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
+
+; 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

``````````

</details>


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


More information about the llvm-commits mailing list