Commit Graph

251 Commits

Author SHA1 Message Date
Fangrui Song 89fae41ef1 [IR] llvm::Optional => std::optional
Many llvm/IR/* files have been migrated by other contributors.
This migrates most remaining files.
2022-12-05 04:13:11 +00:00
Fangrui Song bac974278c CodeGen/CommandFlags: Convert Optional to std::optional 2022-12-03 18:38:12 +00:00
Kazu Hirata b4482f7ca0 [tools] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02 21:11:40 -08:00
Matt Arsenault e748db0f7f Support: Convert Program APIs to std::optional 2022-12-01 17:00:44 -05:00
Matt Arsenault 4ecdbf2e4e llvm-reduce: Fix tsan failures
There's a data race on the UninterestingChunks set. The code seems to
be operating on the assumption that all the tasks completed, so ensure
the unused results do complete. This started showing up about 50% of
the time when running operands-skip-parallel.ll after the recent
switch to use DenseSet; previously it failed much less frequently with
std::set.

We should introduce a mechanism to early terminate unused
results. Alternatively, I've been thinking about ways to to make the
reduction order smarter. I frequently have tests that take multiple
minutes to compile and hit the failure. It may be helpful to see which
chunks took the least time and prefer those over just taking the first
result.
2022-12-01 14:41:21 -05:00
Matt Arsenault d939eeb9db llvm-reduce: Add some missing consts 2022-11-29 10:08:02 -05:00
Matt Arsenault 8e3e218a5f llvm-reduce: Fix producing invalid reductions on ifunc 2022-11-27 12:41:29 -05:00
Abhina Sreeskantharajan 625f08da73 [SystemZ][z/OS] Set text flag correctly for llvm-reduce tool
This patch sets the text flag correctly for llvm-reduce files

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D138304
2022-11-18 13:39:34 -05:00
Florian Hahn 5b6575d50e
[llvm-reduce] Do not crash when accessing landingpads of invokes.
Unconditionally removing landing pads results in invalid IR,
if there is a different `invoke` that uses it. Update the code
to only remove the landing pad if the current invoke is the only
user. Also carefully avoid creating plain branches to bbs with
landing pads we couldn't remove.

Reviewed By: arsenm, aeubanks

Differential Revision: https://reviews.llvm.org/D138072
2022-11-18 15:19:50 +00:00
Matt Arsenault ea48d251c5 llvm-reduce: Minor code cleanups 2022-11-11 14:38:19 -08:00
Matt Arsenault 89b7391632 llvm-reduce: Use DenseSet 2022-11-11 14:33:17 -08:00
Matt Arsenault 8ca7ed7f93 llvm-reduce: Report number of new chunks 2022-11-11 13:00:56 -08:00
Matt Arsenault 4638ba7b45 llvm-reduce: Try to turn calls into something else
Try to turn calls that look like operators into known
intrinsics. Also try to turn calls that look like a load or
a store into a load or store.
2022-11-07 21:04:21 -08:00
Matt Arsenault 412c4a8be5 llvm-reduce: Write bitcode temporary files for bitcode inputs
Most tools accept .ll or .bc inputs interchangably, but some don't.
Default to writing temporary files that match the input. This
will also aid reducing deserialization bugs.
2022-10-31 20:35:08 -07:00
Matt Arsenault 4e21bc0c40 llvm-reduce: Drop guessing output format based on file extension 2022-10-31 20:35:08 -07:00
Matt Arsenault 3c436ab0d4 llvm-reduce: Support emitting bitcode for final result
Previously, this unconditionally emitted text IR. I ran
into a bug that manifested in broken disassembly, so the
desired output was the bitcode format. If the input format
was binary bitcode, the requested output file ends in .bc,
or an explicit -output-bitcode option was used, emit bitcode.
2022-10-31 20:35:08 -07:00
Matt Arsenault 4cf5e22d20 llvm-reduce: Report file opening errors
This was also trying to write the bitcode to the failed file
on failure, which asserts. Also, consistently use
ToolOutputFile, instead of one path manually removing
the temp file.
2022-10-31 18:37:33 -07:00
Matt Arsenault 756ee72a84 llvm-reduce: Fix typo 2022-10-28 21:42:57 -07:00
Matt Arsenault f02bc70c7d llvm-reduce: Fix producing invalid reductions with landingpads
It's not valid to simply branch to a landingpad block, so it
needs to be removed.

Also stop trying to scan forward to find a block that can be merged.
The predecessor merge rules are more complex than this. This also
would need to have considered landingpads. Just do the minimum
to delete the block, and let the simplify-cfg reduction handle
the branch chain cleanups.
2022-10-28 17:07:26 -07:00
Matt Arsenault 45a91c1521 llvm-reduce: Fix block reduction with unreachable blocks
Previously this would produce many invalid reductions with
"Instruction does not dominate uses" verifier errors.

This fixes issues in cases where the incoming IR
has unreachable blocks, and the resulting reduction
introduced new reachable blocks.

Have basic-blocks skip functions that have unreachable
blocks, Introduce a separate reduction which only
deletes unreachable blocks. Cleanup any newly unreachable
blocks after trimming out the requested deletions.

Includes a variety of meta-reduced tests for llvm-reduce
itself with -abort-on-invalid-reduction that were failing
on different iterations of this patch.

Bugpoint's implementation is much simpler (but currently I don't
understand how it avoids disconnecting interesting blocks from the CFG).
2022-10-28 17:07:26 -07:00
Matt Arsenault bc405e37ee llvm-reduce: Don't turn switches into returns
Re-use one of the existing successors as the new default.
This helps with a future patch to fix handling of unreachable
blocks.
2022-10-28 17:07:26 -07:00
Matt Arsenault f041204ec8 llvm-reduce: Stop checking workitem is interesting before each pass
Each delta pass run should have guaranteed the output is still
interesting, so it should be pointless to recheck this each
iteration. I have many issues that take multiple minutes
to reproduce, so this ends up being a huge waste of time.

Also, remove broken line counting. This never worked, since
getLines was failing to open the temporary file which was just
deleted.
2022-10-28 16:39:09 -07:00
Matt Arsenault 11ed12f99f llvm-reduce: Remove pointless template arguments 2022-10-24 22:32:31 -07:00
Matt Arsenault 08d1c43c70 llvm-reduce: Add conditional reduction passes
Copy this technique from bugpoint. Before trying to blindly
delete blocks, try to fold branch conditions. This intuitively
makes more sense for a faster reduction, since you can find
dead paths in the function to prune out before trying to bisect
blocks in source order.

Seems to provide some speedup on my multi-hour reduction samples.

This does have the potential to produce testcases with unreachable
blocks. This is already a problem with the existing block
reduction pass. I'm struggling dealing with invalid reductions
in these cases, so in the future this should probably start
deleting those. However, I do sometimes try to reduce failures
in code that becomes unreachable, so I'm not totally sure
what to do here.
2022-10-23 15:39:39 -07:00
Matt Arsenault 3e6f7ab867 llvm-reduce: Fix opcode reduction leaving behind dead instructions
ce3c3cb291 broke this by
speculatively making transforms before checking shouldKeep.
Originally I tried to roll back changes to the IR, but it's probably
best to not touch it before querying.
2022-10-23 15:37:03 -07:00
Matt Arsenault 827f01c275 llvm-reduce: Remove okToRemove logic in block reduction
This was making decisions based on BBsToDelete, while being
used to determine BBsToDelete which doesn't really work.
Additionally, this is a lot of logic just to avoid deleting
the entry block when we can just skip it.
2022-10-23 15:16:55 -07:00
Matt Arsenault 83da1a6a3f llvm-reduce: Add a reduction to replace atomics with non-atomics
Make load and store non-atomic. Make the others monotonic.

We could probably try to incrementally relax the orderings; not sure
how useful that would be.
2022-10-23 15:16:55 -07:00
Matt Arsenault b1e1719905 llvm-reduce: Add atomic syncscope reduction 2022-10-23 15:16:55 -07:00
Matt Arsenault 596fdf75d9 llvm-reduce: Add volatile reduction pass
Removing volatile may help optimization passes do more to the IR. However,
this will increase scheduler freedom.
2022-10-23 15:16:55 -07:00
Matt Arsenault 27902eea0f llvm-reduce: Add flag reduction pass
Try to remove each flag from instructions. It may make more
sense to introduce these flags instead.
2022-10-23 15:16:54 -07:00
Arthur Eubanks 0fbb261536 [llvm-reduce] Attempt to strip debug info
I often run llvm-reduce on IR that contains debug info, this prevents an
extra step of `opt -passes=strip` I do every time and will result in a
lot less invalid reductions around debug metadata.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D136208
2022-10-21 09:11:58 -07:00
Arthur Eubanks ce3c3cb291 [llvm-reduce] Check if reduction fails/is redundant before invoking oracle
So we don't over count the number of chunks and do unnecessary work reducing more chunks than exist.

This lowers some random reduction I tested with locally from 250s to 232s.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D136127
2022-10-18 08:43:56 -07:00
Arthur Eubanks 2592ccdea7 [llvm-reduce] Unify pass logging
We randomly use outs() or errs(), which makes test logs confusing.
We also randomly add/don't add a line afterward.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D136130
2022-10-18 08:42:37 -07:00
Arthur Eubanks 29cf7dffb8 Revert "llvm-reduce: Color output of child processes"
This reverts commit 86e9181ded.

Seems to be causing bot failures: https://lab.llvm.org/buildbot#builders/5/builds/28313

Plus as noted on D135632 it seems to interact badly with parallel reduction.
2022-10-17 14:37:24 -07:00
Arthur Eubanks 6ae63c90ce [llvm-reduce] Reduce comdats
Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D136054
2022-10-17 10:50:45 -07:00
Kazu Hirata 988025adc7 [llvm-reduce] Fix a warning
This patch fixes:

  llvm/tools/llvm-reduce/deltas/ReduceOpcodes.cpp:96:21: warning:
  unused variable ‘Op’ [-Wunused-variable]
2022-10-15 12:28:11 -07:00
Matt Arsenault 86e9181ded llvm-reduce: Color output of child processes
When reducing llvm-reduce with llvm-reduce, it can be confusing
to figure out what lines are printed by the child or parent. Not
sure this is the most reliable way to set and restore this.
2022-10-12 18:00:46 -07:00
Matt Arsenault 573a5de755 llvm-reduce: Add opcode reduction pass
Try some dumb strength reductions to "simpler" opcodes.
Make some opcode substitutions I typically try to get smaller
MIR out of codegen. This is a bit target specific and I have a
lot of increasingly target specific modifications I try
during manual reduction.
2022-10-12 17:34:03 -07:00
Matt Arsenault c23ac22f0e llvm-reduce: Don't write out IR to score IR complexity
In a testcase I'm working on, the old write out and count IR lines
was taking about 200-300ms per iteration. This drops it out of the
profile.

This doesn't account for everything, but it doesn't seem to matter.
We should probably try to account for metadata and constantexpr tree
depths.
2022-10-12 17:25:23 -07:00
Matt Arsenault c3bc72ccb2 llvm-reduce: Improve delta pass flag handling
Verify all the requested passes exist before trying to run any.
For long reductions, it was really annoying for it to get halfway through
and then I come back later to an incomplete reduction.

Also add a new skip-delta-passes flag. Most of the time I want to opt out
of specific reductions, rather than run a select few.
2022-10-12 17:25:23 -07:00
Arthur Eubanks 47b1623b1f [llvm-reduce] Fail verifier less when removing debug metadata
Without this patch, we hit the following a lot:
"llvm.dbg.declare intrinsic requires a !dbg attachment"
"DICompileUnit not listed in llvm.dbg.cu"

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D135492
2022-10-07 16:22:13 -07:00
Matt Arsenault 7721cba2ee llvm-reduce: Fix another invalid reduction with repeated input phis
ReduceOperandsSkip had the same issue as ReduceOperands when handling
phis with repeated predecessors.
2022-10-07 13:15:15 -07:00
Matt Arsenault 0a159427ad llvm-reduce: Fix invalid reduction for phis with repeat inputs
Phis have a quirk where the same predecessor block may appear multiple times
if the same block branches to it multiple ways. All the values need to match,
but this was replacing each operand independently. If an operand can be simplified,
make sure to replace every instance of the incoming block's value.
2022-10-07 13:15:15 -07:00
Matt Arsenault 3a25b21c4e llvm-reduce: Fix missing C++ mode comments 2022-10-07 13:15:14 -07:00
Matthew Voss fe50eac85c [llvm-reduce] Fix di-metadata pass test failures
We're seeing intermittent failures in upstream bots. See:

https://lab.llvm.org/buildbot/#/builders/139/builds/29185
https://lab.llvm.org/buildbot/#/builders/238/builds/295

This appears to be due to the unstable iteration order of DenseSet.
Since we're trying to reduce a tree, it makes sense to attempt
reductions from the top down.

This also addresses post-review comments from @MatzeB.

Differential Revision: https://reviews.llvm.org/D135473
2022-10-07 12:05:25 -07:00
Nikita Popov b56ef7519f [llvm-reduce] Fix unused variable warning (NFC) 2022-10-07 11:08:25 +02:00
Matthew Voss 2c799b7793 [llvm-reduce] Add pass that reduces DebugInfo metadata
This new pass for llvm-reduce attempts to reduce DebugInfo metadata.
The process used is:
  1. Scan every MD node, keeping track of nodes already visited.
  2. Look for DebugInfo nodes, then record any operands that are lists.
  3. Bisect though all the elements of the collected lists.

Differential Revision: https://reviews.llvm.org/D132077
2022-10-06 14:24:39 -07:00
Ellis Hoag aa065c016b Revert "[llvm-reduce] Remove debug metadata elements"
This reverts commit 69549de865.

The change in D135237 can lead to verification failures like `scope must have two or three operands`.
The ongoing work in D132077 does something similar without these failures, so lets wait for that to land and revert this patch.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D135395
2022-10-06 13:40:03 -07:00
Ellis Hoag 69549de865 [llvm-reduce] Remove debug metadata elements
There can be lots of `MDTuple` debug metadata nodes. For example, `globals: !{!1, !2}` in `!DICompileUnit()`. Search through all debug info to find `MDTuple`'s and remove some of their elements.

For D135114 I was able to get a reproducer with 364 lines without manually deleting elements. After this patch I got it down to 67 lines.

Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D135237
2022-10-06 09:28:55 -07:00
Matthias Braun a946eb160a ReduceOperands: Do not crash on vector of pointer types
Avoid crash in `reduceOperandsOneDeltaPass` function for operands with
vector of pointer type.

While on it add a `reduce-operands-ptr.ll` test in the spirit of the
existing `reduce-operands-int.ll`/`reduce-operands-fp.ll` tests.

Differential Revision: https://reviews.llvm.org/D135307
2022-10-05 15:04:32 -07:00