[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
Tue Sep 1 12:55:01 PDT 2026


https://github.com/resistor updated https://github.com/llvm/llvm-project/pull/218189

>From 8a7f1b0730af76df0a0b9cc3ea0255687747e3d2 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 3d06677d1706b..5f273f3b992fc 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 47e827ee8db18b0a8e9a105a1fce05bc3a4fc7c5 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 5f273f3b992fc..3d06677d1706b 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