[llvm] [CHERI][RISCV] Do not attempt to pass in an alignment granule for globals with explicit sections. (PR #218189)
Owen Anderson via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 14 05:03:19 PDT 2026
https://github.com/resistor updated https://github.com/llvm/llvm-project/pull/218189
>From 301c46f1465665619fd6c66ef0dd51577d87a697 Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Sat, 22 Aug 2026 22:58:24 -0500
Subject: [PATCH] [CHERI][RISCV] Do not attempt to pass in an alignment granule
for globals with explicit sections.
This can break existing code where that depends on the lack of padding between variables, e.g. linker sets.
---
llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp | 7 +++++++
llvm/test/CodeGen/RISCV/cheri-global-bounds.ll | 14 +++++++++++---
2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp b/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
index fba93dede0d02..b535404d25c1f 100644
--- a/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
+++ b/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
@@ -1344,6 +1344,13 @@ RISCVAsmPrinter::getRequiredGlobalAlignmentGranule(const GlobalVariable &GV) {
if (!GV.getValueType()->isSized())
return std::nullopt;
+ // When the alignment granule is determined by a CHERI requirement,
+ // don't increase alignment if a custom section has been specified,
+ // as doing so can break existing code that relies on the lack of
+ // padding (e.g. linker sets).
+ if (GV.hasSection())
+ return std::nullopt;
+
uint64_t Size = GV.getGlobalSize(getDataLayout());
if (MCSTI.hasFeature(RISCV::FeatureVendorXCheriot))
return CHERIoTCapabilityFormat::getRequiredAlignment(Size);
diff --git a/llvm/test/CodeGen/RISCV/cheri-global-bounds.ll b/llvm/test/CodeGen/RISCV/cheri-global-bounds.ll
index 0443338855e62..29bab454b222a 100644
--- a/llvm/test/CodeGen/RISCV/cheri-global-bounds.ll
+++ b/llvm/test/CodeGen/RISCV/cheri-global-bounds.ll
@@ -1,6 +1,6 @@
-; RUN: llc -mtriple riscv32 -mattr=+experimental-y -target-abi il32pc64 %s -o - | FileCheck -check-prefix=RVY32 %s
-; RUN: llc -mtriple riscv64 -mattr=+experimental-y -target-abi l64pc128 %s -o - | FileCheck -check-prefix=RVY64 %s
-; RUN: llc -mtriple riscv64 -mattr=+xcheriot -target-abi cheriot %s -o - | FileCheck -check-prefix=CHERIOT %s
+; RUN: llc -mtriple riscv32 -mattr=+experimental-y -target-abi il32pc64 %s -o - | FileCheck -check-prefix=RVY32,COMMON %s
+; RUN: llc -mtriple riscv64 -mattr=+experimental-y -target-abi l64pc128 %s -o - | FileCheck -check-prefix=RVY64,COMMON %s
+; RUN: llc -mtriple riscv64 -mattr=+xcheriot -target-abi cheriot %s -o - | FileCheck -check-prefix=CHERIOT,COMMON %s
@global1 = global [6995 x i8] zeroinitializer, align 1
@@ -24,3 +24,11 @@
; CHERIOT-NEXT: .zero 6995
; CHERIOT-NEXT: .size global1, 7008
; CHERIOT: .p2align 4, 0x0
+
+;; No alignment padding here since it has a custom section defined.
+ at global_in_section = global [6995 x i8] zeroinitializer, section "foo", align 1
+
+; COMMON-LABEL: .globl global_in_section
+; COMMON-NEXT: global_in_section:
+; COMMON-NEXT: .zero 6995
+; COMMON-NEXT: .size global_in_section, 6995
More information about the llvm-commits
mailing list