[llvm-branch-commits] [llvm] release/21.x: [AArch64][BTI] Add BTI at EH entries. (#155308) (PR #156170)

2025-08-30 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-backend-aarch64

Author: None (llvmbot)


Changes

Backport 1b37b9e6d788d7058381b68b5ab265bcb6181335

Requested by: @davemgreen

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


5 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64BranchTargets.cpp (+35-11) 
- (added) llvm/test/CodeGen/AArch64/bti-ehpad.ll (+44) 
- (modified) llvm/test/CodeGen/AArch64/sign-return-address-pauth-lr.ll (+8-8) 
- (added) llvm/test/CodeGen/AArch64/wineh-bti-funclet.ll (+79) 
- (modified) llvm/test/CodeGen/AArch64/wineh-bti.ll (+1-1) 


``diff
diff --git a/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp 
b/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp
index 3436dc9ef4521..137ff898e86a3 100644
--- a/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp
+++ b/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp
@@ -30,6 +30,14 @@ using namespace llvm;
 #define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets"
 
 namespace {
+// BTI HINT encoding: base (32) plus 'c' (2) and/or 'j' (4).
+enum : unsigned {
+  BTIBase = 32,   // Base immediate for BTI HINT
+  BTIC = 1u << 1, // 2
+  BTIJ = 1u << 2, // 4
+  BTIMask = BTIC | BTIJ,
+};
+
 class AArch64BranchTargets : public MachineFunctionPass {
 public:
   static char ID;
@@ -42,6 +50,7 @@ class AArch64BranchTargets : public MachineFunctionPass {
   void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump,
   bool NeedsWinCFI);
 };
+
 } // end anonymous namespace
 
 char AArch64BranchTargets::ID = 0;
@@ -62,9 +71,8 @@ bool 
AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
   if (!MF.getInfo()->branchTargetEnforcement())
 return false;
 
-  LLVM_DEBUG(
-  dbgs() << "** AArch64 Branch Targets  **\n"
- << "** Function: " << MF.getName() << '\n');
+  LLVM_DEBUG(dbgs() << "** AArch64 Branch Targets  **\n"
+<< "** Function: " << MF.getName() << '\n');
   const Function &F = MF.getFunction();
 
   // LLVM does not consider basic blocks which are the targets of jump tables
@@ -103,6 +111,12 @@ bool 
AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
 JumpTableTargets.count(&MBB))
   CouldJump = true;
 
+if (MBB.isEHPad()) {
+  if (HasWinCFI && (MBB.isEHFuncletEntry() || MBB.isCleanupFuncletEntry()))
+CouldCall = true;
+  else
+CouldJump = true;
+}
 if (CouldCall || CouldJump) {
   addBTI(MBB, CouldCall, CouldJump, HasWinCFI);
   MadeChange = true;
@@ -130,7 +144,12 @@ void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, 
bool CouldCall,
 
   auto MBBI = MBB.begin();
 
-  // Skip the meta instructions, those will be removed anyway.
+  // If the block starts with EH_LABEL(s), skip them first.
+  while (MBBI != MBB.end() && MBBI->isEHLabel()) {
+++MBBI;
+  }
+
+  // Skip meta/CFI/etc. (and EMITBKEY) to reach the first executable insn.
   for (; MBBI != MBB.end() &&
  (MBBI->isMetaInstruction() || MBBI->getOpcode() == AArch64::EMITBKEY);
++MBBI)
@@ -138,16 +157,21 @@ void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, 
bool CouldCall,
 
   // SCTLR_EL1.BT[01] is set to 0 by default which means
   // PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there.
-  if (MBBI != MBB.end() && HintNum == 34 &&
+  if (MBBI != MBB.end() && ((HintNum & BTIMask) == BTIC) &&
   (MBBI->getOpcode() == AArch64::PACIASP ||
MBBI->getOpcode() == AArch64::PACIBSP))
 return;
 
-  if (HasWinCFI && MBBI->getFlag(MachineInstr::FrameSetup)) {
-BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
-TII->get(AArch64::SEH_Nop));
+  // Insert BTI exactly at the first executable instruction.
+  const DebugLoc DL = MBB.findDebugLoc(MBBI);
+  MachineInstr *BTI = BuildMI(MBB, MBBI, DL, TII->get(AArch64::HINT))
+  .addImm(HintNum)
+  .getInstr();
+
+  // WinEH: put .seh_nop after BTI when the first real insn is FrameSetup.
+  if (HasWinCFI && MBBI != MBB.end() &&
+  MBBI->getFlag(MachineInstr::FrameSetup)) {
+auto AfterBTI = std::next(MachineBasicBlock::iterator(BTI));
+BuildMI(MBB, AfterBTI, DL, TII->get(AArch64::SEH_Nop));
   }
-  BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
-  TII->get(AArch64::HINT))
-  .addImm(HintNum);
 }
diff --git a/llvm/test/CodeGen/AArch64/bti-ehpad.ll 
b/llvm/test/CodeGen/AArch64/bti-ehpad.ll
new file mode 100644
index 0..674421adaf51c
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/bti-ehpad.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py 
UTC_ARGS: --version 5
+; RUN: llc -mtriple=aarch64-unknown-linux-gnu %s -o - | FileCheck %s
+
+; Purpose: With BTI enabled, the landing pad (%lpad) begins with an EH_LABEL 
and the
+; first *executed* instruction is `bti j`. (BTI is inserted *after* the EH 
label and meta.)
+

[llvm-branch-commits] [llvm] release/21.x: [AArch64][BTI] Add BTI at EH entries. (#155308) (PR #156170)

2025-08-30 Thread via llvm-branch-commits

https://github.com/llvmbot milestoned 
https://github.com/llvm/llvm-project/pull/156170
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] release/21.x: [AArch64][BTI] Add BTI at EH entries. (#155308) (PR #156170)

2025-08-30 Thread via llvm-branch-commits

llvmbot wrote:

@efriedma-quic What do you think about merging this PR to the release branch?

https://github.com/llvm/llvm-project/pull/156170
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] 1e40681 - Revert "[msan] Detect dereferencing zero-alloc as use-of-uninitialized-value …"

2025-08-30 Thread via llvm-branch-commits

Author: Thurston Dang
Date: 2025-08-30T00:03:56-07:00
New Revision: 1e406814dd21aad7da6bd1c1a8e0c6f1c1cd5369

URL: 
https://github.com/llvm/llvm-project/commit/1e406814dd21aad7da6bd1c1a8e0c6f1c1cd5369
DIFF: 
https://github.com/llvm/llvm-project/commit/1e406814dd21aad7da6bd1c1a8e0c6f1c1cd5369.diff

LOG: Revert "[msan] Detect dereferencing zero-alloc as 
use-of-uninitialized-value …"

This reverts commit 8784dcef3485a3862e5cacc9e18c646d0225483b.

Added: 


Modified: 
compiler-rt/lib/msan/msan_allocator.cpp
compiler-rt/test/msan/zero_alloc.cpp

Removed: 




diff  --git a/compiler-rt/lib/msan/msan_allocator.cpp 
b/compiler-rt/lib/msan/msan_allocator.cpp
index 64df863839c06..2b543db49d36e 100644
--- a/compiler-rt/lib/msan/msan_allocator.cpp
+++ b/compiler-rt/lib/msan/msan_allocator.cpp
@@ -230,12 +230,6 @@ static void *MsanAllocate(BufferedStackTrace *stack, uptr 
size, uptr alignment,
   __msan_set_origin(allocated, size, o.raw_id());
 }
   }
-
-  uptr actually_allocated_size = allocator.GetActuallyAllocatedSize(allocated);
-  // For compatibility, the allocator converted 0-sized allocations into 1 byte
-  if (size == 0 && actually_allocated_size > 0 && flags()->poison_in_malloc)
-__msan_poison(allocated, 1);
-
   UnpoisonParam(2);
   RunMallocHooks(allocated, size);
   return allocated;

diff  --git a/compiler-rt/test/msan/zero_alloc.cpp 
b/compiler-rt/test/msan/zero_alloc.cpp
index 6e38ce4c0a8f8..e60051872eba2 100644
--- a/compiler-rt/test/msan/zero_alloc.cpp
+++ b/compiler-rt/test/msan/zero_alloc.cpp
@@ -1,5 +1,9 @@
 // RUN: %clang_msan -Wno-alloc-size -fsanitize-recover=memory %s -o %t && not 
%run %t 2>&1 | FileCheck %s
 
+// MSan doesn't catch this because internally it translates 0-byte allocations
+// into 1-byte
+// XFAIL: *
+
 #include 
 #include 
 



___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] 2fb8843 - Revert "Emit DW_OP_lit0/1 for constant boolean values (#155539)"

2025-08-30 Thread via llvm-branch-commits

Author: Michael Buch
Date: 2025-08-30T12:03:31+01:00
New Revision: 2fb8843a744113d45f5aaf13984d54a3d7ec620a

URL: 
https://github.com/llvm/llvm-project/commit/2fb8843a744113d45f5aaf13984d54a3d7ec620a
DIFF: 
https://github.com/llvm/llvm-project/commit/2fb8843a744113d45f5aaf13984d54a3d7ec620a.diff

LOG: Revert "Emit DW_OP_lit0/1 for constant boolean values (#155539)"

This reverts commit 3aae4bd13dc4dd2e983b1fa11e1b8c0bf153ed14.

Added: 


Modified: 
llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h

Removed: 
llvm/test/DebugInfo/debug-bool-const-location.ll



diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp 
b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
index 2090157a1a91c..c27f100775625 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
@@ -3111,10 +3111,8 @@ void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, 
const DIBasicType *BT,
 &AP](const DbgValueLocEntry &Entry,
  DIExpressionCursor &Cursor) -> bool {
 if (Entry.isInt()) {
-  if (BT && (BT->getEncoding() == dwarf::DW_ATE_boolean))
-DwarfExpr.addBooleanConstant(Entry.getInt());
-  else if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
-  BT->getEncoding() == dwarf::DW_ATE_signed_char))
+  if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
+ BT->getEncoding() == dwarf::DW_ATE_signed_char))
 DwarfExpr.addSignedConstant(Entry.getInt());
   else
 DwarfExpr.addUnsignedConstant(Entry.getInt());

diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp 
b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
index 8a30714db2fdf..e684054ffa3e4 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
@@ -194,15 +194,6 @@ void DwarfExpression::addStackValue() {
 emitOp(dwarf::DW_OP_stack_value);
 }
 
-void DwarfExpression::addBooleanConstant(int64_t Value) {
-  assert(isImplicitLocation() || isUnknownLocation());
-  LocationKind = Implicit;
-  if (Value == 0)
-emitOp(dwarf::DW_OP_lit0);
-  else
-emitOp(dwarf::DW_OP_lit1);
-}
-
 void DwarfExpression::addSignedConstant(int64_t Value) {
   assert(isImplicitLocation() || isUnknownLocation());
   LocationKind = Implicit;

diff  --git a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h 
b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
index 700e0ec5813ee..06809ab263875 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h
@@ -229,9 +229,6 @@ class DwarfExpression {
   /// This needs to be called last to commit any pending changes.
   void finalize();
 
-  /// Emit a boolean constant.
-  void addBooleanConstant(int64_t Value);
-
   /// Emit a signed constant.
   void addSignedConstant(int64_t Value);
 

diff  --git a/llvm/test/DebugInfo/debug-bool-const-location.ll 
b/llvm/test/DebugInfo/debug-bool-const-location.ll
deleted file mode 100644
index a3e3529d9b5e5..0
--- a/llvm/test/DebugInfo/debug-bool-const-location.ll
+++ /dev/null
@@ -1,48 +0,0 @@
-; REQUIRES: object-emission
-; RUN: %llc_dwarf %s -O3 -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
-
-; CHECK: {{.*}}DW_TAG_variable
-; CHECK: {{.*}} DW_OP_lit1
-; CHECK-NOT: {{.*}} DW_OP_lit0, DW_OP_not
-; CHECK: {{.*}} DW_OP_lit0
-; CHECK: {{.*}} DW_AT_name("arg")
-
-define void @foo(i8 %"arg.arg") !dbg !5
-{
-entry:
-  %".4" = alloca i1
-  %".5" = icmp eq i8 %"arg.arg", 0
-  %arg = alloca i1
-  br i1 %".5", label %"entry.if", label %"entry.else"
-entry.if:
-  store i1 false, i1* %arg
-  call void @"llvm.dbg.value"(metadata i1 false , metadata !9, metadata !10), 
!dbg !6
-  br label %"entry.endif"
-entry.else:
-  store i1 true, i1* %arg
-  call void @"llvm.dbg.value"(metadata i1 true , metadata !9, metadata !10), 
!dbg !7
-  br label %"entry.endif"
-entry.endif:
-  %".11" = load i1, i1* %arg
-  store i1 %".11", i1* %".4", !dbg !8
-  call void @"llvm.dbg.value"(metadata i1 %".11" , metadata !9, metadata !10), 
!dbg !8
-  ret void, !dbg !8
-}
-
-declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
-
-!llvm.dbg.cu = !{ !2 }
-!llvm.module.flags = !{ !11, !12 }
-
-!1 = !DIFile(directory: "", filename: "test")
-!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: 
false, language: DW_LANG_C_plus_plus, runtimeVersion: 0)
-!3 = !DIBasicType(encoding: DW_ATE_boolean, name: "bool", size: 8)
-!4 = !DISubroutineType(types: !{null})
-!5 = distinct !DISubprogram(file: !1, isDefinition: true, isLocal: false, 
isOptimized: false, line: 5, linkageName: "foo", name: "foo", scope: !1, 
scopeLine: 5, type: !4, unit: !2)
-!6 = !DILocation(column: 1, line: 5, scope: !5)
-!7 = !DILocation(column: 1, lin

[llvm-branch-commits] [flang] [flang] Do not use dialect conversion in `AffineDialectPromotion` (PR #156171)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/156171
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [flang] [flang] Do not use dialect conversion in `AffineDialectPromotion` (PR #156171)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/156171
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [flang] [flang] Do not use dialect conversion in `AffineDialectPromotion` (PR #156171)

2025-08-30 Thread Valentin Clement バレンタイン クレメン via llvm-branch-commits

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

LGTM

https://github.com/llvm/llvm-project/pull/156171
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/155244
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer updated 
https://github.com/llvm/llvm-project/pull/155244

>From 196203361a0b07b71f6eb0d4ac0b68f905d496ac Mon Sep 17 00:00:00 2001
From: Matthias Springer 
Date: Sat, 23 Aug 2025 10:36:37 +
Subject: [PATCH] [mlir][Transforms] Add support for
 `ConversionPatternRewriter::replaceAllUsesWith`

---
 mlir/include/mlir/IR/PatternMatch.h   |   2 +-
 .../mlir/Transforms/DialectConversion.h   |  27 ++-
 mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp |   2 +-
 .../Transforms/Utils/DialectConversion.cpp| 158 +++---
 .../Transforms/test-legalizer-rollback.mlir   |   8 +-
 mlir/test/Transforms/test-legalizer.mlir  |  26 ++-
 mlir/test/lib/Dialect/Test/TestPatterns.cpp   |  24 +--
 7 files changed, 158 insertions(+), 89 deletions(-)

diff --git a/mlir/include/mlir/IR/PatternMatch.h 
b/mlir/include/mlir/IR/PatternMatch.h
index 57e73c1d8c7c1..7b0b9cef9c5bd 100644
--- a/mlir/include/mlir/IR/PatternMatch.h
+++ b/mlir/include/mlir/IR/PatternMatch.h
@@ -633,7 +633,7 @@ class RewriterBase : public OpBuilder {
 
   /// Find uses of `from` and replace them with `to`. Also notify the listener
   /// about every in-place op modification (for every use that was replaced).
-  void replaceAllUsesWith(Value from, Value to) {
+  virtual void replaceAllUsesWith(Value from, Value to) {
 for (OpOperand &operand : llvm::make_early_inc_range(from.getUses())) {
   Operation *op = operand.getOwner();
   modifyOpInPlace(op, [&]() { operand.set(to); });
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 14dfbf18836c6..fe48e45a9b98c 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -854,15 +854,26 @@ class ConversionPatternRewriter final : public 
PatternRewriter {
   Region *region, const TypeConverter &converter,
   TypeConverter::SignatureConversion *entryConversion = nullptr);
 
-  /// Replace all the uses of the block argument `from` with `to`. This
-  /// function supports both 1:1 and 1:N replacements.
+  /// Replace all the uses of `from` with `to`. The type of `from` and `to` is
+  /// allowed to differ. The conversion driver will try to reconcile all type
+  /// mismatches that still exist at the end of the conversion with
+  /// materializations. This function supports both 1:1 and 1:N replacements.
   ///
-  /// Note: If `allowPatternRollback` is set to "true", this function replaces
-  /// all current and future uses of the block argument. This same block
-  /// block argument must not be replaced multiple times. Uses are not replaced
-  /// immediately but in a delayed fashion. Patterns may still see the original
-  /// uses when inspecting IR.
-  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to);
+  /// Note: If `allowPatternRollback` is set to "true", this function behaves
+  /// slightly different:
+  ///
+  /// 1. All current and future uses of `from` are replaced. The same value 
must
+  ///not be replaced multiple times. That's an API violation.
+  /// 2. Uses are not replaced immediately but in a delayed fashion. Patterns
+  ///may still see the original uses when inspecting IR.
+  /// 3. Uses within the same block that appear before the defining operation
+  ///of the replacement value are not replaced. This allows users to
+  ///perform certain replaceAllUsesExcept-style replacements, even though
+  ///such API is not directly supported.
+  void replaceAllUsesWith(Value from, ValueRange to);
+  void replaceAllUsesWith(Value from, Value to) override {
+replaceAllUsesWith(from, ValueRange{to});
+  }
 
   /// Return the converted value of 'key' with a type defined by the type
   /// converter of the currently executing pattern. Return nullptr in the case
diff --git a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp 
b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
index 42c76ed475b4c..93fe2edad5274 100644
--- a/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
+++ b/mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp
@@ -284,7 +284,7 @@ static void restoreByValRefArgumentType(
 cast(byValRefAttr->getValue()).getValue());
 
 Value valueArg = LLVM::LoadOp::create(rewriter, arg.getLoc(), resTy, arg);
-rewriter.replaceUsesOfBlockArgument(arg, valueArg);
+rewriter.replaceAllUsesWith(arg, valueArg);
   }
 }
 
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp 
b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index 5ba109d96cf13..d72429298754f 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -277,13 +277,14 @@ class IRRewrite {
 InlineBlock,
 MoveBlock,
 BlockTypeConversion,
-ReplaceBlockArg,
 // Operation rewrites
 MoveOperation,
 ModifyOperation,
 ReplaceOperation,
 CreateOperation,
-UnresolvedMaterialization
+UnresolvedMaterialization

[llvm-branch-commits] [llvm] release/21.x: [AArch64][BTI] Add BTI at EH entries. (#155308) (PR #156170)

2025-08-30 Thread via llvm-branch-commits

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

Backport 1b37b9e6d788d7058381b68b5ab265bcb6181335

Requested by: @davemgreen

>From 51081359fa4a76c9d20489084ce2a399b5bfc520 Mon Sep 17 00:00:00 2001
From: Shashi Shankar 
Date: Sat, 30 Aug 2025 11:56:03 +0200
Subject: [PATCH] [AArch64][BTI] Add BTI at EH entries. (#155308)

Mark EH landing pads as indirect-branch targets (BTI j) and treat WinEH
funclet entries as call-like (BTI c). Add lit tests for ELF and COFF.
Tests:
Adds lit tests: bti-ehpad.ll and wineh-bti-funclet.ll.

Fixes: #149267

Signed-off-by: Shashi Shankar 
(cherry picked from commit 1b37b9e6d788d7058381b68b5ab265bcb6181335)
---
 .../Target/AArch64/AArch64BranchTargets.cpp   | 46 ---
 llvm/test/CodeGen/AArch64/bti-ehpad.ll| 44 +++
 .../AArch64/sign-return-address-pauth-lr.ll   | 16 ++--
 .../test/CodeGen/AArch64/wineh-bti-funclet.ll | 79 +++
 llvm/test/CodeGen/AArch64/wineh-bti.ll|  2 +-
 5 files changed, 167 insertions(+), 20 deletions(-)
 create mode 100644 llvm/test/CodeGen/AArch64/bti-ehpad.ll
 create mode 100644 llvm/test/CodeGen/AArch64/wineh-bti-funclet.ll

diff --git a/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp 
b/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp
index 3436dc9ef4521..137ff898e86a3 100644
--- a/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp
+++ b/llvm/lib/Target/AArch64/AArch64BranchTargets.cpp
@@ -30,6 +30,14 @@ using namespace llvm;
 #define AARCH64_BRANCH_TARGETS_NAME "AArch64 Branch Targets"
 
 namespace {
+// BTI HINT encoding: base (32) plus 'c' (2) and/or 'j' (4).
+enum : unsigned {
+  BTIBase = 32,   // Base immediate for BTI HINT
+  BTIC = 1u << 1, // 2
+  BTIJ = 1u << 2, // 4
+  BTIMask = BTIC | BTIJ,
+};
+
 class AArch64BranchTargets : public MachineFunctionPass {
 public:
   static char ID;
@@ -42,6 +50,7 @@ class AArch64BranchTargets : public MachineFunctionPass {
   void addBTI(MachineBasicBlock &MBB, bool CouldCall, bool CouldJump,
   bool NeedsWinCFI);
 };
+
 } // end anonymous namespace
 
 char AArch64BranchTargets::ID = 0;
@@ -62,9 +71,8 @@ bool 
AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
   if (!MF.getInfo()->branchTargetEnforcement())
 return false;
 
-  LLVM_DEBUG(
-  dbgs() << "** AArch64 Branch Targets  **\n"
- << "** Function: " << MF.getName() << '\n');
+  LLVM_DEBUG(dbgs() << "** AArch64 Branch Targets  **\n"
+<< "** Function: " << MF.getName() << '\n');
   const Function &F = MF.getFunction();
 
   // LLVM does not consider basic blocks which are the targets of jump tables
@@ -103,6 +111,12 @@ bool 
AArch64BranchTargets::runOnMachineFunction(MachineFunction &MF) {
 JumpTableTargets.count(&MBB))
   CouldJump = true;
 
+if (MBB.isEHPad()) {
+  if (HasWinCFI && (MBB.isEHFuncletEntry() || MBB.isCleanupFuncletEntry()))
+CouldCall = true;
+  else
+CouldJump = true;
+}
 if (CouldCall || CouldJump) {
   addBTI(MBB, CouldCall, CouldJump, HasWinCFI);
   MadeChange = true;
@@ -130,7 +144,12 @@ void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, 
bool CouldCall,
 
   auto MBBI = MBB.begin();
 
-  // Skip the meta instructions, those will be removed anyway.
+  // If the block starts with EH_LABEL(s), skip them first.
+  while (MBBI != MBB.end() && MBBI->isEHLabel()) {
+++MBBI;
+  }
+
+  // Skip meta/CFI/etc. (and EMITBKEY) to reach the first executable insn.
   for (; MBBI != MBB.end() &&
  (MBBI->isMetaInstruction() || MBBI->getOpcode() == AArch64::EMITBKEY);
++MBBI)
@@ -138,16 +157,21 @@ void AArch64BranchTargets::addBTI(MachineBasicBlock &MBB, 
bool CouldCall,
 
   // SCTLR_EL1.BT[01] is set to 0 by default which means
   // PACI[AB]SP are implicitly BTI C so no BTI C instruction is needed there.
-  if (MBBI != MBB.end() && HintNum == 34 &&
+  if (MBBI != MBB.end() && ((HintNum & BTIMask) == BTIC) &&
   (MBBI->getOpcode() == AArch64::PACIASP ||
MBBI->getOpcode() == AArch64::PACIBSP))
 return;
 
-  if (HasWinCFI && MBBI->getFlag(MachineInstr::FrameSetup)) {
-BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
-TII->get(AArch64::SEH_Nop));
+  // Insert BTI exactly at the first executable instruction.
+  const DebugLoc DL = MBB.findDebugLoc(MBBI);
+  MachineInstr *BTI = BuildMI(MBB, MBBI, DL, TII->get(AArch64::HINT))
+  .addImm(HintNum)
+  .getInstr();
+
+  // WinEH: put .seh_nop after BTI when the first real insn is FrameSetup.
+  if (HasWinCFI && MBBI != MBB.end() &&
+  MBBI->getFlag(MachineInstr::FrameSetup)) {
+auto AfterBTI = std::next(MachineBasicBlock::iterator(BTI));
+BuildMI(MBB, AfterBTI, DL, TII->get(AArch64::SEH_Nop));
   }
-  BuildMI(MBB, MBB.begin(), MBB.findDebugLoc(MBB.begin()),
-  TII->get(AArch64::HINT))
-  .addImm(HintNum);
 }
diff --git 

[llvm-branch-commits] [flang] [flang] Do not use dialect conversion in `AffineDialectPromotion` (PR #156171)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer updated 
https://github.com/llvm/llvm-project/pull/156171

>From fee875b7f60d59174e0913bc64c37450cf846da1 Mon Sep 17 00:00:00 2001
From: Matthias Springer 
Date: Sat, 30 Aug 2025 10:35:08 +
Subject: [PATCH] [flang] Do not use dialect conversion in
 `AffineDialectPromotion`

---
 .../Optimizer/Transforms/AffinePromotion.cpp  | 33 +++
 1 file changed, 11 insertions(+), 22 deletions(-)

diff --git a/flang/lib/Optimizer/Transforms/AffinePromotion.cpp 
b/flang/lib/Optimizer/Transforms/AffinePromotion.cpp
index b032767eef6f0..061a7d201edd3 100644
--- a/flang/lib/Optimizer/Transforms/AffinePromotion.cpp
+++ b/flang/lib/Optimizer/Transforms/AffinePromotion.cpp
@@ -25,7 +25,7 @@
 #include "mlir/IR/BuiltinAttributes.h"
 #include "mlir/IR/IntegerSet.h"
 #include "mlir/IR/Visitors.h"
-#include "mlir/Transforms/DialectConversion.h"
+#include "mlir/Transforms/WalkPatternRewriteDriver.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/Support/Debug.h"
 #include 
@@ -451,10 +451,10 @@ static void rewriteStore(fir::StoreOp storeOp,
 }
 
 static void rewriteMemoryOps(Block *block, mlir::PatternRewriter &rewriter) {
-  for (auto &bodyOp : block->getOperations()) {
+  for (auto &bodyOp : llvm::make_early_inc_range(block->getOperations())) {
 if (isa(bodyOp))
   rewriteLoad(cast(bodyOp), rewriter);
-if (isa(bodyOp))
+else if (isa(bodyOp))
   rewriteStore(cast(bodyOp), rewriter);
   }
 }
@@ -476,6 +476,8 @@ class AffineLoopConversion : public 
mlir::OpRewritePattern {
loop.dump(););
 LLVM_ATTRIBUTE_UNUSED auto loopAnalysis =
 functionAnalysis.getChildLoopAnalysis(loop);
+if (!loopAnalysis.canPromoteToAffine())
+  return rewriter.notifyMatchFailure(loop, "cannot promote to affine");
 auto &loopOps = loop.getBody()->getOperations();
 auto resultOp = cast(loop.getBody()->getTerminator());
 auto results = resultOp.getOperands();
@@ -576,12 +578,14 @@ class AffineIfConversion : public 
mlir::OpRewritePattern {
 public:
   using OpRewritePattern::OpRewritePattern;
   AffineIfConversion(mlir::MLIRContext *context, AffineFunctionAnalysis &afa)
-  : OpRewritePattern(context) {}
+  : OpRewritePattern(context), functionAnalysis(afa) {}
   llvm::LogicalResult
   matchAndRewrite(fir::IfOp op,
   mlir::PatternRewriter &rewriter) const override {
 LLVM_DEBUG(llvm::dbgs() << "AffineIfConversion: rewriting if:\n";
op.dump(););
+if (!functionAnalysis.getChildIfAnalysis(op).canPromoteToAffine())
+  return rewriter.notifyMatchFailure(op, "cannot promote to affine");
 auto &ifOps = op.getThenRegion().front().getOperations();
 auto affineCondition = AffineIfCondition(op.getCondition());
 if (!affineCondition.hasIntegerSet()) {
@@ -611,6 +615,8 @@ class AffineIfConversion : public 
mlir::OpRewritePattern {
 rewriter.replaceOp(op, affineIf.getOperation()->getResults());
 return success();
   }
+
+  AffineFunctionAnalysis &functionAnalysis;
 };
 
 /// Promote fir.do_loop and fir.if to affine.for and affine.if, in the cases
@@ -627,28 +633,11 @@ class AffineDialectPromotion
 mlir::RewritePatternSet patterns(context);
 patterns.insert(context, functionAnalysis);
 patterns.insert(context, functionAnalysis);
-mlir::ConversionTarget target = *context;
-target.addLegalDialect();
-target.addDynamicallyLegalOp([&functionAnalysis](fir::IfOp op) {
-  return !(functionAnalysis.getChildIfAnalysis(op).canPromoteToAffine());
-});
-target.addDynamicallyLegalOp([&functionAnalysis](
-   fir::DoLoopOp op) {
-  return !(functionAnalysis.getChildLoopAnalysis(op).canPromoteToAffine());
-});
-
 LLVM_DEBUG(llvm::dbgs()
<< "AffineDialectPromotion: running promotion on: \n";
function.print(llvm::dbgs()););
 // apply the patterns
-if (mlir::failed(mlir::applyPartialConversion(function, target,
-  std::move(patterns {
-  mlir::emitError(mlir::UnknownLoc::get(context),
-  "error in converting to affine dialect\n");
-  signalPassFailure();
-}
+walkAndApplyPatterns(function, std::move(patterns));
   }
 };
 } // namespace

___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/155244
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/155244
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/155244
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir-core

Author: Matthias Springer (matthias-springer)


Changes

This commit generalizes `replaceUsesOfBlockArgument` to `replaceAllUsesWith`. 
In rollback mode, the same restrictions keep applying: a value cannot be 
replaced multiple times and a call to `replaceAllUsesWith` will replace all 
current and future uses of the `from` value.

`replaceAllUsesWith` is now fully supported and its behavior is consistent with 
the remaining dialect conversion API. Before this commit, `replaceAllUsesWith` 
was immediately reflected in the IR when running in rollback mode. After this 
commit, `replaceAllUsesWith` changes are materialized in a delayed fashion, at 
the end of the dialect conversion. This is consistent with the 
`replaceUsesOfBlockArgument` and `replaceOp` APIs.

`replaceAllUsesExcept` etc. are still not supported and will be deactivated on 
the `ConversionPatternRewriter` (when running in rollback mode) in a follow-up 
commit.

Note for LLVM integration: Replace `replaceUsesOfBlockArgument` with 
`replaceAllUsesWith`. If you are seeing failures, you may have patterns that 
use `replaceAllUsesWith` incorrectly (e.g., being called multiple times on the 
same value) or bypass the rewriter API entirely. E.g., such failures were 
mitigated in Flang by switching to the walk-patterns driver (#156171).

You can temporarily reactivate the old behavior by calling 
`RewriterBase::replaceAllUsesWith`. However, note that that behavior is faulty 
in a dialect conversion. E.g., the base `RewriterBase::replaceAllUsesWith` 
implementation does not see uses of the `from` value that have not materialized 
yet and will, therefore, not replace them.


---

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


7 Files Affected:

- (modified) mlir/include/mlir/IR/PatternMatch.h (+1-1) 
- (modified) mlir/include/mlir/Transforms/DialectConversion.h (+19-8) 
- (modified) mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp (+1-1) 
- (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+98-60) 
- (modified) mlir/test/Transforms/test-legalizer-rollback.mlir (+5-3) 
- (modified) mlir/test/Transforms/test-legalizer.mlir (+22-4) 
- (modified) mlir/test/lib/Dialect/Test/TestPatterns.cpp (+12-12) 


``diff
diff --git a/mlir/include/mlir/IR/PatternMatch.h 
b/mlir/include/mlir/IR/PatternMatch.h
index 57e73c1d8c7c1..7b0b9cef9c5bd 100644
--- a/mlir/include/mlir/IR/PatternMatch.h
+++ b/mlir/include/mlir/IR/PatternMatch.h
@@ -633,7 +633,7 @@ class RewriterBase : public OpBuilder {
 
   /// Find uses of `from` and replace them with `to`. Also notify the listener
   /// about every in-place op modification (for every use that was replaced).
-  void replaceAllUsesWith(Value from, Value to) {
+  virtual void replaceAllUsesWith(Value from, Value to) {
 for (OpOperand &operand : llvm::make_early_inc_range(from.getUses())) {
   Operation *op = operand.getOwner();
   modifyOpInPlace(op, [&]() { operand.set(to); });
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 14dfbf18836c6..fe48e45a9b98c 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -854,15 +854,26 @@ class ConversionPatternRewriter final : public 
PatternRewriter {
   Region *region, const TypeConverter &converter,
   TypeConverter::SignatureConversion *entryConversion = nullptr);
 
-  /// Replace all the uses of the block argument `from` with `to`. This
-  /// function supports both 1:1 and 1:N replacements.
+  /// Replace all the uses of `from` with `to`. The type of `from` and `to` is
+  /// allowed to differ. The conversion driver will try to reconcile all type
+  /// mismatches that still exist at the end of the conversion with
+  /// materializations. This function supports both 1:1 and 1:N replacements.
   ///
-  /// Note: If `allowPatternRollback` is set to "true", this function replaces
-  /// all current and future uses of the block argument. This same block
-  /// block argument must not be replaced multiple times. Uses are not replaced
-  /// immediately but in a delayed fashion. Patterns may still see the original
-  /// uses when inspecting IR.
-  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to);
+  /// Note: If `allowPatternRollback` is set to "true", this function behaves
+  /// slightly different:
+  ///
+  /// 1. All current and future uses of `from` are replaced. The same value 
must
+  ///not be replaced multiple times. That's an API violation.
+  /// 2. Uses are not replaced immediately but in a delayed fashion. Patterns
+  ///may still see the original uses when inspecting IR.
+  /// 3. Uses within the same block that appear before the defining operation
+  ///of the replacement value are not replaced. This allows users to
+  ///perform certain replaceAllUsesE

[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir

Author: Matthias Springer (matthias-springer)


Changes

This commit generalizes `replaceUsesOfBlockArgument` to `replaceAllUsesWith`. 
In rollback mode, the same restrictions keep applying: a value cannot be 
replaced multiple times and a call to `replaceAllUsesWith` will replace all 
current and future uses of the `from` value.

`replaceAllUsesWith` is now fully supported and its behavior is consistent with 
the remaining dialect conversion API. Before this commit, `replaceAllUsesWith` 
was immediately reflected in the IR when running in rollback mode. After this 
commit, `replaceAllUsesWith` changes are materialized in a delayed fashion, at 
the end of the dialect conversion. This is consistent with the 
`replaceUsesOfBlockArgument` and `replaceOp` APIs.

`replaceAllUsesExcept` etc. are still not supported and will be deactivated on 
the `ConversionPatternRewriter` (when running in rollback mode) in a follow-up 
commit.

Note for LLVM integration: Replace `replaceUsesOfBlockArgument` with 
`replaceAllUsesWith`. If you are seeing failures, you may have patterns that 
use `replaceAllUsesWith` incorrectly (e.g., being called multiple times on the 
same value) or bypass the rewriter API entirely. E.g., such failures were 
mitigated in Flang by switching to the walk-patterns driver (#156171).

You can temporarily reactivate the old behavior by calling 
`RewriterBase::replaceAllUsesWith`. However, note that that behavior is faulty 
in a dialect conversion. E.g., the base `RewriterBase::replaceAllUsesWith` 
implementation does not see uses of the `from` value that have not materialized 
yet and will, therefore, not replace them.


---

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


7 Files Affected:

- (modified) mlir/include/mlir/IR/PatternMatch.h (+1-1) 
- (modified) mlir/include/mlir/Transforms/DialectConversion.h (+19-8) 
- (modified) mlir/lib/Conversion/FuncToLLVM/FuncToLLVM.cpp (+1-1) 
- (modified) mlir/lib/Transforms/Utils/DialectConversion.cpp (+98-60) 
- (modified) mlir/test/Transforms/test-legalizer-rollback.mlir (+5-3) 
- (modified) mlir/test/Transforms/test-legalizer.mlir (+22-4) 
- (modified) mlir/test/lib/Dialect/Test/TestPatterns.cpp (+12-12) 


``diff
diff --git a/mlir/include/mlir/IR/PatternMatch.h 
b/mlir/include/mlir/IR/PatternMatch.h
index 57e73c1d8c7c1..7b0b9cef9c5bd 100644
--- a/mlir/include/mlir/IR/PatternMatch.h
+++ b/mlir/include/mlir/IR/PatternMatch.h
@@ -633,7 +633,7 @@ class RewriterBase : public OpBuilder {
 
   /// Find uses of `from` and replace them with `to`. Also notify the listener
   /// about every in-place op modification (for every use that was replaced).
-  void replaceAllUsesWith(Value from, Value to) {
+  virtual void replaceAllUsesWith(Value from, Value to) {
 for (OpOperand &operand : llvm::make_early_inc_range(from.getUses())) {
   Operation *op = operand.getOwner();
   modifyOpInPlace(op, [&]() { operand.set(to); });
diff --git a/mlir/include/mlir/Transforms/DialectConversion.h 
b/mlir/include/mlir/Transforms/DialectConversion.h
index 14dfbf18836c6..fe48e45a9b98c 100644
--- a/mlir/include/mlir/Transforms/DialectConversion.h
+++ b/mlir/include/mlir/Transforms/DialectConversion.h
@@ -854,15 +854,26 @@ class ConversionPatternRewriter final : public 
PatternRewriter {
   Region *region, const TypeConverter &converter,
   TypeConverter::SignatureConversion *entryConversion = nullptr);
 
-  /// Replace all the uses of the block argument `from` with `to`. This
-  /// function supports both 1:1 and 1:N replacements.
+  /// Replace all the uses of `from` with `to`. The type of `from` and `to` is
+  /// allowed to differ. The conversion driver will try to reconcile all type
+  /// mismatches that still exist at the end of the conversion with
+  /// materializations. This function supports both 1:1 and 1:N replacements.
   ///
-  /// Note: If `allowPatternRollback` is set to "true", this function replaces
-  /// all current and future uses of the block argument. This same block
-  /// block argument must not be replaced multiple times. Uses are not replaced
-  /// immediately but in a delayed fashion. Patterns may still see the original
-  /// uses when inspecting IR.
-  void replaceUsesOfBlockArgument(BlockArgument from, ValueRange to);
+  /// Note: If `allowPatternRollback` is set to "true", this function behaves
+  /// slightly different:
+  ///
+  /// 1. All current and future uses of `from` are replaced. The same value 
must
+  ///not be replaced multiple times. That's an API violation.
+  /// 2. Uses are not replaced immediately but in a delayed fashion. Patterns
+  ///may still see the original uses when inspecting IR.
+  /// 3. Uses within the same block that appear before the defining operation
+  ///of the replacement value are not replaced. This allows users to
+  ///perform certain replaceAllUsesExcept

[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits


@@ -1169,32 +1185,54 @@ void BlockTypeConversionRewrite::rollback() {
   getNewBlock()->replaceAllUsesWith(getOrigBlock());
 }
 
-static void performReplaceBlockArg(RewriterBase &rewriter, BlockArgument arg,
-   Value repl) {
+/// Replace all uses of `from` with `repl`.
+static void performReplaceValue(RewriterBase &rewriter, Value from,
+Value repl) {
   if (isa(repl)) {
-rewriter.replaceAllUsesWith(arg, repl);
+// `repl` is a block argument. Directly replace all uses.
+rewriter.replaceAllUsesWith(from, repl);
 return;
   }
 
-  // If the replacement value is an operation, we check to make sure that we
-  // don't replace uses that are within the parent operation of the
-  // replacement value.
-  Operation *replOp = cast(repl).getOwner();
+  // If the replacement value is an operation, only replace those uses that:

matthias-springer wrote:

Note: The implementation here has not changed. I just added more documentation 
that explains the behavior.

https://github.com/llvm/llvm-project/pull/155244
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer edited 
https://github.com/llvm/llvm-project/pull/155244
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][Transforms] Add support for `ConversionPatternRewriter::replaceAllUsesWith` (PR #155244)

2025-08-30 Thread Matthias Springer via llvm-branch-commits

https://github.com/matthias-springer ready_for_review 
https://github.com/llvm/llvm-project/pull/155244
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] release/21.x: [rtsan] Add versioned pthread_cond interceptors (#155970) (PR #156196)

2025-08-30 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-compiler-rt-sanitizer

Author: None (llvmbot)


Changes

Backport 8f317c1

Requested by: @cjappl

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


2 Files Affected:

- (modified) compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp (+28) 
- (modified) compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp 
(+18) 


``diff
diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp 
b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index a9d864e9fe926..1b1ff9b211733 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -15,6 +15,7 @@
 
 #include "interception/interception.h"
 #include "sanitizer_common/sanitizer_allocator_dlsym.h"
+#include "sanitizer_common/sanitizer_glibc_version.h"
 #include "sanitizer_common/sanitizer_platform_interceptors.h"
 
 #include "interception/interception.h"
@@ -766,6 +767,12 @@ INTERCEPTOR(int, pthread_join, pthread_t thread, void 
**value_ptr) {
   return REAL(pthread_join)(thread, value_ptr);
 }
 
+INTERCEPTOR(int, pthread_cond_init, pthread_cond_t *cond,
+const pthread_condattr_t *a) {
+  __rtsan_notify_intercepted_call("pthread_cond_init");
+  return REAL(pthread_cond_init)(cond, a);
+}
+
 INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {
   __rtsan_notify_intercepted_call("pthread_cond_signal");
   return REAL(pthread_cond_signal)(cond);
@@ -788,6 +795,11 @@ INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t 
*cond,
   return REAL(pthread_cond_timedwait)(cond, mutex, ts);
 }
 
+INTERCEPTOR(int, pthread_cond_destroy, pthread_cond_t *cond) {
+  __rtsan_notify_intercepted_call("pthread_cond_destroy");
+  return REAL(pthread_cond_destroy)(cond);
+}
+
 INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {
   __rtsan_notify_intercepted_call("pthread_rwlock_rdlock");
   return REAL(pthread_rwlock_rdlock)(lock);
@@ -1641,10 +1653,26 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(pthread_mutex_lock);
   INTERCEPT_FUNCTION(pthread_mutex_unlock);
   INTERCEPT_FUNCTION(pthread_join);
+
+  // See the comment in tsan_interceptors_posix.cpp.
+#if SANITIZER_GLIBC && !__GLIBC_PREREQ(2, 36) &&   
\
+(defined(__x86_64__) || defined(__mips__) || SANITIZER_PPC64V1 ||  
\
+ defined(__s390x__))
+  INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2");
+#else
+  INTERCEPT_FUNCTION(pthread_cond_init);
   INTERCEPT_FUNCTION(pthread_cond_signal);
   INTERCEPT_FUNCTION(pthread_cond_broadcast);
   INTERCEPT_FUNCTION(pthread_cond_wait);
   INTERCEPT_FUNCTION(pthread_cond_timedwait);
+  INTERCEPT_FUNCTION(pthread_cond_destroy);
+#endif
+
   INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
   INTERCEPT_FUNCTION(pthread_rwlock_unlock);
   INTERCEPT_FUNCTION(pthread_rwlock_wrlock);
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp 
b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index 26a3b252d3b6b..124c4f5b54ce9 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -1232,6 +1232,24 @@ TEST(TestRtsanInterceptors, 
SpinLockLockDiesWhenRealtime) {
 }
 #endif
 
+TEST(TestRtsanInterceptors, PthreadCondInitDiesWhenRealtime) {
+  pthread_cond_t cond{};
+  auto Func = [&cond]() { pthread_cond_init(&cond, nullptr); };
+  ExpectRealtimeDeath(Func, "pthread_cond_init");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, PthreadCondDestroyDiesWhenRealtime) {
+  pthread_cond_t cond{};
+  ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
+
+  auto Func = [&cond]() { pthread_cond_destroy(&cond); };
+  ExpectRealtimeDeath(Func, "pthread_cond_destroy");
+  ExpectNonRealtimeSurvival(Func);
+
+  pthread_cond_destroy(&cond);
+}
+
 TEST(TestRtsanInterceptors, PthreadCondSignalDiesWhenRealtime) {
   pthread_cond_t cond{};
   ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));

``




https://github.com/llvm/llvm-project/pull/156196
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] release/21.x: [rtsan] Add versioned pthread_cond interceptors (#155970) (PR #156196)

2025-08-30 Thread via llvm-branch-commits

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

Backport 8f317c1

Requested by: @cjappl

>From 95ef59003047ab5ed8d65498baa9e12521de5d30 Mon Sep 17 00:00:00 2001
From: Chris Apple 
Date: Fri, 29 Aug 2025 11:06:31 -0700
Subject: [PATCH] [rtsan] Add versioned pthread_cond interceptors (#155970)

This fixes #146120, confirmed by the original reporter

Previously reviewed as #155181, but re-submitting for better
book-keeping.

Adds versioned pthread_cond interceptors, and the
pthread_cond_init/_destroy interceptors

(cherry picked from commit 8f317c1a37903fea68590dce5b6cde7de6048b8f)
---
 .../lib/rtsan/rtsan_interceptors_posix.cpp| 28 +++
 .../tests/rtsan_test_interceptors_posix.cpp   | 18 
 2 files changed, 46 insertions(+)

diff --git a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp 
b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
index a9d864e9fe926..1b1ff9b211733 100644
--- a/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp
@@ -15,6 +15,7 @@
 
 #include "interception/interception.h"
 #include "sanitizer_common/sanitizer_allocator_dlsym.h"
+#include "sanitizer_common/sanitizer_glibc_version.h"
 #include "sanitizer_common/sanitizer_platform_interceptors.h"
 
 #include "interception/interception.h"
@@ -766,6 +767,12 @@ INTERCEPTOR(int, pthread_join, pthread_t thread, void 
**value_ptr) {
   return REAL(pthread_join)(thread, value_ptr);
 }
 
+INTERCEPTOR(int, pthread_cond_init, pthread_cond_t *cond,
+const pthread_condattr_t *a) {
+  __rtsan_notify_intercepted_call("pthread_cond_init");
+  return REAL(pthread_cond_init)(cond, a);
+}
+
 INTERCEPTOR(int, pthread_cond_signal, pthread_cond_t *cond) {
   __rtsan_notify_intercepted_call("pthread_cond_signal");
   return REAL(pthread_cond_signal)(cond);
@@ -788,6 +795,11 @@ INTERCEPTOR(int, pthread_cond_timedwait, pthread_cond_t 
*cond,
   return REAL(pthread_cond_timedwait)(cond, mutex, ts);
 }
 
+INTERCEPTOR(int, pthread_cond_destroy, pthread_cond_t *cond) {
+  __rtsan_notify_intercepted_call("pthread_cond_destroy");
+  return REAL(pthread_cond_destroy)(cond);
+}
+
 INTERCEPTOR(int, pthread_rwlock_rdlock, pthread_rwlock_t *lock) {
   __rtsan_notify_intercepted_call("pthread_rwlock_rdlock");
   return REAL(pthread_rwlock_rdlock)(lock);
@@ -1641,10 +1653,26 @@ void __rtsan::InitializeInterceptors() {
   INTERCEPT_FUNCTION(pthread_mutex_lock);
   INTERCEPT_FUNCTION(pthread_mutex_unlock);
   INTERCEPT_FUNCTION(pthread_join);
+
+  // See the comment in tsan_interceptors_posix.cpp.
+#if SANITIZER_GLIBC && !__GLIBC_PREREQ(2, 36) &&   
\
+(defined(__x86_64__) || defined(__mips__) || SANITIZER_PPC64V1 ||  
\
+ defined(__s390x__))
+  INTERCEPT_FUNCTION_VER(pthread_cond_init, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_signal, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_broadcast, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_wait, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_timedwait, "GLIBC_2.3.2");
+  INTERCEPT_FUNCTION_VER(pthread_cond_destroy, "GLIBC_2.3.2");
+#else
+  INTERCEPT_FUNCTION(pthread_cond_init);
   INTERCEPT_FUNCTION(pthread_cond_signal);
   INTERCEPT_FUNCTION(pthread_cond_broadcast);
   INTERCEPT_FUNCTION(pthread_cond_wait);
   INTERCEPT_FUNCTION(pthread_cond_timedwait);
+  INTERCEPT_FUNCTION(pthread_cond_destroy);
+#endif
+
   INTERCEPT_FUNCTION(pthread_rwlock_rdlock);
   INTERCEPT_FUNCTION(pthread_rwlock_unlock);
   INTERCEPT_FUNCTION(pthread_rwlock_wrlock);
diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp 
b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
index 26a3b252d3b6b..124c4f5b54ce9 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_interceptors_posix.cpp
@@ -1232,6 +1232,24 @@ TEST(TestRtsanInterceptors, 
SpinLockLockDiesWhenRealtime) {
 }
 #endif
 
+TEST(TestRtsanInterceptors, PthreadCondInitDiesWhenRealtime) {
+  pthread_cond_t cond{};
+  auto Func = [&cond]() { pthread_cond_init(&cond, nullptr); };
+  ExpectRealtimeDeath(Func, "pthread_cond_init");
+  ExpectNonRealtimeSurvival(Func);
+}
+
+TEST(TestRtsanInterceptors, PthreadCondDestroyDiesWhenRealtime) {
+  pthread_cond_t cond{};
+  ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));
+
+  auto Func = [&cond]() { pthread_cond_destroy(&cond); };
+  ExpectRealtimeDeath(Func, "pthread_cond_destroy");
+  ExpectNonRealtimeSurvival(Func);
+
+  pthread_cond_destroy(&cond);
+}
+
 TEST(TestRtsanInterceptors, PthreadCondSignalDiesWhenRealtime) {
   pthread_cond_t cond{};
   ASSERT_EQ(0, pthread_cond_init(&cond, nullptr));

___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] release/21.x: [rtsan] Add versioned pthread_cond interceptors (#155970) (PR #156196)

2025-08-30 Thread via llvm-branch-commits

https://github.com/llvmbot milestoned 
https://github.com/llvm/llvm-project/pull/156196
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] release/21.x: [rtsan] Add versioned pthread_cond interceptors (#155970) (PR #156196)

2025-08-30 Thread Chris Apple via llvm-branch-commits

cjappl wrote:

@fmayer adding you as a reviewer here for visibility, this is our first time 
porting something to the release branch and want to make sure we don't mess 
something up 

https://github.com/llvm/llvm-project/pull/156196
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [compiler-rt] release/21.x: [rtsan] Add versioned pthread_cond interceptors (#155970) (PR #156196)

2025-08-30 Thread via llvm-branch-commits

llvmbot wrote:

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

https://github.com/llvm/llvm-project/pull/156196
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits