From 010f6ec368261b6eb678858323f90e78ac76a19d Mon Sep 17 00:00:00 2001 From: ZhouGuangyuan Date: Sat, 1 Aug 2026 16:05:29 +0800 Subject: [PATCH] SROA: preserve load annotations --- llvm/lib/Transforms/Scalar/SROA.cpp | 6 ++++-- llvm/lib/Transforms/Utils/Local.cpp | 1 + .../SROA/preserve-load-annotation.ll | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/SROA/preserve-load-annotation.ll diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index bae6e766c5ee3..c19af7c68cf55 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -3323,7 +3323,8 @@ class AllocaSliceRewriter : public InstVisitor { LoadInst *Load = IRB.CreateAlignedLoad(NewAllocaTy, &NewAI, NewAI.getAlign(), "load"); - Load->copyMetadata(LI, {LLVMContext::MD_mem_parallel_loop_access, + Load->copyMetadata(LI, {LLVMContext::MD_annotation, + LLVMContext::MD_mem_parallel_loop_access, LLVMContext::MD_access_group}); return extractVector(IRB, Load, BeginIndex, EndIndex, "vec"); } @@ -3420,7 +3421,8 @@ class AllocaSliceRewriter : public InstVisitor { if (LI.isVolatile()) NewLI->setAtomic(LI.getOrdering(), LI.getSyncScopeID()); - NewLI->copyMetadata(LI, {LLVMContext::MD_mem_parallel_loop_access, + NewLI->copyMetadata(LI, {LLVMContext::MD_annotation, + LLVMContext::MD_mem_parallel_loop_access, LLVMContext::MD_access_group}); V = NewLI; diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp index b17740c0bc192..af64ee85fb178 100644 --- a/llvm/lib/Transforms/Utils/Local.cpp +++ b/llvm/lib/Transforms/Utils/Local.cpp @@ -3151,6 +3151,7 @@ void llvm::copyMetadataForLoad(LoadInst &Dest, const LoadInst &Source) { case LLVMContext::MD_noalias: case LLVMContext::MD_nontemporal: case LLVMContext::MD_mem_cache_hint: + case LLVMContext::MD_annotation: case LLVMContext::MD_mem_parallel_loop_access: case LLVMContext::MD_access_group: case LLVMContext::MD_noundef: diff --git a/llvm/test/Transforms/SROA/preserve-load-annotation.ll b/llvm/test/Transforms/SROA/preserve-load-annotation.ll new file mode 100644 index 0000000000000..525eb80b56d85 --- /dev/null +++ b/llvm/test/Transforms/SROA/preserve-load-annotation.ll @@ -0,0 +1,18 @@ +; RUN: opt -passes=sroa -S < %s | FileCheck %s + +; SROA may rewrite a load to use the type and alignment of the promoted alloca. +; Preserve standard annotation metadata on the replacement instruction. +define i8 @volatile_slice_load(ptr %value) { +; CHECK-LABEL: define i8 @volatile_slice_load( +; CHECK: %[[SLOT:.*]] = alloca ptr, align 8 +; CHECK: %[[RESULT:.*]] = load volatile i8, ptr %[[SLOT]], align 8, !annotation ![[ANNOT:[0-9]+]] +; CHECK: ret i8 %[[RESULT]] +entry: + %slot = alloca ptr, align 8 + store ptr %value, ptr %slot, align 8 + %result = load volatile i8, ptr %slot, align 1, !annotation !0 + ret i8 %result +} + +; CHECK: ![[ANNOT]] = !{!"test.annotation"} +!0 = !{!"test.annotation"}