184 Commits

Author SHA1 Message Date
Alexander Richardson
3a4b351ba1
[IR] Introduce the ptrtoaddr instruction
This introduces a new `ptrtoaddr` instruction which is similar to
`ptrtoint` but has two differences:

1) Unlike `ptrtoint`, `ptrtoaddr` does not capture provenance
2) `ptrtoaddr` only extracts (and then extends/truncates) the low
   index-width bits of the pointer

For most architectures, difference 2) does not matter since index (address)
width and pointer representation width are the same, but this does make a
difference for architectures that have pointers that aren't just plain
integer addresses such as AMDGPU fat pointers or CHERI capabilities.

This commit introduces textual and bitcode IR support as well as basic code
generation, but optimization passes do not handle the new instruction yet
so it may result in worse code than using ptrtoint. Follow-up changes will
update capture tracking, etc. for the new instruction.

RFC: https://discourse.llvm.org/t/clarifiying-the-semantics-of-ptrtoint/83987/54

Reviewed By: nikic

Pull Request: https://github.com/llvm/llvm-project/pull/139357
2025-08-08 10:12:39 -07:00
Kazu Hirata
35dd88918f
[llvm] Use llvm::iterator_range::empty (NFC) (#151905) 2025-08-04 07:40:46 -07:00
Andrew Rogers
8f8ed23c62
[llvm] annotate interfaces in llvm/SandboxIR for DLL export (#142863)
## Purpose

This patch is one in a series of code-mods that annotate LLVM’s public
interface for export. This patch annotates the `llvm/SandboxIR` library.
These annotations currently have no meaningful impact on the LLVM build;
however, they are a prerequisite to support an LLVM Windows DLL (shared
library) build.

## Background

This effort is tracked in #109483. Additional context is provided in
[this
discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307),
and documentation for `LLVM_ABI` and related annotations is found in the
LLVM repo
[here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst).

The bulk of these changes were generated automatically using the
[Interface Definition Scanner (IDS)](https://github.com/compnerd/ids)
tool, followed formatting with `git clang-format`.

The following manual adjustments were also applied after running IDS on
Linux:
- Remove explicit `GlobalWithNodeAPI::LLVMGVToGV::operator()` template
function instantiations that were previously added for the dylib build.
Instead, directly annotate the `LLVMGVToGV::operator()` method with
`LLVM_ABI`. This is done so the DLL build works with both MSVC and
clang-cl.
- Explicitly `#include "llvm/SandboxIR/Value.h"` in `Tracker.h` so that
the symbol is available for exported templates in this file. These
templates get fully instantiated on DLL export, so they require the full
definition of `Value`.
- Add extern template instantiation declarations for `GlobalWithNodeAPI`
template types in `Constants.h` and annotate them with
`LLVM_TEMPLATE_ABI`.
- Add `LLVM_EXPORT_TEMPLATE` to `GlobalWithNodeAPI` template
instantiations in `Constants.cpp`.

## Validation

Local builds and tests to validate cross-platform compatibility. This
included llvm, clang, and lldb on the following configurations:

- Windows with MSVC
- Windows with Clang
- Linux with GCC
- Linux with Clang
- Darwin with Clang
2025-06-11 09:19:13 -07:00
Eli Friedman
9f82ac5738
Remove GlobalObject::getAlign/setAlignment (#143188)
Currently, GlobalObject has an "alignment" property... but it's
basically nonsense: alignment doesn't mean the same thing for variables
and functions, and it's completely meaningless for ifuncs.

This "removes" (actually marking protected) the methods from
GlobalObject, adds the relevant methods to Function and GlobalVariable,
and adjusts the code appropriately.

This should make future alignment-related cleanups easier.
2025-06-09 13:51:03 -07:00
Kazu Hirata
228f66807d
[llvm] Remove unused includes (NFC) (#142733)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-06-04 12:30:52 -07:00
vporpo
a2b5fd7f6e
[SandboxIR][Region] Auxiliary vector metadata now requires a region (#137443)
The auxiliary vector is represented by the !sandboxaux metadata. But
until now adding an instruction to the aux vector would not
automatically add it to the region (i.e., it would not mark it with
!sandboxvec). This behavior was problematic when generating regions from
metadata, because you wouldn't know which region owns the auxiliary
vector.

This patch fixes this issue: We now require that an instruction with
!sandboxaux also defines its region with !sandboxvec.
2025-05-27 16:11:13 -07:00
Kazu Hirata
89308de4b0
[llvm] Value-initialize values with *Map::try_emplace (NFC) (#141522)
try_emplace value-initializes values, so we do not need to pass
nullptr to try_emplace when the value types are raw pointers or
std::unique_ptr<T>.
2025-05-26 15:13:02 -07:00
vporpo
a0b2a95226
[SandboxIR] Implement ConstantDataArray functions (#134729)
Mirrors LLVM IR.
2025-04-17 14:03:49 -07:00
Alexandre Ganea
550d148561 [SandboxIR] Fix warning when building on Windows with clang-cl. NFC.
This fixes several of these:
```
[1151/3381] Building CXX object lib\SandboxIR\CMakeFiles\LLVMSandboxIR.dir\Constant.cpp.obj
C:\git\llvm-project\llvm\lib\SandboxIR\Constant.cpp(331,52): warning: duplicate explicit instantiation of 'operator()' ignored as a Microsoft extension [-Wmicrosoft-template]
  331 |                   llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalIFunc
      |                                                    ^
C:\git\llvm-project\llvm\include\llvm/SandboxIR/Constant.h(816,52): note: previous explicit instantiation is here
  816 |                   llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalIFunc
      |                                                    ^
```
2025-04-08 14:40:28 -04:00
vporpo
3756ba3c87
[SandboxIR] Implement ConstantDataSequential and subclasses (#133547)
This patch implements sandboxir::ConstantDataSequential mirroring LLVM
IR.
2025-04-07 13:48:30 -07:00
vporpo
bb047b9ea3
[SandboxIR] Implement missing ConstantVector member functions (#131390)
This patch implements get(), getSplat(), getSplatValue() and getType(),
mirroring LLVM IR.
2025-03-28 17:23:18 -07:00
vporpo
4fcab8a587
[SandboxIR][Region][NFC] Fix windows build issue (#129082)
This should fix the issue reported here:

https://discourse.llvm.org/t/second-stage-of-build-on-windows-fails-in-sandboxir/84841
2025-02-27 14:25:06 -08:00
vporpo
c0c42c8b32
[SandboxIR][NFC] Change order of ifs in Context::getOrCreateValueInternal() (#127891)
Move the most common if statement to the top and the least common ones
to the bottom. This should save CPU cycles during compilation.

This patch also prefixes the llvm variables with the LLVM prefix to make
the naming convention in this function more uniform. For example `C` to
`LLVMC`.
2025-02-19 13:08:43 -08:00
vporpo
1987f93d03
[SandboxIR] OpaqueValue (#127699)
This patch implements a new subclass of the Value class used for Sandbox
IR Values that we don't support, like metadata or inline asm. The goal
is to never have null sandboxir::Value objects, because this is not the
expected behavior.
2025-02-19 11:54:44 -08:00
vporpo
5ecce45ea2
[SandboxVec] Move seed collection into its own separate pass (#127132)
This patch moves the seed collection logic from the BottomUpVec pass
into a new Sandbox IR Function pass. The new "seed-collection" pass
collects the seeds, builds a region and runs the region pass pipeline.
2025-02-18 11:11:07 -08:00
vporpo
0931a2a5a2
[SandboxIR] SetUse callback (#126985)
This patch implements a callback mechanism similar to the existing ones,
but for getting notified whenever a Use edge gets updated. This is going
to be used in a follow up patch by the Dependency Graph.
2025-02-14 11:26:21 -08:00
vporpo
c9ba2b08d5
[SandboxIR][Region] Implement an auxiliary vector in Region (#126376)
This patch adds additional functionality to the sandboxir Region. The
Region is used as a way of passing a set of Instructions across region
passes in a way that can be represented in the IR with metadata. This is
a design choice that allows us to test region passes in isolation with
lit tests.

Up until now the region was only used to tag the instructions generated
by the passes. There is a need to represent an ordered set of
instructions, which can be used as a way to represent the initial seeds
to the first vectorization pass. This patch implements this auxiliary
vector that can be used to convey such information.
2025-02-13 13:51:55 -08:00
vporpo
69b8cf4f06
[SandboxVec][BottomUpVec] Add cost estimation and tr-accept-or-revert pass (#126325)
The TransactionAcceptOrRevert pass is the final pass in the Sandbox
Vectorizer's default pass pipeline. It's job is to check the cost
before/after vectorization and accept or revert the IR to its original
state.

Since we are now starting the transaction in BottomUpVec, tests that run
a custom pipeline need to accept the transaction. This is done with the
help of the TransactionAlwaysAccept pass (tr-accept).
2025-02-08 08:34:18 -08:00
Kazu Hirata
8266eedfdc
[SandboxIR] Avoid repeated hash lookups (NFC) (#125337) 2025-02-01 08:14:39 -08:00
vporpo
79cbad188a
[SandboxVec] Clear Context's state within runOnFunction() (#124842)
`sandboxir::Context` is defined at a pass-level scope with the
`SandboxVectorizerPass` class because the function pass manager `FPM`
object depends on it, and that is in pass-level scope to avoid
recreating the pass pipeline every single time `runOnFunction()` is
called.

This means that the Context's state lives on across function passes. The
problem is twofold:
(i) the LLVM IR to Sandbox IR map can grow very large including objects
from different functions, which is of no use to the vectorizer, as it's
a function-level pass.
(ii) this can result in stale data in the LLVM IR to Sandbox IR object
map, as other passes may delete LLVM IR objects.

To fix both issues this patch introduces a `Context::clear()` function
that clears the `LLVMValueToValueMap`.
2025-01-28 18:28:08 -08:00
vporpo
334a1cdbfa
[SandboxIR] createFunction() should always create a function (#124665)
This patch removes the assertion that checks for an existing function.
If one exists it will remove it and create a new one. This helps remove
a crash when a function declaration object already exists and we are
about to create a SandboxIR object for the definition.
2025-01-27 20:16:30 -08:00
Jeremy Morse
749443a307
[NFC][DebugInfo] Mop up final instruction-insertion call sites (#124289)
These are the final places in the monorepo that make use of instruction
insertion for methods like insertBefore and moveBefore. As part of the
RemoveDIs project, instead use iterators for insertion. (see:
https://discourse.llvm.org/t/rfc-instruction-api-changes-needed-to-eliminate-debug-intrinsics-from-ir/68939
).
2025-01-27 16:07:27 +00:00
vporpo
4b209c5d87
[SandboxIR][Region] Add cost modeling to the region (#124354)
This patch implements cost modeling for Region. All instructions that
are added or removed get their cost counted in the Scoreboard. This is
used for checking if the region before or after a transformation is more
profitable.
2025-01-24 14:28:55 -08:00
Mats Jun Larsen
830bd0e8f2
[SandboxIR] Remove sandboxir::PointerType::get(Type) (#123885)
This was just an alias for getting the LLVM Context from the type.

We are in the process of removing the same function for the regular LLVM
type in https://github.com/llvm/llvm-project/issues/123569
2025-01-22 16:19:33 +09:00
Mats Jun Larsen
416f1c465d
[IR] Replace of PointerType::get(Type) with opaque version (NFC) (#123617)
In accordance with https://github.com/llvm/llvm-project/issues/123569

In order to keep the patch at reasonable size, this PR only covers for
the llvm subproject, unittests excluded.
2025-01-21 00:32:56 +09:00
vporpo
22d4ff155a
[SandboxIR] Fix CmpInst::create() when it gets folded (#123408)
If the operands of a CmpInst are constants then it gets folded into a
constant. Therefore CmpInst::create() should return a Value*, not a
Constant* and should handle the creation of the constant correctly.
2025-01-17 14:37:29 -08:00
Tyler Lanphear
4c0a0f7241
[SandboxVectorizer][NFCI] Fix use of possibly-uninitialized scalar. (#122201)
The `EraseCallbackID` field is not always initialized in the ctor for
SeedCollector; if not, it will be used uninitialized by its dtor. This
could potentially lead to the erasure of a random callback, leading to a
bug.

Fixed by making `CallbackID` an opaque type, which is always
default-initialized to an invalid ID.
2025-01-09 22:43:30 -08:00
Qiongsi Wu
f33e236905
[clang][Modules] Fixing Build Breaks When -DLLVM_ENABLE_MODULES=ON (#119473)
A few recent changes are causing build breaks when
`-DLLVM_ENABLE_MODULES=ON` (such as
834dfd23155351c9885eddf7b9664f7697326946 and
7dfdca1961aadc75ca397818bfb9bd32f1879248).

This PR makes the required updates so that clang/llvm builds when
`-DLLVM_ENABLE_MODULES=ON`.

rdar://140803058
2024-12-11 17:33:25 -08:00
Jorge Gorbe Moya
b22c3c1e88
Register callbacks in Region for instruction creation/deletion. (#117088)
This will keep the current Region updated when region passes add/delete
instructions.
2024-11-21 09:58:51 -08:00
Jorge Gorbe Moya
3b8606be54
Re-land "[SandboxIR] Add debug checker to compare IR before/after a revert (#115968)" (#116671)
This PR re-lands https://github.com/llvm/llvm-project/pull/115968 with a
fix for a buildbot failure.

The `IRSnapshotChecker` class is only defined in debug mode, so its unit
tests must also be inside `#ifndef NDEBUG`.
2024-11-18 11:07:15 -08:00
Jorge Gorbe Moya
0ae58c4533
Revert "[SandboxIR] Add debug checker to compare IR before/after a revert" (#116666)
Reverts llvm/llvm-project#115968. It caused buildbot failures.
2024-11-18 09:54:43 -08:00
Jorge Gorbe Moya
9161e6ab74
[SandboxIR] Add debug checker to compare IR before/after a revert (#115968)
This will help us catch mistakes in change tracking. It's only enabled
when EXPENSIVE_CHECKS are enabled.
2024-11-18 09:39:13 -08:00
Jorge Gorbe Moya
9d85ba5724
[SandboxIR] Preserve the order of switch cases after revert. (#115577)
Preserving the case order is not strictly necessary to preserve
semantics (for example, operations like SwitchInst::removeCase will
happily swap cases around). However, I'm planning to introduce an
optional verification step for SandboxIR that will use StructuralHash to
compare IR after a revert to the original IR to help catch tracker bugs,
and the order difference triggers a difference there.
2024-11-12 14:10:46 -08:00
Kazu Hirata
5b19ed8bb4
[llvm] Migrate away from PointerUnion::{is,get,dyn_cast} (NFC) (#115626)
Note that PointerUnion::{is,get,dyn_cast} have been soft deprecated in
PointerUnion.h:

  // FIXME: Replace the uses of is(), get() and dyn_cast() with
  //        isa<T>, cast<T> and the llvm::dyn_cast<T>
2024-11-10 07:24:06 -08:00
Jorge Gorbe Moya
7a6a52a2f0
[SandboxIR] Remove incorrect assertion. (#115553)
`insertBefore` can be called on a detached instruction, and we can't
check that the underlying instructions are ordered because instructions
without BB parents have no order.

This problem showed up as a different assertion failure in
`llvm::Instruction::comesBefore` in one of the unit tests when
`EXPENSIVE_CHECKS` are enabled.
2024-11-08 14:14:28 -08:00
Kazu Hirata
c79827cd15 [SandboxIR] Fix a warning
This patch fixes:

  llvm/lib/SandboxIR/Context.cpp:684:22: error: unused variable
  'MaxRegisteredCallbacks' [-Werror,-Wunused-const-variable]
2024-10-29 12:05:18 -07:00
Jorge Gorbe Moya
4df71ab78e
[SandboxIR] Add callbacks for instruction insert/remove/move ops (#112965) 2024-10-29 09:25:51 -07:00
vporpo
a461869db3
[SandboxIR][Pass] Implement Analyses class (#113962)
The Analyses class provides a way to pass around commonly used Analyses
to SandboxIR passes throught `runOnFunction()` and `runOnRegion()`
functions.
2024-10-28 18:00:52 -07:00
Thomas Fransham
a905052595
[SandboxIR] Add extern templates for GlobalWithNodeAPI (#111940)
These symbols will need to be explicitly exported for SandboxIRTests
when LLVM is built as shared library on window with explicitly
visibility macros are enabled.

This is part of the work to enable LLVM_BUILD_LLVM_DYLIB and plugins on
windows.
2024-10-24 08:08:32 +03:00
Jorge Gorbe Moya
756ec99c36
[SandboxVec] Re-land "Use sbvec-passes flag to create a pipeline of Region passes after BottomUpVec. (#111223)" (#111772)
https://github.com/llvm/llvm-project/pull/111223 was reverted because of
a build failure with `-DBUILD_SHARED_LIBS=on`.

The Passes component depends on Vectorizer (because PassBuilder needs to
be able to instantiate SandboxVectorizerPass). This resulted in CMake
doing this

1. when it builds lib/libLLVMVectorize.so.20.0git it adds
lib/libLLVMSandboxIR.so.20.0git to the command line, because it's listed
as a dependency (as expected)
2. when it's trying to build lib/libLLVMPasses.so.20.0git it adds
lib/libLLVMVectorize.so.20.0git to the command line, because it's listed
as a dependency (also as expected). But not libLLVMSandboxIR.so.

When SandboxVectorizerPass has its ctors/dtors defined inline, this
caused "undefined reference to vtable" linker errors. This change works
around that by moving ctors/dtors out of line.

Also fix a bazel build problem by adding the new
`llvm/lib/Transforms/Vectorize/SandboxVectorizer/Passes/PassRegistry.def`
as a textual header in the Vectorizer target.
2024-10-09 18:00:17 -07:00
Jorge Gorbe Moya
102c384b57
Revert "[SandboxVectorizer] Use sbvec-passes flag to create a pipeline of Region passes after BottomUpVec." (#111727)
Reverts llvm/llvm-project#111223

It broke one of the build bots:

LLVM Buildbot has detected a new failure on builder flang-aarch64-libcxx
running on linaro-flang-aarch64-libcxx while building llvm at step 5
"build-unified-tree".

Full details are available at:
https://lab.llvm.org/buildbot/#/builders/89/builds/8127
2024-10-09 10:45:23 -07:00
Jorge Gorbe Moya
10ada4ae73
[SandboxVectorizer] Use sbvec-passes flag to create a pipeline of Region passes after BottomUpVec. (#111223)
The main change is that the main SandboxVectorizer pass no longer has a
pipeline of function passes. Now it is a wrapper that creates sandbox IR
from functions before calling BottomUpVec.

BottomUpVec now builds its own RegionPassManager from the `sbvec-passes`
flag, using a PassRegistry.def file. For now, these region passes are
not run (BottomUpVec doesn't create Regions yet), and only a null pass
for testing exists.

This commit also changes the ownership model for sandboxir::PassManager:
instead of having a PassRegistry that owns passes, and PassManagers that
contain non-owning pointers to the passes, now PassManager owns (via
unique pointers) the passes it contains.

PassRegistry is now deleted, and the logic to parse and create a pass
pipeline is now in PassManager::setPassPipeline.
2024-10-09 10:37:05 -07:00
vporpo
ed5088a271
[SandboxIR] Switch more Instruction::create() to InsertPosition (#111213)
Also moves CmpInst definitions from Context.cpp to Instruction.cpp
2024-10-04 15:06:55 -07:00
vporpo
c029702f82
[SandboxIR] Switch more Instruction::create() to InsertPosition (#111208) 2024-10-04 14:26:22 -07:00
vporpo
4f3a0959d6
[SandboxIR] Switch more Instruction::create() to InsertPosition (#111187) 2024-10-04 13:30:53 -07:00
vporpo
3a47bf633c
[SandboxIR] Switch more Instruction::create() to InsertPosition (#111080) 2024-10-04 10:54:20 -07:00
vporpo
e1434a8764
[SandboxIR] Switch more Instruction::create() functions to InsertPosition (#111075) 2024-10-03 17:47:36 -07:00
vporpo
635db5ee97
[SandboxIR] Implement InsertPosition (#110730)
This patch implements the InsertPosition class that is used to specify
where an instruction should be placed.

It also switches a couple of create() functions from the old API to the
new one that uses InsertPosition.
2024-10-03 16:23:23 -07:00
Jorge Gorbe Moya
f35c213c81
[SandboxIR] Add RegionPass/RegionPassManager (#110933)
These classes mirror the existing FunctionPass/FunctionPassManager, and
will be used by the sandbox vectorizer to run a pipeline of region
passes.
2024-10-02 16:32:02 -07:00
vporpo
9e85937b83
[SandboxIR][NFC] Rename SandboxIRValues.def to Values.def (#110538) 2024-09-30 11:24:55 -07:00