[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 Aug 31 07:31:51 PDT 2026
https://github.com/resistor updated https://github.com/llvm/llvm-project/pull/218189
>From d17c0ac5e88a65b11a24469ad939cfbbb54452df 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 1/2] [CHERI][RISCV] Do not attempt to pass in an alignment
granule for globals with explicit sections.
This was already implied to be the case in the comments of emitGlobalVariable(GV, AlignmentGranule),
but it is the caller's responsibility to ensure that this is handled properly. As such the
emitGlobalVariable(GV) caller needs to enforce this requirement.
---
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 4 +++-
llvm/test/CodeGen/RISCV/cheri-global-bounds.ll | 13 ++++++++++---
2 files changed, 13 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index cbd8f1932a285..99981f420b8e3 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -788,7 +788,9 @@ MCSymbol *AsmPrinter::getSymbolPreferLocal(const GlobalValue &GV) const {
/// EmitGlobalVariable - Emit the specified global variable to the .s file.
void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
- MaybeAlign AlignmentGranule = getRequiredGlobalAlignmentGranule(*GV);
+ MaybeAlign AlignmentGranule = std::nullopt;
+ if (!GV->hasSection())
+ AlignmentGranule = getRequiredGlobalAlignmentGranule(*GV);
emitGlobalVariable(GV, AlignmentGranule);
if (AlignmentGranule)
OutStreamer->emitValueToAlignment(*AlignmentGranule);
diff --git a/llvm/test/CodeGen/RISCV/cheri-global-bounds.ll b/llvm/test/CodeGen/RISCV/cheri-global-bounds.ll
index 0443338855e62..5681877341301 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,10 @@
; CHERIOT-NEXT: .zero 6995
; CHERIOT-NEXT: .size global1, 7008
; CHERIOT: .p2align 4, 0x0
+
+ 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
>From 606dfdef481cd91cf88e4380a8c28c43250c40ac Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Mon, 31 Aug 2026 09:31:01 -0500
Subject: [PATCH 2/2] Sink handling into the target hook, and carry over
downstream comment.
---
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 4 +---
llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp | 5 +++++
2 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 99981f420b8e3..cbd8f1932a285 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -788,9 +788,7 @@ MCSymbol *AsmPrinter::getSymbolPreferLocal(const GlobalValue &GV) const {
/// EmitGlobalVariable - Emit the specified global variable to the .s file.
void AsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
- MaybeAlign AlignmentGranule = std::nullopt;
- if (!GV->hasSection())
- AlignmentGranule = getRequiredGlobalAlignmentGranule(*GV);
+ MaybeAlign AlignmentGranule = getRequiredGlobalAlignmentGranule(*GV);
emitGlobalVariable(GV, AlignmentGranule);
if (AlignmentGranule)
OutStreamer->emitValueToAlignment(*AlignmentGranule);
diff --git a/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp b/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
index 6771e9d7e7fa1..cd091c49f32a4 100644
--- a/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
+++ b/llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp
@@ -1333,6 +1333,11 @@ 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.
+ if (GV.hasSection())
+ return std::nullopt;
+
uint64_t Size = GV.getGlobalSize(getDataLayout());
if (MCSTI.hasFeature(RISCV::FeatureVendorXCheriot))
return CHERIoTCapabilityFormat::getRequiredAlignment(Size);
More information about the llvm-commits
mailing list