[llvm-branch-commits] [llvm] llvm-reduce: Do not reduce alloca array sizes to 0 (PR #132864)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/132864

>From 33ce163b669ef76a18bbb6b24f94ade248a242f5 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Tue, 25 Mar 2025 09:39:18 +0700
Subject: [PATCH] llvm-reduce: Do not reduce alloca array sizes to 0

Fixes #64340
---
 .../llvm-reduce/reduce-operands-alloca.ll | 69 +++
 .../llvm-reduce/deltas/ReduceOperands.cpp |  5 ++
 2 files changed, 74 insertions(+)
 create mode 100644 llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll

diff --git a/llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll 
b/llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll
new file mode 100644
index 0..61c46185b3378
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll
@@ -0,0 +1,69 @@
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-zero 
--test FileCheck --test-arg --check-prefix=CHECK --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: FileCheck %s --check-prefixes=CHECK,ZERO < %t
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-one 
--test FileCheck --test-arg --check-prefix=CHECK --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: FileCheck %s --check-prefixes=CHECK,ONE < %t
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-poison 
--test FileCheck --test-arg --check-prefix=CHECK --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: FileCheck %s --check-prefixes=CHECK,POISON < %t
+
+
+; CHECK-LABEL: @dyn_alloca(
+; ZERO: %alloca = alloca i32, i32 %size, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 %size, align 4
+define void @dyn_alloca(i32 %size) {
+ %alloca = alloca i32, i32 %size
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_0_elt(
+; ZERO: %alloca = alloca i32, i32 0, align 4
+; ONE: %alloca = alloca i32, i32 0, align 4
+; POISON:  %alloca = alloca i32, i32 0, align 4
+define void @alloca_0_elt() {
+ %alloca = alloca i32, i32 0
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_1_elt(
+; ZERO: %alloca = alloca i32, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, align 4
+define void @alloca_1_elt() {
+ %alloca = alloca i32, i32 1
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_1024_elt(
+; ZERO: %alloca = alloca i32, i32 1024, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 1024, align 4
+define void @alloca_1024_elt() {
+ %alloca = alloca i32, i32 1024
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_poison_elt(
+; ZERO: %alloca = alloca i32, i32 poison, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 poison, align 4
+define void @alloca_poison_elt() {
+ %alloca = alloca i32, i32 poison
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_constexpr_elt(
+; ZERO: %alloca = alloca i32, i32 ptrtoint (ptr @alloca_constexpr_elt to i32)
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 ptrtoint (ptr @alloca_constexpr_elt to i32)
+define void @alloca_constexpr_elt() {
+ %alloca = alloca i32, i32 ptrtoint (ptr @alloca_constexpr_elt to i32)
+ store i32 0, ptr %alloca
+ ret void
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
index 1c0dd70666cb9..8c69b1cc202eb 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
@@ -130,6 +130,11 @@ void llvm::reduceOperandsZeroDeltaPass(TestRunner &Test) {
   auto ReduceValue = [](Use &Op) -> Value * {
 if (!shouldReduceOperand(Op))
   return nullptr;
+
+// Avoid introducing 0-sized allocations.
+if (isa(Op.getUser()))
+  return nullptr;
+
 // Don't duplicate an existing switch case.
 if (auto *IntTy = dyn_cast(Op->getType()))
   if (switchCaseExists(Op, ConstantInt::get(IntTy, 0)))

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Do not reduce alloca array sizes to 0 (PR #132864)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/132864

>From 33ce163b669ef76a18bbb6b24f94ade248a242f5 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Tue, 25 Mar 2025 09:39:18 +0700
Subject: [PATCH] llvm-reduce: Do not reduce alloca array sizes to 0

Fixes #64340
---
 .../llvm-reduce/reduce-operands-alloca.ll | 69 +++
 .../llvm-reduce/deltas/ReduceOperands.cpp |  5 ++
 2 files changed, 74 insertions(+)
 create mode 100644 llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll

diff --git a/llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll 
b/llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll
new file mode 100644
index 0..61c46185b3378
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-operands-alloca.ll
@@ -0,0 +1,69 @@
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-zero 
--test FileCheck --test-arg --check-prefix=CHECK --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: FileCheck %s --check-prefixes=CHECK,ZERO < %t
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-one 
--test FileCheck --test-arg --check-prefix=CHECK --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: FileCheck %s --check-prefixes=CHECK,ONE < %t
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=operands-poison 
--test FileCheck --test-arg --check-prefix=CHECK --test-arg %s --test-arg 
--input-file %s -o %t
+; RUN: FileCheck %s --check-prefixes=CHECK,POISON < %t
+
+
+; CHECK-LABEL: @dyn_alloca(
+; ZERO: %alloca = alloca i32, i32 %size, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 %size, align 4
+define void @dyn_alloca(i32 %size) {
+ %alloca = alloca i32, i32 %size
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_0_elt(
+; ZERO: %alloca = alloca i32, i32 0, align 4
+; ONE: %alloca = alloca i32, i32 0, align 4
+; POISON:  %alloca = alloca i32, i32 0, align 4
+define void @alloca_0_elt() {
+ %alloca = alloca i32, i32 0
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_1_elt(
+; ZERO: %alloca = alloca i32, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, align 4
+define void @alloca_1_elt() {
+ %alloca = alloca i32, i32 1
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_1024_elt(
+; ZERO: %alloca = alloca i32, i32 1024, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 1024, align 4
+define void @alloca_1024_elt() {
+ %alloca = alloca i32, i32 1024
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_poison_elt(
+; ZERO: %alloca = alloca i32, i32 poison, align 4
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 poison, align 4
+define void @alloca_poison_elt() {
+ %alloca = alloca i32, i32 poison
+ store i32 0, ptr %alloca
+ ret void
+}
+
+; CHECK-LABEL: @alloca_constexpr_elt(
+; ZERO: %alloca = alloca i32, i32 ptrtoint (ptr @alloca_constexpr_elt to i32)
+; ONE: %alloca = alloca i32, align 4
+; POISON: %alloca = alloca i32, i32 ptrtoint (ptr @alloca_constexpr_elt to i32)
+define void @alloca_constexpr_elt() {
+ %alloca = alloca i32, i32 ptrtoint (ptr @alloca_constexpr_elt to i32)
+ store i32 0, ptr %alloca
+ ret void
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
index 1c0dd70666cb9..8c69b1cc202eb 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
@@ -130,6 +130,11 @@ void llvm::reduceOperandsZeroDeltaPass(TestRunner &Test) {
   auto ReduceValue = [](Use &Op) -> Value * {
 if (!shouldReduceOperand(Op))
   return nullptr;
+
+// Avoid introducing 0-sized allocations.
+if (isa(Op.getUser()))
+  return nullptr;
+
 // Don't duplicate an existing switch case.
 if (auto *IntTy = dyn_cast(Op->getType()))
   if (switchCaseExists(Op, ConstantInt::get(IntTy, 0)))

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Defer a shouldKeep call in operand reduction (PR #133387)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/133387

Ideally shouldKeep is only called in contexts that will successfully
do something.

>From 58d33e17db2853fab62c6d762897f23f2f03f414 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 12:58:20 +0700
Subject: [PATCH] llvm-reduce: Defer a shouldKeep call in operand reduction

Ideally shouldKeep is only called in contexts that will successfully
do something.
---
 llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
index 8c69b1cc202eb..6bf1ec8d61688 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperands.cpp
@@ -26,8 +26,8 @@ extractOperandsFromModule(Oracle &O, ReducerWorkItem 
&WorkItem,
 for (auto &I : instructions(&F)) {
   if (PHINode *Phi = dyn_cast(&I)) {
 for (auto &Op : Phi->incoming_values()) {
-  if (!O.shouldKeep()) {
-if (Value *Reduced = ReduceValue(Op))
+  if (Value *Reduced = ReduceValue(Op)) {
+if (!O.shouldKeep())
   Phi->setIncomingValueForBlock(Phi->getIncomingBlock(Op), 
Reduced);
   }
 }

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing metadata when removing arguments (PR #133409)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133409

>From 38d881a2ce55f76900c7dec71b86e1ab1f3d3334 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 16:29:29 +0700
Subject: [PATCH] llvm-reduce: Fix losing metadata when removing arguments

---
 .../tools/llvm-reduce/remove-arguments-preserve-fmf.ll | 7 +--
 llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp  | 4 +++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll 
b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll
index 2ceb3e717200b..4c78920ede44a 100644
--- a/llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll
+++ b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-fmf.ll
@@ -16,11 +16,14 @@ declare float @math_callee_decl(float %a, float %b)
 ; INTERESTING: call
 ; INTERESTING: call
 
-; RESULT: %call0 = call nnan nsz float @math_callee()
+; RESULT: %call0 = call nnan nsz float @math_callee(), !fpmath !0
 ; RESULT: %call1 = call ninf float @math_callee_decl()
 define float @math_caller(float %x) {
-  %call0 = call nnan nsz float @math_callee(float %x, float 2.0)
+  %call0 = call nnan nsz float @math_callee(float %x, float 2.0), !fpmath !0
   %call1 = call ninf float @math_callee_decl(float %x, float 2.0)
   %result = fadd float %call0, %call1
   ret float %result
 }
+
+; RESULT: !0 = !{float 2.00e+00}
+!0 = !{float 2.0}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
index b1bbcf8917b3c..ba8721e09b99c 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
@@ -61,7 +61,7 @@ static void replaceFunctionCalls(Function &OldF, Function 
&NewF,
 }
   }
 
-  // FIXME: Losing bundles and metadata
+  // FIXME: Losing bundles
   CallInst *NewCI = CallInst::Create(&NewF, Args);
   NewCI->setCallingConv(NewF.getCallingConv());
 
@@ -78,6 +78,8 @@ static void replaceFunctionCalls(Function &OldF, Function 
&NewF,
   if (auto *FPOp = dyn_cast(NewCI))
 cast(FPOp)->setFastMathFlags(CI->getFastMathFlags());
 
+  NewCI->copyMetadata(*CI);
+
   if (!CI->use_empty())
 CI->replaceAllUsesWith(NewCI);
   ReplaceInstWithInst(CI, NewCI);

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][debugify] Add --debugify-atoms to add key instructions metadata (PR #133483)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133483
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][SimplifyCFG] Remap atoms after duplication for threading (PR #133484)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133484
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [clang-tools-extra] [lldb] [clang] Template Specialization Resugaring - TypeDecl (PR #132441)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Matheus Izvekov (mizvekov)


Changes

This is the introductory patch for a larger work which intends to solve the 
long standing C++ issue with losing type sugar when acessing template 
specializations.

The well known example here is specializing a template with `std::string`, but 
getting diagnostics related to `std::basic_string` instead.

This implements a transform which, upon member access, propagates type sugar 
from the naming context into the accessed entity.

It also implements a single use of this transform, resugaring access to 
TypeDecls.

For more details and discussion see:
https://discourse.llvm.org/t/rfc-improving-diagnostics-with-template-specialization-resugaring/64294

Even though this patch is ready for review, some dependent patches are not, and 
might not be ready for some time.

There is some more stuff that could be done either here or in follow ups:

* Its worth exploring if a global resugaring cache is worthwhile, besides the 
current operational cache. A global cache would be more expensive to index, so 
there is a tradeoff, and maybe should be used of the whole result of the 
operation, while keeping the existing cache for sub-results.
* It would be ideal if the transform could live in ASTContext instead of Sema. 
There are a few dependencies that would have to be tackled.
  * Template arguments deduced for partial specializations.
  * Some kinds of type adjustments currently require Sema.

Differential Revision: https://reviews.llvm.org/D127695

---

Patch is 53.59 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/132441.diff


17 Files Affected:

- (modified) clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp 
(+1-1) 
- (modified) clang/include/clang/AST/ASTContext.h (+7-6) 
- (modified) clang/include/clang/AST/Type.h (+6-5) 
- (modified) clang/include/clang/Sema/Sema.h (+6-2) 
- (modified) clang/lib/AST/ASTContext.cpp (+8-9) 
- (modified) clang/lib/Sema/SemaCXXScopeSpec.cpp (+3-2) 
- (modified) clang/lib/Sema/SemaCoroutine.cpp (+1) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+5-4) 
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+1-1) 
- (modified) clang/lib/Sema/SemaTemplate.cpp (+721-1) 
- (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+4-2) 
- (modified) clang/lib/Sema/SemaType.cpp (+6-4) 
- (modified) clang/test/AST/ast-dump-openmp-begin-declare-variant_reference.cpp 
(+2-6) 
- (modified) clang/test/AST/ast-dump-template-name.cpp (+2-5) 
- (modified) clang/test/CXX/temp/temp.param/p15-cxx0x.cpp (+2-2) 
- (added) clang/test/Sema/Resugar/resugar-types.cpp (+209) 
- (modified) 
lldb/test/API/commands/expression/import-std-module/iterator/TestIteratorFromStdModule.py
 (+1-1) 


``diff
diff --git a/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp 
b/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
index e843c593a92cc..679fbd75d2479 100644
--- a/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/QualifiedAutoCheck.cpp
@@ -126,7 +126,7 @@ void QualifiedAutoCheck::registerMatchers(MatchFinder 
*Finder) {
   auto UnlessFunctionType = 
unless(hasUnqualifiedDesugaredType(functionType()));
   auto IsAutoDeducedToPointer = [](const auto &...InnerMatchers) {
 return autoType(hasDeducedType(
-hasUnqualifiedDesugaredType(pointerType(pointee(InnerMatchers...);
+hasCanonicalType(pointerType(pointee(InnerMatchers...);
   };
 
   Finder->addMatcher(
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index ef596f99262be..23d789d8466d3 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -1319,8 +1319,6 @@ class ASTContext : public RefCountedBase {
 
   QualType getTypeDeclTypeSlow(const TypeDecl *Decl) const;
 
-  QualType getPipeType(QualType T, bool ReadOnly) const;
-
 public:
   /// Return the uniqued reference to the type for an address space
   /// qualified type with the specified type and address space.
@@ -1500,6 +1498,9 @@ class ASTContext : public RefCountedBase {
   /// blocks.
   QualType getBlockDescriptorType() const;
 
+  // Return a pipe type for the specified type.
+  QualType getPipeType(QualType T, bool ReadOnly) const;
+
   /// Return a read_only pipe type for the specified type.
   QualType getReadPipeType(QualType T) const;
 
@@ -1901,10 +1902,10 @@ class ASTContext : public RefCountedBase {
   /// C++11 decltype.
   QualType getDecltypeType(Expr *e, QualType UnderlyingType) const;
 
-  QualType getPackIndexingType(QualType Pattern, Expr *IndexExpr,
-   bool FullySubstituted = false,
-   ArrayRef Expansions = {},
-   int Index = -1) const;
+  QualType getPackIndexingType(
+  QualType Pattern, Expr *IndexExpr, bool FullySubstituted = false,
+  ArrayRef Expansi

[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms duping bb with cond br on phi into pred (PR #133488)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

OCHyams wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/133488?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#133491** https://app.graphite.dev/github/pr/llvm/llvm-project/133491?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133490** https://app.graphite.dev/github/pr/llvm/llvm-project/133490?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133489** https://app.graphite.dev/github/pr/llvm/llvm-project/133489?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133488** https://app.graphite.dev/github/pr/llvm/llvm-project/133488?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/133488?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#133487** https://app.graphite.dev/github/pr/llvm/llvm-project/133487?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133486** https://app.graphite.dev/github/pr/llvm/llvm-project/133486?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133485** https://app.graphite.dev/github/pr/llvm/llvm-project/133485?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133484** https://app.graphite.dev/github/pr/llvm/llvm-project/133484?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133483** https://app.graphite.dev/github/pr/llvm/llvm-project/133483?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133482** https://app.graphite.dev/github/pr/llvm/llvm-project/133482?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133481** https://app.graphite.dev/github/pr/llvm/llvm-project/133481?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133480** https://app.graphite.dev/github/pr/llvm/llvm-project/133480?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133479** https://app.graphite.dev/github/pr/llvm/llvm-project/133479?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133478** https://app.graphite.dev/github/pr/llvm/llvm-project/133478?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133477** https://app.graphite.dev/github/pr/llvm/llvm-project/133477?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/133488
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][LoopUnroll] Remap atoms while unrolling (PR #133489)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133489
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms after threading (PR #133487)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133487
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][Inline] Don't propagate atoms to inlined nodebug instructions (PR #133485)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:



@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-ir

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


3 Files Affected:

- (modified) llvm/include/llvm/IR/DebugInfoMetadata.h (+7) 
- (modified) llvm/lib/Transforms/Utils/InlineFunction.cpp (+2-2) 
- (added) llvm/test/DebugInfo/KeyInstructions/Generic/inline-nodbg.ll (+43) 


``diff
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h 
b/llvm/include/llvm/IR/DebugInfoMetadata.h
index ede3bda5c68f4..243ea5e1236ad 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -2145,6 +2145,13 @@ class DILocation : public MDNode {
 return 0;
   }
 
+  const DILocation *getOrCloneWithoutAtom() const {
+if (!getAtomGroup() && !getAtomRank())
+  return this;
+return get(getContext(), getLine(), getColumn(), getScope(), 
getInlinedAt(),
+   isImplicitCode());
+  }
+
   // Disallow replacing operands.
   void replaceOperandWith(unsigned I, Metadata *New) = delete;
 
diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp 
b/llvm/lib/Transforms/Utils/InlineFunction.cpp
index 9202451f5726d..fa4f21480b94d 100644
--- a/llvm/lib/Transforms/Utils/InlineFunction.cpp
+++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp
@@ -1821,9 +1821,9 @@ static DebugLoc inlineDebugLoc(DebugLoc OrigDL, 
DILocation *InlinedAt,
 /// to encode location where these instructions are inlined.
 static void fixupLineNumbers(Function *Fn, Function::iterator FI,
  Instruction *TheCall, bool CalleeHasDebugInfo) {
-  const DebugLoc &TheCallDL = TheCall->getDebugLoc();
-  if (!TheCallDL)
+  if (!TheCall->getDebugLoc())
 return;
+  DebugLoc TheCallDL = TheCall->getDebugLoc().get()->getOrCloneWithoutAtom();
 
   auto &Ctx = Fn->getContext();
   DILocation *InlinedAtNode = TheCallDL;
diff --git a/llvm/test/DebugInfo/KeyInstructions/Generic/inline-nodbg.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/inline-nodbg.ll
new file mode 100644
index 0..33f7f673d91c3
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/inline-nodbg.ll
@@ -0,0 +1,43 @@
+; RUN: opt %s -passes=inline -S -o - | FileCheck %s
+
+;; $ cat test.cpp
+;; int g;
+;; [[clang::always_inline, gnu::nodebug]]  void a() { g = 1; }
+;; void b() { a(); }
+;;
+;; Check the inlined instructions don't inherit the call's atom info.
+;; FIXME: Perhaps we want to do actually do that, to preserve existing
+;; behaviour? Unclear what's best.
+
+; CHECK: _Z1bv()
+; CHECK: store i32 1, ptr @g, align 4, !dbg [[DBG:!.*]]
+; CHECK: [[DBG]] = !DILocation(line: 3, scope: ![[#]])
+
+@g = hidden global i32 0, align 4
+
+define hidden void @_Z1av() {
+entry:
+  store i32 1, ptr @g, align 4
+  ret void
+}
+
+define hidden void @_Z1bv() !dbg !15 {
+entry:
+  call void @_Z1av(), !dbg !18
+  ret void, !dbg !19
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+!llvm.ident = !{!10}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_17, file: !1, 
producer: "clang version 19.0.0", isOptimized: true, runtimeVersion: 0, 
emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.cpp", directory: "/")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!10 = !{!"clang version 19.0.0"}
+!15 = distinct !DISubprogram(name: "b", scope: !1, file: !1, line: 3, type: 
!16, scopeLine: 3, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: 
DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+!16 = !DISubroutineType(types: !17)
+!17 = !{}
+!18 = !DILocation(line: 3, scope: !15, atomGroup: 1, atomRank: 1)
+!19 = !DILocation(line: 3, scope: !15, atomGroup: 2, atomRank: 1)

``




https://github.com/llvm/llvm-project/pull/133485
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] release/20.x: [clang] Introduce "binary" StringLiteral for #embed data (#127629) (PR #133460)

2025-03-28 Thread Mariya Podchishchaeva via llvm-branch-commits

https://github.com/Fznamznon updated 
https://github.com/llvm/llvm-project/pull/133460

>From d964cbf7ef72a8d582be37cf074ef3dbaeb5fa33 Mon Sep 17 00:00:00 2001
From: Mariya Podchishchaeva 
Date: Thu, 20 Mar 2025 13:02:29 +0100
Subject: [PATCH 1/2] [clang] Introduce "binary" StringLiteral for #embed data
 (#127629)

StringLiteral is used as internal data of EmbedExpr and we directly use
it as an initializer if a single EmbedExpr appears in the initializer
list of a char array. It is fast and convenient, but it is causing
problems when string literal character values are checked because #embed
data values are within a range [0-2^(char width)] but ordinary
StringLiteral is of maybe signed char type.
This PR introduces new kind of StringLiteral to hold binary data coming
from an embedded resource to mitigate these problems. The new kind of
StringLiteral is not assumed to have signed char type. The new kind of
StringLiteral also helps to prevent crashes when trying to find
StringLiteral token locations since these simply do not exist for binary
data.

Fixes https://github.com/llvm/llvm-project/issues/119256
---
 clang/include/clang/AST/Expr.h| 15 ---
 clang/lib/AST/Expr.cpp|  7 +++
 clang/lib/Parse/ParseInit.cpp |  2 +-
 clang/lib/Sema/SemaInit.cpp   |  1 +
 clang/test/Preprocessor/embed_constexpr.c | 21 +
 5 files changed, 42 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Preprocessor/embed_constexpr.c

diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 7be4022649329..06ac0f1704aa9 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -1752,7 +1752,14 @@ enum class StringLiteralKind {
   UTF8,
   UTF16,
   UTF32,
-  Unevaluated
+  Unevaluated,
+  // Binary kind of string literal is used for the data coming via #embed
+  // directive. File's binary contents is transformed to a special kind of
+  // string literal that in some cases may be used directly as an initializer
+  // and some features of classic string literals are not applicable to this
+  // kind of a string literal, for example finding a particular byte's source
+  // location for better diagnosing.
+  Binary
 };
 
 /// StringLiteral - This represents a string literal expression, e.g. "foo"
@@ -1884,6 +1891,8 @@ class StringLiteral final
   int64_t getCodeUnitS(size_t I, uint64_t BitWidth) const {
 int64_t V = getCodeUnit(I);
 if (isOrdinary() || isWide()) {
+  // Ordinary and wide string literals have types that can be signed.
+  // It is important for checking C23 constexpr initializers.
   unsigned Width = getCharByteWidth() * BitWidth;
   llvm::APInt AInt(Width, (uint64_t)V);
   V = AInt.getSExtValue();
@@ -4965,9 +4974,9 @@ class EmbedExpr final : public Expr {
   assert(EExpr && CurOffset != ULLONG_MAX &&
  "trying to dereference an invalid iterator");
   IntegerLiteral *N = EExpr->FakeChildNode;
-  StringRef DataRef = EExpr->Data->BinaryData->getBytes();
   N->setValue(*EExpr->Ctx,
-  llvm::APInt(N->getValue().getBitWidth(), DataRef[CurOffset],
+  llvm::APInt(N->getValue().getBitWidth(),
+  EExpr->Data->BinaryData->getCodeUnit(CurOffset),
   N->getType()->isSignedIntegerType()));
   // We want to return a reference to the fake child node in the
   // EmbedExpr, not the local variable N.
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 06b0491442673..c43301dbcb120 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1104,6 +1104,7 @@ unsigned StringLiteral::mapCharByteWidth(TargetInfo const 
&Target,
   switch (SK) {
   case StringLiteralKind::Ordinary:
   case StringLiteralKind::UTF8:
+  case StringLiteralKind::Binary:
 CharByteWidth = Target.getCharWidth();
 break;
   case StringLiteralKind::Wide:
@@ -1216,6 +1217,7 @@ void StringLiteral::outputString(raw_ostream &OS) const {
   switch (getKind()) {
   case StringLiteralKind::Unevaluated:
   case StringLiteralKind::Ordinary:
+  case StringLiteralKind::Binary:
 break; // no prefix.
   case StringLiteralKind::Wide:
 OS << 'L';
@@ -1332,6 +1334,11 @@ StringLiteral::getLocationOfByte(unsigned ByteNo, const 
SourceManager &SM,
  const LangOptions &Features,
  const TargetInfo &Target, unsigned 
*StartToken,
  unsigned *StartTokenByteOffset) const {
+  // No source location of bytes for binary literals since they don't come from
+  // source.
+  if (getKind() == StringLiteralKind::Binary)
+return getStrTokenLoc(0);
+
   assert((getKind() == StringLiteralKind::Ordinary ||
   getKind() == StringLiteralKind::UTF8 ||
   getKind() == StringLiteralKind::Unevaluated) &&
diff --git a/clang/lib/Parse/ParseInit.cpp b/clang/lib/Pa

[llvm-branch-commits] [llvm] [KeyInstr][debugify] Add --debugify-atoms to add key instructions metadata (PR #133483)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams created 
https://github.com/llvm/llvm-project/pull/133483

None

>From b848c421338a93c1d031f3038f81b538eac33aab Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams 
Date: Mon, 24 Mar 2025 15:53:27 +
Subject: [PATCH] [KeyInstr][debugify] Add --debugify-atoms to add key
 instructions metadata

---
 llvm/lib/Transforms/Utils/Debugify.cpp | 11 +--
 llvm/test/DebugInfo/debugify.ll| 10 ++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/Debugify.cpp 
b/llvm/lib/Transforms/Utils/Debugify.cpp
index e6b5e267d192b..02c7d7f2f10ed 100644
--- a/llvm/lib/Transforms/Utils/Debugify.cpp
+++ b/llvm/lib/Transforms/Utils/Debugify.cpp
@@ -35,6 +35,8 @@ using namespace llvm;
 
 namespace {
 
+cl::opt ApplyAtomGroups("debugify-atoms", cl::init(false));
+
 cl::opt Quiet("debugify-quiet",
 cl::desc("Suppress verbose debugify output"));
 
@@ -142,8 +144,13 @@ bool llvm::applyDebugifyMetadata(
 
 for (BasicBlock &BB : F) {
   // Attach debug locations.
-  for (Instruction &I : BB)
-I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
+  for (Instruction &I : BB) {
+uint64_t AtomGroup = ApplyAtomGroups ? NextLine : 0;
+uint8_t AtomRank = ApplyAtomGroups ? 1 : 0;
+uint64_t Line = NextLine++;
+I.setDebugLoc(DILocation::get(Ctx, Line, 1, SP, nullptr, false,
+  AtomGroup, AtomRank));
+  }
 
   if (DebugifyLevel < Level::LocationsAndVariables)
 continue;
diff --git a/llvm/test/DebugInfo/debugify.ll b/llvm/test/DebugInfo/debugify.ll
index 191015f825933..269a301ce830c 100644
--- a/llvm/test/DebugInfo/debugify.ll
+++ b/llvm/test/DebugInfo/debugify.ll
@@ -1,6 +1,9 @@
 ; RUN: opt -passes=debugify -S -o - < %s | FileCheck %s
 ; RUN: opt -passes=debugify -S -o - < %s | FileCheck %s
 
+; RUN: opt -passes=debugify --debugify-atoms -S -o - < %s \
+; RUN: | FileCheck %s -check-prefixes=CHECK-ATOMS
+
 ; RUN: opt -passes=debugify,debugify -S -o - < %s 2>&1 | \
 ; RUN:   FileCheck %s -check-prefix=CHECK-REPEAT
 ; RUN: opt -passes=debugify,debugify -S -o - < %s 2>&1 | \
@@ -101,6 +104,13 @@ define i32 @boom() {
 ; CHECK-DAG: ![[musttail]] = !DILocation(line: 5, column: 1
 ; CHECK-DAG: ![[musttailRes]] = !DILocation(line: 6, column: 1
 
+; CHECK-ATOMS-DAG: !DILocation(line: 1{{.*}}, atomGroup: 1, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 2{{.*}}, atomGroup: 2, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 3{{.*}}, atomGroup: 3, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 4{{.*}}, atomGroup: 4, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 5{{.*}}, atomGroup: 5, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 6{{.*}}, atomGroup: 6, atomRank: 1
+
 ; --- DILocalVariables
 ; CHECK-DAG: ![[TY32:.*]] = !DIBasicType(name: "ty32", size: 32, encoding: 
DW_ATE_unsigned)
 ; CHECK-DAG: !DILocalVariable(name: "1", scope: {{.*}}, file: {{.*}}, line: 1, 
type: ![[TY32]])

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] Propagate DebugLocs on phis in BreakCriticalEdges (PR #133492)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

OCHyams wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/133492?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#133494** https://app.graphite.dev/github/pr/llvm/llvm-project/133494?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133493** https://app.graphite.dev/github/pr/llvm/llvm-project/133493?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133492** https://app.graphite.dev/github/pr/llvm/llvm-project/133492?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/133492?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#133491** https://app.graphite.dev/github/pr/llvm/llvm-project/133491?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133490** https://app.graphite.dev/github/pr/llvm/llvm-project/133490?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133489** https://app.graphite.dev/github/pr/llvm/llvm-project/133489?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133488** https://app.graphite.dev/github/pr/llvm/llvm-project/133488?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133487** https://app.graphite.dev/github/pr/llvm/llvm-project/133487?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133486** https://app.graphite.dev/github/pr/llvm/llvm-project/133486?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133485** https://app.graphite.dev/github/pr/llvm/llvm-project/133485?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133484** https://app.graphite.dev/github/pr/llvm/llvm-project/133484?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133483** https://app.graphite.dev/github/pr/llvm/llvm-project/133483?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133482** https://app.graphite.dev/github/pr/llvm/llvm-project/133482?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133481** https://app.graphite.dev/github/pr/llvm/llvm-project/133481?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133480** https://app.graphite.dev/github/pr/llvm/llvm-project/133480?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133479** https://app.graphite.dev/github/pr/llvm/llvm-project/133479?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133478** https://app.graphite.dev/github/pr/llvm/llvm-project/133478?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133477** https://app.graphite.dev/github/pr/llvm/llvm-project/133477?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/133492
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] release/20.x: [clang] Introduce "binary" StringLiteral for #embed data (#127629) (PR #133460)

2025-03-28 Thread Mariya Podchishchaeva via llvm-branch-commits

https://github.com/Fznamznon created 
https://github.com/llvm/llvm-project/pull/133460

StringLiteral is used as internal data of EmbedExpr and we directly use
it as an initializer if a single EmbedExpr appears in the initializer
list of a char array. It is fast and convenient, but it is causing
problems when string literal character values are checked because #embed
data values are within a range [0-2^(char width)] but ordinary
StringLiteral is of maybe signed char type.
This PR introduces new kind of StringLiteral to hold binary data coming
from an embedded resource to mitigate these problems. The new kind of
StringLiteral is not assumed to have signed char type. The new kind of
StringLiteral also helps to prevent crashes when trying to find
StringLiteral token locations since these simply do not exist for binary
data.


>From d964cbf7ef72a8d582be37cf074ef3dbaeb5fa33 Mon Sep 17 00:00:00 2001
From: Mariya Podchishchaeva 
Date: Thu, 20 Mar 2025 13:02:29 +0100
Subject: [PATCH 1/2] [clang] Introduce "binary" StringLiteral for #embed data
 (#127629)

StringLiteral is used as internal data of EmbedExpr and we directly use
it as an initializer if a single EmbedExpr appears in the initializer
list of a char array. It is fast and convenient, but it is causing
problems when string literal character values are checked because #embed
data values are within a range [0-2^(char width)] but ordinary
StringLiteral is of maybe signed char type.
This PR introduces new kind of StringLiteral to hold binary data coming
from an embedded resource to mitigate these problems. The new kind of
StringLiteral is not assumed to have signed char type. The new kind of
StringLiteral also helps to prevent crashes when trying to find
StringLiteral token locations since these simply do not exist for binary
data.

Fixes https://github.com/llvm/llvm-project/issues/119256
---
 clang/include/clang/AST/Expr.h| 15 ---
 clang/lib/AST/Expr.cpp|  7 +++
 clang/lib/Parse/ParseInit.cpp |  2 +-
 clang/lib/Sema/SemaInit.cpp   |  1 +
 clang/test/Preprocessor/embed_constexpr.c | 21 +
 5 files changed, 42 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/Preprocessor/embed_constexpr.c

diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 7be4022649329..06ac0f1704aa9 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -1752,7 +1752,14 @@ enum class StringLiteralKind {
   UTF8,
   UTF16,
   UTF32,
-  Unevaluated
+  Unevaluated,
+  // Binary kind of string literal is used for the data coming via #embed
+  // directive. File's binary contents is transformed to a special kind of
+  // string literal that in some cases may be used directly as an initializer
+  // and some features of classic string literals are not applicable to this
+  // kind of a string literal, for example finding a particular byte's source
+  // location for better diagnosing.
+  Binary
 };
 
 /// StringLiteral - This represents a string literal expression, e.g. "foo"
@@ -1884,6 +1891,8 @@ class StringLiteral final
   int64_t getCodeUnitS(size_t I, uint64_t BitWidth) const {
 int64_t V = getCodeUnit(I);
 if (isOrdinary() || isWide()) {
+  // Ordinary and wide string literals have types that can be signed.
+  // It is important for checking C23 constexpr initializers.
   unsigned Width = getCharByteWidth() * BitWidth;
   llvm::APInt AInt(Width, (uint64_t)V);
   V = AInt.getSExtValue();
@@ -4965,9 +4974,9 @@ class EmbedExpr final : public Expr {
   assert(EExpr && CurOffset != ULLONG_MAX &&
  "trying to dereference an invalid iterator");
   IntegerLiteral *N = EExpr->FakeChildNode;
-  StringRef DataRef = EExpr->Data->BinaryData->getBytes();
   N->setValue(*EExpr->Ctx,
-  llvm::APInt(N->getValue().getBitWidth(), DataRef[CurOffset],
+  llvm::APInt(N->getValue().getBitWidth(),
+  EExpr->Data->BinaryData->getCodeUnit(CurOffset),
   N->getType()->isSignedIntegerType()));
   // We want to return a reference to the fake child node in the
   // EmbedExpr, not the local variable N.
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 06b0491442673..c43301dbcb120 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1104,6 +1104,7 @@ unsigned StringLiteral::mapCharByteWidth(TargetInfo const 
&Target,
   switch (SK) {
   case StringLiteralKind::Ordinary:
   case StringLiteralKind::UTF8:
+  case StringLiteralKind::Binary:
 CharByteWidth = Target.getCharWidth();
 break;
   case StringLiteralKind::Wide:
@@ -1216,6 +1217,7 @@ void StringLiteral::outputString(raw_ostream &OS) const {
   switch (getKind()) {
   case StringLiteralKind::Unevaluated:
   case StringLiteralKind::Ordinary:
+  case StringLiteralKind::Binary:
 break; // no prefix.
   case StringLite

[llvm-branch-commits] [llvm] Propagate DebugLocs on phis in BreakCriticalEdges (PR #133492)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133492
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Filter function based on uses before removing arguments (PR #133412)

2025-03-28 Thread John Regehr via llvm-branch-commits

regehr wrote:

LGTM!

https://github.com/llvm/llvm-project/pull/133412
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] [llvm] [ctxprof] root autodetection mechanism (PR #133147)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133147

>From 0080eef0e2fce86d3b0059b59d8b319a0e78e05f Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Mon, 24 Mar 2025 12:01:10 -0700
Subject: [PATCH] RootAutodetect

---
 compiler-rt/lib/ctx_profile/CMakeLists.txt|   2 +-
 .../lib/ctx_profile/CtxInstrContextNode.h |   1 +
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 119 +++
 .../lib/ctx_profile/CtxInstrProfiling.h   |   2 +-
 .../lib/ctx_profile/RootAutoDetector.cpp  |  83 
 .../lib/ctx_profile/RootAutoDetector.h|  31 +++
 .../TestCases/autodetect-roots.cpp| 188 ++
 .../TestCases/generate-context.cpp|   5 +-
 .../llvm/ProfileData/CtxInstrContextNode.h|   1 +
 .../Instrumentation/PGOCtxProfLowering.cpp|  26 ++-
 .../PGOProfile/ctx-instrumentation.ll |  20 +-
 11 files changed, 416 insertions(+), 62 deletions(-)
 create mode 100644 compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp

diff --git a/compiler-rt/lib/ctx_profile/CMakeLists.txt 
b/compiler-rt/lib/ctx_profile/CMakeLists.txt
index bb606449c61b1..446ebc96408dd 100644
--- a/compiler-rt/lib/ctx_profile/CMakeLists.txt
+++ b/compiler-rt/lib/ctx_profile/CMakeLists.txt
@@ -27,7 +27,7 @@ endif()
 add_compiler_rt_runtime(clang_rt.ctx_profile
   STATIC
   ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
-  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
+  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc 
RTSanitizerCommonSymbolizer
   CFLAGS ${EXTRA_FLAGS}
   SOURCES ${CTX_PROFILE_SOURCES}
   ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h 
b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
index a42bf9ebb01ea..aa052bc7eea6c 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
@@ -127,6 +127,7 @@ class ContextNode final {
 /// MUTEXDECL takes one parameter, the name of a field that is a mutex.
 #define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL)
\
   PTRDECL(FunctionData, Next)  
\
+  PTRDECL(void, EntryAddress)  
\
   VOLATILE_PTRDECL(ContextRoot, CtxRoot)   
\
   VOLATILE_PTRDECL(ContextNode, FlatCtx)   
\
   MUTEXDECL(Mutex)
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index da291e0bbabdd..09ed607cde3aa 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "CtxInstrProfiling.h"
+#include "RootAutoDetector.h"
 #include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_atomic_clang.h"
@@ -43,6 +44,12 @@ Arena *FlatCtxArena = nullptr;
 __thread bool IsUnderContext = false;
 __sanitizer::atomic_uint8_t ProfilingStarted = {};
 
+__sanitizer::atomic_uintptr_t RootDetector = {};
+RootAutoDetector *getRootDetector() {
+  return reinterpret_cast(
+  __sanitizer::atomic_load_relaxed(&RootDetector));
+}
+
 // utility to taint a pointer by setting the LSB. There is an assumption
 // throughout that the addresses of contexts are even (really, they should be
 // align(8), but "even"-ness is the minimum assumption)
@@ -201,7 +208,7 @@ ContextNode *getCallsiteSlow(GUID Guid, ContextNode 
**InsertionPoint,
   return Ret;
 }
 
-ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
+ContextNode *getFlatProfile(FunctionData &Data, void *Callee, GUID Guid,
 uint32_t NumCounters) {
   if (ContextNode *Existing = Data.FlatCtx)
 return Existing;
@@ -232,6 +239,7 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
 auto *Ret = allocContextNode(AllocBuff, Guid, NumCounters, 0);
 Data.FlatCtx = Ret;
 
+Data.EntryAddress = Callee;
 Data.Next = reinterpret_cast(
 __sanitizer::atomic_load_relaxed(&AllFunctionsData));
 while (!__sanitizer::atomic_compare_exchange_strong(
@@ -277,8 +285,29 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
   return Root;
 }
 
-ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
- uint32_t NumCounters) {
+ContextNode *tryStartContextGivenRoot(ContextRoot *Root, GUID Guid,
+  uint32_t Counters, uint32_t Callsites)
+SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
+  IsUnderContext = true;
+  __sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
+__sanitizer::memory_order_relaxed);
+
+  if (!Root->FirstMemBlock) {
+setupContext(Root, Guid, Counters, Callsites);
+  }
+  if (Root->Taken.TryL

[llvm-branch-commits] [llvm] llvm-reduce: Filter function based on uses before removing arguments (PR #133412)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133412

>From 834ae84ad0cbd62834d22fcb9ff4431915d78567 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 17:09:49 +0700
Subject: [PATCH] llvm-reduce: Filter function based on uses before removing
 arguments

Invokes and others are not handled, so this was leaving broken callsites
behind for anything other than CallInst
---
 .../llvm-reduce/reduce-arguments-invoke.ll| 41 +++
 .../reduce-arguments-non-callee-use.ll| 32 +++
 .../llvm-reduce/deltas/ReduceArguments.cpp| 17 +++-
 3 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll
 create mode 100644 
llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll

diff --git a/llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll 
b/llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll
new file mode 100644
index 0..fb8a4d6367b3b
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll
@@ -0,0 +1,41 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=arguments --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+; Test that we don't break the callsite for an unhandled invoke
+
+declare void @did_not_throw(i32)
+declare void @thrown()
+
+; INTERESTING-LABEL: define i32 @maybe_throwing_callee(
+
+; REDUCED-LABEL: define i32 @maybe_throwing_callee(i32 %arg0, i32 %arg1) {
+define i32 @maybe_throwing_callee(i32 %arg0, i32 %arg1) {
+  call void @thrown()
+  ret i32 %arg1
+}
+
+@initializer_user = global [1 x ptr] [ptr @maybe_throwing_callee ]
+
+; REDUCED-LABEL: define void @caller()
+; REDUCED: %i0 = invoke i32 @maybe_throwing_callee(i32 0, i32 0) #0
+define void @caller(i32 %arg0, ptr %arg1) personality ptr 
@__gxx_personality_v0 {
+bb:
+  %val = load i32, ptr %arg1
+  %i0 = invoke i32 @maybe_throwing_callee(i32 0, i32 0) nofree
+  to label %bb3 unwind label %bb1
+
+bb1:
+  landingpad { ptr, i32 }
+  catch ptr null
+  call void @thrown()
+  br label %bb4
+
+bb3:
+  call void @did_not_throw(i32 %i0)
+  br label %bb4
+
+bb4:
+  ret void
+}
+
+declare i32 @__gxx_personality_v0(...)
diff --git a/llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll 
b/llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll
new file mode 100644
index 0..63b2909404b7c
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll
@@ -0,0 +1,32 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=arguments --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+; INTERESTING: @initializer_user
+; REDUCED: @initializer_user = global [1 x ptr] [ptr @captured_func]
+@initializer_user = global [1 x ptr] [ptr @captured_func ]
+
+; INTERESTING-LABEL: define i32 @captured_func(
+
+; REDUCED-LABEL: define i32 @captured_func() {
+define i32 @captured_func(i32 %a, i32 %b) {
+  %mul = mul i32 %a, %b
+  ret i32 %mul
+}
+
+; INTERESTING-LABEL: declare void @captures(
+declare void @captures(i32, ptr, i32)
+
+
+; INTERESTING-LABEL: define i32 @caller(
+; INTERESTING: = call
+; INTERESTING: = call
+
+; REDUCED-LABEL: define i32 @caller(i32 %a, i32 %b) {
+; REDUCED: %call0 = call i32 @captures(i32 %a, ptr @captured_func, i32 %b)
+; REDUCED: %call1 = call i32 @captured_func()
+define i32 @caller(i32 %a, i32 %b) {
+  %call0 = call i32 @captures(i32 %a, ptr @captured_func, i32 %b)
+  %call1 = call i32 @captured_func(i32 %a, i32 %b)
+  %add = add i32 %call0, %call1
+  ret i32 %add
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
index f1b72a75564de..bf9d6ba145f6f 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
@@ -96,6 +96,20 @@ static bool shouldRemoveArguments(const Function &F) {
   return !F.arg_empty() && !F.isIntrinsic();
 }
 
+static bool allFuncUsersRewritable(const Function &F) {
+  for (const Use &U : F.uses()) {
+const CallBase *CB = dyn_cast(U.getUser());
+if (!CB || !CB->isCallee(&U))
+  continue;
+
+// TODO: Handle all CallBase cases.
+if (!isa(CB))
+  return false;
+  }
+
+  return true;
+}
+
 /// Removes out-of-chunk arguments from functions, and modifies their calls
 /// accordingly. It also removes allocations of out-of-chunk arguments.
 static void extractArgumentsFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
@@ -107,7 +121,8 @@ static void extractArgumentsFromModule(Oracle &O, 
ReducerWorkItem &WorkItem) {
   for (auto &F : Program) {
 if (!shouldRemoveArguments(F))
   continue;
-
+if (!allFuncUsersRewritable(F))
+  continue;
 Funcs.push_back(&F);
 fo

[llvm-branch-commits] [llvm] llvm-reduce: Fix losing callsite attributes in operand-to-args (PR #133420)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133420

>From 08e5a7895726219ebf3afad0d09755b5f08d2966 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 17:54:10 +0700
Subject: [PATCH] llvm-reduce: Fix losing callsite attributes in
 operand-to-args

---
 .../operands-to-args-preserve-attributes.ll   | 31 +++
 .../deltas/ReduceOperandsToArgs.cpp   |  1 +
 2 files changed, 32 insertions(+)
 create mode 100644 
llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll

diff --git 
a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll 
b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll
new file mode 100644
index 0..3aef3a06eafa3
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll
@@ -0,0 +1,31 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=operands-to-args --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+@a = dso_local global i8 0, align 1
+@b = dso_local global i16 0, align 2
+
+
+declare ptr @callee(ptr %a, i16)
+
+; INTERESTING-LABEL: define ptr @caller(
+; INTERESTING: sext
+; INTERESTING: icmp
+
+; REDUCED-LABEL: define ptr @caller(ptr %some.ptr, ptr %a, i8 %ld0, ptr %b, 
i16 %ld1, i32 %conv, i32 %conv1, i1 %cmp, i16 %conv2, ptr %callee.ret) #0 {
+
+; REDUCED: %callee.ret8 = call align 8 ptr @callee(ptr align 8 "some-attr" 
%some.ptr, i16 signext %conv2) #1
+
+define ptr @caller(ptr %some.ptr) nounwind {
+entry:
+  %ld0 = load i8, ptr @a, align 1
+  %conv = zext i8 %ld0 to i32
+  %ld1 = load i16, ptr @b, align 2
+  %conv1 = sext i16 %ld1 to i32
+  %cmp = icmp sge i32 %conv, %conv1
+  %conv2 = sext i1 %cmp to i16
+  %callee.ret = call align 8 ptr @callee(ptr align 8 "some-attr" %some.ptr, 
i16 signext %conv2) nocallback
+  ret ptr %callee.ret
+}
+
+; REDUCED: attributes #0 = { nounwind }
+; REDUCED: attributes #1 = { nocallback }
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
index 66d4bd3048171..037ff15fae0f6 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
@@ -105,6 +105,7 @@ static void replaceFunctionCalls(Function *OldF, Function 
*NewF) {
   NewCI = CallInst::Create(NewF, Args, OperandBundles, CI->getName());
 }
 NewCI->setCallingConv(NewF->getCallingConv());
+NewCI->setAttributes(CI->getAttributes());
 
 // Do the replacement for this use.
 if (!CI->use_empty())

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing call metadata in operands-to-args (PR #133422)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133422

>From c2fd5f47cd9d97e0df6652766e78753aab903bf6 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 18:01:39 +0700
Subject: [PATCH] llvm-reduce: Fix using call metadata in operands-to-args

---
 .../tools/llvm-reduce/operands-to-args-preserve-fmf.ll | 7 +--
 llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll 
b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
index b4b19ca28dbb5..fc31a08353b8f 100644
--- a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
+++ b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
@@ -12,9 +12,12 @@ define float @callee(float %a) {
 ; INTERESTING: load float
 
 ; REDUCED-LABEL: define float @caller(ptr %ptr, float %val, float 
%callee.ret1) {
-; REDUCED: %callee.ret12 = call nnan nsz float @callee(float %val, float 
0.00e+00)
+; REDUCED: %callee.ret12 = call nnan nsz float @callee(float %val, float 
0.00e+00), !fpmath !0
 define float @caller(ptr %ptr) {
   %val = load float, ptr %ptr
-  %callee.ret = call nnan nsz float @callee(float %val)
+  %callee.ret = call nnan nsz float @callee(float %val), !fpmath !0
   ret float %callee.ret
 }
+
+; REDUCED: !0 = !{float 2.00e+00}
+!0 = !{float 2.0}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
index e7ad52eb65a5d..33f6463be6581 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
@@ -111,6 +111,8 @@ static void replaceFunctionCalls(Function *OldF, Function 
*NewF) {
 if (auto *FPOp = dyn_cast(NewCI))
   NewCI->setFastMathFlags(CI->getFastMathFlags());
 
+NewCI->copyMetadata(*CI);
+
 // Do the replacement for this use.
 if (!CI->use_empty())
   CI->replaceAllUsesWith(NewCI);

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing callsite attributes in operand-to-args (PR #133420)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133420

>From c1eeec8bc99846b07a4d44ad5b52f95815f648ad Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 17:54:10 +0700
Subject: [PATCH] llvm-reduce: Fix losing callsite attributes in
 operand-to-args

---
 .../operands-to-args-preserve-attributes.ll   | 31 +++
 .../deltas/ReduceOperandsToArgs.cpp   |  1 +
 2 files changed, 32 insertions(+)
 create mode 100644 
llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll

diff --git 
a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll 
b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll
new file mode 100644
index 0..3aef3a06eafa3
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll
@@ -0,0 +1,31 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=operands-to-args --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+@a = dso_local global i8 0, align 1
+@b = dso_local global i16 0, align 2
+
+
+declare ptr @callee(ptr %a, i16)
+
+; INTERESTING-LABEL: define ptr @caller(
+; INTERESTING: sext
+; INTERESTING: icmp
+
+; REDUCED-LABEL: define ptr @caller(ptr %some.ptr, ptr %a, i8 %ld0, ptr %b, 
i16 %ld1, i32 %conv, i32 %conv1, i1 %cmp, i16 %conv2, ptr %callee.ret) #0 {
+
+; REDUCED: %callee.ret8 = call align 8 ptr @callee(ptr align 8 "some-attr" 
%some.ptr, i16 signext %conv2) #1
+
+define ptr @caller(ptr %some.ptr) nounwind {
+entry:
+  %ld0 = load i8, ptr @a, align 1
+  %conv = zext i8 %ld0 to i32
+  %ld1 = load i16, ptr @b, align 2
+  %conv1 = sext i16 %ld1 to i32
+  %cmp = icmp sge i32 %conv, %conv1
+  %conv2 = sext i1 %cmp to i16
+  %callee.ret = call align 8 ptr @callee(ptr align 8 "some-attr" %some.ptr, 
i16 signext %conv2) nocallback
+  ret ptr %callee.ret
+}
+
+; REDUCED: attributes #0 = { nounwind }
+; REDUCED: attributes #1 = { nocallback }
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
index 66d4bd3048171..037ff15fae0f6 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
@@ -105,6 +105,7 @@ static void replaceFunctionCalls(Function *OldF, Function 
*NewF) {
   NewCI = CallInst::Create(NewF, Args, OperandBundles, CI->getName());
 }
 NewCI->setCallingConv(NewF->getCallingConv());
+NewCI->setAttributes(CI->getAttributes());
 
 // Do the replacement for this use.
 if (!CI->use_empty())

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Add Atom Group waterline to LLVMContext (PR #133478)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes

The waterline is managed through DILocation creation,
LLVMContext::incNextAtomGroup, and LLVMContext::updateAtomGroupWaterline.

Add unittest.

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


5 Files Affected:

- (modified) llvm/include/llvm/IR/LLVMContext.h (+8) 
- (modified) llvm/lib/IR/DebugInfoMetadata.cpp (+3) 
- (modified) llvm/lib/IR/LLVMContext.cpp (+8) 
- (modified) llvm/lib/IR/LLVMContextImpl.h (+10) 
- (modified) llvm/unittests/IR/MetadataTest.cpp (+38) 


``diff
diff --git a/llvm/include/llvm/IR/LLVMContext.h 
b/llvm/include/llvm/IR/LLVMContext.h
index bbd125fd38cf1..d3cdd31e0b12f 100644
--- a/llvm/include/llvm/IR/LLVMContext.h
+++ b/llvm/include/llvm/IR/LLVMContext.h
@@ -335,6 +335,14 @@ class LLVMContext {
   StringRef getDefaultTargetFeatures();
   void setDefaultTargetFeatures(StringRef Features);
 
+  /// Key Instructions: update the highest number atom group emitted for any
+  /// function.
+  void updateAtomGroupWaterline(uint64_t G);
+
+  /// Key Instructions: get the next free atom group number and increment
+  /// the global tracker.
+  uint64_t incNextAtomGroup();
+
 private:
   // Module needs access to the add/removeModule methods.
   friend class Module;
diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp 
b/llvm/lib/IR/DebugInfoMetadata.cpp
index f4f9fca38945c..aefda2f7be0b0 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -67,6 +67,9 @@ DILocation::DILocation(LLVMContext &C, StorageType Storage, 
unsigned Line,
 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
   assert(AtomRank <= 7 && "AtomRank number should fit in 3 bits");
 #endif
+  if (AtomRank)
+C.updateAtomGroupWaterline(AtomGroup + 1);
+
   assert((MDs.size() == 1 || MDs.size() == 2) &&
  "Expected a scope and optional inlined-at");
   // Set line and column.
diff --git a/llvm/lib/IR/LLVMContext.cpp b/llvm/lib/IR/LLVMContext.cpp
index 447e5d92e0b99..4781085b30431 100644
--- a/llvm/lib/IR/LLVMContext.cpp
+++ b/llvm/lib/IR/LLVMContext.cpp
@@ -377,3 +377,11 @@ StringRef LLVMContext::getDefaultTargetFeatures() {
 void LLVMContext::setDefaultTargetFeatures(StringRef Features) {
   pImpl->DefaultTargetFeatures = Features;
 }
+
+void LLVMContext::updateAtomGroupWaterline(uint64_t V) {
+  pImpl->NextAtomGroup = std::max(pImpl->NextAtomGroup, V);
+}
+
+uint64_t LLVMContext::incNextAtomGroup() {
+  return pImpl->NextAtomGroup++;
+}
diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h
index 99f0d8837de52..5a52d41834095 100644
--- a/llvm/lib/IR/LLVMContextImpl.h
+++ b/llvm/lib/IR/LLVMContextImpl.h
@@ -1832,6 +1832,16 @@ class LLVMContextImpl {
 
   std::string DefaultTargetCPU;
   std::string DefaultTargetFeatures;
+
+  /// The next available source atom group number. The front end is responsible
+  /// for assigning source atom numbers, but certain optimisations need to
+  /// assign new group numbers to a set of instructions. Most often code
+  /// duplication optimisations like loop unroll. Tracking a global maximum
+  /// value means we can know (cheaply) we're never using a group number that's
+  /// already used within this function.
+  ///
+  /// Start a 1 because 0 means the source location isn't part of an atom 
group.
+  uint64_t NextAtomGroup = 1;
 };
 
 } // end namespace llvm
diff --git a/llvm/unittests/IR/MetadataTest.cpp 
b/llvm/unittests/IR/MetadataTest.cpp
index 94cebb0406598..8a1c1f9d306c6 100644
--- a/llvm/unittests/IR/MetadataTest.cpp
+++ b/llvm/unittests/IR/MetadataTest.cpp
@@ -6,6 +6,7 @@
 //
 
//===--===//
 
+#include "../lib/IR/LLVMContextImpl.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
@@ -1366,6 +1367,43 @@ TEST_F(DILocationTest, discriminatorSpecialCases) {
   EXPECT_EQ(std::nullopt, L4->cloneByMultiplyingDuplicationFactor(0x1000));
 }
 
+TEST_F(DILocationTest, KeyInstructions) {
+  Context.pImpl->NextAtomGroup = 1;
+
+  EXPECT_EQ(Context.pImpl->NextAtomGroup, 1u);
+  DILocation *A1 = DILocation::get(Context, 1, 0, getSubprogram(), nullptr, 
false, 1, 2);
+  // The group is only applied to the DILocation if the build has opted into
+  // the additional DILocation fields needed for the feature.
+#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
+  EXPECT_EQ(A1->getAtomGroup(), 1u);
+  EXPECT_EQ(A1->getAtomRank(), 2u);
+#else
+  EXPECT_EQ(A1->getAtomGroup(), 0u);
+  EXPECT_EQ(A1->getAtomRank(), 0u);
+#endif
+
+  // Group number 1 has been "used" so next available is 2.
+  EXPECT_EQ(Context.pImpl->NextAtomGroup, 2u);
+
+  // Set a group number higher than current + 1, then check the waterline.
+  DILocation::get(Context, 2, 0, getSubprogram(), nullptr, false, 5, 1);
+  EXPECT_EQ(Context.pImpl->NextAtomGroup, 6u);
+
+  // The waterline should be unchanged (group <= next).
+  DILocation::get(Context, 3, 0, getSubp

[llvm-branch-commits] [llvm] llvm-reduce: Fix losing operand bundles when removing arguments (PR #133410)

2025-03-28 Thread Shilei Tian via llvm-branch-commits

https://github.com/shiltian approved this pull request.


https://github.com/llvm/llvm-project/pull/133410
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][LLVM] Delete `LLVMFixedVectorType` and `LLVMScalableVectorType` (PR #133286)

2025-03-28 Thread Andrzej Warzyński via llvm-branch-commits

https://github.com/banach-space approved this pull request.

Nice cleanup, thanks! LGTM

https://github.com/llvm/llvm-project/pull/133286
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][LLVM] Delete `LLVMFixedVectorType` and `LLVMScalableVectorType` (PR #133286)

2025-03-28 Thread Andrzej Warzyński via llvm-branch-commits


@@ -150,8 +150,7 @@ generatedTypePrinter(Type def, AsmPrinter &printer);
 
 bool LLVMArrayType::isValidElementType(Type type) {
   return !llvm::isa(
-  type);
+LLVMFunctionType, LLVMTokenType>(type);

banach-space wrote:

That `LLVMScalableVectorType` was added long before SME:
* https://reviews.llvm.org/D85663

But yes, "arrays of scalable vectors" are a thing and we rely on them. That 
said, I don't see any SME/SVE tests failing (I also check e2e locally), so this 
should be fine.

https://github.com/llvm/llvm-project/pull/133286
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][LLVM] Delete `LLVMFixedVectorType` and `LLVMScalableVectorType` (PR #133286)

2025-03-28 Thread Andrzej Warzyński via llvm-branch-commits


@@ -1033,7 +1033,7 @@ llvm.func @scalable_vector() -> i16 {
 llvm.func @scalable_llvm_vector() -> i16 {
   %0 = llvm.mlir.constant(1 : i32) : i32
   // CHECK: llvm.alloca
-  %1 = llvm.alloca %0 x !llvm.vec : (i32) -> !llvm.ptr
+  %1 = llvm.alloca %0 x vector<[4] x !llvm.ppc_fp128> : (i32) -> !llvm.ptr

banach-space wrote:

The element type shouldn't matter, right? "Scalability" is a fairly abstract 
concept.

https://github.com/llvm/llvm-project/pull/133286
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][LLVM] Delete `LLVMFixedVectorType` and `LLVMScalableVectorType` (PR #133286)

2025-03-28 Thread Andrzej Warzyński via llvm-branch-commits

https://github.com/banach-space edited 
https://github.com/llvm/llvm-project/pull/133286
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Add Atom Group waterline to LLVMContext (PR #133478)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams created 
https://github.com/llvm/llvm-project/pull/133478

The waterline is managed through DILocation creation,
LLVMContext::incNextAtomGroup, and LLVMContext::updateAtomGroupWaterline.

Add unittest.

>From d12e993d5e22cae5c6eeb7088eedbf8e3ec2cb98 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams 
Date: Tue, 18 Mar 2025 16:50:17 +
Subject: [PATCH] [KeyInstr] Add Atom Group waterline to LLVMContext

The waterline is managed through DILocation creation,
LLVMContext::incNextAtomGroup, and LLVMContext::updateAtomGroupWaterline.

Add unittest.
---
 llvm/include/llvm/IR/LLVMContext.h |  8 +++
 llvm/lib/IR/DebugInfoMetadata.cpp  |  3 +++
 llvm/lib/IR/LLVMContext.cpp|  8 +++
 llvm/lib/IR/LLVMContextImpl.h  | 10 
 llvm/unittests/IR/MetadataTest.cpp | 38 ++
 5 files changed, 67 insertions(+)

diff --git a/llvm/include/llvm/IR/LLVMContext.h 
b/llvm/include/llvm/IR/LLVMContext.h
index bbd125fd38cf1..d3cdd31e0b12f 100644
--- a/llvm/include/llvm/IR/LLVMContext.h
+++ b/llvm/include/llvm/IR/LLVMContext.h
@@ -335,6 +335,14 @@ class LLVMContext {
   StringRef getDefaultTargetFeatures();
   void setDefaultTargetFeatures(StringRef Features);
 
+  /// Key Instructions: update the highest number atom group emitted for any
+  /// function.
+  void updateAtomGroupWaterline(uint64_t G);
+
+  /// Key Instructions: get the next free atom group number and increment
+  /// the global tracker.
+  uint64_t incNextAtomGroup();
+
 private:
   // Module needs access to the add/removeModule methods.
   friend class Module;
diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp 
b/llvm/lib/IR/DebugInfoMetadata.cpp
index f4f9fca38945c..aefda2f7be0b0 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -67,6 +67,9 @@ DILocation::DILocation(LLVMContext &C, StorageType Storage, 
unsigned Line,
 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
   assert(AtomRank <= 7 && "AtomRank number should fit in 3 bits");
 #endif
+  if (AtomRank)
+C.updateAtomGroupWaterline(AtomGroup + 1);
+
   assert((MDs.size() == 1 || MDs.size() == 2) &&
  "Expected a scope and optional inlined-at");
   // Set line and column.
diff --git a/llvm/lib/IR/LLVMContext.cpp b/llvm/lib/IR/LLVMContext.cpp
index 447e5d92e0b99..4781085b30431 100644
--- a/llvm/lib/IR/LLVMContext.cpp
+++ b/llvm/lib/IR/LLVMContext.cpp
@@ -377,3 +377,11 @@ StringRef LLVMContext::getDefaultTargetFeatures() {
 void LLVMContext::setDefaultTargetFeatures(StringRef Features) {
   pImpl->DefaultTargetFeatures = Features;
 }
+
+void LLVMContext::updateAtomGroupWaterline(uint64_t V) {
+  pImpl->NextAtomGroup = std::max(pImpl->NextAtomGroup, V);
+}
+
+uint64_t LLVMContext::incNextAtomGroup() {
+  return pImpl->NextAtomGroup++;
+}
diff --git a/llvm/lib/IR/LLVMContextImpl.h b/llvm/lib/IR/LLVMContextImpl.h
index 99f0d8837de52..5a52d41834095 100644
--- a/llvm/lib/IR/LLVMContextImpl.h
+++ b/llvm/lib/IR/LLVMContextImpl.h
@@ -1832,6 +1832,16 @@ class LLVMContextImpl {
 
   std::string DefaultTargetCPU;
   std::string DefaultTargetFeatures;
+
+  /// The next available source atom group number. The front end is responsible
+  /// for assigning source atom numbers, but certain optimisations need to
+  /// assign new group numbers to a set of instructions. Most often code
+  /// duplication optimisations like loop unroll. Tracking a global maximum
+  /// value means we can know (cheaply) we're never using a group number that's
+  /// already used within this function.
+  ///
+  /// Start a 1 because 0 means the source location isn't part of an atom 
group.
+  uint64_t NextAtomGroup = 1;
 };
 
 } // end namespace llvm
diff --git a/llvm/unittests/IR/MetadataTest.cpp 
b/llvm/unittests/IR/MetadataTest.cpp
index 94cebb0406598..8a1c1f9d306c6 100644
--- a/llvm/unittests/IR/MetadataTest.cpp
+++ b/llvm/unittests/IR/MetadataTest.cpp
@@ -6,6 +6,7 @@
 //
 
//===--===//
 
+#include "../lib/IR/LLVMContextImpl.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
@@ -1366,6 +1367,43 @@ TEST_F(DILocationTest, discriminatorSpecialCases) {
   EXPECT_EQ(std::nullopt, L4->cloneByMultiplyingDuplicationFactor(0x1000));
 }
 
+TEST_F(DILocationTest, KeyInstructions) {
+  Context.pImpl->NextAtomGroup = 1;
+
+  EXPECT_EQ(Context.pImpl->NextAtomGroup, 1u);
+  DILocation *A1 = DILocation::get(Context, 1, 0, getSubprogram(), nullptr, 
false, 1, 2);
+  // The group is only applied to the DILocation if the build has opted into
+  // the additional DILocation fields needed for the feature.
+#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
+  EXPECT_EQ(A1->getAtomGroup(), 1u);
+  EXPECT_EQ(A1->getAtomRank(), 2u);
+#else
+  EXPECT_EQ(A1->getAtomGroup(), 0u);
+  EXPECT_EQ(A1->getAtomRank(), 0u);
+#endif
+
+  // Group number 1 has been "used" so next available is 2.
+  EXPECT_EQ(Context.pImpl->NextAtomGroup, 2u);
+
+  // Set a group number

[llvm-branch-commits] [llvm] [KeyInstr][SimplifyCFG] Remap atoms after duplication for threading (PR #133484)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams created 
https://github.com/llvm/llvm-project/pull/133484

Given the same branch condition in `a` and `c` SimplifyCFG converts:

  +> b -+
  | v
  --> a --> c --> e -->
| ^
+> d -+

into:

  +--> bcd ---+
  |   v
  --> a --> c --> e -->

Remap source atoms on instructions duplicated from `c` into `bcd`.

>From e2d5b6ee482fc14091b5c4fd9b72cba7e187987a Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams 
Date: Mon, 24 Mar 2025 16:26:46 +
Subject: [PATCH] [KeyInstr][SimplifyCFG] Remap atoms after duplication for
 threading

Given the same branch condition in `a` and `c` SimplifyCFG converts:

  +> b -+
  | v
  --> a --> c --> e -->
| ^
+> d -+

into:

  +--> bcd ---+
  |   v
  --> a --> c --> e -->

Remap source atoms on instructions duplicated from `c` into `bcd`.
---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 12 ++--
 .../Generic/simplifycfg-thread-phi.ll | 62 +++
 2 files changed, 68 insertions(+), 6 deletions(-)
 create mode 100644 
llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1ba1e4ac81000..c83ff0260e297 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3589,7 +3589,7 @@ foldCondBranchOnValueKnownInPredecessorImpl(BranchInst 
*BI, DomTreeUpdater *DTU,
 // instructions into EdgeBB.  We know that there will be no uses of the
 // cloned instructions outside of EdgeBB.
 BasicBlock::iterator InsertPt = EdgeBB->getFirstInsertionPt();
-DenseMap TranslateMap; // Track translated values.
+ValueToValueMapTy TranslateMap; // Track translated values.
 TranslateMap[Cond] = CB;
 
 // RemoveDIs: track instructions that we optimise away while folding, so
@@ -3609,11 +3609,11 @@ foldCondBranchOnValueKnownInPredecessorImpl(BranchInst 
*BI, DomTreeUpdater *DTU,
 N->setName(BBI->getName() + ".c");
 
   // Update operands due to translation.
-  for (Use &Op : N->operands()) {
-DenseMap::iterator PI = TranslateMap.find(Op);
-if (PI != TranslateMap.end())
-  Op = PI->second;
-  }
+  // Key Instructions: Remap all the atom groups.
+  if (const DebugLoc &DL = BBI->getDebugLoc())
+mapAtomInstance(DL, TranslateMap);
+  RemapInstruction(N, TranslateMap,
+   RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
 
   // Check for trivial simplification.
   if (Value *V = simplifyInstruction(N, {DL, nullptr, nullptr, AC})) {
diff --git 
a/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll
new file mode 100644
index 0..f8477600c6418
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll
@@ -0,0 +1,62 @@
+; RUN: opt %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-S \
+; RUN: | FileCheck %s
+
+;; Generated using:
+;;   opt -passes=debugify --debugify-atoms --debugify-level=locations \
+;;  llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll
+;; With unused/untested metadata nodes removed.
+
+;; Check the duplicated store gets distinct atom info in each branch.
+
+; CHECK-LABEL: @bar(
+; CHECK: if.then:
+; CHECK:   store i32 1{{.*}}, !dbg [[DBG1:!.*]]
+; CHECK: if.end.1.critedge:
+; CHECK:   store i32 1{{.*}}, !dbg [[DBG2:!.*]]
+; CHECK: [[DBG1]] = !DILocation(line: 1{{.*}}, atomGroup: 1
+; CHECK: [[DBG2]] = !DILocation(line: 1{{.*}}, atomGroup: 2
+
+define void @bar(i32 %aa) !dbg !5 {
+entry:
+  %aa.addr = alloca i32, align 4
+  %bb = alloca i32, align 4
+  store i32 %aa, ptr %aa.addr, align 4
+  store i32 0, ptr %bb, align 4
+  %tobool = icmp ne i32 %aa, 0
+  br i1 %tobool, label %if.then, label %if.end
+
+if.then:  ; preds = %entry
+  call void @foo()
+  br label %if.end
+
+if.end:   ; preds = %if.then, %entry
+  store i32 1, ptr %bb, align 4, !dbg !8
+  br i1 %tobool, label %if.then.1, label %if.end.1
+
+if.then.1:; preds = %if.end
+  call void @foo()
+  br label %if.end.1
+
+if.end.1: ; preds = %if.then.1, %if.end
+  store i32 2, ptr %bb, align 4
+  br label %for.end
+
+for.end:  ; preds = %if.end.1
+  ret void
+}
+
+declare void @foo()
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: 
"debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: 
"llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll", directory: "/")
+!2 = !{i32 15}
+!3 = !{i32 0}
+!4 = !

[llvm-branch-commits] [llvm] [NFC][KeyInstr] Add Atom Group (re)mapping (PR #133479)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes

Add:
   mapAtomInstance - map the atom group number to a new group.
   RemapSourceAtom - apply the mapped atom group number to this instruction.

Modify:
  CloneBasicBlock - Call mapAtomInstance on cloned instruction's DebugLocs
if MapAtoms is true (default). Setting to false could
lead to a degraded debugging experience. See code comment.

Optimisations like loop unroll that duplicate instructions need to remap source
atom groups so that each duplicated source construct instance is considered
distinct when determining is_stmt locations.

This commit adds the remapping functionality and a unittest.

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


6 Files Affected:

- (modified) llvm/include/llvm/IR/ValueMap.h (+5) 
- (modified) llvm/include/llvm/Transforms/Utils/Cloning.h (+14-1) 
- (modified) llvm/include/llvm/Transforms/Utils/ValueMapper.h (+10) 
- (modified) llvm/lib/Transforms/Utils/CloneFunction.cpp (+28-1) 
- (modified) llvm/lib/Transforms/Utils/ValueMapper.cpp (+26) 
- (modified) llvm/unittests/Transforms/Utils/CloningTest.cpp (+104) 


``diff
diff --git a/llvm/include/llvm/IR/ValueMap.h b/llvm/include/llvm/IR/ValueMap.h
index d12d639aaa888..398be19331aba 100644
--- a/llvm/include/llvm/IR/ValueMap.h
+++ b/llvm/include/llvm/IR/ValueMap.h
@@ -87,6 +87,8 @@ class ValueMap {
   using ValueMapCVH = ValueMapCallbackVH;
   using MapT = DenseMap>;
   using MDMapT = DenseMap;
+  /// Map {(InlinedAt, old atom number) -> new atom number}.
+  using DMAtomT = DenseMap, uint64_t>;
   using ExtraData = typename Config::ExtraData;
 
   MapT Map;
@@ -117,6 +119,8 @@ class ValueMap {
 return *MDMap;
   }
   std::optional &getMDMap() { return MDMap; }
+  /// Map {(InlinedAt, old atom number) -> new atom number}.
+  DMAtomT AtomMap;
 
   /// Get the mapped metadata, if it's in the map.
   std::optional getMappedMD(const Metadata *MD) const {
@@ -145,6 +149,7 @@ class ValueMap {
   void clear() {
 Map.clear();
 MDMap.reset();
+AtomMap.clear();
   }
 
   /// Return 1 if the specified key is in the map, 0 otherwise.
diff --git a/llvm/include/llvm/Transforms/Utils/Cloning.h 
b/llvm/include/llvm/Transforms/Utils/Cloning.h
index ec1a1d5faa7e9..5e931c6583510 100644
--- a/llvm/include/llvm/Transforms/Utils/Cloning.h
+++ b/llvm/include/llvm/Transforms/Utils/Cloning.h
@@ -22,6 +22,7 @@
 #include "llvm/Analysis/AssumptionCache.h"
 #include "llvm/Analysis/InlineCost.h"
 #include "llvm/IR/BasicBlock.h"
+#include "llvm/IR/DebugLoc.h"
 #include "llvm/IR/ValueHandle.h"
 #include "llvm/Transforms/Utils/ValueMapper.h"
 #include 
@@ -117,9 +118,21 @@ struct ClonedCodeInfo {
 /// If you would like to collect additional information about the cloned
 /// function, you can specify a ClonedCodeInfo object with the optional fifth
 /// parameter.
+///
+/// Set \p MapAtoms to false to skip mapping source atoms for later remapping.
+/// Incorrectly setting false may harm the debugging experience. It's safe to
+/// set false if the cloned basic block is destined for a different function or
+/// if the original block is deleted. Setting true (default) is always safe
+/// (correct) but sometimes unecessary; this option reduces the compile-time
+/// impact of Key Instruction in such cases.
 BasicBlock *CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap,
 const Twine &NameSuffix = "", Function *F = 
nullptr,
-ClonedCodeInfo *CodeInfo = nullptr);
+ClonedCodeInfo *CodeInfo = nullptr,
+bool MapAtoms = true);
+
+/// Mark a cloned instruction as a new instance so that its source loc can
+/// be updated when remapped.
+void mapAtomInstance(const DebugLoc &DL, ValueToValueMapTy &VMap);
 
 /// Return a copy of the specified function and add it to that
 /// function's module.  Also, any references specified in the VMap are changed
diff --git a/llvm/include/llvm/Transforms/Utils/ValueMapper.h 
b/llvm/include/llvm/Transforms/Utils/ValueMapper.h
index 560df1d3f7f29..63680038172e3 100644
--- a/llvm/include/llvm/Transforms/Utils/ValueMapper.h
+++ b/llvm/include/llvm/Transforms/Utils/ValueMapper.h
@@ -105,6 +105,13 @@ enum RemapFlags {
   /// Any global values not in value map are mapped to null instead of mapping
   /// to self.  Illegal if RF_IgnoreMissingLocals is also set.
   RF_NullMapMissingGlobalValues = 8,
+
+  /// Do not remap atom instances. Only safe if to do this if the cloned
+  /// instructions being remapped are inserted into a new function, or an
+  /// existing function where the inlined-at fields are updated. If in doubt,
+  /// don't use this flag. It's used for compiler performance reasons rather
+  /// than correctness.
+  RF_DoNotRemapAtoms = 16,
 };
 
 inline RemapFlags operator|(RemapFlags LHS, RemapFlags RHS) {
@@ -284,6 +291,9 @@ inl

[llvm-branch-commits] [clang] [clang] [sanitizer] add pseudofunction to indicate array-bounds check (PR #128977)

2025-03-28 Thread Florian Mayer via llvm-branch-commits

fmayer wrote:

now actually committed to main: 
https://github.com/llvm/llvm-project/commit/c0952a931c7d556ca9f0073d86d591a37eb60477

https://github.com/llvm/llvm-project/pull/128977
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms in blocks duplicated for threading (PR #133486)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

OCHyams wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/133486?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#133488** https://app.graphite.dev/github/pr/llvm/llvm-project/133488?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133487** https://app.graphite.dev/github/pr/llvm/llvm-project/133487?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133486** https://app.graphite.dev/github/pr/llvm/llvm-project/133486?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/133486?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#133485** https://app.graphite.dev/github/pr/llvm/llvm-project/133485?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133484** https://app.graphite.dev/github/pr/llvm/llvm-project/133484?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133483** https://app.graphite.dev/github/pr/llvm/llvm-project/133483?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133482** https://app.graphite.dev/github/pr/llvm/llvm-project/133482?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133481** https://app.graphite.dev/github/pr/llvm/llvm-project/133481?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133480** https://app.graphite.dev/github/pr/llvm/llvm-project/133480?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133479** https://app.graphite.dev/github/pr/llvm/llvm-project/133479?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133478** https://app.graphite.dev/github/pr/llvm/llvm-project/133478?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133477** https://app.graphite.dev/github/pr/llvm/llvm-project/133477?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/133486
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing metadata when removing arguments (PR #133409)

2025-03-28 Thread Shilei Tian via llvm-branch-commits

https://github.com/shiltian edited 
https://github.com/llvm/llvm-project/pull/133409
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing fast math flags in operands-to-args (PR #133421)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133421

>From d102bc1f95340d3dfba924917f1040f345c9 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 18:00:05 +0700
Subject: [PATCH] llvm-reduce: Fix losing fast math flags in operands-to-args

---
 .../operands-to-args-preserve-fmf.ll  | 20 +++
 .../deltas/ReduceOperandsToArgs.cpp   |  4 
 2 files changed, 24 insertions(+)
 create mode 100644 llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll

diff --git a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll 
b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
new file mode 100644
index 0..b4b19ca28dbb5
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
@@ -0,0 +1,20 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=operands-to-args --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+; INTERESTING-LABEL: define float @callee(
+; INTERESTING: fadd float
+define float @callee(float %a) {
+  %x = fadd float %a, 1.0
+  ret float %x
+}
+
+; INTERESTING-LABEL: define float @caller(
+; INTERESTING: load float
+
+; REDUCED-LABEL: define float @caller(ptr %ptr, float %val, float 
%callee.ret1) {
+; REDUCED: %callee.ret12 = call nnan nsz float @callee(float %val, float 
0.00e+00)
+define float @caller(ptr %ptr) {
+  %val = load float, ptr %ptr
+  %callee.ret = call nnan nsz float @callee(float %val)
+  ret float %callee.ret
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
index 037ff15fae0f6..e7ad52eb65a5d 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
@@ -14,6 +14,7 @@
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instructions.h"
+#include "llvm/IR/Operator.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Cloning.h"
 
@@ -107,6 +108,9 @@ static void replaceFunctionCalls(Function *OldF, Function 
*NewF) {
 NewCI->setCallingConv(NewF->getCallingConv());
 NewCI->setAttributes(CI->getAttributes());
 
+if (auto *FPOp = dyn_cast(NewCI))
+  NewCI->setFastMathFlags(CI->getFastMathFlags());
+
 // Do the replacement for this use.
 if (!CI->use_empty())
   CI->replaceAllUsesWith(NewCI);

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing metadata when removing arguments (PR #133409)

2025-03-28 Thread Shilei Tian via llvm-branch-commits

https://github.com/shiltian approved this pull request.


https://github.com/llvm/llvm-project/pull/133409
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing fast math flags in operands-to-args (PR #133421)

2025-03-28 Thread Arthur Eubanks via llvm-branch-commits

aeubanks wrote:

can we have a CallInst copy all flags/metadata helper function?

https://github.com/llvm/llvm-project/pull/133421
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix using call metadata in operands-to-args (PR #133422)

2025-03-28 Thread John Regehr via llvm-branch-commits

regehr wrote:

LGTM

https://github.com/llvm/llvm-project/pull/133422
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Merge atoms in DILocation::getMergedLocation (PR #133480)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes

NFC for builds with LLVM_EXPERIMENTAL_KEY_INSTRUCTIONS=OFF (default).

In an ideal world we would be able to track that the merged location is used in
multiple source atoms. We can't do this though, so instead we arbitrarily but
deterministically pick one.

In cases where the InlinedAt field is unchanged we keep the atom with the
lowest non-zero rank (highest precedence). If the ranks are equal we choose
the smaller non-zero group number (arbitrary choice).

In cases where the InlinedAt field is adjusted we generate a new atom group.
Keeping the group wouldn't make sense (a source atom is identified by the
group number and InlinedAt pair) but discarding the atom info could result
in missed is_stmts.

Add unittest in MetadataTest.cpp.

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


2 Files Affected:

- (modified) llvm/lib/IR/DebugInfoMetadata.cpp (+48-6) 
- (modified) llvm/unittests/IR/MetadataTest.cpp (+134) 


``diff
diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp 
b/llvm/lib/IR/DebugInfoMetadata.cpp
index aefda2f7be0b0..6463be8a21850 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -189,11 +189,15 @@ DILocation *DILocation::getMergedLocation(DILocation 
*LocA, DILocation *LocB) {
 
   // Merge the two locations if possible, using the supplied
   // inlined-at location for the created location.
-  auto MergeLocPair = [&C](const DILocation *L1, const DILocation *L2,
-   DILocation *InlinedAt) -> DILocation * {
+  auto *LocAIA = LocA->getInlinedAt();
+  auto *LocBIA = LocB->getInlinedAt();
+  auto MergeLocPair = [&C, LocAIA,
+   LocBIA](const DILocation *L1, const DILocation *L2,
+   DILocation *InlinedAt) -> DILocation * {
 if (L1 == L2)
   return DILocation::get(C, L1->getLine(), L1->getColumn(), L1->getScope(),
- InlinedAt);
+ InlinedAt, L1->isImplicitCode(),
+ L1->getAtomGroup(), L1->getAtomRank());
 
 // If the locations originate from different subprograms we can't produce
 // a common location.
@@ -226,8 +230,44 @@ DILocation *DILocation::getMergedLocation(DILocation 
*LocA, DILocation *LocB) {
 bool SameCol = L1->getColumn() == L2->getColumn();
 unsigned Line = SameLine ? L1->getLine() : 0;
 unsigned Col = SameLine && SameCol ? L1->getColumn() : 0;
-
-return DILocation::get(C, Line, Col, Scope, InlinedAt);
+bool IsImplicitCode = L1->isImplicitCode() && L2->isImplicitCode();
+uint64_t Group = 0;
+uint64_t Rank = 0;
+if (SameLine) {
+  if (L1->getAtomGroup() || L2->getAtomGroup()) {
+// If we're preserving the same matching inlined-at field we can
+// preserve the atom.
+if (LocBIA == LocAIA && InlinedAt == LocBIA) {
+  // Deterministically keep the lowest non-zero ranking atom group
+  // number.
+  // FIXME: It would be nice if we could track that an instruction
+  // belongs to two source atoms.
+  bool UseL1Atom = [L1, L2]() {
+if (L1->getAtomRank() == L2->getAtomRank()) {
+  // Arbitrarily choose the lowest non-zero group number.
+  if (!L1->getAtomGroup() || !L2->getAtomGroup())
+return !L2->getAtomGroup();
+  return L1->getAtomGroup() < L2->getAtomGroup();
+}
+// Choose the lowest non-zero rank.
+if (!L1->getAtomRank() || !L2->getAtomRank())
+  return !L2->getAtomRank();
+return L1->getAtomRank() < L2->getAtomRank();
+  }();
+  Group = UseL1Atom ? L1->getAtomGroup() : L2->getAtomGroup();
+  Rank = UseL1Atom ? L1->getAtomRank() : L2->getAtomRank();
+} else {
+  // If either instruction is part of a source atom, reassign it a new
+  // atom group. This essentially regresses to non-key-instructions
+  // behaviour (now that it's the only instruction in its group it'll
+  // probably get is_stmt applied).
+  Group = C.incNextAtomGroup();
+  Rank = 1;
+}
+  }
+}
+return DILocation::get(C, Line, Col, Scope, InlinedAt, IsImplicitCode,
+   Group, Rank);
   };
 
   DILocation *Result = ARIt != ALocs.rend() ? (*ARIt)->getInlinedAt() : 
nullptr;
@@ -254,7 +294,9 @@ DILocation *DILocation::getMergedLocation(DILocation *LocA, 
DILocation *LocB) {
   // historically picked A's scope, and a nullptr inlined-at location, so that
   // behavior is mimicked here but I am not sure if this is always the correct
   // way to handle this.
-  return DILocation::get(C, 0, 0, LocA->getScope(), nullptr);
+  // Key Instructions: it's fine to drop atom group and rank here, as line 0
+  // is a nonsensical is_stmt location.
+  return DILocati

[llvm-branch-commits] [llvm] [KeyInstr][DwarfDebug] Add is_stmt emission support (PR #133495)

2025-03-28 Thread via llvm-branch-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff b49eb510a934913c691d8c05fc9af5918de0e5a8 
0b206ae0569191c8b05f9b77d92ceda3a5271fe2 --extensions cpp,h -- 
llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp 
llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h
``





View the diff from clang-format here.


``diff
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp 
b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 71abef1d23..3990286063 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -2412,7 +2412,8 @@ void DwarfDebug::findKeyInstructions(const 
MachineFunction *MF) {
 uint8_t Rank = MI.getDebugLoc()->getAtomRank();
 if (Group && Rank) {
   auto *InlinedAt = MI.getDebugLoc()->getInlinedAt();
-  auto &[CandidateRank, CandidateInsts] = GroupCandidates[{InlinedAt, 
Group}];
+  auto &[CandidateRank, CandidateInsts] =
+  GroupCandidates[{InlinedAt, Group}];
 
   // This looks similar to the non-call handling code, except that
   // we don't put the call into CandidateInsts so that they can't be
@@ -2470,7 +2471,8 @@ void DwarfDebug::findKeyInstructions(const 
MachineFunction *MF) {
 BuoyAtom = MI.getDebugLoc()->getAtomGroup();
   }
 
-  auto &[CandidateRank, CandidateInsts] = GroupCandidates[{InlinedAt, 
Group}];
+  auto &[CandidateRank, CandidateInsts] =
+  GroupCandidates[{InlinedAt, Group}];
 
   if (CandidateRank == 0) {
 // This is the first time we're seeing an instruction in this atom

``




https://github.com/llvm/llvm-project/pull/133495
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][debugify] Add --debugify-atoms to add key instructions metadata (PR #133483)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


2 Files Affected:

- (modified) llvm/lib/Transforms/Utils/Debugify.cpp (+9-2) 
- (modified) llvm/test/DebugInfo/debugify.ll (+10) 


``diff
diff --git a/llvm/lib/Transforms/Utils/Debugify.cpp 
b/llvm/lib/Transforms/Utils/Debugify.cpp
index e6b5e267d192b..02c7d7f2f10ed 100644
--- a/llvm/lib/Transforms/Utils/Debugify.cpp
+++ b/llvm/lib/Transforms/Utils/Debugify.cpp
@@ -35,6 +35,8 @@ using namespace llvm;
 
 namespace {
 
+cl::opt ApplyAtomGroups("debugify-atoms", cl::init(false));
+
 cl::opt Quiet("debugify-quiet",
 cl::desc("Suppress verbose debugify output"));
 
@@ -142,8 +144,13 @@ bool llvm::applyDebugifyMetadata(
 
 for (BasicBlock &BB : F) {
   // Attach debug locations.
-  for (Instruction &I : BB)
-I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
+  for (Instruction &I : BB) {
+uint64_t AtomGroup = ApplyAtomGroups ? NextLine : 0;
+uint8_t AtomRank = ApplyAtomGroups ? 1 : 0;
+uint64_t Line = NextLine++;
+I.setDebugLoc(DILocation::get(Ctx, Line, 1, SP, nullptr, false,
+  AtomGroup, AtomRank));
+  }
 
   if (DebugifyLevel < Level::LocationsAndVariables)
 continue;
diff --git a/llvm/test/DebugInfo/debugify.ll b/llvm/test/DebugInfo/debugify.ll
index 191015f825933..269a301ce830c 100644
--- a/llvm/test/DebugInfo/debugify.ll
+++ b/llvm/test/DebugInfo/debugify.ll
@@ -1,6 +1,9 @@
 ; RUN: opt -passes=debugify -S -o - < %s | FileCheck %s
 ; RUN: opt -passes=debugify -S -o - < %s | FileCheck %s
 
+; RUN: opt -passes=debugify --debugify-atoms -S -o - < %s \
+; RUN: | FileCheck %s -check-prefixes=CHECK-ATOMS
+
 ; RUN: opt -passes=debugify,debugify -S -o - < %s 2>&1 | \
 ; RUN:   FileCheck %s -check-prefix=CHECK-REPEAT
 ; RUN: opt -passes=debugify,debugify -S -o - < %s 2>&1 | \
@@ -101,6 +104,13 @@ define i32 @boom() {
 ; CHECK-DAG: ![[musttail]] = !DILocation(line: 5, column: 1
 ; CHECK-DAG: ![[musttailRes]] = !DILocation(line: 6, column: 1
 
+; CHECK-ATOMS-DAG: !DILocation(line: 1{{.*}}, atomGroup: 1, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 2{{.*}}, atomGroup: 2, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 3{{.*}}, atomGroup: 3, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 4{{.*}}, atomGroup: 4, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 5{{.*}}, atomGroup: 5, atomRank: 1
+; CHECK-ATOMS-DAG: !DILocation(line: 6{{.*}}, atomGroup: 6, atomRank: 1
+
 ; --- DILocalVariables
 ; CHECK-DAG: ![[TY32:.*]] = !DIBasicType(name: "ty32", size: 32, encoding: 
DW_ATE_unsigned)
 ; CHECK-DAG: !DILocalVariable(name: "1", scope: {{.*}}, file: {{.*}}, line: 1, 
type: ![[TY32]])

``




https://github.com/llvm/llvm-project/pull/133483
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][LoopUnswitch] Remap cloned instructions' atoms (PR #133491)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133491
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms after threading (PR #133487)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams created 
https://github.com/llvm/llvm-project/pull/133487

None

>From 272f806606513125115f0a81db25159d16f83de8 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams 
Date: Tue, 25 Mar 2025 11:56:08 +
Subject: [PATCH] [KeyInstr][JumpThreading] Remap atoms after threading

---
 llvm/lib/Transforms/Scalar/JumpThreading.cpp  |  1 +
 .../Generic/jump-threading-2-bbs.ll   | 30 +--
 .../Generic/jump-threading-basic.ll   | 78 +++
 3 files changed, 102 insertions(+), 7 deletions(-)
 create mode 100644 
llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-basic.ll

diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp 
b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 18dda2f3ad82e..d320f7dce11db 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -2451,6 +2451,7 @@ void JumpThreadingPass::threadEdge(BasicBlock *BB,
{DominatorTree::Insert, PredBB, NewBB},
{DominatorTree::Delete, PredBB, BB}});
 
+  remapSourceAtoms(ValueMapping, NewBB->begin(), NewBB->end());
   updateSSA(BB, NewBB, ValueMapping);
 
   // At this point, the IR is fully up to date and consistent.  Do a quick scan
diff --git 
a/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll
index 3ca81f554034f..612e74792fac2 100644
--- a/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll
@@ -2,27 +2,41 @@
 
 ;; Modified from llvm/test/Transforms/JumpThreading/thread-two-bbs.ll
 ;;
-;; JumpThreading duplicates bb.cond2 to thread through bb.file to bb.f2 or 
exit.
+;; JumpThreading duplicates bb.cond2 to thread through bb.file to bb.file,
+;; bb.f2 or exit.
 ;;
 ;; Check the duplicated instructions get remapped atom groups.
 
 ; CHECK: bb.cond2:
 ; CHECK-NEXT: call void @f1()
 ; CHECK-NEXT: %tobool1 = icmp eq i32 %cond2, 0, !dbg [[G1R2:!.*]]
-; CHECK-NEXT: br i1 %tobool1, label %exit, label %exit, !dbg [[G1R1:!.*]]
+; CHECK-NEXT: br i1 %tobool1, label %bb.file, label %exit, !dbg [[G1R1:!.*]]
 
 ; CHECK: bb.cond2.thread:
-; CHECK-NEXT: %tobool12 = icmp eq i32 %cond2, 0, !dbg [[G2R2:!.*]]
-; CHECK-NEXT: br i1 %tobool12, label %bb.f2, label %exit, !dbg [[G2R1:!.*]]
+; CHECK-NEXT: %tobool12 = icmp eq i32 %cond2, 0, !dbg [[G3R2:!.*]]
+; CHECK-NEXT: br i1 %tobool12, label %bb.f2, label %exit, !dbg [[G3R1:!.*]]
+
+;; After the transform %ptr is null through bb.cond2 and @a through
+;; bb.cond2.thread. Thread bb.cond2.thread->bb.f2 through bb.file.
+;; Check the duplicated store gets a remapped atom group too.
+
+; CHECK: bb.file:
+; CHECK-NEXT: %ptr3 = phi ptr [ null, %bb.cond2 ]
+; CHECK-NEXT: store ptr %ptr3, ptr %p, align 4, !dbg [[G2R1:!.*]]
+
+; CHECK: bb.f2:
+; CHECK-NEXT: store ptr @a, ptr %p, align 4, !dbg [[G4R1:!.*]]
 
 ; CHECK: [[G1R2]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
1, atomRank: 2)
 ; CHECK: [[G1R1]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
1, atomRank: 1)
-; CHECK: [[G2R2]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
2, atomRank: 2)
-; CHECK: [[G2R1]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
2, atomRank: 1)
+; CHECK: [[G3R2]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
3, atomRank: 2)
+; CHECK: [[G3R1]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
3, atomRank: 1)
+; CHECK: [[G2R1]] = !DILocation(line: 2, column: 1, scope: ![[#]], atomGroup: 
2, atomRank: 1)
+; CHECK: [[G4R1]] = !DILocation(line: 2, column: 1, scope: ![[#]], atomGroup: 
4, atomRank: 1)
 
 @a = global i32 0, align 4
 
-define void @foo(i32 %cond1, i32 %cond2) !dbg !5 {
+define void @foo(i32 %cond1, i32 %cond2, ptr %p) !dbg !5 {
 entry:
   %tobool = icmp eq i32 %cond1, 0
   br i1 %tobool, label %bb.cond2, label %bb.f1
@@ -37,6 +51,7 @@ bb.cond2: ; preds = 
%bb.f1, %entry
   br i1 %tobool1, label %bb.file, label %exit, !dbg !10
 
 bb.file:  ; preds = %bb.cond2
+  store ptr %ptr, ptr %p, align 4, !dbg !11
   %cmp = icmp eq ptr %ptr, null
   br i1 %cmp, label %exit, label %bb.f2
 
@@ -66,3 +81,4 @@ declare void @f2()
 !7 = !{}
 !9 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 2)
 !10 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 1)
+!11 = !DILocation(line: 2, column: 1, scope: !5, atomGroup: 2, atomRank: 1)
diff --git 
a/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-basic.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-basic.ll
new file mode 100644
index 0..d653a8d525b79
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-basic.ll
@@ -0,0 +1,78 @@
+; RUN: opt %s --passes=jump-threading -S -o - -S | FileCheck %s
+

[llvm-branch-commits] [llvm] [KeyInstr][SimplifyCFG] Remap atoms after duplication for threading (PR #133484)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

OCHyams wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/133484?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#133485** https://app.graphite.dev/github/pr/llvm/llvm-project/133485?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133484** https://app.graphite.dev/github/pr/llvm/llvm-project/133484?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/133484?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#133483** https://app.graphite.dev/github/pr/llvm/llvm-project/133483?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133482** https://app.graphite.dev/github/pr/llvm/llvm-project/133482?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133481** https://app.graphite.dev/github/pr/llvm/llvm-project/133481?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133480** https://app.graphite.dev/github/pr/llvm/llvm-project/133480?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133479** https://app.graphite.dev/github/pr/llvm/llvm-project/133479?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133478** https://app.graphite.dev/github/pr/llvm/llvm-project/133478?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133477** https://app.graphite.dev/github/pr/llvm/llvm-project/133477?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/133484
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Merge atoms in DILocation::getMergedLocation (PR #133480)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133480
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][DwarfDebug] Add is_stmt emission support (PR #133495)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133495
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][LoopUnroll] Remap atoms while unrolling (PR #133489)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-transforms

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


3 Files Affected:

- (modified) llvm/lib/Transforms/Utils/LoopUnroll.cpp (+10-1) 
- (added) llvm/test/DebugInfo/KeyInstructions/Generic/loop-unroll-runtime.ll 
(+69) 
- (added) llvm/test/DebugInfo/KeyInstructions/Generic/loop-unroll.ll (+48) 


``diff
diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp 
b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
index 45b49671dd3b6..1fa4eddc459c0 100644
--- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp
+++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp
@@ -752,6 +752,14 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo 
*LI,
 }
   }
 
+  // Remap source location atom instance. Do this now, rather than
+  // when we remap instructions, because remap is called once we've
+  // cloned all blocks (all the clones would get the same atom
+  // number).
+  if (!VMap.AtomMap.empty())
+for (Instruction &I : *New)
+  RemapSourceAtom(&I, VMap);
+
   // Update our running map of newest clones
   LastValueMap[*BB] = New;
   for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
@@ -802,7 +810,8 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo 
*LI,
   }
 }
 
-// Remap all instructions in the most recent iteration
+// Remap all instructions in the most recent iteration.
+// Key Instructions: Nothing to do - we've already remapped the atoms.
 remapInstructionsInBlocks(NewBlocks, LastValueMap);
 for (BasicBlock *NewBlock : NewBlocks)
   for (Instruction &I : *NewBlock)
diff --git a/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unroll-runtime.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unroll-runtime.ll
new file mode 100644
index 0..6deb04cc00f8d
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unroll-runtime.ll
@@ -0,0 +1,69 @@
+
+; RUN: opt %s -S --passes=loop-unroll -unroll-runtime=true -unroll-count=4 
-unroll-remainder -o - \
+; RUN: | FileCheck %s
+
+;; Check atoms are remapped for runtime unrolling.
+
+; CHECK: for.body.epil:
+; CHECK-NEXT: store i64 %indvars.iv.unr, ptr %p, align 4, !dbg [[G2R1:!.*]]
+
+; CHECK: for.body.epil.1:
+; CHECK-NEXT: store i64 %indvars.iv.next.epil, ptr %p, align 4, !dbg 
[[G3R1:!.*]]
+
+; CHECK: for.body.epil.2:
+; CHECK-NEXT: store i64 %indvars.iv.next.epil.1, ptr %p, align 4, !dbg 
[[G4R1:!.*]]
+
+; CHECK: for.body:
+; CHECK-NEXT: %indvars.iv = phi i64 [ 0, %for.body.lr.ph.new ], [ 
%indvars.iv.next.3, %for.body ]
+; CHECK-NEXT: %niter = phi i64 [ 0, %for.body.lr.ph.new ], [ %niter.next.3, 
%for.body ]
+; CHECK-NEXT: store i64 %indvars.iv, ptr %p, align 4, !dbg [[G1R1:!.*]]
+; CHECK-NEXT: %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+; CHECK-NEXT: store i64 %indvars.iv.next, ptr %p, align 4, !dbg [[G5R1:!.*]]
+; CHECK-NEXT: %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
+; CHECK-NEXT: store i64 %indvars.iv.next.1, ptr %p, align 4, !dbg [[G6R1:!.*]]
+; CHECK-NEXT: %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
+; CHECK-NEXT: store i64 %indvars.iv.next.2, ptr %p, align 4, !dbg [[G7R1:!.*]]
+; CHECK-NEXT: %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
+
+; CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
+; CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
+; CHECK: [[G4R1]] = !DILocation({{.*}}, atomGroup: 4, atomRank: 1)
+; CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
+; CHECK: [[G5R1]] = !DILocation({{.*}}, atomGroup: 5, atomRank: 1)
+; CHECK: [[G6R1]] = !DILocation({{.*}}, atomGroup: 6, atomRank: 1)
+; CHECK: [[G7R1]] = !DILocation({{.*}}, atomGroup: 7, atomRank: 1)
+
+define i32 @unroll(ptr %p, i32 %N) local_unnamed_addr !dbg !5 {
+entry:
+  %cmp9 = icmp eq i32 %N, 0
+  br i1 %cmp9, label %for.cond.cleanup, label %for.body.lr.ph
+
+for.body.lr.ph:   ; preds = %entry
+  %wide.trip.count = zext i32 %N to i64
+  br label %for.body
+
+for.cond.cleanup: ; preds = %for.body, %entry
+  %r = phi i32 [ 0, %entry ], [ 1, %for.body ]
+  ret i32 %r
+
+for.body: ; preds = %for.body, 
%for.body.lr.ph
+  %indvars.iv = phi i64 [ 0, %for.body.lr.ph ], [ %indvars.iv.next, %for.body ]
+  store i64 %indvars.iv, ptr %p, !dbg !8
+  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
+  %exitcond = icmp eq i64 %indvars.iv.next, %wide.trip.count
+  br i1 %exitcond, label %for.cond.cleanup, label %for.body
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: 
"debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.ll", directory: "/")
+!2 = !{i32 17}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", 

[llvm-branch-commits] [llvm] [KeyInstr] Add MIR parser support (PR #133494)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133494
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Inline atom info (PR #133481)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133481
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms duping bb with cond br on phi into pred (PR #133488)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133488
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms in blocks duplicated for threading (PR #133486)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133486
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Add Atom Group waterline to LLVMContext (PR #133478)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams ready_for_review 
https://github.com/llvm/llvm-project/pull/133478
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Add Atom Group waterline to LLVMContext (PR #133478)

2025-03-28 Thread via llvm-branch-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff a124bd2afc5b330a0679dc61b4b4faa59fb567e2 
d12e993d5e22cae5c6eeb7088eedbf8e3ec2cb98 --extensions h,cpp -- 
llvm/include/llvm/IR/LLVMContext.h llvm/lib/IR/DebugInfoMetadata.cpp 
llvm/lib/IR/LLVMContext.cpp llvm/lib/IR/LLVMContextImpl.h 
llvm/unittests/IR/MetadataTest.cpp
``





View the diff from clang-format here.


``diff
diff --git a/llvm/lib/IR/LLVMContext.cpp b/llvm/lib/IR/LLVMContext.cpp
index 4781085b30..5a11c28671 100644
--- a/llvm/lib/IR/LLVMContext.cpp
+++ b/llvm/lib/IR/LLVMContext.cpp
@@ -382,6 +382,4 @@ void LLVMContext::updateAtomGroupWaterline(uint64_t V) {
   pImpl->NextAtomGroup = std::max(pImpl->NextAtomGroup, V);
 }
 
-uint64_t LLVMContext::incNextAtomGroup() {
-  return pImpl->NextAtomGroup++;
-}
+uint64_t LLVMContext::incNextAtomGroup() { return pImpl->NextAtomGroup++; }
diff --git a/llvm/unittests/IR/MetadataTest.cpp 
b/llvm/unittests/IR/MetadataTest.cpp
index 8a1c1f9d30..a94a5aaf5f 100644
--- a/llvm/unittests/IR/MetadataTest.cpp
+++ b/llvm/unittests/IR/MetadataTest.cpp
@@ -6,8 +6,8 @@
 //
 
//===--===//
 
-#include "../lib/IR/LLVMContextImpl.h"
 #include "llvm/IR/Metadata.h"
+#include "../lib/IR/LLVMContextImpl.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/IR/Constants.h"
@@ -1371,7 +1371,8 @@ TEST_F(DILocationTest, KeyInstructions) {
   Context.pImpl->NextAtomGroup = 1;
 
   EXPECT_EQ(Context.pImpl->NextAtomGroup, 1u);
-  DILocation *A1 = DILocation::get(Context, 1, 0, getSubprogram(), nullptr, 
false, 1, 2);
+  DILocation *A1 =
+  DILocation::get(Context, 1, 0, getSubprogram(), nullptr, false, 1, 2);
   // The group is only applied to the DILocation if the build has opted into
   // the additional DILocation fields needed for the feature.
 #ifdef EXPERIMENTAL_KEY_INSTRUCTIONS

``




https://github.com/llvm/llvm-project/pull/133478
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms in blocks duplicated for threading (PR #133486)

2025-03-28 Thread via llvm-branch-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 637b5b4ae3c32e436aa4f03b980ba3332ebc3590 
3cc84e5c3d83c804332d4b7f9ad8057ddf906902 --extensions cpp -- 
llvm/lib/Transforms/Scalar/JumpThreading.cpp
``





View the diff from clang-format here.


``diff
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp 
b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 18dda2f3ad..14e5e16427 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -2100,7 +2100,7 @@ void 
JumpThreadingPass::cloneInstructions(ValueToValueMapTy &ValueMapping,
 if (const DebugLoc &DL = New->getDebugLoc())
   mapAtomInstance(DL, ValueMapping);
 
-  if (RetargetDbgValueIfPossible(New))
+if (RetargetDbgValueIfPossible(New))
   continue;
 
 // Remap operands to patch up intra-block references.

``




https://github.com/llvm/llvm-project/pull/133486
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][SimplifyCFG] Remap atoms when folding br to common succ into pred (PR #133482)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes

SimplifyCFG folds `b` into `a`.

+---+
|   v
--> a --> b --> c --> d -->
| ^
+-+

Remap source atoms in `b` so that the duplicated instructions are analysed
independently to determine is_stmt positions. This is necessary as the
contents of `b` may be folded into multiple preds in this way.

Add multi-pred test.

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


3 Files Affected:

- (modified) llvm/include/llvm/IR/DebugLoc.h (+10) 
- (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+25-7) 
- (added) 
llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-branch-fold.ll (+81) 


``diff
diff --git a/llvm/include/llvm/IR/DebugLoc.h b/llvm/include/llvm/IR/DebugLoc.h
index c22d3e9b10d27..7fdedc4acd919 100644
--- a/llvm/include/llvm/IR/DebugLoc.h
+++ b/llvm/include/llvm/IR/DebugLoc.h
@@ -76,6 +76,16 @@ namespace llvm {
 LLVMContext &Ctx,
 DenseMap &Cache);
 
+/// Return true if the source locations match, ignoring isImplicitCode and
+/// source atom info.
+bool isSameSourceLocation(const DebugLoc &Other) const {
+  if (get() == Other.get())
+return true;
+  return ((bool)*this == (bool)Other) && getLine() == Other.getLine() &&
+ getCol() == Other.getCol() && getScope() == Other.getScope() &&
+ getInlinedAt() == Other.getInlinedAt();
+}
+
 unsigned getLine() const;
 unsigned getCol() const;
 MDNode *getScope() const;
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index fd83ec1a7f4fe..1ba1e4ac81000 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -73,6 +73,7 @@
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/LockstepReverseIterator.h"
 #include "llvm/Transforms/Utils/ValueMapper.h"
@@ -1129,13 +1130,17 @@ static void 
cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
 
 Instruction *NewBonusInst = BonusInst.clone();
 
-if (!isa(BonusInst) &&
-PTI->getDebugLoc() != NewBonusInst->getDebugLoc()) {
-  // Unless the instruction has the same !dbg location as the original
-  // branch, drop it. When we fold the bonus instructions we want to make
-  // sure we reset their debug locations in order to avoid stepping on
-  // dead code caused by folding dead branches.
-  NewBonusInst->setDebugLoc(DebugLoc());
+if (!isa(BonusInst)) {
+  if (!NewBonusInst->getDebugLoc().isSameSourceLocation(
+  PTI->getDebugLoc())) {
+// Unless the instruction has the same !dbg location as the original
+// branch, drop it. When we fold the bonus instructions we want to make
+// sure we reset their debug locations in order to avoid stepping on
+// dead code caused by folding dead branches.
+NewBonusInst->setDebugLoc(DebugLoc());
+  } else if (const DebugLoc &DL = NewBonusInst->getDebugLoc()) {
+mapAtomInstance(DL, VMap);
+  }
 }
 
 RemapInstruction(NewBonusInst, VMap,
@@ -1182,6 +1187,19 @@ static void 
cloneInstructionsIntoPredecessorBlockAndUpdateSSAUses(
   U.set(NewBonusInst);
 }
   }
+
+  // Key Instructions: We may have propagated atom info into the pred. If the
+  // pred's terminator already has atom info do nothing as merging would drop
+  // one atom group anyway. If it doesn't, propagte the remapped atom group
+  // from BB's terminator.
+  if (auto &PredDL = PredBlock->getTerminator()->getDebugLoc()) {
+auto &DL = BB->getTerminator()->getDebugLoc();
+if (!PredDL->getAtomGroup() && DL && DL->getAtomGroup() &&
+PredDL.isSameSourceLocation(DL)) {
+  PredBlock->getTerminator()->setDebugLoc(DL);
+  RemapSourceAtom(PredBlock->getTerminator(), VMap);
+}
+  }
 }
 
 bool SimplifyCFGOpt::performValueComparisonIntoPredecessorFolding(
diff --git 
a/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-branch-fold.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-branch-fold.ll
new file mode 100644
index 0..8746f242007c3
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-branch-fold.ll
@@ -0,0 +1,81 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 5
+; RUN: opt %s -S -passes=simplifycfg -bonus-inst-threshold=2 | FileCheck %s
+
+;; Block d gets folded into preds b and c. Check the cloned instructions get
+;; remapped DILocation atomGroup numbers in each of the preds. Additionally
+;; check that the branches each inherit the atomGro

[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms in blocks duplicated for threading (PR #133486)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams created 
https://github.com/llvm/llvm-project/pull/133486

None

>From 3cc84e5c3d83c804332d4b7f9ad8057ddf906902 Mon Sep 17 00:00:00 2001
From: Orlando Cazalet-Hyams 
Date: Tue, 25 Mar 2025 09:51:43 +
Subject: [PATCH] [KeyInstr][JumpThreading] Remap atoms in blocks duplicated
 for threading

---
 llvm/lib/Transforms/Scalar/JumpThreading.cpp  | 15 +++-
 .../Generic/jump-threading-2-bbs.ll   | 68 +++
 2 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 
llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll

diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp 
b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 18d5f201413c8..18dda2f3ad82e 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -1995,6 +1995,14 @@ void JumpThreadingPass::updateSSA(BasicBlock *BB, 
BasicBlock *NewBB,
   }
 }
 
+static void remapSourceAtoms(ValueToValueMapTy &VM, BasicBlock::iterator Begin,
+ BasicBlock::iterator End) {
+  if (VM.AtomMap.empty())
+return;
+  for (auto It = Begin; It != End; ++It)
+RemapSourceAtom(&*It, VM);
+}
+
 /// Clone instructions in range [BI, BE) to NewBB.  For PHI nodes, we only 
clone
 /// arguments that come from PredBB.  Return the map from the variables in the
 /// source basic block to the variables in the newly created basic block.
@@ -2059,6 +2067,8 @@ void 
JumpThreadingPass::cloneInstructions(ValueToValueMapTy &ValueMapping,
 PHINode *NewPN = PHINode::Create(PN->getType(), 1, PN->getName(), NewBB);
 NewPN->addIncoming(PN->getIncomingValueForBlock(PredBB), PredBB);
 ValueMapping[PN] = NewPN;
+if (const DebugLoc &DL = PN->getDebugLoc())
+  mapAtomInstance(DL, ValueMapping);
   }
 
   // Clone noalias scope declarations in the threaded block. When threading a
@@ -2087,8 +2097,10 @@ void 
JumpThreadingPass::cloneInstructions(ValueToValueMapTy &ValueMapping,
 adaptNoAliasScopes(New, ClonedScopes, Context);
 
 CloneAndRemapDbgInfo(New, &*BI);
+if (const DebugLoc &DL = New->getDebugLoc())
+  mapAtomInstance(DL, ValueMapping);
 
-if (RetargetDbgValueIfPossible(New))
+  if (RetargetDbgValueIfPossible(New))
   continue;
 
 // Remap operands to patch up intra-block references.
@@ -2314,6 +2326,7 @@ void 
JumpThreadingPass::threadThroughTwoBasicBlocks(BasicBlock *PredPredBB,
{DominatorTree::Insert, PredPredBB, NewBB},
{DominatorTree::Delete, PredPredBB, PredBB}});
 
+  remapSourceAtoms(ValueMapping, NewBB->begin(), NewBB->end());
   updateSSA(PredBB, NewBB, ValueMapping);
 
   // Clean up things like PHI nodes with single operands, dead instructions,
diff --git 
a/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll
new file mode 100644
index 0..3ca81f554034f
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-2-bbs.ll
@@ -0,0 +1,68 @@
+; RUN: opt -S -passes=jump-threading,verify %s | FileCheck %s
+
+;; Modified from llvm/test/Transforms/JumpThreading/thread-two-bbs.ll
+;;
+;; JumpThreading duplicates bb.cond2 to thread through bb.file to bb.f2 or 
exit.
+;;
+;; Check the duplicated instructions get remapped atom groups.
+
+; CHECK: bb.cond2:
+; CHECK-NEXT: call void @f1()
+; CHECK-NEXT: %tobool1 = icmp eq i32 %cond2, 0, !dbg [[G1R2:!.*]]
+; CHECK-NEXT: br i1 %tobool1, label %exit, label %exit, !dbg [[G1R1:!.*]]
+
+; CHECK: bb.cond2.thread:
+; CHECK-NEXT: %tobool12 = icmp eq i32 %cond2, 0, !dbg [[G2R2:!.*]]
+; CHECK-NEXT: br i1 %tobool12, label %bb.f2, label %exit, !dbg [[G2R1:!.*]]
+
+; CHECK: [[G1R2]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
1, atomRank: 2)
+; CHECK: [[G1R1]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
1, atomRank: 1)
+; CHECK: [[G2R2]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
2, atomRank: 2)
+; CHECK: [[G2R1]] = !DILocation(line: 1, column: 1, scope: ![[#]], atomGroup: 
2, atomRank: 1)
+
+@a = global i32 0, align 4
+
+define void @foo(i32 %cond1, i32 %cond2) !dbg !5 {
+entry:
+  %tobool = icmp eq i32 %cond1, 0
+  br i1 %tobool, label %bb.cond2, label %bb.f1
+
+bb.f1:; preds = %entry
+  call void @f1()
+  br label %bb.cond2
+
+bb.cond2: ; preds = %bb.f1, %entry
+  %ptr = phi ptr [ null, %bb.f1 ], [ @a, %entry ]
+  %tobool1 = icmp eq i32 %cond2, 0, !dbg !9
+  br i1 %tobool1, label %bb.file, label %exit, !dbg !10
+
+bb.file:  ; preds = %bb.cond2
+  %cmp = icmp eq ptr %ptr, null
+  br i1 %cmp, label %exit, label %bb.f2
+
+bb.f2:; preds = %bb.file
+  call void @f2()
+  br label %exit
+
+exit: ; preds = %bb.f2, %bb.file, 
%bb.cond2
+  ret void
+}
+
+declare 

[llvm-branch-commits] [llvm] [KeyInstr][DwarfDebug] Add is_stmt emission support (PR #133495)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes

Interpret Key Instructions metadata to determine is_stmt placement.

The lowest rank (highest precedent) instructions in each {InlinedAt, atomGroup}
set are candidates for is_stmt. Only the last instruction in each set in a given
block gets is_stmt. Calls always get is_stmt.

---

Patch is 35.53 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/133495.diff


8 Files Affected:

- (modified) llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp (+198-12) 
- (modified) llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h (+9) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/dwarf-basic-ranks.ll (+68) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/dwarf-basic.ll (+62) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/dwarf-buoy-multi-key.mir 
(+78) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/dwarf-buoy.mir (+66) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/dwarf-calls.ll (+117) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/dwarf-ranks-blocks.ll (+65) 


``diff
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp 
b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 39f1299a24e81..71abef1d2383b 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -17,6 +17,7 @@
 #include "DwarfExpression.h"
 #include "DwarfUnit.h"
 #include "llvm/ADT/APInt.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/Twine.h"
@@ -170,6 +171,9 @@ static cl::opt 
MinimizeAddrInV5Option(
   "Stuff")),
 cl::init(DwarfDebug::MinimizeAddrInV5::Default));
 
+static cl::opt KeyInstructionsAreStmts("dwarf-use-key-instructions",
+ cl::Hidden, cl::init(false));
+
 static constexpr unsigned ULEB128PadSize = 4;
 
 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
@@ -2069,6 +2073,10 @@ void DwarfDebug::beginInstruction(const MachineInstr 
*MI) {
   unsigned LastAsmLine =
   Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
 
+  bool IsKey = false;
+  if (KeyInstructionsAreStmts && DL && DL.getLine())
+IsKey = KeyInstructions.contains(MI);
+
   if (!DL && MI == PrologEndLoc) {
 // In rare situations, we might want to place the end of the prologue
 // somewhere that doesn't have a source location already. It should be in
@@ -2087,13 +2095,18 @@ void DwarfDebug::beginInstruction(const MachineInstr 
*MI) {
 // If we have an ongoing unspecified location, nothing to do here.
 if (!DL)
   return;
-// We have an explicit location, same as the previous location.
-// But we might be coming back to it after a line 0 record.
-if ((LastAsmLine == 0 && DL.getLine() != 0) || Flags) {
-  // Reinstate the source location but not marked as a statement.
-  RecordSourceLine(DL, Flags);
+
+// Skip this if the instruction is Key, else we might accidentally miss an
+// is_stmt.
+if (!IsKey) {
+  // We have an explicit location, same as the previous location.
+  // But we might be coming back to it after a line 0 record.
+  if ((LastAsmLine == 0 && DL.getLine() != 0) || Flags) {
+// Reinstate the source location but not marked as a statement.
+RecordSourceLine(DL, Flags);
+  }
+  return;
 }
-return;
   }
 
   if (!DL) {
@@ -2136,11 +2149,17 @@ void DwarfDebug::beginInstruction(const MachineInstr 
*MI) {
 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
 PrologEndLoc = nullptr;
   }
-  // If the line changed, we call that a new statement; unless we went to
-  // line 0 and came back, in which case it is not a new statement.
-  unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
-  if (DL.getLine() && (DL.getLine() != OldLine || ForceIsStmt))
-Flags |= DWARF2_FLAG_IS_STMT;
+
+  if (KeyInstructionsAreStmts) {
+if (IsKey)
+  Flags |= DWARF2_FLAG_IS_STMT;
+  } else {
+// If the line changed, we call that a new statement; unless we went to
+// line 0 and came back, in which case it is not a new statement.
+unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
+if (DL.getLine() && (DL.getLine() != OldLine || ForceIsStmt))
+  Flags |= DWARF2_FLAG_IS_STMT;
+  }
 
   RecordSourceLine(DL, Flags);
 
@@ -2333,6 +2352,170 @@ DwarfDebug::emitInitialLocDirective(const 
MachineFunction &MF, unsigned CUID) {
   return PrologEndLoc;
 }
 
+void DwarfDebug::findKeyInstructions(const MachineFunction *MF) {
+  // New function - reset KeyInstructions.
+  KeyInstructions.clear();
+
+  // The current candidate is_stmt instructions for each source atom.
+  // Map {(InlinedAt, Group): (Rank, Instructions)}.
+  DenseMap,
+   std::pair>>
+  GroupCandidates;
+
+  // For each instruction:
+  //   * Skip insts without DebugLoc, 

[llvm-branch-commits] [llvm] [KeyInstr] Add MIR parser support (PR #133494)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


2 Files Affected:

- (modified) llvm/lib/CodeGen/MIRParser/MIParser.cpp (+25-1) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/parse.mir (+39) 


``diff
diff --git a/llvm/lib/CodeGen/MIRParser/MIParser.cpp 
b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
index 5c8e32d11cfb0..3a46654c59df1 100644
--- a/llvm/lib/CodeGen/MIRParser/MIParser.cpp
+++ b/llvm/lib/CodeGen/MIRParser/MIParser.cpp
@@ -2329,6 +2329,8 @@ bool MIParser::parseDILocation(MDNode *&Loc) {
   MDNode *Scope = nullptr;
   MDNode *InlinedAt = nullptr;
   bool ImplicitCode = false;
+  uint64_t AtomGroup = 0;
+  uint64_t AtomRank = 0;
 
   if (expectAndConsume(MIToken::lparen))
 return true;
@@ -2403,6 +2405,28 @@ bool MIParser::parseDILocation(MDNode *&Loc) {
   lex();
   continue;
 }
+if (Token.stringValue() == "atomGroup") {
+  lex();
+  if (expectAndConsume(MIToken::colon))
+return true;
+  if (Token.isNot(MIToken::IntegerLiteral) ||
+  Token.integerValue().isSigned())
+return error("expected unsigned integer");
+  AtomGroup = Token.integerValue().getZExtValue();
+  lex();
+  continue;
+}
+if (Token.stringValue() == "atomRank") {
+  lex();
+  if (expectAndConsume(MIToken::colon))
+return true;
+  if (Token.isNot(MIToken::IntegerLiteral) ||
+  Token.integerValue().isSigned())
+return error("expected unsigned integer");
+  AtomRank = Token.integerValue().getZExtValue();
+  lex();
+  continue;
+}
   }
   return error(Twine("invalid DILocation argument '") +
Token.stringValue() + "'");
@@ -2418,7 +2442,7 @@ bool MIParser::parseDILocation(MDNode *&Loc) {
 return error("DILocation requires a scope");
 
   Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
-InlinedAt, ImplicitCode);
+InlinedAt, ImplicitCode, AtomGroup, AtomRank);
   return false;
 }
 
diff --git a/llvm/test/DebugInfo/KeyInstructions/X86/parse.mir 
b/llvm/test/DebugInfo/KeyInstructions/X86/parse.mir
new file mode 100644
index 0..45cc23831412c
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/X86/parse.mir
@@ -0,0 +1,39 @@
+# RUN: llc %s --run-pass=none -o - | FileCheck %s
+
+## Check the MIR parser understands atomGroup and atomRank.
+
+# CHECK: RET64 $eax, debug-location !DILocation(line: 2, scope: ![[#]], 
atomGroup: 1, atomRank: 2)
+
+--- |
+  target triple = "x86_64-unknown-linux-gnu"
+  define hidden noundef i32 @p() local_unnamed_addr !dbg !5 {
+  entry:
+ret i32 0
+  }
+
+  declare void @_Z12prologue_endv() local_unnamed_addr
+
+  !llvm.dbg.cu = !{!0}
+  !llvm.module.flags = !{!2, !3}
+  !llvm.ident = !{!4}
+
+  !0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_17, file: !1, 
producer: "clang version 19.0.0", isOptimized: true, runtimeVersion: 0, 
emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
+  !1 = !DIFile(filename: "test.cpp", directory: "/")
+  !2 = !{i32 7, !"Dwarf Version", i32 5}
+  !3 = !{i32 2, !"Debug Info Version", i32 3}
+  !4 = !{!"clang version 19.0.0"}
+  !5 = distinct !DISubprogram(name: "p", scope: !1, file: !1, line: 1, type: 
!6, scopeLine: 1, flags: DIFlagPrototyped | DIFlagAllCallsDescribed, spFlags: 
DISPFlagDefinition | DISPFlagOptimized, unit: !0)
+  !6 = !DISubroutineType(types: !7)
+  !7 = !{}
+
+...
+---
+name:p
+alignment:   16
+body: |
+  bb.0.entry:
+liveins: $edx, $esi, $rbp, $rbx
+renamable $eax = XOR32rr undef $eax, undef $eax, implicit-def dead $eflags
+RET64 $eax, debug-location !DILocation(line: 2, scope: !5, atomGroup: 1, 
atomRank: 2)
+
+...

``




https://github.com/llvm/llvm-project/pull/133494
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][JumpThreading] Remap atoms duping bb with cond br on phi into pred (PR #133488)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:



@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-llvm-transforms

Author: Orlando Cazalet-Hyams (OCHyams)


Changes

See test for details.

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/JumpThreading.cpp (+9) 
- (added) 
llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll
 (+87) 


``diff
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp 
b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index d320f7dce11db..67f59b9864b1e 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -2683,6 +2683,9 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
   // PredBB block.  Evaluate PHI nodes in BB.
   ValueToValueMapTy ValueMapping;
 
+  // Remember the position before the inserted instructions.
+  auto RItBeforeInsertPt = std::next(OldPredBranch->getReverseIterator());
+
   BasicBlock::iterator BI = BB->begin();
   for (; PHINode *PN = dyn_cast(BI); ++BI)
 ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB);
@@ -2702,6 +2705,8 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
 
 // Remap debug variable operands.
 remapDebugVariable(ValueMapping, New);
+if (const DebugLoc &DL = New->getDebugLoc())
+  mapAtomInstance(DL, ValueMapping);
 
 // If this instruction can be simplified after the operands are updated,
 // just use the simplified value instead.  This frequently happens due to
@@ -2740,6 +2745,10 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
   addPHINodeEntriesForMappedBlock(BBBranch->getSuccessor(1), BB, PredBB,
   ValueMapping);
 
+  // KeyInstructions: Remap the cloned instructions' atoms only.
+  remapSourceAtoms(ValueMapping, std::prev(RItBeforeInsertPt)->getIterator(),
+   OldPredBranch->getIterator());
+
   updateSSA(BB, PredBB, ValueMapping);
 
   // PredBB no longer jumps to BB, remove entries in the PHI node for the edge
diff --git 
a/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll
 
b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll
new file mode 100644
index 0..ca1c748cc787a
--- /dev/null
+++ 
b/llvm/test/DebugInfo/KeyInstructions/Generic/jump-threading-dup-cond-br-on-phi-into-pred.ll
@@ -0,0 +1,87 @@
+; RUN: opt %s --passes=jump-threading -S -o - -S | FileCheck %s
+
+;;+-> T1 -+
+;;|   v  +-> T2
+;; Entry -+   Merge -+
+;;|   ^  +-> F2
+;;+-> F1 -+
+;;
+;; Duplicate Merge into T1 then fold Merge into its only pred F1 (taking its 
name).
+;;
+;;+-> T1 -> T2
+;;| \   ^
+;;|  \ /
+;;|   \   /
+;; Entry -+++
+;;| /   v 
+;;+--> Merge -> F2
+;;
+;; Check the duplicated  (into T1) instructions' atoms are remapped.
+
+; CHECK: T1:
+; CHECK-NEXT: %v1 = call i32 @f1()
+; CHECK-NEXT: %cond3 = icmp eq i32 %v1, 412
+; CHECK-NEXT: %C1 = add i32 %v1, 1, !dbg [[G3R2:!.*]]
+; CHECK-NEXT: store i32 %C1, ptr %p, align 4, !dbg [[G3R1:!.*]]
+
+; CHECK: Merge:
+; CHECK-NEXT: %v2 = call i32 @f2()
+; CHECK-NEXT: store i32 1, ptr %p, align 4, !dbg [[G1R1:!.*]]
+; CHECK-NEXT: %C = add i32 %v2, 1, !dbg [[G2R2:!.*]]
+; CHECK-NEXT: store i32 %C, ptr %p, align 4, !dbg [[G2R1:!.*]]
+
+; CHECK: [[G3R2]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 2)
+; CHECK: [[G3R1]] = !DILocation({{.*}}, atomGroup: 3, atomRank: 1)
+; CHECK: [[G1R1]] = !DILocation({{.*}}, atomGroup: 1, atomRank: 1)
+; CHECK: [[G2R2]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 2)
+; CHECK: [[G2R1]] = !DILocation({{.*}}, atomGroup: 2, atomRank: 1)
+
+define i32 @test5(i1 %cond, i1 %cond2, ptr %p) !dbg !5 {
+  br i1 %cond, label %T1, label %F1
+
+T1:   ; preds = %0
+  %v1 = call i32 @f1()
+  %cond3 = icmp eq i32 %v1, 412
+  br label %Merge
+
+F1:   ; preds = %0
+  %v2 = call i32 @f2()
+  store i32 1, ptr %p, align 4, !dbg !8
+  br label %Merge
+
+Merge:; preds = %F1, %T1
+  %A = phi i1 [ %cond3, %T1 ], [ %cond2, %F1 ]
+  %B = phi i32 [ %v1, %T1 ], [ %v2, %F1 ]
+  %C = add i32 %B, 1, !dbg !9
+  store i32 %C, ptr %p, align 4, !dbg !10
+  br i1 %A, label %T2, label %F2
+
+T2:   ; preds = %Merge
+  call void @f3()
+  ret i32 %B
+
+F2:   ; preds = %Merge
+  ret i32 %B
+}
+
+declare i32 @f1()
+
+declare i32 @f2()
+
+declare void @f3()
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: 
"debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "te

[llvm-branch-commits] [llvm] Propagate DebugLocs on phis in BreakCriticalEdges (PR #133492)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-transforms

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


2 Files Affected:

- (modified) llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp (+3) 
- (added) llvm/test/Transforms/CodeGenPrepare/X86/split-dbg.ll (+49) 


``diff
diff --git a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp 
b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
index d20902c577d3a..4d9d9e1fec48d 100644
--- a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
+++ b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
@@ -445,6 +445,7 @@ bool llvm::SplitIndirectBrCriticalEdges(Function &F,
   PHINode *NewIndPHI = PHINode::Create(IndPHI->getType(), 1, "ind", 
InsertPt);
   NewIndPHI->addIncoming(IndPHI->getIncomingValueForBlock(IBRPred),
  IBRPred);
+  NewIndPHI->setDebugLoc(IndPHI->getDebugLoc());
 
   // Create a PHI in the body block, to merge the direct and indirect
   // predecessors.
@@ -452,6 +453,8 @@ bool llvm::SplitIndirectBrCriticalEdges(Function &F,
   MergePHI->insertBefore(MergeInsert);
   MergePHI->addIncoming(NewIndPHI, Target);
   MergePHI->addIncoming(DirPHI, DirectSucc);
+  MergePHI->applyMergedLocation(DirPHI->getDebugLoc(),
+IndPHI->getDebugLoc());
 
   IndPHI->replaceAllUsesWith(MergePHI);
   IndPHI->eraseFromParent();
diff --git a/llvm/test/Transforms/CodeGenPrepare/X86/split-dbg.ll 
b/llvm/test/Transforms/CodeGenPrepare/X86/split-dbg.ll
new file mode 100644
index 0..773ae3ff9a3e5
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/split-dbg.ll
@@ -0,0 +1,49 @@
+; RUN: opt -passes='require,function(codegenprepare)' -S 
-mtriple=x86_64 < %s \
+; RUN: | FileCheck %s
+
+;; Check debug locations are propagated onto new PHIs.
+
+; CHECK: .split:
+; CHECK-NEXT: %merge = phi i32 [ poison, %while.body ], [ %dest.sroa.clone, 
%while.body.clone ], !dbg [[DBG:!.*]]
+
+; CHECK: while.body.clone:
+; CHECK-NEXT: %dest.sroa.clone = phi i32 [ %1, %.split ], [ poison, %if.else 
], !dbg [[DBG]]
+
+define void @test(i1 %c) !dbg !5 {
+entry:
+  br label %if.else
+
+if.else:  ; preds = %if.else1, %entry
+  br i1 %c, label %while.body, label %preheader
+
+preheader:; preds = %if.else
+  br label %if.else1
+
+if.then:  ; preds = %if.else1
+  unreachable
+
+while.body:   ; preds = %if.else1, 
%while.body, %if.else
+  %dest.sroa = phi i32 [ %1, %while.body ], [ poison, %if.else1 ], [ poison, 
%if.else ], !dbg !8
+  %0 = inttoptr i32 %dest.sroa to ptr
+  %incdec.ptr = getelementptr inbounds i8, ptr %0, i32 -1
+  %1 = ptrtoint ptr %incdec.ptr to i32
+  store i8 0, ptr %incdec.ptr, align 1
+  br label %while.body
+
+if.else1: ; preds = %if.else1, 
%preheader
+  indirectbr ptr poison, [label %if.then, label %while.body, label %if.else, 
label %if.else1]
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: 
"debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.ll", directory: "/")
+!2 = !{i32 11}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "test", linkageName: "test", scope: null, 
file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | 
DISPFlagOptimized, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!8 = !DILocation(line: 1, column: 1, scope: !5)

``




https://github.com/llvm/llvm-project/pull/133492
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][SimplifyCFG] Remap atoms after duplication for threading (PR #133484)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes

Given the same branch condition in `a` and `c` SimplifyCFG converts:

  +> b -+
  | v
  --> a --> c --> e -->
| ^
+> d -+

into:

  +--> bcd ---+
  |   v
  --> a --> c --> e -->

Remap source atoms on instructions duplicated from `c` into `bcd`.

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


2 Files Affected:

- (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+6-6) 
- (added) llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll 
(+62) 


``diff
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp 
b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1ba1e4ac81000..c83ff0260e297 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -3589,7 +3589,7 @@ foldCondBranchOnValueKnownInPredecessorImpl(BranchInst 
*BI, DomTreeUpdater *DTU,
 // instructions into EdgeBB.  We know that there will be no uses of the
 // cloned instructions outside of EdgeBB.
 BasicBlock::iterator InsertPt = EdgeBB->getFirstInsertionPt();
-DenseMap TranslateMap; // Track translated values.
+ValueToValueMapTy TranslateMap; // Track translated values.
 TranslateMap[Cond] = CB;
 
 // RemoveDIs: track instructions that we optimise away while folding, so
@@ -3609,11 +3609,11 @@ foldCondBranchOnValueKnownInPredecessorImpl(BranchInst 
*BI, DomTreeUpdater *DTU,
 N->setName(BBI->getName() + ".c");
 
   // Update operands due to translation.
-  for (Use &Op : N->operands()) {
-DenseMap::iterator PI = TranslateMap.find(Op);
-if (PI != TranslateMap.end())
-  Op = PI->second;
-  }
+  // Key Instructions: Remap all the atom groups.
+  if (const DebugLoc &DL = BBI->getDebugLoc())
+mapAtomInstance(DL, TranslateMap);
+  RemapInstruction(N, TranslateMap,
+   RF_IgnoreMissingLocals | RF_NoModuleLevelChanges);
 
   // Check for trivial simplification.
   if (Value *V = simplifyInstruction(N, {DL, nullptr, nullptr, AC})) {
diff --git 
a/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll
new file mode 100644
index 0..f8477600c6418
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/simplifycfg-thread-phi.ll
@@ -0,0 +1,62 @@
+; RUN: opt %s -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 
-S \
+; RUN: | FileCheck %s
+
+;; Generated using:
+;;   opt -passes=debugify --debugify-atoms --debugify-level=locations \
+;;  llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll
+;; With unused/untested metadata nodes removed.
+
+;; Check the duplicated store gets distinct atom info in each branch.
+
+; CHECK-LABEL: @bar(
+; CHECK: if.then:
+; CHECK:   store i32 1{{.*}}, !dbg [[DBG1:!.*]]
+; CHECK: if.end.1.critedge:
+; CHECK:   store i32 1{{.*}}, !dbg [[DBG2:!.*]]
+; CHECK: [[DBG1]] = !DILocation(line: 1{{.*}}, atomGroup: 1
+; CHECK: [[DBG2]] = !DILocation(line: 1{{.*}}, atomGroup: 2
+
+define void @bar(i32 %aa) !dbg !5 {
+entry:
+  %aa.addr = alloca i32, align 4
+  %bb = alloca i32, align 4
+  store i32 %aa, ptr %aa.addr, align 4
+  store i32 0, ptr %bb, align 4
+  %tobool = icmp ne i32 %aa, 0
+  br i1 %tobool, label %if.then, label %if.end
+
+if.then:  ; preds = %entry
+  call void @foo()
+  br label %if.end
+
+if.end:   ; preds = %if.then, %entry
+  store i32 1, ptr %bb, align 4, !dbg !8
+  br i1 %tobool, label %if.then.1, label %if.end.1
+
+if.then.1:; preds = %if.end
+  call void @foo()
+  br label %if.end.1
+
+if.end.1: ; preds = %if.then.1, %if.end
+  store i32 2, ptr %bb, align 4
+  br label %for.end
+
+for.end:  ; preds = %if.end.1
+  ret void
+}
+
+declare void @foo()
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: 
"debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: 
"llvm/test/Transforms/SimplifyCFG/debug-info-thread-phi.ll", directory: "/")
+!2 = !{i32 15}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "bar", linkageName: "bar", scope: null, 
file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | 
DISPFlagOptimized, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!8 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 1)

``




https://github.com/llvm/llvm-project/pull/133484
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lis

[llvm-branch-commits] [llvm] [KeyInstr][LoopUnswitch] Remap cloned instructions' atoms (PR #133491)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp (+4) 
- (added) llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll (+137) 


``diff
diff --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp 
b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index 4f7956514b7b5..e0b403a18a718 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -297,6 +297,10 @@ static void buildPartialInvariantUnswitchConditionalBranch(
   for (auto *Val : reverse(ToDuplicate)) {
 Instruction *Inst = cast(Val);
 Instruction *NewInst = Inst->clone();
+
+if (const DebugLoc &DL = Inst->getDebugLoc())
+  mapAtomInstance(DL, VMap);
+
 NewInst->insertInto(&BB, BB.end());
 RemapInstruction(NewInst, VMap,
  RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
diff --git a/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll 
b/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll
new file mode 100644
index 0..9d696e78b2639
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/Generic/loop-unswitch.ll
@@ -0,0 +1,137 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 5
+; RUN: opt %s -S --passes="loop-mssa(simple-loop-unswitch)" -o - \
+; RUN: | FileCheck %s
+
+;; The important thing here is that the instructions duplicated from
+;; LOOP_HEADER into ENTRY, and those duplicated from LOOP_LATCH into
+;; LOOP_LATCH_US, get remapped atom numbers.
+
+define i32 @partial_unswitch_true_successor_hoist_invariant(ptr %ptr, i32 %N) 
!dbg !5 {
+; CHECK-LABEL: define i32 @partial_unswitch_true_successor_hoist_invariant(
+; CHECK-SAME: ptr [[PTR:%.*]], i32 [[N:%.*]]) !dbg [[DBG5:![0-9]+]] {
+; CHECK-NEXT:  [[ENTRY:.*:]]
+; CHECK-NEXT:[[TMP0:%.*]] = getelementptr i32, ptr [[PTR]], i64 1, !dbg 
[[DBG8:![0-9]+]]
+; CHECK-NEXT:[[TMP1:%.*]] = load i32, ptr [[TMP0]], align 4, !dbg 
[[DBG9:![0-9]+]]
+; CHECK-NEXT:[[TMP2:%.*]] = icmp eq i32 [[TMP1]], 100, !dbg 
[[DBG10:![0-9]+]]
+; CHECK-NEXT:br i1 [[TMP2]], label %[[ENTRY_SPLIT_US:.*]], label 
%[[ENTRY_SPLIT:.*]]
+; CHECK:   [[ENTRY_SPLIT_US]]:
+; CHECK-NEXT:br label %[[LOOP_HEADER_US:.*]], !dbg [[DBG11:![0-9]+]]
+; CHECK:   [[LOOP_HEADER_US]]:
+; CHECK-NEXT:[[IV_US:%.*]] = phi i32 [ 0, %[[ENTRY_SPLIT_US]] ], [ 
[[IV_NEXT_US:%.*]], %[[LOOP_LATCH_US:.*]] ], !dbg [[DBG12:![0-9]+]]
+; CHECK-NEXT:br label %[[NOCLOBBER_US:.*]], !dbg [[DBG13:![0-9]+]]
+; CHECK:   [[NOCLOBBER_US]]:
+; CHECK-NEXT:br label %[[LOOP_LATCH_US]], !dbg [[DBG14:![0-9]+]]
+; CHECK:   [[LOOP_LATCH_US]]:
+; CHECK-NEXT:[[C_US:%.*]] = icmp ult i32 [[IV_US]], [[N]], !dbg 
[[DBG15:![0-9]+]]
+; CHECK-NEXT:[[IV_NEXT_US]] = add i32 [[IV_US]], 1, !dbg [[DBG16:![0-9]+]]
+; CHECK-NEXT:br i1 [[C_US]], label %[[LOOP_HEADER_US]], label 
%[[EXIT_SPLIT_US:.*]], !dbg [[DBG17:![0-9]+]]
+; CHECK:   [[EXIT_SPLIT_US]]:
+; CHECK-NEXT:br label %[[EXIT:.*]], !dbg [[DBG18:![0-9]+]]
+; CHECK:   [[ENTRY_SPLIT]]:
+; CHECK-NEXT:br label %[[LOOP_HEADER:.*]], !dbg [[DBG19:![0-9]+]]
+; CHECK:   [[LOOP_HEADER]]:
+; CHECK-NEXT:[[IV:%.*]] = phi i32 [ 0, %[[ENTRY_SPLIT]] ], [ 
[[IV_NEXT:%.*]], %[[LOOP_LATCH:.*]] ], !dbg [[DBG20:![0-9]+]]
+; CHECK-NEXT:[[GEP:%.*]] = getelementptr i32, ptr [[PTR]], i64 1, !dbg 
[[DBG21:![0-9]+]]
+; CHECK-NEXT:[[LV:%.*]] = load i32, ptr [[GEP]], align 4, !dbg 
[[DBG22:![0-9]+]]
+; CHECK-NEXT:[[SC:%.*]] = icmp eq i32 [[LV]], 100, !dbg [[DBG23:![0-9]+]]
+; CHECK-NEXT:br i1 [[SC]], label %[[NOCLOBBER:.*]], label %[[CLOBBER:.*]], 
!dbg [[DBG24:![0-9]+]]
+; CHECK:   [[NOCLOBBER]]:
+; CHECK-NEXT:br label %[[LOOP_LATCH]], !dbg [[DBG25:![0-9]+]]
+; CHECK:   [[CLOBBER]]:
+; CHECK-NEXT:call void @clobber(), !dbg [[DBG26:![0-9]+]]
+; CHECK-NEXT:br label %[[LOOP_LATCH]], !dbg [[DBG27:![0-9]+]]
+; CHECK:   [[LOOP_LATCH]]:
+; CHECK-NEXT:[[C:%.*]] = icmp ult i32 [[IV]], [[N]], !dbg [[DBG28:![0-9]+]]
+; CHECK-NEXT:[[IV_NEXT]] = add i32 [[IV]], 1, !dbg [[DBG29:![0-9]+]]
+; CHECK-NEXT:br i1 [[C]], label %[[LOOP_HEADER]], label 
%[[EXIT_SPLIT:.*]], !dbg [[DBG30:![0-9]+]], !llvm.loop [[LOOP31:![0-9]+]]
+; CHECK:   [[EXIT_SPLIT]]:
+; CHECK-NEXT:br label %[[EXIT]], !dbg [[DBG33:![0-9]+]]
+; CHECK:   [[EXIT]]:
+; CHECK-NEXT:ret i32 10, !dbg [[DBG33]]
+;
+entry:
+  br label %loop.header, !dbg !8
+
+loop.header:  ; preds = %loop.latch, %entry
+  %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop.latch ], !dbg !9
+  %gep = getelementptr i32, ptr %ptr, i64 1, !dbg !10
+  %lv = load i32, ptr %gep, align 4, !dbg !11
+  %sc = icmp eq i32 %lv, 100, !dbg !12
+  br i1 %sc, label %noclobber, lab

[llvm-branch-commits] [llvm] [KeyInstr] Remap cloned PHIs in BreakCriticalEdges (PR #133493)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-debuginfo

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


3 Files Affected:

- (modified) llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp (+3) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll 
(+52) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg (+2) 


``diff
diff --git a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp 
b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
index 4d9d9e1fec48d..3a1aa5cfab191 100644
--- a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
+++ b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
@@ -397,6 +397,9 @@ bool llvm::SplitIndirectBrCriticalEdges(Function &F,
 // preds.
 ValueToValueMapTy VMap;
 BasicBlock *DirectSucc = CloneBasicBlock(Target, VMap, ".clone", &F);
+if (!VMap.AtomMap.empty())
+  for (Instruction &I : *DirectSucc)
+RemapSourceAtom(&I, VMap);
 
 BlockFrequency BlockFreqForDirectSucc;
 for (BasicBlock *Pred : OtherPreds) {
diff --git a/llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll 
b/llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll
new file mode 100644
index 0..e46b5c1ef6936
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll
@@ -0,0 +1,52 @@
+; RUN: opt -passes='require,function(codegenprepare)' -S 
-mtriple=x86_64 < %s \
+; RUN: | FileCheck %s
+
+;; Check debug locations are propagated onto new PHIs.
+
+; CHECK: .split:
+; CHECK-NEXT: %merge = phi i32 [ poison, %while.body ], [ %dest.sroa.clone, 
%while.body.clone ], !dbg [[G1R1:!.*]]
+
+; CHECK: while.body.clone:
+; CHECK-NEXT: %dest.sroa.clone = phi i32 [ %1, %.split ], [ poison, %if.else 
], !dbg [[G2R1:!.*]]
+
+; CHECK: [[G1R1]] = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, 
atomRank: 1)
+; CHECK: [[G2R1]] = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 2, 
atomRank: 1)
+
+define void @test(i1 %c) !dbg !5 {
+entry:
+  br label %if.else
+
+if.else:  ; preds = %if.else1, %entry
+  br i1 %c, label %while.body, label %preheader
+
+preheader:; preds = %if.else
+  br label %if.else1
+
+if.then:  ; preds = %if.else1
+  unreachable
+
+while.body:   ; preds = %if.else1, 
%while.body, %if.else
+  %dest.sroa = phi i32 [ %1, %while.body ], [ poison, %if.else1 ], [ poison, 
%if.else ], !dbg !12
+  %0 = inttoptr i32 %dest.sroa to ptr
+  %incdec.ptr = getelementptr inbounds i8, ptr %0, i32 -1
+  %1 = ptrtoint ptr %incdec.ptr to i32
+  store i8 0, ptr %incdec.ptr, align 1
+  br label %while.body
+
+if.else1: ; preds = %if.else1, 
%preheader
+  indirectbr ptr poison, [label %if.then, label %while.body, label %if.else, 
label %if.else1]
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: 
"debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.ll", directory: "/")
+!2 = !{i32 11}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "test", linkageName: "test", scope: null, 
file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | 
DISPFlagOptimized, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!12 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 1)
diff --git a/llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg 
b/llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg
new file mode 100644
index 0..42bf50dcc13c3
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg
@@ -0,0 +1,2 @@
+if not "X86" in config.root.targets:
+config.unsupported = True

``




https://github.com/llvm/llvm-project/pull/133493
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr] Remap cloned PHIs in BreakCriticalEdges (PR #133493)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-transforms

Author: Orlando Cazalet-Hyams (OCHyams)


Changes



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


3 Files Affected:

- (modified) llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp (+3) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll 
(+52) 
- (added) llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg (+2) 


``diff
diff --git a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp 
b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
index 4d9d9e1fec48d..3a1aa5cfab191 100644
--- a/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
+++ b/llvm/lib/Transforms/Utils/BreakCriticalEdges.cpp
@@ -397,6 +397,9 @@ bool llvm::SplitIndirectBrCriticalEdges(Function &F,
 // preds.
 ValueToValueMapTy VMap;
 BasicBlock *DirectSucc = CloneBasicBlock(Target, VMap, ".clone", &F);
+if (!VMap.AtomMap.empty())
+  for (Instruction &I : *DirectSucc)
+RemapSourceAtom(&I, VMap);
 
 BlockFrequency BlockFreqForDirectSucc;
 for (BasicBlock *Pred : OtherPreds) {
diff --git a/llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll 
b/llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll
new file mode 100644
index 0..e46b5c1ef6936
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/X86/cgp-break-critical-edge.ll
@@ -0,0 +1,52 @@
+; RUN: opt -passes='require,function(codegenprepare)' -S 
-mtriple=x86_64 < %s \
+; RUN: | FileCheck %s
+
+;; Check debug locations are propagated onto new PHIs.
+
+; CHECK: .split:
+; CHECK-NEXT: %merge = phi i32 [ poison, %while.body ], [ %dest.sroa.clone, 
%while.body.clone ], !dbg [[G1R1:!.*]]
+
+; CHECK: while.body.clone:
+; CHECK-NEXT: %dest.sroa.clone = phi i32 [ %1, %.split ], [ poison, %if.else 
], !dbg [[G2R1:!.*]]
+
+; CHECK: [[G1R1]] = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, 
atomRank: 1)
+; CHECK: [[G2R1]] = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 2, 
atomRank: 1)
+
+define void @test(i1 %c) !dbg !5 {
+entry:
+  br label %if.else
+
+if.else:  ; preds = %if.else1, %entry
+  br i1 %c, label %while.body, label %preheader
+
+preheader:; preds = %if.else
+  br label %if.else1
+
+if.then:  ; preds = %if.else1
+  unreachable
+
+while.body:   ; preds = %if.else1, 
%while.body, %if.else
+  %dest.sroa = phi i32 [ %1, %while.body ], [ poison, %if.else1 ], [ poison, 
%if.else ], !dbg !12
+  %0 = inttoptr i32 %dest.sroa to ptr
+  %incdec.ptr = getelementptr inbounds i8, ptr %0, i32 -1
+  %1 = ptrtoint ptr %incdec.ptr to i32
+  store i8 0, ptr %incdec.ptr, align 1
+  br label %while.body
+
+if.else1: ; preds = %if.else1, 
%preheader
+  indirectbr ptr poison, [label %if.then, label %while.body, label %if.else, 
label %if.else1]
+}
+
+!llvm.dbg.cu = !{!0}
+!llvm.debugify = !{!2, !3}
+!llvm.module.flags = !{!4}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: 
"debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.ll", directory: "/")
+!2 = !{i32 11}
+!3 = !{i32 0}
+!4 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "test", linkageName: "test", scope: null, 
file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | 
DISPFlagOptimized, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{}
+!12 = !DILocation(line: 1, column: 1, scope: !5, atomGroup: 1, atomRank: 1)
diff --git a/llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg 
b/llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg
new file mode 100644
index 0..42bf50dcc13c3
--- /dev/null
+++ b/llvm/test/DebugInfo/KeyInstructions/X86/lit.local.cfg
@@ -0,0 +1,2 @@
+if not "X86" in config.root.targets:
+config.unsupported = True

``




https://github.com/llvm/llvm-project/pull/133493
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [KeyInstr][SimplifyCFG] Remap atoms when folding br to common succ into pred (PR #133482)

2025-03-28 Thread Orlando Cazalet-Hyams via llvm-branch-commits

https://github.com/OCHyams edited 
https://github.com/llvm/llvm-project/pull/133482
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Defer a shouldKeep call in operand reduction (PR #133387)

2025-03-28 Thread Arthur Eubanks via llvm-branch-commits

https://github.com/aeubanks approved this pull request.


https://github.com/llvm/llvm-project/pull/133387
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing operand bundles when removing arguments (PR #133410)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/133410

None

>From d7da2747f2b740f85e74dec9655d4136be9ce64a Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 16:32:06 +0700
Subject: [PATCH] llvm-reduce: Fix losing operand bundles when removing
 arguments

---
 .../remove-arguments-preserve-bundles.ll  | 28 +++
 .../llvm-reduce/deltas/ReduceArguments.cpp|  6 ++--
 2 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 
llvm/test/tools/llvm-reduce/remove-arguments-preserve-bundles.ll

diff --git a/llvm/test/tools/llvm-reduce/remove-arguments-preserve-bundles.ll 
b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-bundles.ll
new file mode 100644
index 0..b370aa5d1dd3b
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-bundles.ll
@@ -0,0 +1,28 @@
+; Check that when removing arguments, existing bundles are preserved
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=arguments 
--test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s 
--test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=RESULT %s < %t
+
+; INTERESTING-LABEL: @convergent_callee(
+define i32 @convergent_callee(i32 %a, i32 %b) convergent {
+  ret i32 %a
+}
+
+; INTERESTING-LABEL: @convergent_callee_decl(
+declare i32 @convergent_callee_decl(i32 %a, i32 %b) convergent
+
+; INTERESTING-LABEL: @convergent_caller(
+; INTERESTING: call i32
+; INTERESTING: call i32
+
+; RESULT-LABEL: define i32 @convergent_caller()
+; RESULT: %call0 = call i32 @convergent_callee() [ "convergencectrl"(token 
%entry.token) ]
+; RESULT: %call1 = call i32 @convergent_callee_decl() [ 
"convergencectrl"(token %entry.token) ]
+define i32 @convergent_caller(i32 %x) convergent {
+  %entry.token = call token @llvm.experimental.convergence.entry()
+  %call0 = call i32 @convergent_callee(i32 %x, i32 2) [ 
"convergencectrl"(token %entry.token) ]
+  %call1 = call i32 @convergent_callee_decl(i32 %x, i32 2) [ 
"convergencectrl"(token %entry.token) ]
+  %result = add i32 %call0, %call1
+  ret i32 %result
+}
+
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
index 11c3ab027f61f..e9d9fb4153994 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
@@ -61,8 +61,10 @@ static void replaceFunctionCalls(Function &OldF, Function 
&NewF,
 }
   }
 
-  // FIXME: Losing bundles
-  CallInst *NewCI = CallInst::Create(&NewF, Args);
+  SmallVector OpBundles;
+  CI->getOperandBundlesAsDefs(OpBundles);
+
+  CallInst *NewCI = CallInst::Create(&NewF, Args, OpBundles);
   NewCI->setCallingConv(NewF.getCallingConv());
 
   AttrBuilder CallSiteAttrs(Ctx, CI->getAttributes().getFnAttrs());

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] [llvm] [ctxprof] root autodetection mechanism (PR #133147)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133147

>From 0080eef0e2fce86d3b0059b59d8b319a0e78e05f Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Mon, 24 Mar 2025 12:01:10 -0700
Subject: [PATCH] RootAutodetect

---
 compiler-rt/lib/ctx_profile/CMakeLists.txt|   2 +-
 .../lib/ctx_profile/CtxInstrContextNode.h |   1 +
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 119 +++
 .../lib/ctx_profile/CtxInstrProfiling.h   |   2 +-
 .../lib/ctx_profile/RootAutoDetector.cpp  |  83 
 .../lib/ctx_profile/RootAutoDetector.h|  31 +++
 .../TestCases/autodetect-roots.cpp| 188 ++
 .../TestCases/generate-context.cpp|   5 +-
 .../llvm/ProfileData/CtxInstrContextNode.h|   1 +
 .../Instrumentation/PGOCtxProfLowering.cpp|  26 ++-
 .../PGOProfile/ctx-instrumentation.ll |  20 +-
 11 files changed, 416 insertions(+), 62 deletions(-)
 create mode 100644 compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp

diff --git a/compiler-rt/lib/ctx_profile/CMakeLists.txt 
b/compiler-rt/lib/ctx_profile/CMakeLists.txt
index bb606449c61b1..446ebc96408dd 100644
--- a/compiler-rt/lib/ctx_profile/CMakeLists.txt
+++ b/compiler-rt/lib/ctx_profile/CMakeLists.txt
@@ -27,7 +27,7 @@ endif()
 add_compiler_rt_runtime(clang_rt.ctx_profile
   STATIC
   ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
-  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
+  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc 
RTSanitizerCommonSymbolizer
   CFLAGS ${EXTRA_FLAGS}
   SOURCES ${CTX_PROFILE_SOURCES}
   ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h 
b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
index a42bf9ebb01ea..aa052bc7eea6c 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
@@ -127,6 +127,7 @@ class ContextNode final {
 /// MUTEXDECL takes one parameter, the name of a field that is a mutex.
 #define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL)
\
   PTRDECL(FunctionData, Next)  
\
+  PTRDECL(void, EntryAddress)  
\
   VOLATILE_PTRDECL(ContextRoot, CtxRoot)   
\
   VOLATILE_PTRDECL(ContextNode, FlatCtx)   
\
   MUTEXDECL(Mutex)
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index da291e0bbabdd..09ed607cde3aa 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "CtxInstrProfiling.h"
+#include "RootAutoDetector.h"
 #include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_atomic_clang.h"
@@ -43,6 +44,12 @@ Arena *FlatCtxArena = nullptr;
 __thread bool IsUnderContext = false;
 __sanitizer::atomic_uint8_t ProfilingStarted = {};
 
+__sanitizer::atomic_uintptr_t RootDetector = {};
+RootAutoDetector *getRootDetector() {
+  return reinterpret_cast(
+  __sanitizer::atomic_load_relaxed(&RootDetector));
+}
+
 // utility to taint a pointer by setting the LSB. There is an assumption
 // throughout that the addresses of contexts are even (really, they should be
 // align(8), but "even"-ness is the minimum assumption)
@@ -201,7 +208,7 @@ ContextNode *getCallsiteSlow(GUID Guid, ContextNode 
**InsertionPoint,
   return Ret;
 }
 
-ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
+ContextNode *getFlatProfile(FunctionData &Data, void *Callee, GUID Guid,
 uint32_t NumCounters) {
   if (ContextNode *Existing = Data.FlatCtx)
 return Existing;
@@ -232,6 +239,7 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
 auto *Ret = allocContextNode(AllocBuff, Guid, NumCounters, 0);
 Data.FlatCtx = Ret;
 
+Data.EntryAddress = Callee;
 Data.Next = reinterpret_cast(
 __sanitizer::atomic_load_relaxed(&AllFunctionsData));
 while (!__sanitizer::atomic_compare_exchange_strong(
@@ -277,8 +285,29 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
   return Root;
 }
 
-ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
- uint32_t NumCounters) {
+ContextNode *tryStartContextGivenRoot(ContextRoot *Root, GUID Guid,
+  uint32_t Counters, uint32_t Callsites)
+SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
+  IsUnderContext = true;
+  __sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
+__sanitizer::memory_order_relaxed);
+
+  if (!Root->FirstMemBlock) {
+setupContext(Root, Guid, Counters, Callsites);
+  }
+  if (Root->Taken.TryL

[llvm-branch-commits] [llvm] llvm-reduce: Fix losing fast math flags when removing arguments (PR #133408)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

arsenm wrote:

### Merge activity

* **Mar 28, 12:07 PM EDT**: A user started a stack merge that includes this 
pull request via 
[Graphite](https://app.graphite.dev/github/pr/llvm/llvm-project/133408).


https://github.com/llvm/llvm-project/pull/133408
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing fast math flags in operands-to-args (PR #133421)

2025-03-28 Thread Shilei Tian via llvm-branch-commits

https://github.com/shiltian approved this pull request.


https://github.com/llvm/llvm-project/pull/133421
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] [ctxprof][nfc] Move 2 implementation functions up in `CtxInstrProfiling.cpp` (PR #133146)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133146

>From 9065433243519e0d44bca6eee04f4b9cd72d35ca Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Wed, 26 Mar 2025 10:10:43 -0700
Subject: [PATCH] [ctxprof][nfc] Move 2 implementation functions up in
 `CtxInstrProfiling.cpp`

---
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 66 +--
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index b0e63a8861d86..da291e0bbabdd 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -244,6 +244,39 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
   return Data.FlatCtx;
 }
 
+// This should be called once for a Root. Allocate the first arena, set up the
+// first context.
+void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
+  uint32_t NumCallsites) {
+  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
+  &AllContextsMutex);
+  // Re-check - we got here without having had taken a lock.
+  if (Root->FirstMemBlock)
+return;
+  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
+  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
+  Root->FirstMemBlock = M;
+  Root->CurrentMem = M;
+  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
+ NumCounters, NumCallsites);
+  AllContextRoots.PushBack(Root);
+}
+
+ContextRoot *FunctionData::getOrAllocateContextRoot() {
+  auto *Root = CtxRoot;
+  if (Root)
+return Root;
+  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
+  Root = CtxRoot;
+  if (!Root) {
+Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
+CtxRoot = Root;
+  }
+
+  assert(Root);
+  return Root;
+}
+
 ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
  uint32_t NumCounters) {
 
@@ -333,39 +366,6 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData 
*Data, void *Callee,
   return Ret;
 }
 
-// This should be called once for a Root. Allocate the first arena, set up the
-// first context.
-void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
-  uint32_t NumCallsites) {
-  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
-  &AllContextsMutex);
-  // Re-check - we got here without having had taken a lock.
-  if (Root->FirstMemBlock)
-return;
-  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
-  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
-  Root->FirstMemBlock = M;
-  Root->CurrentMem = M;
-  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
- NumCounters, NumCallsites);
-  AllContextRoots.PushBack(Root);
-}
-
-ContextRoot *FunctionData::getOrAllocateContextRoot() {
-  auto *Root = CtxRoot;
-  if (Root)
-return Root;
-  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
-  Root = CtxRoot;
-  if (!Root) {
-Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
-CtxRoot = Root;
-  }
-
-  assert(Root);
-  return Root;
-}
-
 ContextNode *__llvm_ctx_profile_start_context(
 FunctionData *FData, GUID Guid, uint32_t Counters,
 uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Preserve original callsite calling conv when removing arguments (PR #133411)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133411

>From dc033ac0955d646d7b778273a144c29ce71c10cf Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 16:43:21 +0700
Subject: [PATCH] llvm-reduce: Preserve original callsite calling conv when
 removing arguments

In undefined mismatch cases, this was fixing the callsite to use the calling
convention of the new function. Preserve the original wrong callsite's calling
convention.
---
 .../remove-arguments-preserve-wrong-cc.ll | 27 +++
 .../llvm-reduce/deltas/ReduceArguments.cpp|  2 +-
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 
llvm/test/tools/llvm-reduce/remove-arguments-preserve-wrong-cc.ll

diff --git a/llvm/test/tools/llvm-reduce/remove-arguments-preserve-wrong-cc.ll 
b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-wrong-cc.ll
new file mode 100644
index 0..463b06c5d248a
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/remove-arguments-preserve-wrong-cc.ll
@@ -0,0 +1,27 @@
+; Check that when removing arguments, incorrect callsite calling conventions 
are preserved
+
+; RUN: llvm-reduce --abort-on-invalid-reduction --delta-passes=arguments 
--test FileCheck --test-arg --check-prefixes=INTERESTING --test-arg %s 
--test-arg --input-file %s -o %t
+; RUN: FileCheck --check-prefixes=RESULT %s < %t
+
+; INTERESTING-LABEL: @fastcc_callee(
+define fastcc i32 @fastcc_callee(i32 %a, i32 %b) {
+  ret i32 %a
+}
+
+; INTERESTING-LABEL: @fastcc_callee_decl(
+declare fastcc i32 @fastcc_callee_decl(i32 %a, i32 %b)
+
+; INTERESTING-LABEL: @caller_wrong_callsites(
+; INTERESTING: call
+; INTERESTING: call
+
+; RESULT-LABEL: define i32 @caller_wrong_callsites()
+; RESULT: %call0 = call coldcc i32 @fastcc_callee()
+; RESULT: %call1 = call i32 @fastcc_callee_decl()
+define i32 @caller_wrong_callsites(i32 %x) {
+  %call0 = call coldcc i32 @fastcc_callee(i32 %x, i32 2)
+  %call1 = call ccc i32 @fastcc_callee_decl(i32 %x, i32 2)
+  %result = add i32 %call0, %call1
+  ret i32 %result
+}
+
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
index a7689688fdc2b..f1b72a75564de 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
@@ -65,7 +65,7 @@ static void replaceFunctionCalls(Function &OldF, Function 
&NewF,
   CI->getOperandBundlesAsDefs(OpBundles);
 
   CallInst *NewCI = CallInst::Create(&NewF, Args, OpBundles);
-  NewCI->setCallingConv(NewF.getCallingConv());
+  NewCI->setCallingConv(CI->getCallingConv());
 
   AttrBuilder CallSiteAttrs(Ctx, CI->getAttributes().getFnAttrs());
   NewCI->setAttributes(

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Filter function based on uses before removing arguments (PR #133412)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133412

>From 374d2d95f283bf1d8be187a24ede8bd5bbfa4c8c Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 17:09:49 +0700
Subject: [PATCH] llvm-reduce: Filter function based on uses before removing
 arguments

Invokes and others are not handled, so this was leaving broken callsites
behind for anything other than CallInst
---
 .../llvm-reduce/reduce-arguments-invoke.ll| 41 +++
 .../reduce-arguments-non-callee-use.ll| 32 +++
 .../llvm-reduce/deltas/ReduceArguments.cpp| 17 +++-
 3 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll
 create mode 100644 
llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll

diff --git a/llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll 
b/llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll
new file mode 100644
index 0..fb8a4d6367b3b
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-arguments-invoke.ll
@@ -0,0 +1,41 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=arguments --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+; Test that we don't break the callsite for an unhandled invoke
+
+declare void @did_not_throw(i32)
+declare void @thrown()
+
+; INTERESTING-LABEL: define i32 @maybe_throwing_callee(
+
+; REDUCED-LABEL: define i32 @maybe_throwing_callee(i32 %arg0, i32 %arg1) {
+define i32 @maybe_throwing_callee(i32 %arg0, i32 %arg1) {
+  call void @thrown()
+  ret i32 %arg1
+}
+
+@initializer_user = global [1 x ptr] [ptr @maybe_throwing_callee ]
+
+; REDUCED-LABEL: define void @caller()
+; REDUCED: %i0 = invoke i32 @maybe_throwing_callee(i32 0, i32 0) #0
+define void @caller(i32 %arg0, ptr %arg1) personality ptr 
@__gxx_personality_v0 {
+bb:
+  %val = load i32, ptr %arg1
+  %i0 = invoke i32 @maybe_throwing_callee(i32 0, i32 0) nofree
+  to label %bb3 unwind label %bb1
+
+bb1:
+  landingpad { ptr, i32 }
+  catch ptr null
+  call void @thrown()
+  br label %bb4
+
+bb3:
+  call void @did_not_throw(i32 %i0)
+  br label %bb4
+
+bb4:
+  ret void
+}
+
+declare i32 @__gxx_personality_v0(...)
diff --git a/llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll 
b/llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll
new file mode 100644
index 0..63b2909404b7c
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/reduce-arguments-non-callee-use.ll
@@ -0,0 +1,32 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=arguments --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+; INTERESTING: @initializer_user
+; REDUCED: @initializer_user = global [1 x ptr] [ptr @captured_func]
+@initializer_user = global [1 x ptr] [ptr @captured_func ]
+
+; INTERESTING-LABEL: define i32 @captured_func(
+
+; REDUCED-LABEL: define i32 @captured_func() {
+define i32 @captured_func(i32 %a, i32 %b) {
+  %mul = mul i32 %a, %b
+  ret i32 %mul
+}
+
+; INTERESTING-LABEL: declare void @captures(
+declare void @captures(i32, ptr, i32)
+
+
+; INTERESTING-LABEL: define i32 @caller(
+; INTERESTING: = call
+; INTERESTING: = call
+
+; REDUCED-LABEL: define i32 @caller(i32 %a, i32 %b) {
+; REDUCED: %call0 = call i32 @captures(i32 %a, ptr @captured_func, i32 %b)
+; REDUCED: %call1 = call i32 @captured_func()
+define i32 @caller(i32 %a, i32 %b) {
+  %call0 = call i32 @captures(i32 %a, ptr @captured_func, i32 %b)
+  %call1 = call i32 @captured_func(i32 %a, i32 %b)
+  %add = add i32 %call0, %call1
+  ret i32 %add
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
index f1b72a75564de..bf9d6ba145f6f 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceArguments.cpp
@@ -96,6 +96,20 @@ static bool shouldRemoveArguments(const Function &F) {
   return !F.arg_empty() && !F.isIntrinsic();
 }
 
+static bool allFuncUsersRewritable(const Function &F) {
+  for (const Use &U : F.uses()) {
+const CallBase *CB = dyn_cast(U.getUser());
+if (!CB || !CB->isCallee(&U))
+  continue;
+
+// TODO: Handle all CallBase cases.
+if (!isa(CB))
+  return false;
+  }
+
+  return true;
+}
+
 /// Removes out-of-chunk arguments from functions, and modifies their calls
 /// accordingly. It also removes allocations of out-of-chunk arguments.
 static void extractArgumentsFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
@@ -107,7 +121,8 @@ static void extractArgumentsFromModule(Oracle &O, 
ReducerWorkItem &WorkItem) {
   for (auto &F : Program) {
 if (!shouldRemoveArguments(F))
   continue;
-
+if (!allFuncUsersRewritable(F))
+  continue;
 Funcs.push_back(&F);
 fo

[llvm-branch-commits] [llvm] llvm-reduce: Fix losing callsite attributes in operand-to-args (PR #133420)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133420

>From 20a68d2e6fc9360e84d17822450ba3dcdc0210f8 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 17:54:10 +0700
Subject: [PATCH] llvm-reduce: Fix losing callsite attributes in
 operand-to-args

---
 .../operands-to-args-preserve-attributes.ll   | 31 +++
 .../deltas/ReduceOperandsToArgs.cpp   |  1 +
 2 files changed, 32 insertions(+)
 create mode 100644 
llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll

diff --git 
a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll 
b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll
new file mode 100644
index 0..3aef3a06eafa3
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-attributes.ll
@@ -0,0 +1,31 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=operands-to-args --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+@a = dso_local global i8 0, align 1
+@b = dso_local global i16 0, align 2
+
+
+declare ptr @callee(ptr %a, i16)
+
+; INTERESTING-LABEL: define ptr @caller(
+; INTERESTING: sext
+; INTERESTING: icmp
+
+; REDUCED-LABEL: define ptr @caller(ptr %some.ptr, ptr %a, i8 %ld0, ptr %b, 
i16 %ld1, i32 %conv, i32 %conv1, i1 %cmp, i16 %conv2, ptr %callee.ret) #0 {
+
+; REDUCED: %callee.ret8 = call align 8 ptr @callee(ptr align 8 "some-attr" 
%some.ptr, i16 signext %conv2) #1
+
+define ptr @caller(ptr %some.ptr) nounwind {
+entry:
+  %ld0 = load i8, ptr @a, align 1
+  %conv = zext i8 %ld0 to i32
+  %ld1 = load i16, ptr @b, align 2
+  %conv1 = sext i16 %ld1 to i32
+  %cmp = icmp sge i32 %conv, %conv1
+  %conv2 = sext i1 %cmp to i16
+  %callee.ret = call align 8 ptr @callee(ptr align 8 "some-attr" %some.ptr, 
i16 signext %conv2) nocallback
+  ret ptr %callee.ret
+}
+
+; REDUCED: attributes #0 = { nounwind }
+; REDUCED: attributes #1 = { nocallback }
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
index 66d4bd3048171..037ff15fae0f6 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
@@ -105,6 +105,7 @@ static void replaceFunctionCalls(Function *OldF, Function 
*NewF) {
   NewCI = CallInst::Create(NewF, Args, OperandBundles, CI->getName());
 }
 NewCI->setCallingConv(NewF->getCallingConv());
+NewCI->setAttributes(CI->getAttributes());
 
 // Do the replacement for this use.
 if (!CI->use_empty())

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [SDAG] Introduce inbounds flag for pointer arithmetic (PR #131862)

2025-03-28 Thread Eli Friedman via llvm-branch-commits

efriedma-quic wrote:

> That's for instance useful if your architecture has memory segments whose 
> borders allocated objects cannot cross and where you can only fold offsets 
> into memory access instructions if they don't leave the memory segment of the 
> base address.

Hmm, I hadn't really thought about that.  I guess that's true.  That isn't 
really usable information on a conventional CPU target, but I guess on a target 
where the address-space has unusual properties, it could make sense.

I think if we add something called "inbounds" to SelectionDAG, it will 
inevitably get misused for other purposes, though.

https://github.com/llvm/llvm-project/pull/131862
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] [ctxprof][nfc] Move 2 implementation functions up in `CtxInstrProfiling.cpp` (PR #133146)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133146

>From 319d31f9e172d8320b290a3cd59f8814cd25cd3f Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Wed, 26 Mar 2025 10:10:43 -0700
Subject: [PATCH] [ctxprof][nfc] Move 2 implementation functions up in
 `CtxInstrProfiling.cpp`

---
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 66 +--
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index b0e63a8861d86..da291e0bbabdd 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -244,6 +244,39 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
   return Data.FlatCtx;
 }
 
+// This should be called once for a Root. Allocate the first arena, set up the
+// first context.
+void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
+  uint32_t NumCallsites) {
+  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
+  &AllContextsMutex);
+  // Re-check - we got here without having had taken a lock.
+  if (Root->FirstMemBlock)
+return;
+  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
+  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
+  Root->FirstMemBlock = M;
+  Root->CurrentMem = M;
+  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
+ NumCounters, NumCallsites);
+  AllContextRoots.PushBack(Root);
+}
+
+ContextRoot *FunctionData::getOrAllocateContextRoot() {
+  auto *Root = CtxRoot;
+  if (Root)
+return Root;
+  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
+  Root = CtxRoot;
+  if (!Root) {
+Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
+CtxRoot = Root;
+  }
+
+  assert(Root);
+  return Root;
+}
+
 ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
  uint32_t NumCounters) {
 
@@ -333,39 +366,6 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData 
*Data, void *Callee,
   return Ret;
 }
 
-// This should be called once for a Root. Allocate the first arena, set up the
-// first context.
-void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
-  uint32_t NumCallsites) {
-  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
-  &AllContextsMutex);
-  // Re-check - we got here without having had taken a lock.
-  if (Root->FirstMemBlock)
-return;
-  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
-  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
-  Root->FirstMemBlock = M;
-  Root->CurrentMem = M;
-  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
- NumCounters, NumCallsites);
-  AllContextRoots.PushBack(Root);
-}
-
-ContextRoot *FunctionData::getOrAllocateContextRoot() {
-  auto *Root = CtxRoot;
-  if (Root)
-return Root;
-  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
-  Root = CtxRoot;
-  if (!Root) {
-Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
-CtxRoot = Root;
-  }
-
-  assert(Root);
-  return Root;
-}
-
 ContextNode *__llvm_ctx_profile_start_context(
 FunctionData *FData, GUID Guid, uint32_t Counters,
 uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] [ctxprof][nfc] Move 2 implementation functions up in `CtxInstrProfiling.cpp` (PR #133146)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133146

>From 319d31f9e172d8320b290a3cd59f8814cd25cd3f Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Wed, 26 Mar 2025 10:10:43 -0700
Subject: [PATCH] [ctxprof][nfc] Move 2 implementation functions up in
 `CtxInstrProfiling.cpp`

---
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 66 +--
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index b0e63a8861d86..da291e0bbabdd 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -244,6 +244,39 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
   return Data.FlatCtx;
 }
 
+// This should be called once for a Root. Allocate the first arena, set up the
+// first context.
+void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
+  uint32_t NumCallsites) {
+  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
+  &AllContextsMutex);
+  // Re-check - we got here without having had taken a lock.
+  if (Root->FirstMemBlock)
+return;
+  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
+  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
+  Root->FirstMemBlock = M;
+  Root->CurrentMem = M;
+  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
+ NumCounters, NumCallsites);
+  AllContextRoots.PushBack(Root);
+}
+
+ContextRoot *FunctionData::getOrAllocateContextRoot() {
+  auto *Root = CtxRoot;
+  if (Root)
+return Root;
+  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
+  Root = CtxRoot;
+  if (!Root) {
+Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
+CtxRoot = Root;
+  }
+
+  assert(Root);
+  return Root;
+}
+
 ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
  uint32_t NumCounters) {
 
@@ -333,39 +366,6 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData 
*Data, void *Callee,
   return Ret;
 }
 
-// This should be called once for a Root. Allocate the first arena, set up the
-// first context.
-void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
-  uint32_t NumCallsites) {
-  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
-  &AllContextsMutex);
-  // Re-check - we got here without having had taken a lock.
-  if (Root->FirstMemBlock)
-return;
-  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
-  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
-  Root->FirstMemBlock = M;
-  Root->CurrentMem = M;
-  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
- NumCounters, NumCallsites);
-  AllContextRoots.PushBack(Root);
-}
-
-ContextRoot *FunctionData::getOrAllocateContextRoot() {
-  auto *Root = CtxRoot;
-  if (Root)
-return Root;
-  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
-  Root = CtxRoot;
-  if (!Root) {
-Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
-CtxRoot = Root;
-  }
-
-  assert(Root);
-  return Root;
-}
-
 ContextNode *__llvm_ctx_profile_start_context(
 FunctionData *FData, GUID Guid, uint32_t Counters,
 uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] [ctxprof][nfc] Move 2 implementation functions up in `CtxInstrProfiling.cpp` (PR #133146)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133146

>From f36ebc768d8d5f31b9e857114191d1b627fe5d13 Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Wed, 26 Mar 2025 10:10:43 -0700
Subject: [PATCH] [ctxprof][nfc] Move 2 implementation functions up in
 `CtxInstrProfiling.cpp`

---
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 66 +--
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index b0e63a8861d86..da291e0bbabdd 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -244,6 +244,39 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
   return Data.FlatCtx;
 }
 
+// This should be called once for a Root. Allocate the first arena, set up the
+// first context.
+void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
+  uint32_t NumCallsites) {
+  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
+  &AllContextsMutex);
+  // Re-check - we got here without having had taken a lock.
+  if (Root->FirstMemBlock)
+return;
+  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
+  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
+  Root->FirstMemBlock = M;
+  Root->CurrentMem = M;
+  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
+ NumCounters, NumCallsites);
+  AllContextRoots.PushBack(Root);
+}
+
+ContextRoot *FunctionData::getOrAllocateContextRoot() {
+  auto *Root = CtxRoot;
+  if (Root)
+return Root;
+  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
+  Root = CtxRoot;
+  if (!Root) {
+Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
+CtxRoot = Root;
+  }
+
+  assert(Root);
+  return Root;
+}
+
 ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
  uint32_t NumCounters) {
 
@@ -333,39 +366,6 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData 
*Data, void *Callee,
   return Ret;
 }
 
-// This should be called once for a Root. Allocate the first arena, set up the
-// first context.
-void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
-  uint32_t NumCallsites) {
-  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
-  &AllContextsMutex);
-  // Re-check - we got here without having had taken a lock.
-  if (Root->FirstMemBlock)
-return;
-  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
-  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
-  Root->FirstMemBlock = M;
-  Root->CurrentMem = M;
-  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
- NumCounters, NumCallsites);
-  AllContextRoots.PushBack(Root);
-}
-
-ContextRoot *FunctionData::getOrAllocateContextRoot() {
-  auto *Root = CtxRoot;
-  if (Root)
-return Root;
-  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
-  Root = CtxRoot;
-  if (!Root) {
-Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
-CtxRoot = Root;
-  }
-
-  assert(Root);
-  return Root;
-}
-
 ContextNode *__llvm_ctx_profile_start_context(
 FunctionData *FData, GUID Guid, uint32_t Counters,
 uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] [llvm] [ctxprof] root autodetection mechanism (PR #133147)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133147

>From a984fc7261d0f6f8710ed7cdf7d51446185d95f2 Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Mon, 24 Mar 2025 12:01:10 -0700
Subject: [PATCH] RootAutodetect

---
 compiler-rt/lib/ctx_profile/CMakeLists.txt|   2 +-
 .../lib/ctx_profile/CtxInstrContextNode.h |   1 +
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 119 +++
 .../lib/ctx_profile/CtxInstrProfiling.h   |   2 +-
 .../lib/ctx_profile/RootAutoDetector.cpp  |  83 
 .../lib/ctx_profile/RootAutoDetector.h|  31 +++
 .../TestCases/autodetect-roots.cpp| 188 ++
 .../TestCases/generate-context.cpp|   5 +-
 .../llvm/ProfileData/CtxInstrContextNode.h|   1 +
 .../Instrumentation/PGOCtxProfLowering.cpp|  26 ++-
 .../PGOProfile/ctx-instrumentation.ll |  20 +-
 11 files changed, 416 insertions(+), 62 deletions(-)
 create mode 100644 compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp

diff --git a/compiler-rt/lib/ctx_profile/CMakeLists.txt 
b/compiler-rt/lib/ctx_profile/CMakeLists.txt
index bb606449c61b1..446ebc96408dd 100644
--- a/compiler-rt/lib/ctx_profile/CMakeLists.txt
+++ b/compiler-rt/lib/ctx_profile/CMakeLists.txt
@@ -27,7 +27,7 @@ endif()
 add_compiler_rt_runtime(clang_rt.ctx_profile
   STATIC
   ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
-  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
+  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc 
RTSanitizerCommonSymbolizer
   CFLAGS ${EXTRA_FLAGS}
   SOURCES ${CTX_PROFILE_SOURCES}
   ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h 
b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
index a42bf9ebb01ea..aa052bc7eea6c 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
@@ -127,6 +127,7 @@ class ContextNode final {
 /// MUTEXDECL takes one parameter, the name of a field that is a mutex.
 #define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL)
\
   PTRDECL(FunctionData, Next)  
\
+  PTRDECL(void, EntryAddress)  
\
   VOLATILE_PTRDECL(ContextRoot, CtxRoot)   
\
   VOLATILE_PTRDECL(ContextNode, FlatCtx)   
\
   MUTEXDECL(Mutex)
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index da291e0bbabdd..09ed607cde3aa 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "CtxInstrProfiling.h"
+#include "RootAutoDetector.h"
 #include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_atomic_clang.h"
@@ -43,6 +44,12 @@ Arena *FlatCtxArena = nullptr;
 __thread bool IsUnderContext = false;
 __sanitizer::atomic_uint8_t ProfilingStarted = {};
 
+__sanitizer::atomic_uintptr_t RootDetector = {};
+RootAutoDetector *getRootDetector() {
+  return reinterpret_cast(
+  __sanitizer::atomic_load_relaxed(&RootDetector));
+}
+
 // utility to taint a pointer by setting the LSB. There is an assumption
 // throughout that the addresses of contexts are even (really, they should be
 // align(8), but "even"-ness is the minimum assumption)
@@ -201,7 +208,7 @@ ContextNode *getCallsiteSlow(GUID Guid, ContextNode 
**InsertionPoint,
   return Ret;
 }
 
-ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
+ContextNode *getFlatProfile(FunctionData &Data, void *Callee, GUID Guid,
 uint32_t NumCounters) {
   if (ContextNode *Existing = Data.FlatCtx)
 return Existing;
@@ -232,6 +239,7 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
 auto *Ret = allocContextNode(AllocBuff, Guid, NumCounters, 0);
 Data.FlatCtx = Ret;
 
+Data.EntryAddress = Callee;
 Data.Next = reinterpret_cast(
 __sanitizer::atomic_load_relaxed(&AllFunctionsData));
 while (!__sanitizer::atomic_compare_exchange_strong(
@@ -277,8 +285,29 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
   return Root;
 }
 
-ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
- uint32_t NumCounters) {
+ContextNode *tryStartContextGivenRoot(ContextRoot *Root, GUID Guid,
+  uint32_t Counters, uint32_t Callsites)
+SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
+  IsUnderContext = true;
+  __sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
+__sanitizer::memory_order_relaxed);
+
+  if (!Root->FirstMemBlock) {
+setupContext(Root, Guid, Counters, Callsites);
+  }
+  if (Root->Taken.TryL

[llvm-branch-commits] [compiler-rt] [llvm] [ctxprof] root autodetection mechanism (PR #133147)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133147

>From e2b4b9ce7cb0f9b7d39142d419f59de8d60ec71c Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Mon, 24 Mar 2025 12:01:10 -0700
Subject: [PATCH] RootAutodetect

---
 compiler-rt/lib/ctx_profile/CMakeLists.txt|   2 +-
 .../lib/ctx_profile/CtxInstrContextNode.h |   1 +
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 119 +++
 .../lib/ctx_profile/CtxInstrProfiling.h   |   2 +-
 .../lib/ctx_profile/RootAutoDetector.cpp  |  83 
 .../lib/ctx_profile/RootAutoDetector.h|  31 +++
 .../TestCases/autodetect-roots.cpp| 188 ++
 .../TestCases/generate-context.cpp|   5 +-
 .../llvm/ProfileData/CtxInstrContextNode.h|   1 +
 .../Instrumentation/PGOCtxProfLowering.cpp|  26 ++-
 .../PGOProfile/ctx-instrumentation.ll |  20 +-
 11 files changed, 416 insertions(+), 62 deletions(-)
 create mode 100644 compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp

diff --git a/compiler-rt/lib/ctx_profile/CMakeLists.txt 
b/compiler-rt/lib/ctx_profile/CMakeLists.txt
index bb606449c61b1..446ebc96408dd 100644
--- a/compiler-rt/lib/ctx_profile/CMakeLists.txt
+++ b/compiler-rt/lib/ctx_profile/CMakeLists.txt
@@ -27,7 +27,7 @@ endif()
 add_compiler_rt_runtime(clang_rt.ctx_profile
   STATIC
   ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
-  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
+  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc 
RTSanitizerCommonSymbolizer
   CFLAGS ${EXTRA_FLAGS}
   SOURCES ${CTX_PROFILE_SOURCES}
   ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h 
b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
index a42bf9ebb01ea..aa052bc7eea6c 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
@@ -127,6 +127,7 @@ class ContextNode final {
 /// MUTEXDECL takes one parameter, the name of a field that is a mutex.
 #define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL)
\
   PTRDECL(FunctionData, Next)  
\
+  PTRDECL(void, EntryAddress)  
\
   VOLATILE_PTRDECL(ContextRoot, CtxRoot)   
\
   VOLATILE_PTRDECL(ContextNode, FlatCtx)   
\
   MUTEXDECL(Mutex)
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index da291e0bbabdd..09ed607cde3aa 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "CtxInstrProfiling.h"
+#include "RootAutoDetector.h"
 #include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_atomic_clang.h"
@@ -43,6 +44,12 @@ Arena *FlatCtxArena = nullptr;
 __thread bool IsUnderContext = false;
 __sanitizer::atomic_uint8_t ProfilingStarted = {};
 
+__sanitizer::atomic_uintptr_t RootDetector = {};
+RootAutoDetector *getRootDetector() {
+  return reinterpret_cast(
+  __sanitizer::atomic_load_relaxed(&RootDetector));
+}
+
 // utility to taint a pointer by setting the LSB. There is an assumption
 // throughout that the addresses of contexts are even (really, they should be
 // align(8), but "even"-ness is the minimum assumption)
@@ -201,7 +208,7 @@ ContextNode *getCallsiteSlow(GUID Guid, ContextNode 
**InsertionPoint,
   return Ret;
 }
 
-ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
+ContextNode *getFlatProfile(FunctionData &Data, void *Callee, GUID Guid,
 uint32_t NumCounters) {
   if (ContextNode *Existing = Data.FlatCtx)
 return Existing;
@@ -232,6 +239,7 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
 auto *Ret = allocContextNode(AllocBuff, Guid, NumCounters, 0);
 Data.FlatCtx = Ret;
 
+Data.EntryAddress = Callee;
 Data.Next = reinterpret_cast(
 __sanitizer::atomic_load_relaxed(&AllFunctionsData));
 while (!__sanitizer::atomic_compare_exchange_strong(
@@ -277,8 +285,29 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
   return Root;
 }
 
-ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
- uint32_t NumCounters) {
+ContextNode *tryStartContextGivenRoot(ContextRoot *Root, GUID Guid,
+  uint32_t Counters, uint32_t Callsites)
+SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
+  IsUnderContext = true;
+  __sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
+__sanitizer::memory_order_relaxed);
+
+  if (!Root->FirstMemBlock) {
+setupContext(Root, Guid, Counters, Callsites);
+  }
+  if (Root->Taken.TryL

[llvm-branch-commits] [compiler-rt] [llvm] [ctxprof] root autodetection mechanism (PR #133147)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133147

>From 8ab329e153ec9fe7c9d24a5109b33909e204296a Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Mon, 24 Mar 2025 12:01:10 -0700
Subject: [PATCH] RootAutodetect

---
 compiler-rt/lib/ctx_profile/CMakeLists.txt|   2 +-
 .../lib/ctx_profile/CtxInstrContextNode.h |   1 +
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 119 +++
 .../lib/ctx_profile/CtxInstrProfiling.h   |   2 +-
 .../lib/ctx_profile/RootAutoDetector.cpp  |  83 
 .../lib/ctx_profile/RootAutoDetector.h|  31 +++
 .../TestCases/autodetect-roots.cpp| 188 ++
 .../TestCases/generate-context.cpp|   5 +-
 .../llvm/ProfileData/CtxInstrContextNode.h|   1 +
 .../Instrumentation/PGOCtxProfLowering.cpp|  26 ++-
 .../PGOProfile/ctx-instrumentation.ll |  20 +-
 11 files changed, 416 insertions(+), 62 deletions(-)
 create mode 100644 compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp

diff --git a/compiler-rt/lib/ctx_profile/CMakeLists.txt 
b/compiler-rt/lib/ctx_profile/CMakeLists.txt
index bb606449c61b1..446ebc96408dd 100644
--- a/compiler-rt/lib/ctx_profile/CMakeLists.txt
+++ b/compiler-rt/lib/ctx_profile/CMakeLists.txt
@@ -27,7 +27,7 @@ endif()
 add_compiler_rt_runtime(clang_rt.ctx_profile
   STATIC
   ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
-  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
+  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc 
RTSanitizerCommonSymbolizer
   CFLAGS ${EXTRA_FLAGS}
   SOURCES ${CTX_PROFILE_SOURCES}
   ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h 
b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
index a42bf9ebb01ea..aa052bc7eea6c 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
@@ -127,6 +127,7 @@ class ContextNode final {
 /// MUTEXDECL takes one parameter, the name of a field that is a mutex.
 #define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL)
\
   PTRDECL(FunctionData, Next)  
\
+  PTRDECL(void, EntryAddress)  
\
   VOLATILE_PTRDECL(ContextRoot, CtxRoot)   
\
   VOLATILE_PTRDECL(ContextNode, FlatCtx)   
\
   MUTEXDECL(Mutex)
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index da291e0bbabdd..09ed607cde3aa 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "CtxInstrProfiling.h"
+#include "RootAutoDetector.h"
 #include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_atomic_clang.h"
@@ -43,6 +44,12 @@ Arena *FlatCtxArena = nullptr;
 __thread bool IsUnderContext = false;
 __sanitizer::atomic_uint8_t ProfilingStarted = {};
 
+__sanitizer::atomic_uintptr_t RootDetector = {};
+RootAutoDetector *getRootDetector() {
+  return reinterpret_cast(
+  __sanitizer::atomic_load_relaxed(&RootDetector));
+}
+
 // utility to taint a pointer by setting the LSB. There is an assumption
 // throughout that the addresses of contexts are even (really, they should be
 // align(8), but "even"-ness is the minimum assumption)
@@ -201,7 +208,7 @@ ContextNode *getCallsiteSlow(GUID Guid, ContextNode 
**InsertionPoint,
   return Ret;
 }
 
-ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
+ContextNode *getFlatProfile(FunctionData &Data, void *Callee, GUID Guid,
 uint32_t NumCounters) {
   if (ContextNode *Existing = Data.FlatCtx)
 return Existing;
@@ -232,6 +239,7 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
 auto *Ret = allocContextNode(AllocBuff, Guid, NumCounters, 0);
 Data.FlatCtx = Ret;
 
+Data.EntryAddress = Callee;
 Data.Next = reinterpret_cast(
 __sanitizer::atomic_load_relaxed(&AllFunctionsData));
 while (!__sanitizer::atomic_compare_exchange_strong(
@@ -277,8 +285,29 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
   return Root;
 }
 
-ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
- uint32_t NumCounters) {
+ContextNode *tryStartContextGivenRoot(ContextRoot *Root, GUID Guid,
+  uint32_t Counters, uint32_t Callsites)
+SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
+  IsUnderContext = true;
+  __sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
+__sanitizer::memory_order_relaxed);
+
+  if (!Root->FirstMemBlock) {
+setupContext(Root, Guid, Counters, Callsites);
+  }
+  if (Root->Taken.TryL

[llvm-branch-commits] [compiler-rt] [llvm] [ctxprof] root autodetection mechanism (PR #133147)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133147

>From a984fc7261d0f6f8710ed7cdf7d51446185d95f2 Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Mon, 24 Mar 2025 12:01:10 -0700
Subject: [PATCH] RootAutodetect

---
 compiler-rt/lib/ctx_profile/CMakeLists.txt|   2 +-
 .../lib/ctx_profile/CtxInstrContextNode.h |   1 +
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 119 +++
 .../lib/ctx_profile/CtxInstrProfiling.h   |   2 +-
 .../lib/ctx_profile/RootAutoDetector.cpp  |  83 
 .../lib/ctx_profile/RootAutoDetector.h|  31 +++
 .../TestCases/autodetect-roots.cpp| 188 ++
 .../TestCases/generate-context.cpp|   5 +-
 .../llvm/ProfileData/CtxInstrContextNode.h|   1 +
 .../Instrumentation/PGOCtxProfLowering.cpp|  26 ++-
 .../PGOProfile/ctx-instrumentation.ll |  20 +-
 11 files changed, 416 insertions(+), 62 deletions(-)
 create mode 100644 compiler-rt/test/ctx_profile/TestCases/autodetect-roots.cpp

diff --git a/compiler-rt/lib/ctx_profile/CMakeLists.txt 
b/compiler-rt/lib/ctx_profile/CMakeLists.txt
index bb606449c61b1..446ebc96408dd 100644
--- a/compiler-rt/lib/ctx_profile/CMakeLists.txt
+++ b/compiler-rt/lib/ctx_profile/CMakeLists.txt
@@ -27,7 +27,7 @@ endif()
 add_compiler_rt_runtime(clang_rt.ctx_profile
   STATIC
   ARCHS ${CTX_PROFILE_SUPPORTED_ARCH}
-  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc
+  OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc 
RTSanitizerCommonSymbolizer
   CFLAGS ${EXTRA_FLAGS}
   SOURCES ${CTX_PROFILE_SOURCES}
   ADDITIONAL_HEADERS ${CTX_PROFILE_HEADERS}
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h 
b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
index a42bf9ebb01ea..aa052bc7eea6c 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
+++ b/compiler-rt/lib/ctx_profile/CtxInstrContextNode.h
@@ -127,6 +127,7 @@ class ContextNode final {
 /// MUTEXDECL takes one parameter, the name of a field that is a mutex.
 #define CTXPROF_FUNCTION_DATA(PTRDECL, VOLATILE_PTRDECL, MUTEXDECL)
\
   PTRDECL(FunctionData, Next)  
\
+  PTRDECL(void, EntryAddress)  
\
   VOLATILE_PTRDECL(ContextRoot, CtxRoot)   
\
   VOLATILE_PTRDECL(ContextNode, FlatCtx)   
\
   MUTEXDECL(Mutex)
diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index da291e0bbabdd..09ed607cde3aa 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "CtxInstrProfiling.h"
+#include "RootAutoDetector.h"
 #include "sanitizer_common/sanitizer_allocator_internal.h"
 #include "sanitizer_common/sanitizer_atomic.h"
 #include "sanitizer_common/sanitizer_atomic_clang.h"
@@ -43,6 +44,12 @@ Arena *FlatCtxArena = nullptr;
 __thread bool IsUnderContext = false;
 __sanitizer::atomic_uint8_t ProfilingStarted = {};
 
+__sanitizer::atomic_uintptr_t RootDetector = {};
+RootAutoDetector *getRootDetector() {
+  return reinterpret_cast(
+  __sanitizer::atomic_load_relaxed(&RootDetector));
+}
+
 // utility to taint a pointer by setting the LSB. There is an assumption
 // throughout that the addresses of contexts are even (really, they should be
 // align(8), but "even"-ness is the minimum assumption)
@@ -201,7 +208,7 @@ ContextNode *getCallsiteSlow(GUID Guid, ContextNode 
**InsertionPoint,
   return Ret;
 }
 
-ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
+ContextNode *getFlatProfile(FunctionData &Data, void *Callee, GUID Guid,
 uint32_t NumCounters) {
   if (ContextNode *Existing = Data.FlatCtx)
 return Existing;
@@ -232,6 +239,7 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
 auto *Ret = allocContextNode(AllocBuff, Guid, NumCounters, 0);
 Data.FlatCtx = Ret;
 
+Data.EntryAddress = Callee;
 Data.Next = reinterpret_cast(
 __sanitizer::atomic_load_relaxed(&AllFunctionsData));
 while (!__sanitizer::atomic_compare_exchange_strong(
@@ -277,8 +285,29 @@ ContextRoot *FunctionData::getOrAllocateContextRoot() {
   return Root;
 }
 
-ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
- uint32_t NumCounters) {
+ContextNode *tryStartContextGivenRoot(ContextRoot *Root, GUID Guid,
+  uint32_t Counters, uint32_t Callsites)
+SANITIZER_NO_THREAD_SAFETY_ANALYSIS {
+  IsUnderContext = true;
+  __sanitizer::atomic_fetch_add(&Root->TotalEntries, 1,
+__sanitizer::memory_order_relaxed);
+
+  if (!Root->FirstMemBlock) {
+setupContext(Root, Guid, Counters, Callsites);
+  }
+  if (Root->Taken.TryL

[llvm-branch-commits] [compiler-rt] [ctxprof][nfc] Move 2 implementation functions up in `CtxInstrProfiling.cpp` (PR #133146)

2025-03-28 Thread Mircea Trofin via llvm-branch-commits

https://github.com/mtrofin updated 
https://github.com/llvm/llvm-project/pull/133146

>From f36ebc768d8d5f31b9e857114191d1b627fe5d13 Mon Sep 17 00:00:00 2001
From: Mircea Trofin 
Date: Wed, 26 Mar 2025 10:10:43 -0700
Subject: [PATCH] [ctxprof][nfc] Move 2 implementation functions up in
 `CtxInstrProfiling.cpp`

---
 .../lib/ctx_profile/CtxInstrProfiling.cpp | 66 +--
 1 file changed, 33 insertions(+), 33 deletions(-)

diff --git a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp 
b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
index b0e63a8861d86..da291e0bbabdd 100644
--- a/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
+++ b/compiler-rt/lib/ctx_profile/CtxInstrProfiling.cpp
@@ -244,6 +244,39 @@ ContextNode *getFlatProfile(FunctionData &Data, GUID Guid,
   return Data.FlatCtx;
 }
 
+// This should be called once for a Root. Allocate the first arena, set up the
+// first context.
+void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
+  uint32_t NumCallsites) {
+  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
+  &AllContextsMutex);
+  // Re-check - we got here without having had taken a lock.
+  if (Root->FirstMemBlock)
+return;
+  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
+  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
+  Root->FirstMemBlock = M;
+  Root->CurrentMem = M;
+  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
+ NumCounters, NumCallsites);
+  AllContextRoots.PushBack(Root);
+}
+
+ContextRoot *FunctionData::getOrAllocateContextRoot() {
+  auto *Root = CtxRoot;
+  if (Root)
+return Root;
+  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
+  Root = CtxRoot;
+  if (!Root) {
+Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
+CtxRoot = Root;
+  }
+
+  assert(Root);
+  return Root;
+}
+
 ContextNode *getUnhandledContext(FunctionData &Data, GUID Guid,
  uint32_t NumCounters) {
 
@@ -333,39 +366,6 @@ ContextNode *__llvm_ctx_profile_get_context(FunctionData 
*Data, void *Callee,
   return Ret;
 }
 
-// This should be called once for a Root. Allocate the first arena, set up the
-// first context.
-void setupContext(ContextRoot *Root, GUID Guid, uint32_t NumCounters,
-  uint32_t NumCallsites) {
-  __sanitizer::GenericScopedLock<__sanitizer::SpinMutex> Lock(
-  &AllContextsMutex);
-  // Re-check - we got here without having had taken a lock.
-  if (Root->FirstMemBlock)
-return;
-  const auto Needed = ContextNode::getAllocSize(NumCounters, NumCallsites);
-  auto *M = Arena::allocateNewArena(getArenaAllocSize(Needed));
-  Root->FirstMemBlock = M;
-  Root->CurrentMem = M;
-  Root->FirstNode = allocContextNode(M->tryBumpAllocate(Needed), Guid,
- NumCounters, NumCallsites);
-  AllContextRoots.PushBack(Root);
-}
-
-ContextRoot *FunctionData::getOrAllocateContextRoot() {
-  auto *Root = CtxRoot;
-  if (Root)
-return Root;
-  __sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex> L(&Mutex);
-  Root = CtxRoot;
-  if (!Root) {
-Root = new (__sanitizer::InternalAlloc(sizeof(ContextRoot))) ContextRoot();
-CtxRoot = Root;
-  }
-
-  assert(Root);
-  return Root;
-}
-
 ContextNode *__llvm_ctx_profile_start_context(
 FunctionData *FData, GUID Guid, uint32_t Counters,
 uint32_t Callsites) SANITIZER_NO_THREAD_SAFETY_ANALYSIS {

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Try to preserve instruction metadata as argument attributes (PR #133557)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/133557

Fixes #131825

>From 9867ba4e47993357adf1f833f2ba816e277656fc Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Sat, 29 Mar 2025 09:32:55 +0700
Subject: [PATCH] llvm-reduce: Try to preserve instruction metadata as argument
 attributes

Fixes #131825
---
 llvm/include/llvm/IR/Attributes.h |  6 ++
 llvm/lib/IR/Attributes.cpp| 32 
 ...operands-to-args-metadata-to-attributes.ll | 77 +++
 .../deltas/ReduceOperandsToArgs.cpp   | 11 ++-
 4 files changed, 125 insertions(+), 1 deletion(-)
 create mode 100644 
llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll

diff --git a/llvm/include/llvm/IR/Attributes.h 
b/llvm/include/llvm/IR/Attributes.h
index d6533b9bcbea1..5252f26f398d2 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -42,6 +42,7 @@ class ConstantRangeList;
 class FoldingSetNodeID;
 class Function;
 class LLVMContext;
+class Instruction;
 class Type;
 class raw_ostream;
 enum FPClassTest : unsigned;
@@ -1285,6 +1286,11 @@ class AttrBuilder {
   /// Add initializes attribute.
   AttrBuilder &addInitializesAttr(const ConstantRangeList &CRL);
 
+  /// Add 0 or more parameter attributes which are equivalent to metadata
+  /// attached to \p I. e.g. !align -> align. This assumes the argument type is
+  /// the same as the original instruction and the attribute is compatible.
+  AttrBuilder &addFromEquivalentMetadata(const Instruction &I);
+
   ArrayRef attrs() const { return Attrs; }
 
   bool operator==(const AttrBuilder &B) const;
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 8da1dfe914818..8cb8b0d927afd 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -2291,6 +2291,38 @@ AttrBuilder &AttrBuilder::addInitializesAttr(const 
ConstantRangeList &CRL) {
   return addConstantRangeListAttr(Attribute::Initializes, CRL.rangesRef());
 }
 
+AttrBuilder &AttrBuilder::addFromEquivalentMetadata(const Instruction &I) {
+  if (const MDNode *NonNull = I.getMetadata(LLVMContext::MD_nonnull))
+addAttribute(Attribute::NonNull);
+
+  if (const MDNode *NoUndef = I.getMetadata(LLVMContext::MD_noundef))
+addAttribute(Attribute::NoUndef);
+
+  if (const MDNode *Align = I.getMetadata(LLVMContext::MD_align)) {
+ConstantInt *CI = mdconst::extract(Align->getOperand(0));
+addAlignmentAttr(CI->getZExtValue());
+  }
+
+  if (const MDNode *Dereferenceable =
+  I.getMetadata(LLVMContext::MD_dereferenceable)) {
+ConstantInt *CI =
+mdconst::extract(Dereferenceable->getOperand(0));
+addDereferenceableAttr(CI->getZExtValue());
+  }
+
+  if (const MDNode *DereferenceableOrNull =
+  I.getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
+ConstantInt *CI =
+mdconst::extract(DereferenceableOrNull->getOperand(0));
+addDereferenceableAttr(CI->getZExtValue());
+  }
+
+  if (const MDNode *Range = I.getMetadata(LLVMContext::MD_range))
+addRangeAttr(getConstantRangeFromMetadata(*Range));
+
+  return *this;
+}
+
 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
   // TODO: Could make this O(n) as we're merging two sorted lists.
   for (const auto &I : B.attrs())
diff --git 
a/llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll 
b/llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll
new file mode 100644
index 0..913ba9d3218fd
--- /dev/null
+++ 
b/llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll
@@ -0,0 +1,77 @@
+; Check that equivalent parameter attributes are introduced when
+; moving instructions with metadata to arguments.
+
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=operands-to-args --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck --input-file %t --check-prefix=REDUCED %s
+
+; INTERESTING-LABEL: define ptr @use_nonnull(
+; REDUCED-LABEL: define ptr @use_nonnull(ptr nonnull %nonnull) {
+define ptr @use_nonnull() {
+  %nonnull = load ptr, ptr null, !nonnull !0
+  ret ptr %nonnull
+}
+
+; INTERESTING-LABEL: define void @use_noundef(
+; REDUCED-LABEL: define void @use_noundef(ptr noundef %noundef, <2 x ptr> 
noundef %noundef_vec) {
+define void @use_noundef() {
+  %noundef = load ptr, ptr null, !noundef !0
+  %noundef_vec = load <2 x ptr>, ptr null, !noundef !0
+  store ptr %noundef, ptr null
+  store <2 x ptr> %noundef_vec, ptr null
+  ret void
+}
+
+; INTERESTING-LABEL: define ptr @use_align(
+; REDUCED-LABEL: define ptr @use_align(ptr align 16 %align) {
+define ptr @use_align() {
+  %align = load ptr, ptr null, !align !1
+  ret ptr %align
+}
+
+; INTERESTING-LABEL: define ptr @use_dereferenceable(
+; REDUCED-LABEL: define ptr @use_dereferenceable(ptr dereferenceable(12345) 
%deref) {
+define ptr @use_dereferenceable() {
+  %de

[llvm-branch-commits] [llvm] llvm-reduce: Try to preserve instruction metadata as argument attributes (PR #133557)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

arsenm wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/133557?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#133557** https://app.graphite.dev/github/pr/llvm/llvm-project/133557?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/133557?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#133422** https://app.graphite.dev/github/pr/llvm/llvm-project/133422?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133421** https://app.graphite.dev/github/pr/llvm/llvm-project/133421?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133420** https://app.graphite.dev/github/pr/llvm/llvm-project/133420?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133412** https://app.graphite.dev/github/pr/llvm/llvm-project/133412?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133411** https://app.graphite.dev/github/pr/llvm/llvm-project/133411?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133410** https://app.graphite.dev/github/pr/llvm/llvm-project/133410?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133409** https://app.graphite.dev/github/pr/llvm/llvm-project/133409?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133408** https://app.graphite.dev/github/pr/llvm/llvm-project/133408?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133407** https://app.graphite.dev/github/pr/llvm/llvm-project/133407?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#133396** https://app.graphite.dev/github/pr/llvm/llvm-project/133396?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/133557
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing call metadata in operands-to-args (PR #133422)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133422

>From b6a919ffe2922712704c9c051e3f73e5d03edd7e Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 18:01:39 +0700
Subject: [PATCH] llvm-reduce: Fix using call metadata in operands-to-args

---
 .../tools/llvm-reduce/operands-to-args-preserve-fmf.ll | 7 +--
 llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll 
b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
index b4b19ca28dbb5..fc31a08353b8f 100644
--- a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
+++ b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
@@ -12,9 +12,12 @@ define float @callee(float %a) {
 ; INTERESTING: load float
 
 ; REDUCED-LABEL: define float @caller(ptr %ptr, float %val, float 
%callee.ret1) {
-; REDUCED: %callee.ret12 = call nnan nsz float @callee(float %val, float 
0.00e+00)
+; REDUCED: %callee.ret12 = call nnan nsz float @callee(float %val, float 
0.00e+00), !fpmath !0
 define float @caller(ptr %ptr) {
   %val = load float, ptr %ptr
-  %callee.ret = call nnan nsz float @callee(float %val)
+  %callee.ret = call nnan nsz float @callee(float %val), !fpmath !0
   ret float %callee.ret
 }
+
+; REDUCED: !0 = !{float 2.00e+00}
+!0 = !{float 2.0}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
index e1c1c9c7372f9..6b1958e24c932 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
@@ -111,6 +111,8 @@ static void replaceFunctionCalls(Function *OldF, Function 
*NewF) {
 if (auto *FPOp = dyn_cast(NewCI))
   NewCI->setFastMathFlags(CI->getFastMathFlags());
 
+NewCI->copyMetadata(*CI);
+
 // Do the replacement for this use.
 if (!CI->use_empty())
   CI->replaceAllUsesWith(NewCI);

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Fix losing fast math flags in operands-to-args (PR #133421)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm updated 
https://github.com/llvm/llvm-project/pull/133421

>From 02186b904f0aefc91d83431f1de4c08f5c11909f Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Fri, 28 Mar 2025 18:00:05 +0700
Subject: [PATCH] llvm-reduce: Fix losing fast math flags in operands-to-args

---
 .../operands-to-args-preserve-fmf.ll  | 20 +++
 .../deltas/ReduceOperandsToArgs.cpp   |  4 
 2 files changed, 24 insertions(+)
 create mode 100644 llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll

diff --git a/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll 
b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
new file mode 100644
index 0..b4b19ca28dbb5
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/operands-to-args-preserve-fmf.ll
@@ -0,0 +1,20 @@
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=operands-to-args --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck %s --input-file %t --check-prefix=REDUCED
+
+; INTERESTING-LABEL: define float @callee(
+; INTERESTING: fadd float
+define float @callee(float %a) {
+  %x = fadd float %a, 1.0
+  ret float %x
+}
+
+; INTERESTING-LABEL: define float @caller(
+; INTERESTING: load float
+
+; REDUCED-LABEL: define float @caller(ptr %ptr, float %val, float 
%callee.ret1) {
+; REDUCED: %callee.ret12 = call nnan nsz float @callee(float %val, float 
0.00e+00)
+define float @caller(ptr %ptr) {
+  %val = load float, ptr %ptr
+  %callee.ret = call nnan nsz float @callee(float %val)
+  ret float %callee.ret
+}
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp 
b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
index b9e07f2c9f63c..e1c1c9c7372f9 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp
@@ -14,6 +14,7 @@
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instructions.h"
+#include "llvm/IR/Operator.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Cloning.h"
 
@@ -107,6 +108,9 @@ static void replaceFunctionCalls(Function *OldF, Function 
*NewF) {
 NewCI->setCallingConv(NewF->getCallingConv());
 NewCI->setAttributes(CI->getAttributes());
 
+if (auto *FPOp = dyn_cast(NewCI))
+  NewCI->setFastMathFlags(CI->getFastMathFlags());
+
 // Do the replacement for this use.
 if (!CI->use_empty())
   CI->replaceAllUsesWith(NewCI);

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Try to preserve instruction metadata as argument attributes (PR #133557)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-ir

Author: Matt Arsenault (arsenm)


Changes

Fixes #131825

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


4 Files Affected:

- (modified) llvm/include/llvm/IR/Attributes.h (+6) 
- (modified) llvm/lib/IR/Attributes.cpp (+32) 
- (added) 
llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll 
(+77) 
- (modified) llvm/tools/llvm-reduce/deltas/ReduceOperandsToArgs.cpp (+10-1) 


``diff
diff --git a/llvm/include/llvm/IR/Attributes.h 
b/llvm/include/llvm/IR/Attributes.h
index d6533b9bcbea1..5252f26f398d2 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -42,6 +42,7 @@ class ConstantRangeList;
 class FoldingSetNodeID;
 class Function;
 class LLVMContext;
+class Instruction;
 class Type;
 class raw_ostream;
 enum FPClassTest : unsigned;
@@ -1285,6 +1286,11 @@ class AttrBuilder {
   /// Add initializes attribute.
   AttrBuilder &addInitializesAttr(const ConstantRangeList &CRL);
 
+  /// Add 0 or more parameter attributes which are equivalent to metadata
+  /// attached to \p I. e.g. !align -> align. This assumes the argument type is
+  /// the same as the original instruction and the attribute is compatible.
+  AttrBuilder &addFromEquivalentMetadata(const Instruction &I);
+
   ArrayRef attrs() const { return Attrs; }
 
   bool operator==(const AttrBuilder &B) const;
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 8da1dfe914818..8cb8b0d927afd 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -2291,6 +2291,38 @@ AttrBuilder &AttrBuilder::addInitializesAttr(const 
ConstantRangeList &CRL) {
   return addConstantRangeListAttr(Attribute::Initializes, CRL.rangesRef());
 }
 
+AttrBuilder &AttrBuilder::addFromEquivalentMetadata(const Instruction &I) {
+  if (const MDNode *NonNull = I.getMetadata(LLVMContext::MD_nonnull))
+addAttribute(Attribute::NonNull);
+
+  if (const MDNode *NoUndef = I.getMetadata(LLVMContext::MD_noundef))
+addAttribute(Attribute::NoUndef);
+
+  if (const MDNode *Align = I.getMetadata(LLVMContext::MD_align)) {
+ConstantInt *CI = mdconst::extract(Align->getOperand(0));
+addAlignmentAttr(CI->getZExtValue());
+  }
+
+  if (const MDNode *Dereferenceable =
+  I.getMetadata(LLVMContext::MD_dereferenceable)) {
+ConstantInt *CI =
+mdconst::extract(Dereferenceable->getOperand(0));
+addDereferenceableAttr(CI->getZExtValue());
+  }
+
+  if (const MDNode *DereferenceableOrNull =
+  I.getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
+ConstantInt *CI =
+mdconst::extract(DereferenceableOrNull->getOperand(0));
+addDereferenceableAttr(CI->getZExtValue());
+  }
+
+  if (const MDNode *Range = I.getMetadata(LLVMContext::MD_range))
+addRangeAttr(getConstantRangeFromMetadata(*Range));
+
+  return *this;
+}
+
 AttrBuilder &AttrBuilder::merge(const AttrBuilder &B) {
   // TODO: Could make this O(n) as we're merging two sorted lists.
   for (const auto &I : B.attrs())
diff --git 
a/llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll 
b/llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll
new file mode 100644
index 0..913ba9d3218fd
--- /dev/null
+++ 
b/llvm/test/tools/llvm-reduce/reduce-operands-to-args-metadata-to-attributes.ll
@@ -0,0 +1,77 @@
+; Check that equivalent parameter attributes are introduced when
+; moving instructions with metadata to arguments.
+
+; RUN: llvm-reduce %s -o %t --abort-on-invalid-reduction 
--delta-passes=operands-to-args --test FileCheck --test-arg %s --test-arg 
--check-prefix=INTERESTING --test-arg --input-file
+; RUN: FileCheck --input-file %t --check-prefix=REDUCED %s
+
+; INTERESTING-LABEL: define ptr @use_nonnull(
+; REDUCED-LABEL: define ptr @use_nonnull(ptr nonnull %nonnull) {
+define ptr @use_nonnull() {
+  %nonnull = load ptr, ptr null, !nonnull !0
+  ret ptr %nonnull
+}
+
+; INTERESTING-LABEL: define void @use_noundef(
+; REDUCED-LABEL: define void @use_noundef(ptr noundef %noundef, <2 x ptr> 
noundef %noundef_vec) {
+define void @use_noundef() {
+  %noundef = load ptr, ptr null, !noundef !0
+  %noundef_vec = load <2 x ptr>, ptr null, !noundef !0
+  store ptr %noundef, ptr null
+  store <2 x ptr> %noundef_vec, ptr null
+  ret void
+}
+
+; INTERESTING-LABEL: define ptr @use_align(
+; REDUCED-LABEL: define ptr @use_align(ptr align 16 %align) {
+define ptr @use_align() {
+  %align = load ptr, ptr null, !align !1
+  ret ptr %align
+}
+
+; INTERESTING-LABEL: define ptr @use_dereferenceable(
+; REDUCED-LABEL: define ptr @use_dereferenceable(ptr dereferenceable(12345) 
%deref) {
+define ptr @use_dereferenceable() {
+  %deref = load ptr, ptr null, !dereferenceable !2
+  ret ptr %deref
+}
+
+; INTERESTING-LABEL: define ptr @use_dereferenceable_or_null(
+; REDUCED-LABEL: define ptr @use_dereferenceable_or_null(ptr 
dereferenceable(7) %deref) {
+define ptr @use_dere

[llvm-branch-commits] [llvm] llvm-reduce: Make run-ir-passes error more consistent (PR #133564)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm ready_for_review 
https://github.com/llvm/llvm-project/pull/133564
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Make run-ir-passes error more consistent (PR #133564)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

arsenm wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.dev/github/pr/llvm/llvm-project/133564?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#133564** https://app.graphite.dev/github/pr/llvm/llvm-project/133564?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/133564?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#133563** https://app.graphite.dev/github/pr/llvm/llvm-project/133563?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/133564
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Make run-ir-passes error more consistent (PR #133564)

2025-03-28 Thread John Regehr via llvm-branch-commits

regehr wrote:

LGTM!

https://github.com/llvm/llvm-project/pull/133564
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] llvm-reduce: Make run-ir-passes error more consistent (PR #133564)

2025-03-28 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/133564

Avoid capitalized Error. This loses the "Error constructing pass pipeline"
part, and just forwards the error to the default report_fatal_error case. Not
sure if it's worth trying to keep.

>From dd572195e429782047cb2f3b0db741908c901708 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Sat, 29 Mar 2025 11:00:26 +0700
Subject: [PATCH] llvm-reduce: Make run-ir-passes error more consistent

Avoid capitalized Error. This loses the "Error constructing pass pipeline"
part, and just forwards the error to the default report_fatal_error case. Not
sure if it's worth trying to keep.
---
 .../tools/llvm-reduce/run-ir-passes-error.ll   | 18 ++
 llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp  |  6 ++
 2 files changed, 20 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/tools/llvm-reduce/run-ir-passes-error.ll

diff --git a/llvm/test/tools/llvm-reduce/run-ir-passes-error.ll 
b/llvm/test/tools/llvm-reduce/run-ir-passes-error.ll
new file mode 100644
index 0..772555d1ec5fb
--- /dev/null
+++ b/llvm/test/tools/llvm-reduce/run-ir-passes-error.ll
@@ -0,0 +1,18 @@
+; RUN: not llvm-reduce --abort-on-invalid-reduction --delta-passes=ir-passes 
--ir-passes=does-not-parse --test FileCheck --test-arg 
--check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s 
-o /dev/null 2>&1 | FileCheck -check-prefix=ERR %s
+
+; CHECK-INTERESTINGNESS-LABEL: @f1
+; ERR: LLVM ERROR: unknown pass name 'does-not-parse'
+
+define i32 @f1(i32 %a) {
+  %b = add i32 %a, 5
+  %c = add i32 %b, 5
+  ret i32 %c
+}
+
+define i32 @f2(i32 %a) {
+  %b = add i32 %a, 5
+  %c = add i32 %b, 5
+  ret i32 %c
+}
+
+declare void @f3()
diff --git a/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp 
b/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp
index e3af05616fe04..f31c5d86dad1e 100644
--- a/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp
+++ b/llvm/tools/llvm-reduce/deltas/RunIRPasses.cpp
@@ -43,10 +43,8 @@ static void runPasses(Oracle &O, ReducerWorkItem &WorkItem) {
   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
 
   ModulePassManager MPM;
-  if (auto Err = PB.parsePassPipeline(MPM, PassPipeline)) {
-errs() << toString(std::move(Err)) << "\n";
-report_fatal_error("Error constructing pass pipeline");
-  }
+  if (auto Err = PB.parsePassPipeline(MPM, PassPipeline))
+report_fatal_error(std::move(Err), false);
   MPM.run(Program, MAM);
 }
 

___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [CI] Exclude docs directories from triggering rebuilds (PR #133185)

2025-03-28 Thread Aiden Grossman via llvm-branch-commits

https://github.com/boomanaiden154 updated 
https://github.com/llvm/llvm-project/pull/133185


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [CI] Exclude docs directories from triggering rebuilds (PR #133185)

2025-03-28 Thread Aiden Grossman via llvm-branch-commits

https://github.com/boomanaiden154 updated 
https://github.com/llvm/llvm-project/pull/133185


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [CI] Exclude docs directories from triggering rebuilds (PR #133185)

2025-03-28 Thread Aiden Grossman via llvm-branch-commits

https://github.com/boomanaiden154 updated 
https://github.com/llvm/llvm-project/pull/133185


___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] Add deactivation symbol operand to ConstantPtrAuth. (PR #133537)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:



@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-llvm-transforms

Author: Peter Collingbourne (pcc)


Changes

Deactivation symbol operands are supported in the code generator by
building on the previously added support for IRELATIVE relocations.

TODO:
- Fix broken test.
- Add bitcode and IR writer support.
- Add tests.


---

Patch is 22.34 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/133537.diff


16 Files Affected:

- (modified) clang/lib/CodeGen/CGPointerAuth.cpp (+3-3) 
- (modified) llvm/include/llvm/Bitcode/LLVMBitCodes.h (+1) 
- (modified) llvm/include/llvm/IR/Constants.h (+9-4) 
- (modified) llvm/include/llvm/SandboxIR/Constant.h (+4-1) 
- (modified) llvm/lib/AsmParser/LLParser.cpp (+21-8) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+17-1) 
- (modified) llvm/lib/IR/AsmWriter.cpp (+3-1) 
- (modified) llvm/lib/IR/Constants.cpp (+8-4) 
- (modified) llvm/lib/IR/ConstantsContext.h (+2-1) 
- (modified) llvm/lib/IR/Core.cpp (+3-1) 
- (modified) llvm/lib/SandboxIR/Constant.cpp (+9-2) 
- (modified) llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp (+31-6) 
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+2-2) 
- (modified) llvm/lib/Transforms/Utils/ValueMapper.cpp (+3-2) 
- (modified) llvm/unittests/SandboxIR/SandboxIRTest.cpp (+1-1) 
- (modified) llvm/unittests/Transforms/Utils/ValueMapperTest.cpp (+9-4) 


``diff
diff --git a/clang/lib/CodeGen/CGPointerAuth.cpp 
b/clang/lib/CodeGen/CGPointerAuth.cpp
index 4b032306ead72..2d72fef470af6 100644
--- a/clang/lib/CodeGen/CGPointerAuth.cpp
+++ b/clang/lib/CodeGen/CGPointerAuth.cpp
@@ -308,9 +308,9 @@ CodeGenModule::getConstantSignedPointer(llvm::Constant 
*Pointer, unsigned Key,
 IntegerDiscriminator = llvm::ConstantInt::get(Int64Ty, 0);
   }
 
-  return llvm::ConstantPtrAuth::get(Pointer,
-llvm::ConstantInt::get(Int32Ty, Key),
-IntegerDiscriminator, 
AddressDiscriminator);
+  return llvm::ConstantPtrAuth::get(
+  Pointer, llvm::ConstantInt::get(Int32Ty, Key), IntegerDiscriminator,
+  AddressDiscriminator, llvm::Constant::getNullValue(UnqualPtrTy));
 }
 
 /// Does a given PointerAuthScheme require us to sign a value
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h 
b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index ec2535ac85966..13521ba6cd00f 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -431,6 +431,7 @@ enum ConstantsCodes {
   CST_CODE_CE_GEP_WITH_INRANGE = 31,  // [opty, flags, range, n x operands]
   CST_CODE_CE_GEP = 32,   // [opty, flags, n x operands]
   CST_CODE_PTRAUTH = 33,  // [ptr, key, disc, addrdisc]
+  CST_CODE_PTRAUTH2 = 34, // [ptr, key, disc, addrdisc, 
DeactivationSymbol]
 };
 
 /// CastOpcodes - These are values used in the bitcode files to encode which
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index a50217078d0ed..45d5352bf06a6 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1022,10 +1022,10 @@ class ConstantPtrAuth final : public Constant {
   friend struct ConstantPtrAuthKeyType;
   friend class Constant;
 
-  constexpr static IntrusiveOperandsAllocMarker AllocMarker{4};
+  constexpr static IntrusiveOperandsAllocMarker AllocMarker{5};
 
   ConstantPtrAuth(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc,
-  Constant *AddrDisc);
+  Constant *AddrDisc, Constant *DeactivationSymbol);
 
   void *operator new(size_t s) { return User::operator new(s, AllocMarker); }
 
@@ -1035,7 +1035,8 @@ class ConstantPtrAuth final : public Constant {
 public:
   /// Return a pointer signed with the specified parameters.
   static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
-  ConstantInt *Disc, Constant *AddrDisc);
+  ConstantInt *Disc, Constant *AddrDisc,
+  Constant *DeactivationSymbol);
 
   /// Produce a new ptrauth expression signing the given value using
   /// the same schema as is stored in one.
@@ -1067,6 +1068,10 @@ class ConstantPtrAuth final : public Constant {
 return !getAddrDiscriminator()->isNullValue();
   }
 
+  Constant *getDeactivationSymbol() const {
+return cast(Op<4>().get());
+  }
+
   /// A constant value for the address discriminator which has special
   /// significance to ctors/dtors lowering. Regular address discrimination 
can't
   /// be applied for them since uses of llvm.global_{c|d}tors are disallowed
@@ -1094,7 +1099,7 @@ class ConstantPtrAuth final : public Constant {
 
 template <>
 struct OperandTraits
-: public FixedNumOperandTraits {};
+: public FixedNumOperandTraits {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPtrAuth, Constant)
 
diff --git a/llvm/include/llvm/SandboxIR/Constant.h 
b/llvm/include/llvm/San

[llvm-branch-commits] Add pointer field protection feature. (PR #133538)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Peter Collingbourne (pcc)


Changes

Pointer field protection is a use-after-free vulnerability
mitigation that works by changing how data structures' pointer
fields are stored in memory. For more information, see the RFC:
https://discourse.llvm.org/t/rfc-structure-protection-a-family-of-uaf-mitigation-techniques/8

TODO:
- Fix test failure.
- Add more tests.
- Add documentation.


---

Patch is 82.31 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/133538.diff


52 Files Affected:

- (modified) clang/include/clang/AST/ASTContext.h (+22) 
- (modified) clang/include/clang/Basic/Attr.td (+6) 
- (modified) clang/include/clang/Basic/Features.def (+3) 
- (modified) clang/include/clang/Basic/LangOptions.def (+3) 
- (modified) clang/include/clang/Basic/LangOptions.h (+11) 
- (modified) clang/include/clang/Basic/TokenKinds.def (+1) 
- (modified) clang/include/clang/Driver/Options.td (+6) 
- (modified) clang/lib/AST/ASTContext.cpp (+95) 
- (modified) clang/lib/AST/ExprConstant.cpp (+1) 
- (modified) clang/lib/AST/Type.cpp (+3-1) 
- (modified) clang/lib/AST/TypePrinter.cpp (+3) 
- (modified) clang/lib/CodeGen/CGCall.cpp (+108-6) 
- (modified) clang/lib/CodeGen/CGClass.cpp (+45-7) 
- (modified) clang/lib/CodeGen/CGExpr.cpp (+7-1) 
- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+1-1) 
- (modified) clang/lib/CodeGen/CGExprCXX.cpp (+10) 
- (modified) clang/lib/CodeGen/CGExprConstant.cpp (+38-1) 
- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+38-5) 
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+6-2) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+39) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+7) 
- (modified) clang/lib/CodeGen/ItaniumCXXABI.cpp (+1) 
- (modified) clang/lib/CodeGen/MicrosoftCXXABI.cpp (+1) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+4) 
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+8) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+5) 
- (added) clang/test/CodeGen/pfp-attribute-disable.cpp (+33) 
- (added) clang/test/CodeGen/pfp-load-store.cpp (+40) 
- (added) clang/test/CodeGen/pfp-memcpy.cpp (+19) 
- (added) clang/test/CodeGen/pfp-null-init.cpp (+16) 
- (added) clang/test/CodeGen/pfp-struct-gep.cpp (+25) 
- (modified) clang/test/CodeGenCXX/trivial_abi.cpp (+1-3) 
- (modified) libcxx/include/__config (+23) 
- (modified) libcxx/include/__functional/function.h (+1-1) 
- (modified) libcxx/include/__memory/shared_ptr.h (+2-2) 
- (modified) libcxx/include/__memory/unique_ptr.h (+2-2) 
- (modified) libcxx/include/__tree (+1-1) 
- (modified) libcxx/include/__type_traits/is_trivially_relocatable.h (+6-2) 
- (modified) libcxx/include/__vector/vector.h (+1-1) 
- (modified) libcxx/include/typeinfo (+1-1) 
- (modified) libcxx/test/libcxx/gdb/gdb_pretty_printer_test.sh.cpp (+9) 
- (modified) libcxxabi/include/__cxxabi_config.h (+10) 
- (modified) libcxxabi/src/private_typeinfo.h (+3-3) 
- (modified) llvm/include/llvm/Analysis/PtrUseVisitor.h (+15) 
- (modified) llvm/include/llvm/IR/Intrinsics.td (+23) 
- (modified) llvm/include/llvm/Transforms/Utils/Local.h (+2) 
- (modified) llvm/lib/Analysis/PtrUseVisitor.cpp (+2-1) 
- (modified) llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp (+246) 
- (modified) llvm/lib/Target/AArch64/AArch64InstrFormats.td (+3) 
- (modified) llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp (+2-4) 
- (modified) llvm/lib/Transforms/Scalar/SROA.cpp (+35-5) 
- (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+20-5) 


``diff
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index af8c49e99a7ce..abba83e1ff9c4 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -183,6 +183,12 @@ struct TypeInfoChars {
   }
 };
 
+struct PFPField {
+  CharUnits structOffset;
+  CharUnits offset;
+  FieldDecl *field;
+};
+
 /// Holds long-lived AST nodes (such as types and decls) that can be
 /// referred to throughout the semantic analysis of a file.
 class ASTContext : public RefCountedBase {
@@ -3618,6 +3624,22 @@ OPT_LIST(V)
 
   StringRef getCUIDHash() const;
 
+  bool isPFPStruct(const RecordDecl *rec) const;
+  void findPFPFields(QualType Ty, CharUnits Offset,
+ std::vector &Fields, bool IncludeVBases) const;
+  bool hasPFPFields(QualType ty) const;
+  bool isPFPField(const FieldDecl *field) const;
+
+  /// Returns whether this record's PFP fields (if any) are trivially
+  /// relocatable (i.e. may be memcpy'd). This may also return true if the
+  /// record does not have any PFP fields, so it may be necessary for the 
caller
+  /// to check for PFP fields, e.g. by calling hasPFPFields().
+  bool arePFPFieldsTriviallyRelocatable(const RecordDecl *RD) const;
+
+  llvm::SetVector PFPFieldsWithEvaluatedOffset;
+  void recordMemberDataPointerEvaluation(const ValueDecl *VD);
+  void recordOffsetOfEvaluation(const OffsetOfExpr *E);
+
 private:
   /// All OMPTraitI

[llvm-branch-commits] Add pointer field protection feature. (PR #133538)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Peter Collingbourne (pcc)


Changes

Pointer field protection is a use-after-free vulnerability
mitigation that works by changing how data structures' pointer
fields are stored in memory. For more information, see the RFC:
https://discourse.llvm.org/t/rfc-structure-protection-a-family-of-uaf-mitigation-techniques/8

TODO:
- Fix test failure.
- Add more tests.
- Add documentation.


---

Patch is 82.31 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/133538.diff


52 Files Affected:

- (modified) clang/include/clang/AST/ASTContext.h (+22) 
- (modified) clang/include/clang/Basic/Attr.td (+6) 
- (modified) clang/include/clang/Basic/Features.def (+3) 
- (modified) clang/include/clang/Basic/LangOptions.def (+3) 
- (modified) clang/include/clang/Basic/LangOptions.h (+11) 
- (modified) clang/include/clang/Basic/TokenKinds.def (+1) 
- (modified) clang/include/clang/Driver/Options.td (+6) 
- (modified) clang/lib/AST/ASTContext.cpp (+95) 
- (modified) clang/lib/AST/ExprConstant.cpp (+1) 
- (modified) clang/lib/AST/Type.cpp (+3-1) 
- (modified) clang/lib/AST/TypePrinter.cpp (+3) 
- (modified) clang/lib/CodeGen/CGCall.cpp (+108-6) 
- (modified) clang/lib/CodeGen/CGClass.cpp (+45-7) 
- (modified) clang/lib/CodeGen/CGExpr.cpp (+7-1) 
- (modified) clang/lib/CodeGen/CGExprAgg.cpp (+1-1) 
- (modified) clang/lib/CodeGen/CGExprCXX.cpp (+10) 
- (modified) clang/lib/CodeGen/CGExprConstant.cpp (+38-1) 
- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+38-5) 
- (modified) clang/lib/CodeGen/CodeGenFunction.h (+6-2) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+39) 
- (modified) clang/lib/CodeGen/CodeGenModule.h (+7) 
- (modified) clang/lib/CodeGen/ItaniumCXXABI.cpp (+1) 
- (modified) clang/lib/CodeGen/MicrosoftCXXABI.cpp (+1) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+4) 
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+8) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+5) 
- (added) clang/test/CodeGen/pfp-attribute-disable.cpp (+33) 
- (added) clang/test/CodeGen/pfp-load-store.cpp (+40) 
- (added) clang/test/CodeGen/pfp-memcpy.cpp (+19) 
- (added) clang/test/CodeGen/pfp-null-init.cpp (+16) 
- (added) clang/test/CodeGen/pfp-struct-gep.cpp (+25) 
- (modified) clang/test/CodeGenCXX/trivial_abi.cpp (+1-3) 
- (modified) libcxx/include/__config (+23) 
- (modified) libcxx/include/__functional/function.h (+1-1) 
- (modified) libcxx/include/__memory/shared_ptr.h (+2-2) 
- (modified) libcxx/include/__memory/unique_ptr.h (+2-2) 
- (modified) libcxx/include/__tree (+1-1) 
- (modified) libcxx/include/__type_traits/is_trivially_relocatable.h (+6-2) 
- (modified) libcxx/include/__vector/vector.h (+1-1) 
- (modified) libcxx/include/typeinfo (+1-1) 
- (modified) libcxx/test/libcxx/gdb/gdb_pretty_printer_test.sh.cpp (+9) 
- (modified) libcxxabi/include/__cxxabi_config.h (+10) 
- (modified) libcxxabi/src/private_typeinfo.h (+3-3) 
- (modified) llvm/include/llvm/Analysis/PtrUseVisitor.h (+15) 
- (modified) llvm/include/llvm/IR/Intrinsics.td (+23) 
- (modified) llvm/include/llvm/Transforms/Utils/Local.h (+2) 
- (modified) llvm/lib/Analysis/PtrUseVisitor.cpp (+2-1) 
- (modified) llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp (+246) 
- (modified) llvm/lib/Target/AArch64/AArch64InstrFormats.td (+3) 
- (modified) llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp (+2-4) 
- (modified) llvm/lib/Transforms/Scalar/SROA.cpp (+35-5) 
- (modified) llvm/lib/Transforms/Utils/SimplifyCFG.cpp (+20-5) 


``diff
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index af8c49e99a7ce..abba83e1ff9c4 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -183,6 +183,12 @@ struct TypeInfoChars {
   }
 };
 
+struct PFPField {
+  CharUnits structOffset;
+  CharUnits offset;
+  FieldDecl *field;
+};
+
 /// Holds long-lived AST nodes (such as types and decls) that can be
 /// referred to throughout the semantic analysis of a file.
 class ASTContext : public RefCountedBase {
@@ -3618,6 +3624,22 @@ OPT_LIST(V)
 
   StringRef getCUIDHash() const;
 
+  bool isPFPStruct(const RecordDecl *rec) const;
+  void findPFPFields(QualType Ty, CharUnits Offset,
+ std::vector &Fields, bool IncludeVBases) const;
+  bool hasPFPFields(QualType ty) const;
+  bool isPFPField(const FieldDecl *field) const;
+
+  /// Returns whether this record's PFP fields (if any) are trivially
+  /// relocatable (i.e. may be memcpy'd). This may also return true if the
+  /// record does not have any PFP fields, so it may be necessary for the 
caller
+  /// to check for PFP fields, e.g. by calling hasPFPFields().
+  bool arePFPFieldsTriviallyRelocatable(const RecordDecl *RD) const;
+
+  llvm::SetVector PFPFieldsWithEvaluatedOffset;
+  void recordMemberDataPointerEvaluation(const ValueDecl *VD);
+  void recordOffsetOfEvaluation(const OffsetOfExpr *E);
+
 private:
   /// All OMPTraitInfo obje

[llvm-branch-commits] Add deactivation symbol operand to ConstantPtrAuth. (PR #133537)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-ir

Author: Peter Collingbourne (pcc)


Changes

Deactivation symbol operands are supported in the code generator by
building on the previously added support for IRELATIVE relocations.

TODO:
- Fix broken test.
- Add bitcode and IR writer support.
- Add tests.


---

Patch is 22.34 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/133537.diff


16 Files Affected:

- (modified) clang/lib/CodeGen/CGPointerAuth.cpp (+3-3) 
- (modified) llvm/include/llvm/Bitcode/LLVMBitCodes.h (+1) 
- (modified) llvm/include/llvm/IR/Constants.h (+9-4) 
- (modified) llvm/include/llvm/SandboxIR/Constant.h (+4-1) 
- (modified) llvm/lib/AsmParser/LLParser.cpp (+21-8) 
- (modified) llvm/lib/Bitcode/Reader/BitcodeReader.cpp (+17-1) 
- (modified) llvm/lib/IR/AsmWriter.cpp (+3-1) 
- (modified) llvm/lib/IR/Constants.cpp (+8-4) 
- (modified) llvm/lib/IR/ConstantsContext.h (+2-1) 
- (modified) llvm/lib/IR/Core.cpp (+3-1) 
- (modified) llvm/lib/SandboxIR/Constant.cpp (+9-2) 
- (modified) llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp (+31-6) 
- (modified) llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp (+2-2) 
- (modified) llvm/lib/Transforms/Utils/ValueMapper.cpp (+3-2) 
- (modified) llvm/unittests/SandboxIR/SandboxIRTest.cpp (+1-1) 
- (modified) llvm/unittests/Transforms/Utils/ValueMapperTest.cpp (+9-4) 


``diff
diff --git a/clang/lib/CodeGen/CGPointerAuth.cpp 
b/clang/lib/CodeGen/CGPointerAuth.cpp
index 4b032306ead72..2d72fef470af6 100644
--- a/clang/lib/CodeGen/CGPointerAuth.cpp
+++ b/clang/lib/CodeGen/CGPointerAuth.cpp
@@ -308,9 +308,9 @@ CodeGenModule::getConstantSignedPointer(llvm::Constant 
*Pointer, unsigned Key,
 IntegerDiscriminator = llvm::ConstantInt::get(Int64Ty, 0);
   }
 
-  return llvm::ConstantPtrAuth::get(Pointer,
-llvm::ConstantInt::get(Int32Ty, Key),
-IntegerDiscriminator, 
AddressDiscriminator);
+  return llvm::ConstantPtrAuth::get(
+  Pointer, llvm::ConstantInt::get(Int32Ty, Key), IntegerDiscriminator,
+  AddressDiscriminator, llvm::Constant::getNullValue(UnqualPtrTy));
 }
 
 /// Does a given PointerAuthScheme require us to sign a value
diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h 
b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
index ec2535ac85966..13521ba6cd00f 100644
--- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h
+++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h
@@ -431,6 +431,7 @@ enum ConstantsCodes {
   CST_CODE_CE_GEP_WITH_INRANGE = 31,  // [opty, flags, range, n x operands]
   CST_CODE_CE_GEP = 32,   // [opty, flags, n x operands]
   CST_CODE_PTRAUTH = 33,  // [ptr, key, disc, addrdisc]
+  CST_CODE_PTRAUTH2 = 34, // [ptr, key, disc, addrdisc, 
DeactivationSymbol]
 };
 
 /// CastOpcodes - These are values used in the bitcode files to encode which
diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h
index a50217078d0ed..45d5352bf06a6 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -1022,10 +1022,10 @@ class ConstantPtrAuth final : public Constant {
   friend struct ConstantPtrAuthKeyType;
   friend class Constant;
 
-  constexpr static IntrusiveOperandsAllocMarker AllocMarker{4};
+  constexpr static IntrusiveOperandsAllocMarker AllocMarker{5};
 
   ConstantPtrAuth(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc,
-  Constant *AddrDisc);
+  Constant *AddrDisc, Constant *DeactivationSymbol);
 
   void *operator new(size_t s) { return User::operator new(s, AllocMarker); }
 
@@ -1035,7 +1035,8 @@ class ConstantPtrAuth final : public Constant {
 public:
   /// Return a pointer signed with the specified parameters.
   static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
-  ConstantInt *Disc, Constant *AddrDisc);
+  ConstantInt *Disc, Constant *AddrDisc,
+  Constant *DeactivationSymbol);
 
   /// Produce a new ptrauth expression signing the given value using
   /// the same schema as is stored in one.
@@ -1067,6 +1068,10 @@ class ConstantPtrAuth final : public Constant {
 return !getAddrDiscriminator()->isNullValue();
   }
 
+  Constant *getDeactivationSymbol() const {
+return cast(Op<4>().get());
+  }
+
   /// A constant value for the address discriminator which has special
   /// significance to ctors/dtors lowering. Regular address discrimination 
can't
   /// be applied for them since uses of llvm.global_{c|d}tors are disallowed
@@ -1094,7 +1099,7 @@ class ConstantPtrAuth final : public Constant {
 
 template <>
 struct OperandTraits
-: public FixedNumOperandTraits {};
+: public FixedNumOperandTraits {};
 
 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPtrAuth, Constant)
 
diff --git a/llvm/include/llvm/SandboxIR/Constant.h 
b/llvm/include/llvm/SandboxIR/Constant.h
index 17f55e973cd76..5243

[llvm-branch-commits] ELF: Add support for R_AARCH64_INST32 relocation. (PR #133534)

2025-03-28 Thread via llvm-branch-commits

llvmbot wrote:



@llvm/pr-subscribers-llvm-binary-utilities

@llvm/pr-subscribers-lld

Author: Peter Collingbourne (pcc)


Changes

The R_AARCH64_INST32 relocation type is to support deactivation
symbols. For more information, see the RFC:
https://discourse.llvm.org/t/rfc-deactivation-symbols/85556

TODO:
- Agree on semantics and relocation type number.
- Add tests.


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


2 Files Affected:

- (modified) lld/ELF/Arch/AArch64.cpp (+8) 
- (modified) llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def (+1) 


``diff
diff --git a/lld/ELF/Arch/AArch64.cpp b/lld/ELF/Arch/AArch64.cpp
index 9538dd4a70bae..110d087230a9c 100644
--- a/lld/ELF/Arch/AArch64.cpp
+++ b/lld/ELF/Arch/AArch64.cpp
@@ -135,6 +135,7 @@ RelExpr AArch64::getRelExpr(RelType type, const Symbol &s,
   switch (type) {
   case R_AARCH64_ABS16:
   case R_AARCH64_ABS32:
+  case R_AARCH64_INST32:
   case R_AARCH64_ABS64:
   case R_AARCH64_ADD_ABS_LO12_NC:
   case R_AARCH64_LDST128_ABS_LO12_NC:
@@ -278,6 +279,7 @@ int64_t AArch64::getImplicitAddend(const uint8_t *buf, 
RelType type) const {
   case R_AARCH64_PREL16:
 return SignExtend64<16>(read16(ctx, buf));
   case R_AARCH64_ABS32:
+  case R_AARCH64_INST32:
   case R_AARCH64_PREL32:
 return SignExtend64<32>(read32(ctx, buf));
   case R_AARCH64_ABS64:
@@ -505,6 +507,12 @@ void AArch64::relocate(uint8_t *loc, const Relocation &rel,
 checkIntUInt(ctx, loc, val, 32, rel);
 write32(ctx, loc, val);
 break;
+  case R_AARCH64_INST32:
+if (!rel.sym->isUndefined()) {
+  checkIntUInt(ctx, loc, val, 32, rel);
+  write32(ctx, loc, val);
+}
+break;
   case R_AARCH64_PLT32:
   case R_AARCH64_GOTPCREL32:
 checkInt(ctx, loc, val, 32, rel);
diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def 
b/llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def
index 05b79eae573f7..c9f17ee4e0c7a 100644
--- a/llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def
+++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/AArch64.def
@@ -61,6 +61,7 @@ ELF_RELOC(R_AARCH64_LD64_GOT_LO12_NC,0x138)
 ELF_RELOC(R_AARCH64_LD64_GOTPAGE_LO15,   0x139)
 ELF_RELOC(R_AARCH64_PLT32,   0x13a)
 ELF_RELOC(R_AARCH64_GOTPCREL32,  0x13b)
+ELF_RELOC(R_AARCH64_INST32,  0x13c)
 // General dynamic TLS relocations
 ELF_RELOC(R_AARCH64_TLSGD_ADR_PREL21,0x200)
 ELF_RELOC(R_AARCH64_TLSGD_ADR_PAGE21,0x201)

``




https://github.com/llvm/llvm-project/pull/133534
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


  1   2   3   >