[clang] [llvm] [Inliner] Propagate inlined.from metadata through intermediate calls (PR #248805)

via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 3 22:19:35 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Jaeyong Lee (npmania)

<details>
<summary>Changes</summary>

PropagateInlinedFromMetadata() currently only attaches !inlined.from to direct calls to dontcall-error/dontcall-warn functions. When an outer wrapper is inlined before an inner callee (e.g. via always_inline), the intermediate call is skipped, dropping the outer frame and misattributing its source location.

This propagates !inlined.from to inlinable callees whenever the call site or callee call carries !srcloc or !inlined.from, and merges the call site's existing !inlined.from chain when the intermediate call is later inlined.

This was exposed by https://github.com/llvm/llvm-project/pull/248097, where early single-call inlining inlines outer wrappers before inner multi-call functions.

LLM used for test cases.

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


3 Files Affected:

- (modified) clang/test/Frontend/backend-attribute-inlining.c (+13) 
- (modified) llvm/lib/Transforms/Utils/InlineFunction.cpp (+57-20) 
- (added) llvm/test/Transforms/Inline/inlined-from-metadata.ll (+312) 


``````````diff
diff --git a/clang/test/Frontend/backend-attribute-inlining.c b/clang/test/Frontend/backend-attribute-inlining.c
index 734ce0fc30bba2..a2be17a6e7e240 100644
--- a/clang/test/Frontend/backend-attribute-inlining.c
+++ b/clang/test/Frontend/backend-attribute-inlining.c
@@ -108,5 +108,18 @@ void test_incidental(void) { not_marked_inline(); }
 // CHECK: note: inlined by function 'test_incidental'
 // CHECK-NOT: :{{.*}}: note: inlined by function 'test_incidental'
 
+// Outer always_inline inlined before inner function.
+[[gnu::warning("always_inline outer")]]
+void __warn_always_outer(void);
+
+static inline void ai_inner(void) { __warn_always_outer(); }
+[[gnu::always_inline]] static void ai_middle(void) { ai_inner(); }
+
+void test_always_inline_outer(void) { ai_middle(); }
+// CHECK: warning: call to '__warn_always_outer' declared with 'warning' attribute: always_inline outer
+// CHECK: note: called by function 'ai_inner'
+// CHECK: :116:{{.*}}: note: inlined by function 'ai_middle'
+// CHECK: :118:{{.*}}: note: inlined by function 'test_always_inline_outer'
+
 // Fallback note should appear (no debug info).
 // CHECK: note: use '-gline-directives-only' (implied by '-g1') or higher for more accurate inlining chain locations
diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp
index 8ddac9c2c73771..6e7c44f2ef6ff5 100644
--- a/llvm/lib/Transforms/Utils/InlineFunction.cpp
+++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp
@@ -1054,36 +1054,73 @@ static void PropagateInlinedFromMetadata(CallBase &CB, StringRef CalledFuncName,
                                          Function::iterator FStart,
                                          Function::iterator FEnd) {
   LLVMContext &Ctx = CB.getContext();
-  uint64_t InlineSiteLoc = 0;
-  if (auto *MD = CB.getMetadata("srcloc"))
-    if (auto *CI = mdconst::dyn_extract<ConstantInt>(MD->getOperand(0)))
-      InlineSiteLoc = CI->getZExtValue();
-
-  auto *I64Ty = Type::getInt64Ty(Ctx);
-  auto MakeMDInt = [&](uint64_t V) {
-    return ConstantAsMetadata::get(ConstantInt::get(I64Ty, V));
-  };
+  MDNode *CBSrcLoc = nullptr;
+  MDNode *CBInlinedFrom = nullptr;
+  if (CB.hasMetadataOtherThanDebugLoc()) {
+    CBSrcLoc = CB.getMetadata("srcloc");
+    CBInlinedFrom = CB.getMetadata("inlined.from");
+  }
+  bool CBHasChain = CBSrcLoc || CBInlinedFrom;
 
+  SmallVector<Metadata *, 8> CBSuffix;
   for (BasicBlock &BB : make_range(FStart, FEnd)) {
     for (Instruction &I : BB) {
-      auto *CI = dyn_cast<CallInst>(&I);
-      if (!CI || !CI->getMetadata("srcloc"))
+      auto *ICB = dyn_cast<CallBase>(&I);
+      if (!ICB)
+        continue;
+      if (!CBHasChain && !ICB->hasMetadataOtherThanDebugLoc())
         continue;
-      auto *Callee = CI->getCalledFunction();
-      if (!Callee || (!Callee->hasFnAttribute("dontcall-error") &&
-                      !Callee->hasFnAttribute("dontcall-warn")))
+      auto *Callee = ICB->getCalledFunction();
+      if (!Callee || Callee->isIntrinsic())
         continue;
 
+      MDNode *Existing = nullptr;
+      bool HasSrcLoc = false;
+      if (ICB->hasMetadataOtherThanDebugLoc()) {
+        Existing = ICB->getMetadata("inlined.from");
+        HasSrcLoc = ICB->getMetadata("srcloc") != nullptr;
+      }
+      if (!HasSrcLoc && !Existing && !CBHasChain)
+        continue;
+
+      if (Callee->isDeclaration() || ICB->isNoInline() ||
+          Callee->isInterposable(/*CheckNoIPA=*/false)) {
+        if (!HasSrcLoc || (!Callee->hasFnAttribute("dontcall-error") &&
+                           !Callee->hasFnAttribute("dontcall-warn")))
+          continue;
+      }
+
+      Type *I64Ty = Type::getInt64Ty(Ctx);
+      if (CBSuffix.empty()) {
+        uint64_t InlineSiteLoc = 0;
+        if (CBSrcLoc && CBSrcLoc->getNumOperands() > 0)
+          if (auto *LocCI = mdconst::dyn_extract_or_null<ConstantInt>(
+                  CBSrcLoc->getOperand(0)))
+            InlineSiteLoc = LocCI->getZExtValue();
+
+        auto *LocMD =
+            ConstantAsMetadata::get(ConstantInt::get(I64Ty, InlineSiteLoc));
+        // CB's existing chain starts with (OrigFn, 0); fill in OrigFn's
+        // location with CB's srcloc so chains compose across inline orders.
+        if (CBInlinedFrom && CBInlinedFrom->getNumOperands() >= 2) {
+          CBSuffix.push_back(CBInlinedFrom->getOperand(0));
+          CBSuffix.push_back(LocMD);
+          append_range(CBSuffix, drop_begin(CBInlinedFrom->operands(), 2));
+        } else {
+          CBSuffix.push_back(MDString::get(Ctx, CallerFuncName));
+          CBSuffix.push_back(LocMD);
+        }
+      }
+
       SmallVector<Metadata *, 8> Ops;
-      if (MDNode *Existing = CI->getMetadata("inlined.from"))
+      if (Existing) {
         append_range(Ops, Existing->operands());
-      else {
+      } else {
         Ops.push_back(MDString::get(Ctx, CalledFuncName));
-        Ops.push_back(MakeMDInt(0));
+        Ops.push_back(ConstantAsMetadata::get(ConstantInt::get(I64Ty, 0)));
       }
-      Ops.push_back(MDString::get(Ctx, CallerFuncName));
-      Ops.push_back(MakeMDInt(InlineSiteLoc));
-      CI->setMetadata("inlined.from", MDNode::get(Ctx, Ops));
+      append_range(Ops, CBSuffix);
+      ICB->setMetadata("inlined.from", MDNode::get(Ctx, Ops));
     }
   }
 }
diff --git a/llvm/test/Transforms/Inline/inlined-from-metadata.ll b/llvm/test/Transforms/Inline/inlined-from-metadata.ll
new file mode 100644
index 00000000000000..84482b5a4e49dd
--- /dev/null
+++ b/llvm/test/Transforms/Inline/inlined-from-metadata.ll
@@ -0,0 +1,312 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -passes=always-inline -S %s | FileCheck %s --check-prefixes=CHECK,ALWAYS --implicit-check-not=!inlined.from
+; RUN: opt -passes='always-inline,cgscc(inline)' -S %s | FileCheck %s --check-prefixes=CHECK,FULL --implicit-check-not=!inlined.from
+
+declare void @warn_fn() "dontcall-warn"
+declare void @ext_decl()
+declare i32 @__gxx_personality_v0(...)
+
+define internal void @bottom_up_inner() {
+; ALWAYS-LABEL: define internal void @bottom_up_inner() {
+; ALWAYS-NEXT:    call void @warn_fn(), !srcloc [[META0:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+  call void @warn_fn(), !srcloc !0
+  ret void
+}
+
+define internal void @bottom_up_mid() {
+; ALWAYS-LABEL: define internal void @bottom_up_mid() {
+; ALWAYS-NEXT:    call void @bottom_up_inner(), !srcloc [[META1:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+  call void @bottom_up_inner(), !srcloc !1
+  ret void
+}
+
+define void @test_bottom_up() {
+; ALWAYS-LABEL: define void @test_bottom_up() {
+; ALWAYS-NEXT:    call void @bottom_up_mid(), !srcloc [[META2:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_bottom_up() {
+; FULL-NEXT:    call void @warn_fn(), !srcloc [[META0:![0-9]+]], !inlined.from [[META1:![0-9]+]]
+; FULL-NEXT:    ret void
+;
+  call void @bottom_up_mid(), !srcloc !2
+  ret void
+}
+
+define internal void @top_down_inner() {
+; ALWAYS-LABEL: define internal void @top_down_inner() {
+; ALWAYS-NEXT:    call void @warn_fn(), !srcloc [[META0]]
+; ALWAYS-NEXT:    ret void
+;
+  call void @warn_fn(), !srcloc !0
+  ret void
+}
+
+define internal void @top_down_mid() alwaysinline {
+  call void @top_down_inner(), !srcloc !1
+  ret void
+}
+
+define void @test_top_down() {
+; ALWAYS-LABEL: define void @test_top_down() {
+; ALWAYS-NEXT:    call void @top_down_inner(), !srcloc [[META1]], !inlined.from [[META3:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_top_down() {
+; FULL-NEXT:    call void @warn_fn(), !srcloc [[META0]], !inlined.from [[META2:![0-9]+]]
+; FULL-NEXT:    ret void
+;
+  call void @top_down_mid(), !srcloc !2
+  ret void
+}
+
+define internal void @middle_out_1() alwaysinline {
+  call void @warn_fn(), !srcloc !0
+  ret void
+}
+
+define internal void @middle_out_2() {
+; ALWAYS-LABEL: define internal void @middle_out_2() {
+; ALWAYS-NEXT:    call void @warn_fn(), !srcloc [[META0]], !inlined.from [[META4:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+  call void @middle_out_1(), !srcloc !1
+  ret void
+}
+
+define internal void @middle_out_3() alwaysinline {
+  call void @middle_out_2(), !srcloc !2
+  ret void
+}
+
+define void @test_middle_out() {
+; ALWAYS-LABEL: define void @test_middle_out() {
+; ALWAYS-NEXT:    call void @middle_out_2(), !srcloc [[META2]], !inlined.from [[META5:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_middle_out() {
+; FULL-NEXT:    call void @warn_fn(), !srcloc [[META0]], !inlined.from [[META3:![0-9]+]]
+; FULL-NEXT:    ret void
+;
+  call void @middle_out_3(), !srcloc !3
+  ret void
+}
+
+define internal void @untracked_mid_inner() {
+; ALWAYS-LABEL: define internal void @untracked_mid_inner() {
+; ALWAYS-NEXT:    call void @warn_fn(), !srcloc [[META0]]
+; ALWAYS-NEXT:    ret void
+;
+  call void @warn_fn(), !srcloc !0
+  ret void
+}
+
+define internal void @untracked_mid_outer() alwaysinline {
+  call void @untracked_mid_inner()
+  ret void
+}
+
+define void @test_untracked_mid() {
+; ALWAYS-LABEL: define void @test_untracked_mid() {
+; ALWAYS-NEXT:    call void @untracked_mid_inner(), !inlined.from [[META6:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_untracked_mid() {
+; FULL-NEXT:    call void @warn_fn(), !srcloc [[META0]], !inlined.from [[META4:![0-9]+]]
+; FULL-NEXT:    ret void
+;
+  call void @untracked_mid_outer(), !srcloc !4
+  ret void
+}
+
+define internal void @untracked_outer_inner() {
+; ALWAYS-LABEL: define internal void @untracked_outer_inner() {
+; ALWAYS-NEXT:    call void @warn_fn(), !srcloc [[META0]]
+; ALWAYS-NEXT:    ret void
+;
+  call void @warn_fn(), !srcloc !0
+  ret void
+}
+
+define internal void @untracked_outer_mid() alwaysinline {
+  call void @untracked_outer_inner(), !srcloc !1
+  ret void
+}
+
+define void @test_untracked_outer() {
+; ALWAYS-LABEL: define void @test_untracked_outer() {
+; ALWAYS-NEXT:    call void @untracked_outer_inner(), !srcloc [[META1]], !inlined.from [[META7:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_untracked_outer() {
+; FULL-NEXT:    call void @warn_fn(), !srcloc [[META0]], !inlined.from [[META5:![0-9]+]]
+; FULL-NEXT:    ret void
+;
+  call void @untracked_outer_mid()
+  ret void
+}
+
+define internal void @invoke_inner() {
+; ALWAYS-LABEL: define internal void @invoke_inner() {
+; ALWAYS-NEXT:    call void @warn_fn() #[[ATTR3:[0-9]+]], !srcloc [[META0]]
+; ALWAYS-NEXT:    ret void
+;
+  call void @warn_fn() nounwind, !srcloc !0
+  ret void
+}
+
+define internal void @invoke_mid() alwaysinline personality ptr @__gxx_personality_v0 {
+entry:
+  invoke void @invoke_inner()
+  to label %cont unwind label %lpad, !srcloc !1
+
+cont:
+  ret void
+
+lpad:
+  %lp = landingpad { ptr, i32 } cleanup
+  resume { ptr, i32 } %lp
+}
+
+define void @test_invoke() {
+; ALWAYS-LABEL: define void @test_invoke() personality ptr @__gxx_personality_v0 {
+; ALWAYS-NEXT:    invoke void @invoke_inner()
+; ALWAYS-NEXT:            to label %[[INVOKE_MID_EXIT:.*]] unwind label %[[LPAD_I:.*]], !srcloc [[META1]], !inlined.from [[META8:![0-9]+]]
+; ALWAYS:       [[LPAD_I]]:
+; ALWAYS-NEXT:    [[LP_I:%.*]] = landingpad { ptr, i32 }
+; ALWAYS-NEXT:            cleanup
+; ALWAYS-NEXT:    resume { ptr, i32 } [[LP_I]]
+; ALWAYS:       [[INVOKE_MID_EXIT]]:
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_invoke() personality ptr @__gxx_personality_v0 {
+; FULL-NEXT:    call void @warn_fn() #[[ATTR3:[0-9]+]], !srcloc [[META0]], !inlined.from [[META6:![0-9]+]]
+; FULL-NEXT:    br label %[[INVOKE_MID_EXIT:.*]]
+; FULL:       [[LPAD_I:.*:]]
+; FULL-NEXT:    [[LP_I:%.*]] = landingpad { ptr, i32 }
+; FULL-NEXT:            cleanup
+; FULL-NEXT:    resume { ptr, i32 } [[LP_I]]
+; FULL:       [[INVOKE_MID_EXIT]]:
+; FULL-NEXT:    ret void
+;
+  call void @invoke_mid(), !srcloc !2
+  ret void
+}
+
+define internal void @dontcall_def_wrap() alwaysinline {
+  call void @warn_def(), !srcloc !0
+  ret void
+}
+
+define void @test_dontcall_def() {
+; ALWAYS-LABEL: define void @test_dontcall_def() {
+; ALWAYS-NEXT:    call void @warn_def(), !srcloc [[META0]], !inlined.from [[META9:![0-9]+]]
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_dontcall_def() {
+; FULL-NEXT:    call void @warn_def(), !srcloc [[META0]], !inlined.from [[META7:![0-9]+]]
+; FULL-NEXT:    ret void
+;
+  call void @dontcall_def_wrap(), !srcloc !1
+  ret void
+}
+
+define internal void @skip_callee() alwaysinline {
+  call void @ext_decl(), !srcloc !0
+  call void asm sideeffect "", ""(), !srcloc !0
+  call void @noinline_fn(), !srcloc !0
+  call void @normal_def() noinline, !srcloc !0
+  call void @weak_fn(), !srcloc !0
+  ret void
+}
+
+define void @test_skip_non_inlinable() {
+; CHECK-LABEL: define void @test_skip_non_inlinable() {
+; CHECK-NEXT:    call void @ext_decl(), !srcloc [[META0:![0-9]+]]
+; CHECK-NEXT:    call void asm sideeffect "", ""(), !srcloc [[META0]]
+; CHECK-NEXT:    call void @noinline_fn(), !srcloc [[META0]]
+; CHECK-NEXT:    call void @normal_def() #[[ATTR1:[0-9]+]], !srcloc [[META0]]
+; CHECK-NEXT:    call void @weak_fn(), !srcloc [[META0]]
+; CHECK-NEXT:    ret void
+;
+  call void @skip_callee(), !srcloc !0
+  ret void
+}
+
+define internal void @skip_untracked_callee() alwaysinline {
+  call void @normal_def()
+  ret void
+}
+
+define void @test_skip_untracked() {
+; ALWAYS-LABEL: define void @test_skip_untracked() {
+; ALWAYS-NEXT:    call void @normal_def()
+; ALWAYS-NEXT:    ret void
+;
+; FULL-LABEL: define void @test_skip_untracked() {
+; FULL-NEXT:    ret void
+;
+  call void @skip_untracked_callee()
+  ret void
+}
+
+define void @noinline_fn() noinline {
+; CHECK-LABEL: define void @noinline_fn(
+; CHECK-SAME: ) #[[ATTR1]] {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define void @normal_def() {
+; CHECK-LABEL: define void @normal_def() {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define weak void @weak_fn() {
+; CHECK-LABEL: define weak void @weak_fn() {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define void @warn_def() noinline "dontcall-warn" {
+; CHECK-LABEL: define void @warn_def(
+; CHECK-SAME: ) #[[ATTR2:[0-9]+]] {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+!0 = !{i64 10}
+!1 = !{i64 20}
+!2 = !{i64 30}
+!3 = !{i64 40}
+!4 = !{i64 50}
+;.
+; ALWAYS: [[META0]] = !{i64 10}
+; ALWAYS: [[META1]] = !{i64 20}
+; ALWAYS: [[META2]] = !{i64 30}
+; ALWAYS: [[META3]] = !{!"top_down_mid", i64 0, !"test_top_down", i64 30}
+; ALWAYS: [[META4]] = !{!"middle_out_1", i64 0, !"middle_out_2", i64 20}
+; ALWAYS: [[META5]] = !{!"middle_out_3", i64 0, !"test_middle_out", i64 40}
+; ALWAYS: [[META6]] = !{!"untracked_mid_outer", i64 0, !"test_untracked_mid", i64 50}
+; ALWAYS: [[META7]] = !{!"untracked_outer_mid", i64 0, !"test_untracked_outer", i64 0}
+; ALWAYS: [[META8]] = !{!"invoke_mid", i64 0, !"test_invoke", i64 30}
+; ALWAYS: [[META9]] = !{!"dontcall_def_wrap", i64 0, !"test_dontcall_def", i64 20}
+;.
+; FULL: [[META0]] = !{i64 10}
+; FULL: [[META1]] = !{!"bottom_up_inner", i64 0, !"bottom_up_mid", i64 20, !"test_bottom_up", i64 30}
+; FULL: [[META2]] = !{!"top_down_inner", i64 0, !"top_down_mid", i64 20, !"test_top_down", i64 30}
+; FULL: [[META3]] = !{!"middle_out_1", i64 0, !"middle_out_2", i64 20, !"middle_out_3", i64 30, !"test_middle_out", i64 40}
+; FULL: [[META4]] = !{!"untracked_mid_inner", i64 0, !"untracked_mid_outer", i64 0, !"test_untracked_mid", i64 50}
+; FULL: [[META5]] = !{!"untracked_outer_inner", i64 0, !"untracked_outer_mid", i64 20, !"test_untracked_outer", i64 0}
+; FULL: [[META6]] = !{!"invoke_inner", i64 0, !"invoke_mid", i64 20, !"test_invoke", i64 30}
+; FULL: [[META7]] = !{!"dontcall_def_wrap", i64 0, !"test_dontcall_def", i64 20}
+;.

``````````

</details>


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


More information about the cfe-commits mailing list