[llvm] [CHERI][RISCV] Do not attempt to pass in an alignment granule for globals with explicit sections. (PR #218189)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 22 21:01:36 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 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.


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


2 Files Affected:

- (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+3-1) 
- (modified) llvm/test/CodeGen/RISCV/cheri-global-bounds.ll (+10-3) 


``````````diff
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

``````````

</details>


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


More information about the llvm-commits mailing list