[llvm-branch-commits] [llvm] release/20.x: Fix crash lowering stack guard on OpenBSD/aarch64. (#125416) (PR #136458)

2025-04-19 Thread via llvm-branch-commits

https://github.com/llvmbot created 
https://github.com/llvm/llvm-project/pull/136458

Backport c180e249d0013474d502cd779ec65b33cf7e9468

Requested by: @brad0

>From 30dbc5ffd9816052991242cfeb29ebb6372354fb Mon Sep 17 00:00:00 2001
From: 3405691582 
Date: Mon, 31 Mar 2025 12:17:55 -0400
Subject: [PATCH] Fix crash lowering stack guard on OpenBSD/aarch64. (#125416)

TargetLoweringBase::getIRStackGuard refers to a platform-specific guard
variable. Before this change, TargetLoweringBase::getSDagStackGuard only
referred to a different variable.

This means that SelectionDAGBuilder's getLoadStackGuard does not get
memory operands. However, AArch64InstrInfo::expandPostRAPseudo assumes
that the passed MachineInstr has nonzero memoperands, causing a
segfault.

We have two possible options here: either disabling the LOAD_STACK_GUARD
node entirely in AArch64TargetLowering::useLoadStackGuardNode or just
making the platform-specific values match across TargetLoweringBase.
Here, we try the latter.

(cherry picked from commit c180e249d0013474d502cd779ec65b33cf7e9468)
---
 llvm/lib/CodeGen/TargetLoweringBase.cpp | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp 
b/llvm/lib/CodeGen/TargetLoweringBase.cpp
index 9c56912aa6ba0..411f59e714b0e 100644
--- a/llvm/lib/CodeGen/TargetLoweringBase.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp
@@ -1987,6 +1987,9 @@ void TargetLoweringBase::insertSSPDeclarations(Module &M) 
const {
 // Currently only support "standard" __stack_chk_guard.
 // TODO: add LOAD_STACK_GUARD support.
 Value *TargetLoweringBase::getSDagStackGuard(const Module &M) const {
+  if (getTargetMachine().getTargetTriple().isOSOpenBSD()) {
+return M.getNamedValue("__guard_local");
+  }
   return M.getNamedValue("__stack_chk_guard");
 }
 

___
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] [ConstraintElim] Opimize abs based on known constraints (PR #135754)

2025-04-19 Thread via llvm-branch-commits

https://github.com/el-ev converted_to_draft 
https://github.com/llvm/llvm-project/pull/135754
___
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] "Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" (PR #136467)

2025-04-19 Thread via llvm-branch-commits

https://github.com/el-ev created 
https://github.com/llvm/llvm-project/pull/136467

None

>From 66a876c54233d7bf928b72753485ac2733563b0f Mon Sep 17 00:00:00 2001
From: Iris Shi <0...@owo.li>
Date: Sun, 20 Apr 2025 10:32:29 +0800
Subject: [PATCH] "Recommit "[ConstraintElim] Simplify cmp after
 uadd.sat/usub.sat (#135603)"

---
 .../Transforms/Scalar/ConstraintElimination.cpp  | 16 
 .../ConstraintElimination/uadd-usub-sat.ll   |  7 ++-
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp 
b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 8684420b73671..9fd6b067debdf 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1139,6 +1139,8 @@ void State::addInfoFor(BasicBlock &BB) {
   // when simplifying uses of the min/max intrinsics.
   [[fallthrough]];
 case Intrinsic::abs:
+case Intrinsic::uadd_sat:
+case Intrinsic::usub_sat:
   if (!isGuaranteedNotToBePoison(&I))
 break;
   WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
@@ -1896,6 +1898,20 @@ static bool eliminateConstraints(Function &F, 
DominatorTree &DT, LoopInfo &LI,
 AddFact(Pred, MinMax, MinMax->getRHS());
 continue;
   }
+  if (auto *USatI = dyn_cast(CB.Inst)) {
+switch (USatI->getIntrinsicID()) {
+default:
+  llvm_unreachable("Unexpected intrinsic.");
+case Intrinsic::uadd_sat:
+  AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getLHS());
+  AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getRHS());
+  break;
+case Intrinsic::usub_sat:
+  AddFact(ICmpInst::ICMP_ULE, USatI, USatI->getLHS());
+  break;
+}
+continue;
+  }
 }
 
 Value *A = nullptr, *B = nullptr;
diff --git a/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll 
b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
index 42bbe67024e58..c19a0548d78b2 100644
--- a/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
+++ b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
@@ -8,9 +8,7 @@ define i1 @uadd_sat_uge(i64 noundef %a, i64 noundef %b) {
 ; CHECK-LABEL: define i1 @uadd_sat_uge(
 ; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
 ; CHECK-NEXT:[[ADD_SAT:%.*]] = call i64 @llvm.uadd.sat.i64(i64 [[A]], i64 
[[B]])
-; CHECK-NEXT:[[CMP1:%.*]] = icmp uge i64 [[ADD_SAT]], [[A]]
-; CHECK-NEXT:[[CMP2:%.*]] = icmp uge i64 [[ADD_SAT]], [[B]]
-; CHECK-NEXT:[[CMP:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT:[[CMP:%.*]] = and i1 true, true
 ; CHECK-NEXT:ret i1 [[CMP]]
 ;
   %add.sat = call i64 @llvm.uadd.sat.i64(i64 %a, i64 %b)
@@ -24,8 +22,7 @@ define i1 @usub_sat_ule_lhs(i64 noundef %a, i64 noundef %b) {
 ; CHECK-LABEL: define i1 @usub_sat_ule_lhs(
 ; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
 ; CHECK-NEXT:[[SUB_SAT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[A]], i64 
[[B]])
-; CHECK-NEXT:[[CMP:%.*]] = icmp ule i64 [[SUB_SAT]], [[A]]
-; CHECK-NEXT:ret i1 [[CMP]]
+; CHECK-NEXT:ret i1 true
 ;
   %sub.sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
   %cmp = icmp ule i64 %sub.sat, %a

___
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] Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" (PR #136467)

2025-04-19 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-llvm-transforms

Author: Iris (el-ev)


Changes



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


2 Files Affected:

- (modified) llvm/lib/Transforms/Scalar/ConstraintElimination.cpp (+16) 
- (modified) llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll (+2-5) 


``diff
diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp 
b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index 8684420b73671..9fd6b067debdf 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -1139,6 +1139,8 @@ void State::addInfoFor(BasicBlock &BB) {
   // when simplifying uses of the min/max intrinsics.
   [[fallthrough]];
 case Intrinsic::abs:
+case Intrinsic::uadd_sat:
+case Intrinsic::usub_sat:
   if (!isGuaranteedNotToBePoison(&I))
 break;
   WorkList.push_back(FactOrCheck::getInstFact(DT.getNode(&BB), &I));
@@ -1896,6 +1898,20 @@ static bool eliminateConstraints(Function &F, 
DominatorTree &DT, LoopInfo &LI,
 AddFact(Pred, MinMax, MinMax->getRHS());
 continue;
   }
+  if (auto *USatI = dyn_cast(CB.Inst)) {
+switch (USatI->getIntrinsicID()) {
+default:
+  llvm_unreachable("Unexpected intrinsic.");
+case Intrinsic::uadd_sat:
+  AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getLHS());
+  AddFact(ICmpInst::ICMP_UGE, USatI, USatI->getRHS());
+  break;
+case Intrinsic::usub_sat:
+  AddFact(ICmpInst::ICMP_ULE, USatI, USatI->getLHS());
+  break;
+}
+continue;
+  }
 }
 
 Value *A = nullptr, *B = nullptr;
diff --git a/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll 
b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
index 42bbe67024e58..c19a0548d78b2 100644
--- a/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
+++ b/llvm/test/Transforms/ConstraintElimination/uadd-usub-sat.ll
@@ -8,9 +8,7 @@ define i1 @uadd_sat_uge(i64 noundef %a, i64 noundef %b) {
 ; CHECK-LABEL: define i1 @uadd_sat_uge(
 ; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
 ; CHECK-NEXT:[[ADD_SAT:%.*]] = call i64 @llvm.uadd.sat.i64(i64 [[A]], i64 
[[B]])
-; CHECK-NEXT:[[CMP1:%.*]] = icmp uge i64 [[ADD_SAT]], [[A]]
-; CHECK-NEXT:[[CMP2:%.*]] = icmp uge i64 [[ADD_SAT]], [[B]]
-; CHECK-NEXT:[[CMP:%.*]] = and i1 [[CMP1]], [[CMP2]]
+; CHECK-NEXT:[[CMP:%.*]] = and i1 true, true
 ; CHECK-NEXT:ret i1 [[CMP]]
 ;
   %add.sat = call i64 @llvm.uadd.sat.i64(i64 %a, i64 %b)
@@ -24,8 +22,7 @@ define i1 @usub_sat_ule_lhs(i64 noundef %a, i64 noundef %b) {
 ; CHECK-LABEL: define i1 @usub_sat_ule_lhs(
 ; CHECK-SAME: i64 noundef [[A:%.*]], i64 noundef [[B:%.*]]) {
 ; CHECK-NEXT:[[SUB_SAT:%.*]] = call i64 @llvm.usub.sat.i64(i64 [[A]], i64 
[[B]])
-; CHECK-NEXT:[[CMP:%.*]] = icmp ule i64 [[SUB_SAT]], [[A]]
-; CHECK-NEXT:ret i1 [[CMP]]
+; CHECK-NEXT:ret i1 true
 ;
   %sub.sat = call i64 @llvm.usub.sat.i64(i64 %a, i64 %b)
   %cmp = icmp ule i64 %sub.sat, %a

``




https://github.com/llvm/llvm-project/pull/136467
___
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] "Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" (PR #136467)

2025-04-19 Thread via llvm-branch-commits

https://github.com/el-ev ready_for_review 
https://github.com/llvm/llvm-project/pull/136467
___
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] Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" (PR #136467)

2025-04-19 Thread via llvm-branch-commits

https://github.com/el-ev edited https://github.com/llvm/llvm-project/pull/136467
___
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] release/20.x: Fix crash lowering stack guard on OpenBSD/aarch64. (#125416) (PR #136458)

2025-04-19 Thread via llvm-branch-commits

llvmbot wrote:

@Sterling-Augustine What do you think about merging this PR to the release 
branch?

https://github.com/llvm/llvm-project/pull/136458
___
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] release/20.x: Fix crash lowering stack guard on OpenBSD/aarch64. (#125416) (PR #136458)

2025-04-19 Thread via llvm-branch-commits

https://github.com/llvmbot milestoned 
https://github.com/llvm/llvm-project/pull/136458
___
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] [ConstraintElim] Add `noundef` to several testcases (NFC) (PR #135799)

2025-04-19 Thread via llvm-branch-commits

https://github.com/el-ev closed https://github.com/llvm/llvm-project/pull/135799
___
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] [lldb] [lldb] Fix SBTarget::ReadInstruction with flavor (PR #136034)

2025-04-19 Thread Jonas Devlieghere via llvm-branch-commits

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


https://github.com/llvm/llvm-project/pull/136034
___
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] "Recommit "[ConstraintElim] Simplify cmp after uadd.sat/usub.sat (#135603)" (PR #136467)

2025-04-19 Thread via llvm-branch-commits

el-ev 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/136467?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#136467** https://app.graphite.dev/github/pr/llvm/llvm-project/136467?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/136467?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#136291** https://app.graphite.dev/github/pr/llvm/llvm-project/136291?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/136467
___
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] [SPARC] Promote i32 CTTZ when we have VIS3 (PR #135894)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits


@@ -1,70 +1,438 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
-; RUN: llc < %s -mtriple=sparc -mcpu=v9 | FileCheck %s
+; RUN: llc < %s -mtriple=sparc | FileCheck %s -check-prefix=SPARC
+; RUN: llc < %s -mtriple=sparc -mattr=popc | FileCheck %s 
-check-prefix=SPARC-POPC
+; RUN: llc < %s -mtriple=sparc -mattr=vis3 | FileCheck %s 
-check-prefix=SPARC-VIS3
+; RUN: llc < %s -mtriple=sparcv9 | FileCheck %s -check-prefix=SPARC64
+; RUN: llc < %s -mtriple=sparcv9 -mattr=popc | FileCheck %s 
-check-prefix=SPARC64-POPC
+; RUN: llc < %s -mtriple=sparcv9 -mattr=vis3 | FileCheck %s 
-check-prefix=SPARC64-VIS3
 
-define i32 @f(i32 %x) {
-; CHECK-LABEL: f:
-; CHECK: .cfi_startproc
-; CHECK-NEXT:  ! %bb.0: ! %entry
-; CHECK-NEXT:sub %g0, %o0, %o1
-; CHECK-NEXT:and %o0, %o1, %o1
-; CHECK-NEXT:sethi 122669, %o2
-; CHECK-NEXT:or %o2, 305, %o2
-; CHECK-NEXT:smul %o1, %o2, %o1
-; CHECK-NEXT:srl %o1, 27, %o1
-; CHECK-NEXT:sethi %hi(.LCPI0_0), %o2
-; CHECK-NEXT:add %o2, %lo(.LCPI0_0), %o2
-; CHECK-NEXT:ldub [%o2+%o1], %o1
-; CHECK-NEXT:cmp %o0, 0
-; CHECK-NEXT:move %icc, 0, %o1
-; CHECK-NEXT:retl
-; CHECK-NEXT:mov %o1, %o0
-entry:
-  %0 = call i32 @llvm.cttz.i32(i32 %x, i1 true)
-  %1 = icmp eq i32 %x, 0
-  %2 = select i1 %1, i32 0, i32 %0
-  %3 = trunc i32 %2 to i8
-  %conv = zext i8 %3 to i32
-  ret i32 %conv
+;; FIXME the extensionless versions should be a libcall.

s-barannikov wrote:

This should be moved/adjusted somewhere in SparcISelLowering.cpp to make it 
more visible (no one reads tests).
libcall should be used for both i32/i64 when neither popc nor lzcnt is 
available and has nothing to do with zero/sign-extension


https://github.com/llvm/llvm-project/pull/135894
___
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] [SPARC] Promote i32 CTTZ when we have VIS3 (PR #135894)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov edited 
https://github.com/llvm/llvm-project/pull/135894
___
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] [SPARC] Promote i32 CTTZ when we have VIS3 (PR #135894)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov approved this pull request.

LGTM with one note

https://github.com/llvm/llvm-project/pull/135894
___
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] [SPARC] Use op-then-halve instructions when we have VIS3 (PR #135718)

2025-04-19 Thread Brad Smith via llvm-branch-commits

brad0 wrote:

cc @s-barannikov 

https://github.com/llvm/llvm-project/pull/135718
___
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] [mlir] [IR] Make @llvm.memset prototype byte width dependent (PR #106537)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov updated 
https://github.com/llvm/llvm-project/pull/106537

>From 0bf73e36bee4838ec17742b123f7e5b97c4550b8 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov 
Date: Thu, 1 Aug 2024 23:47:25 +0300
Subject: [PATCH] [IR] Make @llvm.memset prototype byte width dependent

This patch changes the type of the value argument of @llvm.memset and
similar intrinsics from i8 to iN, where N is the byte width specified
in data layout string.
Note that the argument still has fixed type (not overloaded), but type
checker will complain if the type does not match the byte width.

Ideally, the type of the argument would be dependent on the address
space of the pointer argument. It is easy to do this (and I did it
downstream as a PoC), but since data layout string doesn't currently
allow different byte widths for different address spaces, I refrained
from doing it now.
---
 llvm/include/llvm-c/Core.h|  2 +-
 llvm/include/llvm/IR/Intrinsics.h | 11 ++--
 llvm/include/llvm/IR/Intrinsics.td| 13 ++--
 llvm/lib/AsmParser/LLParser.cpp   |  4 +-
 llvm/lib/IR/AutoUpgrade.cpp   |  4 +-
 llvm/lib/IR/Core.cpp  |  4 +-
 llvm/lib/IR/Function.cpp  |  4 +-
 llvm/lib/IR/IRBuilder.cpp |  2 +-
 llvm/lib/IR/Intrinsics.cpp| 61 +++
 llvm/lib/IR/Verifier.cpp  |  2 +-
 llvm/lib/Transforms/IPO/ExpandVariadics.cpp   |  2 +-
 .../NumericalStabilitySanitizer.cpp   |  2 +-
 .../LLVMIR/LLVMToLLVMIRTranslation.cpp|  4 +-
 13 files changed, 69 insertions(+), 46 deletions(-)

diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 871e5eb8457d7..5b0a4b1fedf55 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2816,7 +2816,7 @@ LLVMValueRef LLVMGetIntrinsicDeclaration(LLVMModuleRef 
Mod,
  *
  * @see llvm::Intrinsic::getType()
  */
-LLVMTypeRef LLVMIntrinsicGetType(LLVMContextRef Ctx, unsigned ID,
+LLVMTypeRef LLVMIntrinsicGetType(LLVMModuleRef Mod, unsigned ID,
  LLVMTypeRef *ParamTypes, size_t ParamCount);
 
 /**
diff --git a/llvm/include/llvm/IR/Intrinsics.h 
b/llvm/include/llvm/IR/Intrinsics.h
index 6fb1bf9359b9a..eaf452788cea3 100644
--- a/llvm/include/llvm/IR/Intrinsics.h
+++ b/llvm/include/llvm/IR/Intrinsics.h
@@ -22,6 +22,7 @@
 
 namespace llvm {
 
+class DataLayout;
 class Type;
 class FunctionType;
 class Function;
@@ -74,7 +75,7 @@ namespace Intrinsic {
   std::string getNameNoUnnamedTypes(ID Id, ArrayRef Tys);
 
   /// Return the function type for an intrinsic.
-  FunctionType *getType(LLVMContext &Context, ID id, ArrayRef Tys = 
{});
+  FunctionType *getType(Module *M, ID id, ArrayRef Tys = {});
 
   /// Returns true if the intrinsic can be overloaded.
   bool isOverloaded(ID id);
@@ -135,6 +136,7 @@ namespace Intrinsic {
   struct IITDescriptor {
 enum IITDescriptorKind {
   Void,
+  Byte,
   VarArg,
   MMX,
   Token,
@@ -251,7 +253,8 @@ namespace Intrinsic {
   /// Returns false if the given type matches with the constraints, true
   /// otherwise.
   MatchIntrinsicTypesResult
-  matchIntrinsicSignature(FunctionType *FTy, ArrayRef &Infos,
+  matchIntrinsicSignature(const DataLayout &DL, FunctionType *FTy,
+  ArrayRef &Infos,
   SmallVectorImpl &ArgTys);
 
   /// Verify if the intrinsic has variable arguments. This method is intended 
to
@@ -266,8 +269,8 @@ namespace Intrinsic {
   ///
   /// Returns false if the given ID and function type combination is not a
   /// valid intrinsic call.
-  bool getIntrinsicSignature(Intrinsic::ID, FunctionType *FT,
- SmallVectorImpl &ArgTys);
+  bool getIntrinsicSignature(const DataLayout &DL, Intrinsic::ID,
+ FunctionType *FT, SmallVectorImpl 
&ArgTys);
 
   /// Same as previous, but accepts a Function instead of ID and FunctionType.
   bool getIntrinsicSignature(Function *F, SmallVectorImpl &ArgTys);
diff --git a/llvm/include/llvm/IR/Intrinsics.td 
b/llvm/include/llvm/IR/Intrinsics.td
index a174ccbf61002..ea88558dc1391 100644
--- a/llvm/include/llvm/IR/Intrinsics.td
+++ b/llvm/include/llvm/IR/Intrinsics.td
@@ -308,6 +308,7 @@ def IIT_V1 : IIT_Vec<1, 28>;
 def IIT_VARARG : IIT_VT;
 def IIT_HALF_VEC_ARG : IIT_Base<30>;
 def IIT_SAME_VEC_WIDTH_ARG : IIT_Base<31>;
+def IIT_BYTE : IIT_Base<32>;
 def IIT_VEC_OF_ANYPTRS_TO_ELT : IIT_Base<34>;
 def IIT_I128 : IIT_Int<128, 35>;
 def IIT_V512 : IIT_Vec<512, 36>;
@@ -393,6 +394,10 @@ class LLVMType {
 !foreach(iit, IITs, iit.Number));
 }
 
+class LLVMByteType : LLVMType {
+  let Sig = [IIT_BYTE.Number];
+}
+
 class LLVMAnyType : LLVMType {
   let ArgCode = !cond(
 !eq(vt, Any) : ArgKind.Any,
@@ -501,7 +506,7 @@ class LLVMVectorOfBitcastsToInt
   : LLVMMatchType;
 
 def llvm_void_ty   : LLVMType;
-
+def llvm_byte_ty 

[llvm-branch-commits] [llvm] [IRBuilder] Add getByteTy and use it in CreatePtrAdd (PR #106539)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov updated 
https://github.com/llvm/llvm-project/pull/106539

>From c335952171cd7907e38c8a12b4b50ea1fb384bb0 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov 
Date: Thu, 22 Aug 2024 15:10:58 +0300
Subject: [PATCH] [IRBuilder] Add getByteTy and use it in CreatePtrAdd

The change requires DataLayout instance to be available, which, in turn,
requires insertion point to be set. In-tree tests detected only one case
when the function was called without setting an insertion point, it was
changed to create a constant expression directly.
---
 llvm/include/llvm/IR/IRBuilder.h  | 10 +++--
 .../Instrumentation/SanitizerCoverage.cpp |  5 ++---
 llvm/unittests/IR/IRBuilderTest.cpp   | 22 +++
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/IR/IRBuilder.h b/llvm/include/llvm/IR/IRBuilder.h
index 0e68ffadc6939..02c9c80e0e5f2 100644
--- a/llvm/include/llvm/IR/IRBuilder.h
+++ b/llvm/include/llvm/IR/IRBuilder.h
@@ -526,6 +526,12 @@ class IRBuilderBase {
   // Type creation methods
   
//======//
 
+  /// Fetch the type representing a byte.
+  IntegerType *getByteTy() {
+const DataLayout &DL = BB->getDataLayout();
+return Type::getIntNTy(Context, DL.getByteWidth());
+  }
+
   /// Fetch the type representing a single bit
   IntegerType *getInt1Ty() {
 return Type::getInt1Ty(Context);
@@ -2022,12 +2028,12 @@ class IRBuilderBase {
 
   Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
   GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
-return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
+return CreateGEP(getByteTy(), Ptr, Offset, Name, NW);
   }
 
   Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
   const Twine &Name = "") {
-return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
+return CreateGEP(getByteTy(), Ptr, Offset, Name,
  GEPNoWrapFlags::inBounds());
   }
 
diff --git a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp 
b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
index e52269637b92d..5200bbb0b662a 100644
--- a/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
+++ b/llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
@@ -344,14 +344,13 @@ ModuleSanitizerCoverage::CreateSecStartEnd(Module &M, 
const char *Section,
   new GlobalVariable(M, Ty, false, Linkage, nullptr,
  getSectionEnd(Section));
   SecEnd->setVisibility(GlobalValue::HiddenVisibility);
-  IRBuilder<> IRB(M.getContext());
   if (!TargetTriple.isOSBinFormatCOFF())
 return std::make_pair(SecStart, SecEnd);
 
   // Account for the fact that on windows-msvc __start_* symbols actually
   // point to a uint64_t before the start of the array.
-  auto GEP =
-  IRB.CreatePtrAdd(SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
+  Constant *GEP = ConstantExpr::getGetElementPtr(
+  Int8Ty, SecStart, ConstantInt::get(IntptrTy, sizeof(uint64_t)));
   return std::make_pair(GEP, SecEnd);
 }
 
diff --git a/llvm/unittests/IR/IRBuilderTest.cpp 
b/llvm/unittests/IR/IRBuilderTest.cpp
index b7eb0af728331..6aa3e61f8bcbf 100644
--- a/llvm/unittests/IR/IRBuilderTest.cpp
+++ b/llvm/unittests/IR/IRBuilderTest.cpp
@@ -533,6 +533,14 @@ TEST_F(IRBuilderTest, DataLayout) {
   EXPECT_FALSE(M->getDataLayout().isLegalInteger(32));
 }
 
+TEST_F(IRBuilderTest, GetByteTy) {
+  IRBuilder<> Builder(BB);
+
+  EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(8));
+  M->setDataLayout("b:32");
+  EXPECT_TRUE(Builder.getByteTy()->isIntegerTy(32));
+}
+
 TEST_F(IRBuilderTest, GetIntTy) {
   IRBuilder<> Builder(BB);
   IntegerType *Ty1 = Builder.getInt1Ty();
@@ -544,6 +552,20 @@ TEST_F(IRBuilderTest, GetIntTy) {
   EXPECT_EQ(IntPtrTy, IntegerType::get(Ctx, IntPtrBitSize));
 }
 
+TEST_F(IRBuilderTest, CreatePtrAdd) {
+  IRBuilder<> Builder(BB);
+
+  M->setDataLayout("b:16-p:32:32");
+  Value *V = Builder.CreatePtrAdd(GV, ConstantInt::get(Ctx, APInt(32, 42)));
+  ASSERT_TRUE(isa(V));
+  EXPECT_TRUE(cast(V)->getResultElementType()->isIntegerTy(16));
+
+  M->setDataLayout("b:32-p:64:32");
+  V = Builder.CreateInBoundsPtrAdd(GV, ConstantInt::get(Ctx, APInt(64, 42)));
+  ASSERT_TRUE(isa(V));
+  EXPECT_TRUE(cast(V)->getResultElementType()->isIntegerTy(32));
+}
+
 TEST_F(IRBuilderTest, UnaryOperators) {
   IRBuilder Builder(BB);
   Value *V = Builder.CreateLoad(GV->getValueType(), GV);

___
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] [ValueTracking] Make isBytewiseValue byte width agnostic (PR #106538)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov updated 
https://github.com/llvm/llvm-project/pull/106538

>From 3036dfc0ddfad4aeee1b5bd23627ad1949875489 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov 
Date: Fri, 2 Aug 2024 13:14:49 +0300
Subject: [PATCH] [ValueTracking] Make isBytewiseValue byte width agnostic

This is a simple change to show how easy it can be to support unusual
byte widths in the middle end.
---
 llvm/lib/Analysis/ValueTracking.cpp | 30 +++--
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index cdf7f052943c8..2007a9599114c 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -6348,21 +6348,22 @@ std::optional llvm::computeKnownFPSignBit(const 
Value *V, unsigned Depth,
 }
 
 Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
+  unsigned ByteWidth = DL.getByteWidth();
 
   // All byte-wide stores are splatable, even of arbitrary variables.
-  if (V->getType()->isIntegerTy(8))
+  if (V->getType()->isIntegerTy(ByteWidth))
 return V;
 
   LLVMContext &Ctx = V->getContext();
 
   // Undef don't care.
-  auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
+  auto *UndefByte = UndefValue::get(Type::getIntNTy(Ctx, ByteWidth));
   if (isa(V))
-return UndefInt8;
+return UndefByte;
 
   // Return poison for zero-sized type.
   if (DL.getTypeStoreSize(V->getType()).isZero())
-return PoisonValue::get(Type::getInt8Ty(Ctx));
+return PoisonValue::get(Type::getIntNTy(Ctx, ByteWidth));
 
   Constant *C = dyn_cast(V);
   if (!C) {
@@ -6377,7 +6378,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout 
&DL) {
 
   // Handle 'null' ConstantArrayZero etc.
   if (C->isNullValue())
-return Constant::getNullValue(Type::getInt8Ty(Ctx));
+return Constant::getNullValue(Type::getIntNTy(Ctx, ByteWidth));
 
   // Constant floating-point values can be handled as integer values if the
   // corresponding integer value is "byteable".  An important case is 0.0.
@@ -6394,13 +6395,14 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout 
&DL) {
   : nullptr;
   }
 
-  // We can handle constant integers that are multiple of 8 bits.
+  // We can handle constant integers that are multiple of the byte width.
   if (ConstantInt *CI = dyn_cast(C)) {
-if (CI->getBitWidth() % 8 == 0) {
-  assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
-  if (!CI->getValue().isSplat(8))
+if (CI->getBitWidth() % ByteWidth == 0) {
+  assert(CI->getBitWidth() > ByteWidth &&
+ "single byte should be handled above!");
+  if (!CI->getValue().isSplat(ByteWidth))
 return nullptr;
-  return ConstantInt::get(Ctx, CI->getValue().trunc(8));
+  return ConstantInt::get(Ctx, CI->getValue().trunc(ByteWidth));
 }
   }
 
@@ -6420,15 +6422,15 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout 
&DL) {
   return LHS;
 if (!LHS || !RHS)
   return nullptr;
-if (LHS == UndefInt8)
+if (LHS == UndefByte)
   return RHS;
-if (RHS == UndefInt8)
+if (RHS == UndefByte)
   return LHS;
 return nullptr;
   };
 
   if (ConstantDataSequential *CA = dyn_cast(C)) {
-Value *Val = UndefInt8;
+Value *Val = UndefByte;
 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
   if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), 
DL
 return nullptr;
@@ -6436,7 +6438,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout 
&DL) {
   }
 
   if (isa(C)) {
-Value *Val = UndefInt8;
+Value *Val = UndefByte;
 for (Value *Op : C->operands())
   if (!(Val = Merge(Val, isBytewiseValue(Op, DL
 return nullptr;

___
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] [SimplifyLibCalls] Add initial support for non-8-bit bytes (PR #106542)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov updated 
https://github.com/llvm/llvm-project/pull/106542

>From bb76990f184c59abfcaa7c5f9409ec8d9b309636 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov 
Date: Wed, 28 Aug 2024 16:09:44 +0300
Subject: [PATCH] [SimplifyLibCalls] Add initial support for non-8-bit bytes

The patch makes CharWidth argument of `getStringLength` mandatory
and ensures the correct values are passed in most cases.
This is *not* a complete support for unusual byte widths in
SimplifyLibCalls since `getConstantStringInfo` returns false for those.
The code guarded by `getConstantStringInfo` returning true is unchanged
because the changes are currently not testable.
---
 llvm/include/llvm/Analysis/ValueTracking.h|   4 +-
 .../llvm/Transforms/Utils/SimplifyLibCalls.h  |   4 +-
 llvm/lib/Analysis/MemoryBuiltins.cpp  |   3 +-
 llvm/lib/Analysis/ValueTracking.cpp   |  40 ++--
 .../InstCombine/InstCombineCalls.cpp  |  12 +-
 .../InstCombine/InstructionCombining.cpp  |   5 +-
 .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 191 --
 .../InstCombine/SimplifyLibCalls/fputs-b16.ll |  19 ++
 .../SimplifyLibCalls/fwrite-b16.ll|  19 ++
 .../SimplifyLibCalls/memchr-b16.ll|  34 
 .../SimplifyLibCalls/memcmp-b32.ll|  32 +++
 .../SimplifyLibCalls/memcpy-b16.ll|  69 +++
 .../SimplifyLibCalls/memcpy_chk-b16.ll|  17 ++
 .../SimplifyLibCalls/mempcpy-b16.ll   |  17 ++
 .../SimplifyLibCalls/memrchr-b16.ll   |  20 ++
 .../SimplifyLibCalls/memset-b16.ll|  66 ++
 .../SimplifyLibCalls/stpcpy-b16.ll|  31 +++
 .../SimplifyLibCalls/stpcpy_chk-b16.ll|  44 
 .../SimplifyLibCalls/stpncpy-b16.ll   |  47 +
 .../SimplifyLibCalls/strcat-b16.ll|  20 ++
 .../SimplifyLibCalls/strchr-b16.ll|  45 +
 .../SimplifyLibCalls/strcmp-b32.ll|  50 +
 .../SimplifyLibCalls/strcpy-b16.ll|  18 ++
 .../SimplifyLibCalls/strcpy_chk-b16.ll|  30 +++
 .../SimplifyLibCalls/strlcpy-b16.ll   |  18 ++
 .../SimplifyLibCalls/strlen-b16.ll|  16 ++
 .../SimplifyLibCalls/strncat-b16.ll   |  20 ++
 .../SimplifyLibCalls/strncmp-b32.ll   |  34 
 .../SimplifyLibCalls/strncpy-b16.ll   |  43 
 .../SimplifyLibCalls/strndup-b16.ll   |  17 ++
 .../SimplifyLibCalls/strnlen-b16.ll   |  18 ++
 .../SimplifyLibCalls/wcslen-b16.ll|  19 ++
 llvm/test/Transforms/InstCombine/bcmp-1.ll|   2 +-
 llvm/test/Transforms/InstCombine/memcmp-1.ll  |   2 +-
 llvm/test/Transforms/InstCombine/strncmp-1.ll |   2 +-
 35 files changed, 928 insertions(+), 100 deletions(-)
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/fputs-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/fwrite-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/memchr-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/memcmp-b32.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/memcpy-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/memcpy_chk-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/mempcpy-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/memrchr-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/memset-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/stpcpy-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/stpcpy_chk-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/stpncpy-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strcat-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strchr-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strcmp-b32.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strcpy-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strcpy_chk-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strlcpy-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strlen-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strncat-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strncmp-b32.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strncpy-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strndup-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/strnlen-b16.ll
 create mode 100644 
llvm/test/Transforms/InstCombine/SimplifyLibCalls/wcslen-b16.ll

diff --git a/llvm/include/llvm/Analysis/ValueTracking.h 
b/llvm/include/llvm/Analysis/ValueTracking.h
index 

[llvm-branch-commits] [clang] [llvm] [ValueTracking] Add CharWidth argument to getConstantStringInfo (NFC) (PR #106541)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov updated 
https://github.com/llvm/llvm-project/pull/106541

>From 24ff5d64d01e02e2d3641fad6b72ddab6bb32eea Mon Sep 17 00:00:00 2001
From: Sergei Barannikov 
Date: Wed, 28 Aug 2024 23:51:13 +0300
Subject: [PATCH] [ValueTracking] Add CharWidth argument to
 getConstantStringInfo (NFC)

The method assumes that host chars and target chars have the same width.
Add a CharWidth argument so that it can bail out if the requested char
width differs from the host char width.

Alternatively, the check could be done at call sites, but this is more
error-prone.

In the future, this method will be replaced with a different one that
allows host/target chars to have different widths. The prototype will
be the same except that StringRef is replaced with something that is
byte width agnostic. Adding CharWidth argument now reduces the future
diff.
---
 clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp   |   4 +-
 llvm/include/llvm/Analysis/ValueTracking.h|   2 +-
 llvm/lib/Analysis/ValueTracking.cpp   |   7 +-
 .../AMDGPU/AMDGPUPrintfRuntimeBinding.cpp |   4 +-
 llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp |   2 +-
 .../Target/SPIRV/SPIRVPrepareFunctions.cpp|   2 +-
 .../WebAssembly/WebAssemblyAsmPrinter.cpp |   2 +-
 .../AggressiveInstCombine.cpp |  12 +-
 .../lib/Transforms/Utils/AMDGPUEmitPrintf.cpp |   4 +-
 .../lib/Transforms/Utils/SimplifyLibCalls.cpp | 103 --
 10 files changed, 96 insertions(+), 46 deletions(-)

diff --git a/clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp 
b/clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp
index ad012d98635ff..95c4ae0ac3583 100644
--- a/clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp
+++ b/clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp
@@ -205,7 +205,7 @@ void CodeGenFunction::ProcessOrderScopeAMDGCN(Value *Order, 
Value *Scope,
 
   // Some of the atomic builtins take the scope as a string name.
   StringRef scp;
-  if (llvm::getConstantStringInfo(Scope, scp)) {
+  if (llvm::getConstantStringInfo(Scope, scp, /*CharWidth=*/8)) {
 SSID = getLLVMContext().getOrInsertSyncScopeID(scp);
 return;
   }
@@ -260,7 +260,7 @@ void 
CodeGenFunction::AddAMDGPUFenceAddressSpaceMMRA(llvm::Instruction *Inst,
   for (unsigned K = 2; K < E->getNumArgs(); ++K) {
 llvm::Value *V = EmitScalarExpr(E->getArg(K));
 StringRef AS;
-if (llvm::getConstantStringInfo(V, AS)) {
+if (llvm::getConstantStringInfo(V, AS, /*CharWidth=*/8)) {
   MMRAs.push_back({Tag, AS});
   // TODO: Delete the resulting unused constant?
   continue;
diff --git a/llvm/include/llvm/Analysis/ValueTracking.h 
b/llvm/include/llvm/Analysis/ValueTracking.h
index f927838c843ac..51d0712a243fb 100644
--- a/llvm/include/llvm/Analysis/ValueTracking.h
+++ b/llvm/include/llvm/Analysis/ValueTracking.h
@@ -412,7 +412,7 @@ bool getConstantDataArrayInfo(const Value *V, 
ConstantDataArraySlice &Slice,
 /// character by default. If TrimAtNul is set to false, then this returns any
 /// trailing null characters as well as any other characters that come after
 /// it.
-bool getConstantStringInfo(const Value *V, StringRef &Str,
+bool getConstantStringInfo(const Value *V, StringRef &Str, unsigned CharWidth,
bool TrimAtNul = true);
 
 /// If we can compute the length of the string pointed to by the specified
diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index 2007a9599114c..472e44b0b8ea5 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -6737,9 +6737,12 @@ bool llvm::getConstantDataArrayInfo(const Value *V,
 /// return true.  When TrimAtNul is set, Str will contain only the bytes up
 /// to but not including the first nul.  Return false on failure.
 bool llvm::getConstantStringInfo(const Value *V, StringRef &Str,
- bool TrimAtNul) {
+ unsigned CharWidth, bool TrimAtNul) {
+  if (CharWidth != CHAR_BIT)
+return false;
+
   ConstantDataArraySlice Slice;
-  if (!getConstantDataArrayInfo(V, Slice, 8))
+  if (!getConstantDataArrayInfo(V, Slice, CharWidth))
 return false;
 
   if (Slice.Array == nullptr) {
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp 
b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
index 9847fbf108b0c..c569c160dce2c 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp
@@ -121,7 +121,7 @@ static_assert(NonLiteralStr.size() == 3);
 
 static StringRef getAsConstantStr(Value *V) {
   StringRef S;
-  if (!getConstantStringInfo(V, S))
+  if (!getConstantStringInfo(V, S, /*CharWidth=*/8))
 S = NonLiteralStr;
 
   return S;
@@ -155,7 +155,7 @@ bool 
AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
 Value *Op = CI->getArgOperand(0);
 
 StringRef FormatStr;
-if (!getConstantStringInfo(Op, FormatStr)) {
+if (!getConstantStringInfo(Op, FormatStr,

[llvm-branch-commits] [llvm] [IR] Account for byte width in m_PtrAdd (PR #106540)

2025-04-19 Thread Sergei Barannikov via llvm-branch-commits

https://github.com/s-barannikov updated 
https://github.com/llvm/llvm-project/pull/106540

>From 5604e7a7d06d4b367f765e15a929b9dd7760d6f8 Mon Sep 17 00:00:00 2001
From: Sergei Barannikov 
Date: Thu, 29 Aug 2024 00:54:20 +0300
Subject: [PATCH] [IR] Account for byte width in m_PtrAdd

The method has few uses yet, so just pass DL argument to it. The change
follows m_PtrToIntSameSize, and I don't see a better way of delivering
the byte width to the method.
---
 llvm/include/llvm/IR/PatternMatch.h   | 13 ++
 llvm/lib/Analysis/InstructionSimplify.cpp |  2 +-
 .../InstCombineSimplifyDemanded.cpp   |  7 ++---
 .../InstCombine/InstructionCombining.cpp  |  2 +-
 llvm/unittests/IR/PatternMatch.cpp| 26 ++-
 5 files changed, 34 insertions(+), 16 deletions(-)

diff --git a/llvm/include/llvm/IR/PatternMatch.h 
b/llvm/include/llvm/IR/PatternMatch.h
index 2d27c19e1b85e..0f5b75ddd95c1 100644
--- a/llvm/include/llvm/IR/PatternMatch.h
+++ b/llvm/include/llvm/IR/PatternMatch.h
@@ -1924,15 +1924,17 @@ struct m_SplatOrPoisonMask {
 };
 
 template  struct PtrAdd_match {
+  const DataLayout &DL;
   PointerOpTy PointerOp;
   OffsetOpTy OffsetOp;
 
-  PtrAdd_match(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp)
-  : PointerOp(PointerOp), OffsetOp(OffsetOp) {}
+  PtrAdd_match(const DataLayout &DL, const PointerOpTy &PointerOp,
+   const OffsetOpTy &OffsetOp)
+  : DL(DL), PointerOp(PointerOp), OffsetOp(OffsetOp) {}
 
   template  bool match(OpTy *V) {
 auto *GEP = dyn_cast(V);
-return GEP && GEP->getSourceElementType()->isIntegerTy(8) &&
+return GEP && GEP->getSourceElementType()->isIntegerTy(DL.getByteWidth()) 
&&
PointerOp.match(GEP->getPointerOperand()) &&
OffsetOp.match(GEP->idx_begin()->get());
   }
@@ -1974,8 +1976,9 @@ inline auto m_GEP(const OperandTypes &...Ops) {
 /// Matches GEP with i8 source element type
 template 
 inline PtrAdd_match
-m_PtrAdd(const PointerOpTy &PointerOp, const OffsetOpTy &OffsetOp) {
-  return PtrAdd_match(PointerOp, OffsetOp);
+m_PtrAdd(const DataLayout &DL, const PointerOpTy &PointerOp,
+ const OffsetOpTy &OffsetOp) {
+  return PtrAdd_match(DL, PointerOp, OffsetOp);
 }
 
 
//===--===//
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp 
b/llvm/lib/Analysis/InstructionSimplify.cpp
index 8ffb471070d91..08898f34ba5d0 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5397,7 +5397,7 @@ static Value *simplifyCastInst(unsigned CastOpc, Value 
*Op, Type *Ty,
   // ptrtoint (ptradd (Ptr, X - ptrtoint(Ptr))) -> X
   Value *Ptr, *X;
   if (CastOpc == Instruction::PtrToInt &&
-  match(Op, m_PtrAdd(m_Value(Ptr),
+  match(Op, m_PtrAdd(Q.DL, m_Value(Ptr),
  m_Sub(m_Value(X), m_PtrToInt(m_Deferred(Ptr) &&
   X->getType() == Ty && Ty == Q.DL.getIndexType(Ptr->getType()))
 return X;
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
index 2c8939b5a0514..a26f4db275637 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -990,9 +990,10 @@ Value 
*InstCombinerImpl::SimplifyDemandedUseBits(Instruction *I,
 Value *InnerPtr;
 uint64_t GEPIndex;
 uint64_t PtrMaskImmediate;
-if (match(I, m_Intrinsic(
- m_PtrAdd(m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
- m_ConstantInt(PtrMaskImmediate {
+if (match(I,
+  m_Intrinsic(
+  m_PtrAdd(DL, m_Value(InnerPtr), m_ConstantInt(GEPIndex)),
+  m_ConstantInt(PtrMaskImmediate {
 
   LHSKnown = computeKnownBits(InnerPtr, Depth + 1, I);
   if (!LHSKnown.isZero()) {
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp 
b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index f807f5f4519fc..fc2945d415722 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2419,7 +2419,7 @@ static Instruction 
*canonicalizeGEPOfConstGEPI8(GetElementPtrInst &GEP,
   auto &DL = IC.getDataLayout();
   Value *Base;
   const APInt *C1;
-  if (!match(Src, m_PtrAdd(m_Value(Base), m_APInt(C1
+  if (!match(Src, m_PtrAdd(DL, m_Value(Base), m_APInt(C1
 return nullptr;
   Value *VarIndex;
   const APInt *C2;
diff --git a/llvm/unittests/IR/PatternMatch.cpp 
b/llvm/unittests/IR/PatternMatch.cpp
index e7600209e0b14..ce2f9ae29c4bc 100644
--- a/llvm/unittests/IR/PatternMatch.cpp
+++ b/llvm/unittests/IR/PatternMatch.cpp
@@ -2618,26 +2618,40 @@ TEST_F(PatternMatchTest, ConstExpr) {
   EXPECT_TRUE(match(V, m_ConstantExpr()));
 }
 
-TEST_F(PatternMatchTest, 

[llvm-branch-commits] [libcxx] release/20.x: [libc++] Fix check for _LIBCPP_HAS_NO_WIDE_CHARACTERS in features.py (#131675) (PR #136449)

2025-04-19 Thread via llvm-branch-commits

https://github.com/llvmbot created 
https://github.com/llvm/llvm-project/pull/136449

Backport f909b2229ac16ae3898d8b158bee85c384173dfa 
297f6d9f6b215bd7f58cf500b979b94dedbba7bb

Requested by: @mstorsjo

>From 79e10b190029b749e042d1aaec3ee697a2f5d41a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= 
Date: Fri, 28 Feb 2025 20:43:46 -0100
Subject: [PATCH 1/2] [libcxx] Provide locale conversions to tests through lit
 substitution (#105651)

There are 2 problems today that this PR resolves:

libcxx tests assume the thousands separator for fr_FR locale is x00A0 on
Windows. This currently fails when run on newer versions of Windows (it
seems to have been updated to the new correct value of 0x202F around
windows 11. The exact windows version where it changed doesn't seem to
be documented anywhere). Depending the OS version, you need different
values.

There are several ifdefs to determine the environment/platform-specific
locale conversion values and it leads to maintenance as things change
over time.

This PR includes the following changes:

- Provide the environment's locale conversion values through a
  substitution. The test can opt in by placing the substitution value in a
  define flag.
- Remove the platform ifdefs (the swapping of values between Windows,
  Linux, Apple, AIX).

This is accomplished through a lit feature action that fetches the
environment's locale conversions (lconv) for members like
'thousands_sep' that we need to provide. This should ensure that we
don't lose the effectiveness of the test itself.

In addition, as a result of the above, this PR:

- Fixes a handful of locale tests which unexpectedly fail on newer
  Windows versions.
- Resolves 3 XFAIL FIX-MEs.

Originally submitted in https://github.com/llvm/llvm-project/pull/86649.

Co-authored-by: Rodrigo Salazar <4rodrigosala...@gmail.com>
(cherry picked from commit f909b2229ac16ae3898d8b158bee85c384173dfa)
---
 .../get_long_double_fr_FR.pass.cpp|  5 +-
 .../get_long_double_ru_RU.pass.cpp|  5 +-
 .../put_long_double_fr_FR.pass.cpp|  5 +-
 .../put_long_double_ru_RU.pass.cpp|  5 +-
 .../thousands_sep.pass.cpp| 34 ++-
 .../thousands_sep.pass.cpp| 20 ++--
 .../time.duration.nonmember/ostream.pass.cpp  | 24 ++---
 libcxx/test/support/locale_helpers.h  | 37 ++--
 libcxx/utils/libcxx/test/features.py  | 91 ++-
 9 files changed, 138 insertions(+), 88 deletions(-)

diff --git 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
index bbb67d694970a..f02241ad36a5b 100644
--- 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
+++ 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
@@ -13,6 +13,8 @@
 
 // REQUIRES: locale.fr_FR.UTF-8
 
+// ADDITIONAL_COMPILE_FLAGS: 
-DFR_MON_THOU_SEP=%{LOCALE_CONV_FR_FR_UTF_8_MON_THOUSANDS_SEP}
+
 // 
 
 // class money_get
@@ -59,7 +61,8 @@ class my_facetw
 };
 
 static std::wstring convert_thousands_sep(std::wstring const& in) {
-  return LocaleHelpers::convert_thousands_sep_fr_FR(in);
+  const wchar_t fr_sep = 
LocaleHelpers::mon_thousands_sep_or_default(FR_MON_THOU_SEP);
+  return LocaleHelpers::convert_thousands_sep(in, fr_sep);
 }
 #endif // TEST_HAS_NO_WIDE_CHARACTERS
 
diff --git 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
index e680f2ea8816a..371cf0e90c8d3 100644
--- 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
+++ 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
@@ -11,6 +11,8 @@
 
 // REQUIRES: locale.ru_RU.UTF-8
 
+// ADDITIONAL_COMPILE_FLAGS: 
-DRU_MON_THOU_SEP=%{LOCALE_CONV_RU_RU_UTF_8_MON_THOUSANDS_SEP}
+
 // XFAIL: glibc-old-ru_RU-decimal-point
 
 // 
@@ -52,7 +54,8 @@ class my_facetw
 };
 
 static std::wstring convert_thousands_sep(std::wstring const& in) {
-  return LocaleHelpers::convert_thousands_sep_ru_RU(in);
+  const wchar_t ru_sep = 
LocaleHelpers::mon_thousands_sep_or_default(RU_MON_THOU_SEP);
+  return LocaleHelpers::convert_thousands_sep(in, ru_sep);
 }
 #endif // TEST_HAS_NO_WIDE_CHARACTERS
 
diff --git 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_long_double_f

[llvm-branch-commits] [libcxx] release/20.x: [libc++] Fix check for _LIBCPP_HAS_NO_WIDE_CHARACTERS in features.py (#131675) (PR #136449)

2025-04-19 Thread via llvm-branch-commits

https://github.com/llvmbot milestoned 
https://github.com/llvm/llvm-project/pull/136449
___
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] [libcxx] release/20.x: [libc++] Fix check for _LIBCPP_HAS_NO_WIDE_CHARACTERS in features.py (#131675) (PR #136449)

2025-04-19 Thread via llvm-branch-commits

llvmbot wrote:

@ldionne @mstorsjo What do you think about merging this PR to the release 
branch?

https://github.com/llvm/llvm-project/pull/136449
___
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] [libcxx] release/20.x: [libc++] Fix check for _LIBCPP_HAS_NO_WIDE_CHARACTERS in features.py (#131675) (PR #136449)

2025-04-19 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-libcxx

Author: None (llvmbot)


Changes

Backport f909b2229ac16ae3898d8b158bee85c384173dfa 
297f6d9f6b215bd7f58cf500b979b94dedbba7bb

Requested by: @mstorsjo

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


9 Files Affected:

- (modified) 
libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
 (+4-1) 
- (modified) 
libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
 (+4-1) 
- (modified) 
libcxx/test/std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_long_double_fr_FR.pass.cpp
 (+4-1) 
- (modified) 
libcxx/test/std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_long_double_ru_RU.pass.cpp
 (+4-1) 
- (modified) 
libcxx/test/std/localization/locale.categories/category.monetary/locale.moneypunct.byname/thousands_sep.pass.cpp
 (+9-25) 
- (modified) 
libcxx/test/std/localization/locale.categories/facet.numpunct/locale.numpunct.byname/thousands_sep.pass.cpp
 (+8-12) 
- (modified) 
libcxx/test/std/time/time.duration/time.duration.nonmember/ostream.pass.cpp 
(+9-15) 
- (modified) libcxx/test/support/locale_helpers.h (+6-31) 
- (modified) libcxx/utils/libcxx/test/features.py (+91-1) 


``diff
diff --git 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
index bbb67d694970a..f02241ad36a5b 100644
--- 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
+++ 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_fr_FR.pass.cpp
@@ -13,6 +13,8 @@
 
 // REQUIRES: locale.fr_FR.UTF-8
 
+// ADDITIONAL_COMPILE_FLAGS: 
-DFR_MON_THOU_SEP=%{LOCALE_CONV_FR_FR_UTF_8_MON_THOUSANDS_SEP}
+
 // 
 
 // class money_get
@@ -59,7 +61,8 @@ class my_facetw
 };
 
 static std::wstring convert_thousands_sep(std::wstring const& in) {
-  return LocaleHelpers::convert_thousands_sep_fr_FR(in);
+  const wchar_t fr_sep = 
LocaleHelpers::mon_thousands_sep_or_default(FR_MON_THOU_SEP);
+  return LocaleHelpers::convert_thousands_sep(in, fr_sep);
 }
 #endif // TEST_HAS_NO_WIDE_CHARACTERS
 
diff --git 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
index e680f2ea8816a..371cf0e90c8d3 100644
--- 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
+++ 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.get/locale.money.get.members/get_long_double_ru_RU.pass.cpp
@@ -11,6 +11,8 @@
 
 // REQUIRES: locale.ru_RU.UTF-8
 
+// ADDITIONAL_COMPILE_FLAGS: 
-DRU_MON_THOU_SEP=%{LOCALE_CONV_RU_RU_UTF_8_MON_THOUSANDS_SEP}
+
 // XFAIL: glibc-old-ru_RU-decimal-point
 
 // 
@@ -52,7 +54,8 @@ class my_facetw
 };
 
 static std::wstring convert_thousands_sep(std::wstring const& in) {
-  return LocaleHelpers::convert_thousands_sep_ru_RU(in);
+  const wchar_t ru_sep = 
LocaleHelpers::mon_thousands_sep_or_default(RU_MON_THOU_SEP);
+  return LocaleHelpers::convert_thousands_sep(in, ru_sep);
 }
 #endif // TEST_HAS_NO_WIDE_CHARACTERS
 
diff --git 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_long_double_fr_FR.pass.cpp
 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_long_double_fr_FR.pass.cpp
index 47a48deb3368c..9ac95cc52ac07 100644
--- 
a/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_long_double_fr_FR.pass.cpp
+++ 
b/libcxx/test/std/localization/locale.categories/category.monetary/locale.money.put/locale.money.put.members/put_long_double_fr_FR.pass.cpp
@@ -13,6 +13,8 @@
 
 // REQUIRES: locale.fr_FR.UTF-8
 
+// ADDITIONAL_COMPILE_FLAGS: 
-DFR_MON_THOU_SEP=%{LOCALE_CONV_FR_FR_UTF_8_MON_THOUSANDS_SEP}
+
 // 
 
 // class money_put
@@ -59,7 +61,8 @@ class my_facetw
 };
 
 static std::wstring convert_thousands_sep(std::wstring const& in) {
-  return LocaleHelpers::convert_thousands_sep_fr_FR(in);
+  const wchar_t fr_sep = 
LocaleHelpers::mon_thousands_sep_or_default(FR_MON_THOU_SEP);
+  return LocaleHelpers::convert_thousands_sep(in, fr_sep);
 }
 #endif // TEST_HAS_NO_WIDE_CHARACTERS
 
diff --git 
a/libcxx/test/std/localization/loca

[llvm-branch-commits] [libcxx] release/20.x: [libcxx] Provide locale conversions to tests through lit substitution (#105651) (PR #136449)

2025-04-19 Thread Martin Storsjö via llvm-branch-commits

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