Skip to content

[Transforms/Utils] Preserve alias metadata when converting calls to invokes - #233813

Open
connortsui20 wants to merge 1 commit into
llvm:mainfrom
connortsui20:ct/preserve-invoke-alias-metadata
Open

connortsui20 wants to merge 1 commit into
llvm:mainfrom
connortsui20:ct/preserve-invoke-alias-metadata

Conversation

@connortsui20

@connortsui20 connortsui20 commented Oct 20, 2026 •

Copy link
Copy Markdown

changeToInvokeAndSplitBasicBlock currently preserves only !prof. When inlining converts a call into an invoke, it drops alias scopes and loop-access metadata that remain valid on the same memory operation.

Preserve those attachments with an explicit list of metadata kinds valid on invokes. Copying all call metadata would also copy invalid attachments such as !tbaa and !noalias.addrspace. The regression checks inliner-generated alias scopes, explicit call-site metadata, the existing profile count, and exclusion of !noalias.addrspace.

AI assistance disclosure: I used LLMs to help with the investigation and implementation. I'm still getting familiar with LLVM.

…nvokes

Keep alias scopes and memory-access groups when `changeToInvokeAndSplitBasicBlock` replaces a call with an invoke. Inlining into an unwinding call site currently drops this information, which can leave the inlined memory accesses with weaker alias information than the ordinary-call path.

Signed-off-by: Connor Tsui <[email protected]>
@github-actions

Copy link
Copy Markdown

Hello @connortsui20 👋

Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.

  • All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
  • The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
  • Our LLVM Developer Policy describes our expectations for code quality, commit summaries and contains notes on our CI system.

Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.


Frequently asked questions

How do I add reviewers?

This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.

You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.

What if there are no comments?

If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.

Are any special GitHub settings required to contribute to LLVM?

We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.


If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.

Thank you,
The LLVM Community

@connortsui20

Copy link
Copy Markdown
Author

I have read the policies and have disclosed AI usage

@connortsui20
connortsui20 marked this pull request as ready for review October 20, 2026 12:33
@llvmorg-github-actions

Copy link
Copy Markdown

@llvm/pr-subscribers-llvm-transforms

Author: Connor Tsui (connortsui20)

Changes

changeToInvokeAndSplitBasicBlock currently preserves only !prof. When inlining converts a call into an invoke, it drops alias scopes and loop-access metadata that remain valid on the same memory operation.

Preserve those attachments with an explicit list of metadata kinds valid on invokes. Copying all call metadata would also copy invalid attachments such as !tbaa and !noalias.addrspace. The regression checks inliner-generated alias scopes, explicit call-site metadata, the existing profile count, and exclusion of !noalias.addrspace.

AI assistance disclosure: I used LLMs to help with the investigation and implementation. I'm still getting familiar with LLVM.


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

2 Files Affected:

  • (modified) llvm/lib/Transforms/Utils/Local.cpp (+4-1)
  • (added) llvm/test/Transforms/Inline/invoke-callsite-metadata.ll (+74)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 781138c2daffe..e119ce0b8de6f 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2655,7 +2655,10 @@ BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
   II->setDebugLoc(CI->getDebugLoc());
   II->setCallingConv(CI->getCallingConv());
   II->setAttributes(CI->getAttributes());
-  II->setMetadata(LLVMContext::MD_prof, CI->getMetadata(LLVMContext::MD_prof));
+  // Some call metadata, such as !tbaa, is not valid on invokes.
+  II->copyMetadata(*CI, {LLVMContext::MD_prof, LLVMContext::MD_alias_scope,
+                         LLVMContext::MD_noalias, LLVMContext::MD_access_group,
+                         LLVMContext::MD_mem_parallel_loop_access});
 
   if (DTU)
     DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}});
diff --git a/llvm/test/Transforms/Inline/invoke-callsite-metadata.ll b/llvm/test/Transforms/Inline/invoke-callsite-metadata.ll
new file mode 100644
index 0000000000000..49db41ec5fd2b
--- /dev/null
+++ b/llvm/test/Transforms/Inline/invoke-callsite-metadata.ll
@@ -0,0 +1,74 @@
+; RUN: opt -S -verify-each -passes=always-inline %s | FileCheck %s
+
+declare i64 @getter(ptr)
+declare void @opaque(ptr)
+declare i32 @__gxx_personality_v0(...)
+
+define internal i64 @map(ptr %source, ptr noalias %out) alwaysinline {
+  %value = call i64 @getter(ptr %source)
+  store i64 %value, ptr %out, align 8
+  ret i64 %value
+}
+
+define i64 @ordinary_call(ptr %source, ptr %out) {
+; CHECK-LABEL: define i64 @ordinary_call(
+; CHECK: call i64 @getter(ptr %source), !noalias ![[ORDINARY:[0-9]+]]
+; CHECK: store i64 {{.*}}, ptr %out, align 8, !alias.scope ![[ORDINARY]]
+  %value = call i64 @map(ptr %source, ptr %out)
+  ret i64 %value
+}
+
+define i64 @unwinding_call(ptr %source, ptr %out) personality ptr @__gxx_personality_v0 {
+; CHECK-LABEL: define i64 @unwinding_call(
+; CHECK: invoke i64 @getter(ptr %source)
+; CHECK-NEXT: to label {{.*}} unwind label %cleanup, !noalias ![[UNWINDING:[0-9]+]]
+; CHECK: store i64 {{.*}}, ptr %out, align 8, !alias.scope ![[UNWINDING]]
+entry:
+  %value = invoke i64 @map(ptr %source, ptr %out)
+      to label %exit unwind label %cleanup
+exit:
+  ret i64 %value
+cleanup:
+  %exception = landingpad { ptr, i32 } cleanup
+  resume { ptr, i32 } %exception
+}
+
+define internal void @explicit_metadata(ptr %source) alwaysinline {
+  call void @opaque(ptr %source), !prof !10, !alias.scope !0, !noalias !3, !llvm.mem.parallel_loop_access !7, !llvm.access.group !6, !noalias.addrspace !9
+  ret void
+}
+
+define void @unwinding_explicit_metadata(ptr %source) personality ptr @__gxx_personality_v0 {
+; CHECK-LABEL: define void @unwinding_explicit_metadata(
+; CHECK: invoke void @opaque(ptr %source)
+; CHECK-NEXT: to label {{.*}} unwind label %cleanup, !prof ![[PROF:[0-9]+]], !alias.scope ![[ALIAS:[0-9]+]], !noalias ![[NOALIAS:[0-9]+]], !llvm.mem.parallel_loop_access ![[PARALLEL:[0-9]+]], !llvm.access.group ![[ACCESS:[0-9]+]]{{$}}
+entry:
+  invoke void @explicit_metadata(ptr %source)
+      to label %exit unwind label %cleanup
+exit:
+  ret void
+cleanup:
+  %exception = landingpad { ptr, i32 } cleanup
+  resume { ptr, i32 } %exception
+}
+
+; CHECK: ![[PROF]] = !{!"branch_weights", i32 17}
+; CHECK: ![[ALIAS]] = !{![[ALIAS_SCOPE:[0-9]+]]}
+; CHECK: ![[ALIAS_SCOPE]] = distinct !{![[ALIAS_SCOPE]], !{{[0-9]+}}, !"scope"}
+; CHECK: ![[NOALIAS]] = !{![[NOALIAS_SCOPE:[0-9]+]]}
+; CHECK: ![[NOALIAS_SCOPE]] = distinct !{![[NOALIAS_SCOPE]], !{{[0-9]+}}, !"noalias"}
+; CHECK: ![[PARALLEL]] = !{![[LOOP:[0-9]+]]}
+; CHECK: ![[LOOP]] = distinct !{![[LOOP]]}
+; CHECK: ![[ACCESS]] = distinct !{}
+
+!0 = !{!1}
+!1 = distinct !{!1, !2, !"scope"}
+!2 = distinct !{!2, i1 false, !"domain"}
+!3 = !{!4}
+!4 = distinct !{!4, !5, !"noalias"}
+!5 = distinct !{!5, i1 false, !"noalias domain"}
+!6 = distinct !{}
+!7 = !{!8}
+!8 = distinct !{!8}
+!9 = !{i32 1, i32 2}
+!10 = !{!"branch_weights", i32 17}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant