[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

https://github.com/AmrDeveloper created 
https://github.com/llvm/llvm-project/pull/144236

None

>From dc824e4ff23a7ff5124de5a21362e5b543c55d27 Mon Sep 17 00:00:00 2001
From: AmrDeveloper 
Date: Sat, 14 Jun 2025 22:19:08 +0200
Subject: [PATCH] [CIR] Upstream ComplexImagPtrOp for ComplexType

---
 clang/include/clang/CIR/Dialect/IR/CIROps.td  | 29 +++
 clang/lib/CIR/CodeGen/CIRGenBuilder.h | 14 +
 clang/lib/CIR/CodeGen/CIRGenExpr.cpp  |  9 ++
 clang/lib/CIR/Dialect/IR/CIRDialect.cpp   | 19 
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 20 -
 .../CIR/Lowering/DirectToLLVM/LowerToLLVM.h   | 10 +++
 clang/test/CIR/CodeGen/complex.cpp| 14 +
 7 files changed, 108 insertions(+), 7 deletions(-)

diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 17279f0a9985a..f90784c99602c 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2414,4 +2414,33 @@ def ComplexRealPtrOp : CIR_Op<"complex.real_ptr", 
[Pure]> {
   let hasVerifier = 1;
 }
 
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+def ComplexImagPtrOp : CIR_Op<"complex.imag_ptr", [Pure]> {
+  let summary = "Derive a pointer to the imaginary part of a complex value";
+  let description = [{
+`cir.complex.imag_ptr` operation takes a pointer operand that points to a
+complex value of type `!cir.complex` and yields a pointer to the imaginary
+part of the operand.
+
+Example:
+
+```mlir
+%1 = cir.complex.imag_ptr %0 : !cir.ptr> -> 
!cir.ptr
+```
+  }];
+
+  let results = (outs CIR_PtrToIntOrFloatType:$result);
+  let arguments = (ins CIR_PtrToComplexType:$operand);
+
+  let assemblyFormat = [{
+$operand `:`
+qualified(type($operand)) `->` qualified(type($result)) attr-dict
+  }];
+
+  let hasVerifier = 1;
+}
+
 #endif // CLANG_CIR_DIALECT_IR_CIROPS_TD
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h 
b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 3f7ea5bccb6d5..df3f4d65610a6 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -379,6 +379,20 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
 return Address{createRealPtr(loc, addr.getPointer()), addr.getAlignment()};
   }
 
+  /// Create a cir.complex.imag_ptr operation that derives a pointer to the
+  /// imaginary part of the complex value pointed to by the specified pointer
+  /// value.
+  mlir::Value createImagPtr(mlir::Location loc, mlir::Value value) {
+auto srcPtrTy = mlir::cast(value.getType());
+auto srcComplexTy = mlir::cast(srcPtrTy.getPointee());
+return create(
+loc, getPointerTo(srcComplexTy.getElementType()), value);
+  }
+
+  Address createImagPtr(mlir::Location loc, Address addr) {
+return Address{createImagPtr(loc, addr.getPointer()), addr.getAlignment()};
+  }
+
   /// Create a cir.ptr_stride operation to get access to an array element.
   /// \p idx is the index of the element to access, \p shouldDecay is true if
   /// the result should decay to a pointer to the element type.
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index a682586562e04..8e3d9ab620621 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -541,11 +541,6 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
   }
   case UO_Real:
   case UO_Imag: {
-if (op == UO_Imag) {
-  cgm.errorNYI(e->getSourceRange(), "UnaryOp real/imag");
-  return LValue();
-}
-
 LValue lv = emitLValue(e->getSubExpr());
 assert(lv.isSimple() && "real/imag on non-ordinary l-value");
 
@@ -560,7 +555,9 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
 QualType exprTy = 
getContext().getCanonicalType(e->getSubExpr()->getType());
 QualType elemTy = exprTy->castAs()->getElementType();
 mlir::Location loc = getLoc(e->getExprLoc());
-Address component = builder.createRealPtr(loc, lv.getAddress());
+Address component = op == UO_Real
+? builder.createRealPtr(loc, lv.getAddress())
+: builder.createImagPtr(loc, lv.getAddress());
 LValue elemLV = makeAddrLValue(component, elemTy);
 elemLV.getQuals().addQualifiers(lv.getQuals());
 return elemLV;
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 99ae4dd59120a..40488f6af5676 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1794,6 +1794,25 @@ LogicalResult cir::ComplexRealPtrOp::verify() {
   return success();
 }
 
+//===--===//
+// ComplexImagPtrOp
+

[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

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

* **#144236** https://app.graphite.dev/github/pr/llvm/llvm-project/144236?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/144236?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#144235** https://app.graphite.dev/github/pr/llvm/llvm-project/144235?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/144236
___
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Amr Hesham (AmrDeveloper)


Changes



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


7 Files Affected:

- (modified) clang/include/clang/CIR/Dialect/IR/CIROps.td (+29) 
- (modified) clang/lib/CIR/CodeGen/CIRGenBuilder.h (+14) 
- (modified) clang/lib/CIR/CodeGen/CIRGenExpr.cpp (+3-6) 
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+19) 
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp (+19-1) 
- (modified) clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.h (+10) 
- (modified) clang/test/CIR/CodeGen/complex.cpp (+14) 


``diff
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td 
b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 17279f0a9985a..f90784c99602c 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -2414,4 +2414,33 @@ def ComplexRealPtrOp : CIR_Op<"complex.real_ptr", 
[Pure]> {
   let hasVerifier = 1;
 }
 
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+def ComplexImagPtrOp : CIR_Op<"complex.imag_ptr", [Pure]> {
+  let summary = "Derive a pointer to the imaginary part of a complex value";
+  let description = [{
+`cir.complex.imag_ptr` operation takes a pointer operand that points to a
+complex value of type `!cir.complex` and yields a pointer to the imaginary
+part of the operand.
+
+Example:
+
+```mlir
+%1 = cir.complex.imag_ptr %0 : !cir.ptr> -> 
!cir.ptr
+```
+  }];
+
+  let results = (outs CIR_PtrToIntOrFloatType:$result);
+  let arguments = (ins CIR_PtrToComplexType:$operand);
+
+  let assemblyFormat = [{
+$operand `:`
+qualified(type($operand)) `->` qualified(type($result)) attr-dict
+  }];
+
+  let hasVerifier = 1;
+}
+
 #endif // CLANG_CIR_DIALECT_IR_CIROPS_TD
diff --git a/clang/lib/CIR/CodeGen/CIRGenBuilder.h 
b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
index 3f7ea5bccb6d5..df3f4d65610a6 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuilder.h
+++ b/clang/lib/CIR/CodeGen/CIRGenBuilder.h
@@ -379,6 +379,20 @@ class CIRGenBuilderTy : public cir::CIRBaseBuilderTy {
 return Address{createRealPtr(loc, addr.getPointer()), addr.getAlignment()};
   }
 
+  /// Create a cir.complex.imag_ptr operation that derives a pointer to the
+  /// imaginary part of the complex value pointed to by the specified pointer
+  /// value.
+  mlir::Value createImagPtr(mlir::Location loc, mlir::Value value) {
+auto srcPtrTy = mlir::cast(value.getType());
+auto srcComplexTy = mlir::cast(srcPtrTy.getPointee());
+return create(
+loc, getPointerTo(srcComplexTy.getElementType()), value);
+  }
+
+  Address createImagPtr(mlir::Location loc, Address addr) {
+return Address{createImagPtr(loc, addr.getPointer()), addr.getAlignment()};
+  }
+
   /// Create a cir.ptr_stride operation to get access to an array element.
   /// \p idx is the index of the element to access, \p shouldDecay is true if
   /// the result should decay to a pointer to the element type.
diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp 
b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
index a682586562e04..8e3d9ab620621 100644
--- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp
@@ -541,11 +541,6 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
   }
   case UO_Real:
   case UO_Imag: {
-if (op == UO_Imag) {
-  cgm.errorNYI(e->getSourceRange(), "UnaryOp real/imag");
-  return LValue();
-}
-
 LValue lv = emitLValue(e->getSubExpr());
 assert(lv.isSimple() && "real/imag on non-ordinary l-value");
 
@@ -560,7 +555,9 @@ LValue CIRGenFunction::emitUnaryOpLValue(const 
UnaryOperator *e) {
 QualType exprTy = 
getContext().getCanonicalType(e->getSubExpr()->getType());
 QualType elemTy = exprTy->castAs()->getElementType();
 mlir::Location loc = getLoc(e->getExprLoc());
-Address component = builder.createRealPtr(loc, lv.getAddress());
+Address component = op == UO_Real
+? builder.createRealPtr(loc, lv.getAddress())
+: builder.createImagPtr(loc, lv.getAddress());
 LValue elemLV = makeAddrLValue(component, elemTy);
 elemLV.getQuals().addQualifiers(lv.getQuals());
 return elemLV;
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp 
b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 99ae4dd59120a..40488f6af5676 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -1794,6 +1794,25 @@ LogicalResult cir::ComplexRealPtrOp::verify() {
   return success();
 }
 
+//===--===//
+// ComplexImagPtrOp
+//===--===//
+
+LogicalResult cir::ComplexImagPtrOp::verify() {
+  mlir::Type resultPoi

[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

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


[llvm-branch-commits] [clang] [CIR] Upstream ComplexImagPtrOp for ComplexType (PR #144236)

2025-06-14 Thread Amr Hesham via llvm-branch-commits

https://github.com/AmrDeveloper edited 
https://github.com/llvm/llvm-project/pull/144236
___
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] [BOLT] Explicitly check for returns when extending call continuation profile (PR #143295)

2025-06-14 Thread Amir Ayupov via llvm-branch-commits

https://github.com/aaupov updated 
https://github.com/llvm/llvm-project/pull/143295


___
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] [BOLT] Explicitly check for returns when extending call continuation profile (PR #143295)

2025-06-14 Thread Amir Ayupov via llvm-branch-commits

https://github.com/aaupov edited 
https://github.com/llvm/llvm-project/pull/143295
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits


@@ -0,0 +1,94 @@
+//===- TargetImpl.h -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLD_ELF_ARCH_TARGETIMPL_H
+#define LLD_ELF_ARCH_TARGETIMPL_H
+
+#include "InputFiles.h"
+#include "InputSection.h"
+#include "Relocations.h"
+#include "Symbols.h"
+#include "llvm/BinaryFormat/ELF.h"
+
+namespace lld {
+namespace elf {
+
+// getControlTransferAddend: If this relocation is used for control transfer
+// instructions (e.g. branch, branch-link or call) or code references (e.g.
+// virtual function pointers) and indicates an address-insignificant reference,
+// return the effective addend for the relocation, otherwise return
+// std::nullopt. The effective addend for a relocation is the addend that is
+// used to determine its branch destination.
+//
+// getBranchInfoAtTarget: If a control transfer relocation referring to
+// is+offset directly transfers control to a relocated branch instruction in 
the
+// specified section, return the relocation for the branch target as well as 
its
+// effective addend (see above). Otherwise return {nullptr, 0}.
+//
+// redirectControlTransferRelocations: Given r1, a relocation for which
+// getControlTransferAddend() returned a value, and r2, a relocation returned 
by
+// getBranchInfo(), modify r1 so that it branches directly to the target of r2.
+template 
+inline void applyBranchToBranchOptImpl(
+Ctx &ctx, GetControlTransferAddend getControlTransferAddend,
+GetBranchInfoAtTarget getBranchInfoAtTarget,
+RedirectControlTransferRelocations redirectControlTransferRelocations) {
+  // Needs to run serially because it writes to the relocations array as well 
as
+  // reading relocations of other sections.
+  for (ELFFileBase *f : ctx.objectFiles) {
+auto getRelocBranchInfo =
+[&getBranchInfoAtTarget](
+Relocation &r,
+uint64_t addend) -> std::pair {
+  auto *target = dyn_cast_or_null(r.sym);
+  // We don't allow preemptible symbols or ifuncs (may go somewhere else),
+  // absolute symbols (runtime behavior unknown), non-executable or 
writable
+  // memory (ditto) or non-regular sections (no section data).
+  if (!target || target->isPreemptible || target->isGnuIFunc() ||
+  !target->section ||
+  !(target->section->flags & llvm::ELF::SHF_EXECINSTR) ||
+  (target->section->flags & llvm::ELF::SHF_WRITE) ||
+  target->section->kind() != SectionBase::Regular)
+return {nullptr, 0};
+  return getBranchInfoAtTarget(*cast(target->section),
+   target->value + addend);
+};
+for (InputSectionBase *s : f->getSections()) {
+  if (!s)
+continue;
+  for (Relocation &r : s->relocations) {
+if (std::optional addend =

MaskRay wrote:

can use early return for this if the next if to reduce indentation

https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits


@@ -0,0 +1,98 @@
+# REQUIRES: x86
+
+## Test that the branch-to-branch optimization follows the links
+## from f1 -> f2 -> f3 and updates all references to point to f3.
+ 
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t --branch-to-branch
+# RUN: llvm-objdump -d -s %t | FileCheck --check-prefixes=CHECK,B2B %s
+# RUN: ld.lld %t.o -o %t -O2
+# RUN: llvm-objdump -d -s %t | FileCheck --check-prefixes=CHECK,B2B %s
+
+## Test that branch-to-branch is disabled by default.
+
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -d -s %t | FileCheck --check-prefixes=CHECK,NOB2B %s
+# RUN: ld.lld %t.o -o %t -O2 --no-branch-to-branch
+# RUN: llvm-objdump -d -s %t | FileCheck --check-prefixes=CHECK,NOB2B %s
+
+## Test that branch-to-branch is disabled for preemptible symbols.
+
+# RUN: ld.lld %t.o -o %t --branch-to-branch -shared
+# RUN: llvm-objdump -d -s %t | FileCheck --check-prefixes=CHECK,NOB2B %s
+
+.section .rodata.vtable,"a"
+.globl vtable
+vtable:
+# B2B: Contents of section .rodata:
+# B2B-NEXT: [[VF:[0-9a-f]{8}]]

MaskRay wrote:

We need to check the exact value, otherwise the PLT32 with an addend not -4 is 
not tested.

https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits


@@ -0,0 +1,92 @@
+//===- TargetImpl.h -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLD_ELF_ARCH_TARGETIMPL_H
+#define LLD_ELF_ARCH_TARGETIMPL_H
+
+#include "InputFiles.h"
+#include "InputSection.h"
+#include "Relocations.h"
+#include "Symbols.h"
+#include "llvm/BinaryFormat/ELF.h"
+
+namespace lld {
+namespace elf {
+
+// getControlTransferAddend: If this relocation is used for control transfer
+// instructions (e.g. branch, branch-link or call) or code references (e.g.
+// virtual function pointers) and indicates an address-insignificant reference,
+// return the effective addend for the relocation, otherwise return
+// std::nullopt. The effective addend for a relocation is the addend that is
+// used to determine its branch destination.
+//
+// getBranchInfo: If a control transfer relocation referring to is+offset
+// directly transfers control to a relocated branch instruction in the 
specified
+// section, return the relocation for the branch target as well as its 
effective
+// addend (see above). Otherwise return {nullptr, 0}.
+//
+// mergeControlTransferRelocations: Given r1, a relocation for which
+// getControlTransferAddend() returned a value, and r2, a relocation returned 
by
+// getBranchInfo(), modify r1 so that it branches directly to the target of r2.
+template 
+inline void applyBranchToBranchOptImpl(
+Ctx &ctx, GetBranchInfo getBranchInfo,
+GetControlTransferAddend getControlTransferAddend,
+MergeControlTransferRelocations mergeControlTransferRelocations) {
+  // Needs to run serially because it writes to the relocations array as well 
as
+  // reading relocations of other sections.
+  for (ELFFileBase *f : ctx.objectFiles) {
+auto getRelocBranchInfo =
+[&getBranchInfo](Relocation &r,
+ uint64_t addend) -> std::pair 
{
+  auto *target = dyn_cast_or_null(r.sym);
+  // We don't allow preemptible symbols or ifuncs (may go somewhere else),
+  // absolute symbols (runtime behavior unknown), non-executable memory
+  // (ditto) or non-regular sections (no section data).
+  if (!target || target->isPreemptible || target->isGnuIFunc() ||

MaskRay wrote:

I agree that there should be a --emit-relocs test

https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits


@@ -0,0 +1,94 @@
+//===- TargetImpl.h -*- C++ 
-*-===//

MaskRay wrote:

`//===--===//`
 for new file per https://llvm.org/docs/CodingStandards.html#file-headers

https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits

MaskRay wrote:

> [pcc](https://github.com/pcc) wants to merge 1 commit into 
> [users/pcc/spr/main.elf-add-branch-to-branch-optimization](https://github.com/llvm/llvm-project/tree/users/pcc/spr/main.elf-add-branch-to-branch-optimization)
>  from 
> [users/pcc/spr/elf-add-branch-to-branch-optimization](https://github.com/llvm/llvm-project/tree/users/pcc/spr/elf-add-branch-to-branch-optimization)

Is the base branch associated with an open PR? 

Neither
```
curl -L https://github.com/llvm/llvm-project/pull/138366.diff | patch -p1

spr patch 138366
```
works.

https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits


@@ -975,6 +977,62 @@ void AArch64::relocateAlloc(InputSectionBase &sec, uint8_t 
*buf) const {
   }
 }
 
+static std::optional getControlTransferAddend(InputSection &is,
+Relocation &r) {
+  // Identify a control transfer relocation for the branch-to-branch
+  // optimization. A "control transfer relocation" means a B or BL
+  // target but it also includes relative vtable relocations for example.
+  //
+  // We require the relocation type to be JUMP26, CALL26 or PLT32. With a
+  // relocation type of PLT32 the value may be assumed to be used for branching
+  // directly to the symbol and the addend is only used to produce the 
relocated
+  // value (hence the effective addend is always 0). This is because if a PLT 
is
+  // needed the addend will be added to the address of the PLT, and it doesn't
+  // make sense to branch into the middle of a PLT. For example, relative 
vtable
+  // relocations use PLT32 and 0 or a positive value as the addend but still 
are
+  // used to branch to the symbol.
+  //
+  // With JUMP26 or CALL26 the only reasonable interpretation of a non-zero
+  // addend is that we are branching to symbol+addend so that becomes the
+  // effective addend.
+  if (r.type == R_AARCH64_PLT32)
+return 0;
+  if (r.type == R_AARCH64_JUMP26 || r.type == R_AARCH64_CALL26)
+return r.addend;
+  return std::nullopt;
+}
+
+static std::pair getBranchInfo(InputSection &is,
+   uint64_t offset) {
+  auto *i = std::lower_bound(

MaskRay wrote:

Complex lower_bound/upper_bound can be simplified with `partition_point` 

https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Peter Collingbourne via llvm-branch-commits

pcc wrote:

Sorry, I was having a problem with spr with this change so I pushed the
updated change directly to the branch.

I think there are some merge conflicts by now in the include list but they
should be easy to resolve.
-- 
Peter

On Sat, Jun 14, 2025, 17:58 Fangrui Song ***@***.***> wrote:

> *MaskRay* left a comment (llvm/llvm-project#138366)
> 
>
> pcc  wants to merge 1 commit into
> users/pcc/spr/main.elf-add-branch-to-branch-optimization
> 
> from users/pcc/spr/elf-add-branch-to-branch-optimization
> 
>
> Is the base branch associated with an open PR?
>
> Neither
>
> curl -L https://github.com/llvm/llvm-project/pull/138366.diff | patch -p1
>
> spr patch 138366
>
> works.
>
> —
> Reply to this email directly, view it on GitHub
> ,
> or unsubscribe
> 
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>


https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits

MaskRay wrote:

Perhaps you'll need to change the base branch to `main` and force push to 
users/pcc/spr/elf-add-branch-to-branch-optimization?

https://github.com/llvm/llvm-project/pull/138366
___
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] [lld] ELF: Add branch-to-branch optimization. (PR #138366)

2025-06-14 Thread Fangrui Song via llvm-branch-commits


@@ -0,0 +1,94 @@
+//===- TargetImpl.h -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLD_ELF_ARCH_TARGETIMPL_H
+#define LLD_ELF_ARCH_TARGETIMPL_H
+
+#include "InputFiles.h"
+#include "InputSection.h"
+#include "Relocations.h"
+#include "Symbols.h"
+#include "llvm/BinaryFormat/ELF.h"
+

MaskRay wrote:

namespace lld::elf

https://github.com/llvm/llvm-project/pull/138366
___
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] [BOLT] Explicitly check for returns when extending call continuation profile (PR #143295)

2025-06-14 Thread Amir Ayupov via llvm-branch-commits

https://github.com/aaupov edited 
https://github.com/llvm/llvm-project/pull/143295
___
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] [BOLT] Explicitly check for returns when extending call continuation profile (PR #143295)

2025-06-14 Thread Amir Ayupov via llvm-branch-commits

https://github.com/aaupov updated 
https://github.com/llvm/llvm-project/pull/143295


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