Repository navigation
Roll-up of tweaks and enhancements made to the core compiler dialects. - #7312
Conversation
06278e2 to
3127872
Compare
3127872 to
bedc63b
Compare
bedc63b to
e648cd6
Compare
|
(the failure here is #7323) |
| Returns the set of results that may be tied to operands as an | ||
| (index, length) pair ala getODSResultIndexAndLength. |
There was a problem hiding this comment.
Just dropping a comment to point out that I didn't really understand what "tied" ops were until I looked above this at the comment for IREE::Util::TiedOpInterface. The examples there are really useful :D
| return; | ||
| } | ||
| p << "{"; | ||
| llvm::interleaveComma( |
There was a problem hiding this comment.
TIL. Nice util method :D
... then below this, if (i < resultTypes.size() - 1) p << ", "; is still needed :P
| return true; | ||
| } else { | ||
| // See if block the value is defined in dominates the forOp block. | ||
| // TODO(benvanik): optimize this, it's terribly expensive to recompute. |
There was a problem hiding this comment.
Not for this in particular, but have you profiled the compiler with your set of changes? I'm curious about the general performance and if the changes uncovered any slow paths in MLIR core.
There was a problem hiding this comment.
not yet - I'm sure that some of these passes need to be split up so they can parallelize better but it's hard to get a feel for just how bad something is until you have real IR flowing through. My plan before doing the flag flip to the new streams path (that uses these quite a bit) is to ensure the time comes down to ~match the old path (it'll definitely be slower because it's running more passes, but it shouldn't be an order of magnitude slower).
| return didChange; | ||
| } | ||
|
|
||
| class FoldGlobalsPass |
There was a problem hiding this comment.
This pass looks great - lots of nice optimizations that I expect we'll see having use in real progarms, with how much globals are used in the programs we've seen so far. It also could report some statistics like https://github.com/google/iree/blob/df4e156da5b575e2721e9f363d3f56cd761cdf1d/iree/compiler/Dialect/Flow/Transforms/DeduplicateExecutables.cpp#L305-L314
Ideas:
- Original number of globals
- Number of globals [inlined, erased, deduplicated]
- Number of globals changed from immutable to mutable
I'm also happy to add those myself to sate my own curiosity :)
There was a problem hiding this comment.
that'd be great! lots of the folding/fusing could use those stats. I still don't use the stats because they get compiled out - if we made all our builds have them then I'd be more excited to add them!
| // Propagate constant into the initial value. Note that there may have been | ||
| // a previous initial value that is being replaced. | ||
| global.op.initial_valueAttr(constantValue); |
There was a problem hiding this comment.
Is this correct in the case of a load happening before the first store?
initial value: 5
load value -> 5
store value: 6
store value: 6
load value -> 6
or does that fail to match since the initial value is checked?
Maybe the comment is just confusing me - the previous "initial value" resolves to the same numeric value but has a different MLIR SSA value?
There was a problem hiding this comment.
good catch - originally this did have that bug (probably when I wrote the comment) - then I added the L134 initial value for constantValue - so now this only kicks in if there's either no default initializer or all stores match that initializer. I still think it may not be right though, as an uninitialized value is defaulted to 0 so the load before store could still observe 0. Currently we don't rely on that though and it's just a safety thing to avoid uninitialized memory. A proper DFA using the DFX stuff would solve all these issues as it would propagate the value at specific times around the program. A single DFA pass for optimizing globals could replace nearly all these passes (SimplifyGlobalAccess and FoldGlobals for sure). I'll fix the comment and add a TODO.
| // Deduplicates immutable globals with constant initial values. | ||
| // This is a simplified and safer version of the global fusion pass. | ||
| static bool deduplicateConstantGlobals(GlobalTable &globalTable) { |
There was a problem hiding this comment.
Another case where our test models with weights removed may have quite different behavior from real models (after optimizations).
Have we thought about grouping passes like this into IREE-specific -O0, -O2, etc. optimization levels? There's probably no substitute for real models in benchmarking, but some grouping for debugging might be helpful.
There was a problem hiding this comment.
the only solution is to move away from improper emulation and use proxies that match the behavior. This would be things like #util.random that behave as if the data is actually unique and would have all the proper behavior with mapping/staging/etc. Splatting out uniform values is just not the right way to simulate things.
| for (auto i : llvm::seq<unsigned>(0, numElems / 2)) { | ||
| constexpr float kEpsilon = std::numeric_limits<float>::epsilon(); | ||
| constexpr float kTwoPi = 2.0 * M_PI; | ||
| constexpr float kTwoPi = static_cast<float>(2.0 * M_PI); |
There was a problem hiding this comment.
thanks for the warnings fixes, they were bugging me too :P
| def Util_CmpEQOp : Util_PureOp<"cmp.eq", [ | ||
| AllTypesMatch<["lhs", "rhs"]>, | ||
| Commutative, | ||
| ]> { | ||
| let summary = [{compares two values for equality}]; | ||
| let description = [{ | ||
| Compares two operands for equality. This is intended for comparing IREE | ||
| reference types (like !util.byte_buffer) that cannot be used with std.cmpi. | ||
| }]; |
There was a problem hiding this comment.
how do you feel about cmp.ref.eq to disambiguate even further?
then no need for
return failure(); // not used for non-ref types currentlyThere was a problem hiding this comment.
I may want to use it for more in the future - especially now that stuff is getting microsharded to different dialects. For example, list comparison, pointer value comparison, etc.
| // TODO(#7277): kill nearly all tensor related mappings from HAL when using | ||
| // streams. Only tensors that get turned into buffer views on boundaries should |
There was a problem hiding this comment.
+1
I tried to spot which ops should be allowed to use tensor types after flow in #6883 and found many places that were using them still
There was a problem hiding this comment.
totally missed that PR! yeah, we don't want allowlists like that on intermediate dialects meant to compose with other user-defined dialects - we only want denylists on dialects we should have removed (like ensuring there are no tensor.* ops remaining as we should have converted them all), but even that is difficult to do in the case of dialects that may have their own requirements - if something needs a tensor.dim and throws a do_not_optimize on it then it's going to live to HAL turns it into a buffer_view.dim, and that's totally fine.
Future changes will move tensor size calculations out of HAL and this makes that easier to remove by hiding the implementations.
The stream dialect heavily uses size-aware types and tied ops and exposed some assumptions around the tie storage that needn't be the case.
This doesn't trigger today but does in the new streams dialect that performs more sophisticated casts.
…n't. IREE relies on the use-def chain being complete to model all potential hazards.
This allows the inlineRegion helper to be used.
This allows us to run it on util.initializer.
Running this early on after initializers are materialized allows for better folding/CSE/etc as many initializers end up doing similar things.
Today only branches/blocks are optimized but the intent is that we'll do function/call/return optimization in the same way. (these predate the explorer but could be changed to use that in the future to work more generically)
This does a lot of heavily lifting for simplification of globals. The flow dialect today benefits from this after shape dimensions are expanded, but the stream dialect does several types of global expansion and leans on this to make most of the globals fold away. (this predates the explorer but could be changed to use that in the future to work more generically)
There's no way outside of vm to compare a ref type to null today. This can be extended to support more types but is kept limited now for this use case.
Shape aware ops can return dynamic shape dimensions without materializing any shape ops. This avoids quite a bit of shape dialect IR during dynamic tensor handling and avoids the need to rely on shape dialect canonicalizers throughout the pipelines.
This finds globals that are correlated across the program (always set to the same values) and fuses them together. This dramatically reduces the global count for things like dynamic shape dimensions or resources sizes/timepoints/subviews (as seen in the stream dialect).
There's only one place it's valid to have this op - before the flow dialect - so being in util is just a footgun. This move is mandatory in the streams branch as the current shady HAL handling of this op is all dead code.
These allow for efficient emission and canonicalization of ranges of offsets/lengths.
This allows stream formation to be disabled for when the stream dialect is added.
8ea233e to
fcfac91
Compare
Following up on #7312 (comment), this adds more [statistics](https://mlir.llvm.org/docs/PassManagement/#pass-statistics) to IREE's compiler passes. I personally build with the CMake option `-DLLVM_FORCE_ENABLE_STATS=ON` so the `-pass-statistics` flag works in `RelWithDebInfo` builds, but we don't yet have anything like that configured on our CI or recommended in our developer documentation. See also #6161 (comment) for how this could be useful. Sample output on [bert_encoder_unrolled_fake_weights.mlir](https://github.com/google/iree/blob/main/iree/test/e2e/models/bert_encoder_unrolled_fake_weights.mlir) (fake weights are folded aggressively 😛): ``` mlir::iree_compiler::IREE::Util::`anonymous-namespace'::FoldGlobalsPass (S) 1113 global ops before folding - Number of util.global ops before folding (S) 7 global ops after folding - Number of util.global ops after folding ```
These are standalone updates made to the util dialect as part of the streams work. For context of where these are used see the full branch... or use imagination.