Repository navigation
Conversation
|
Azure Pipelines: Successfully started running 6 pipeline(s). 9 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
|
/azp run Fuzzlyn, runtime-coreclr outerloop, runtime-coreclr libraries-jitstress |
|
Azure Pipelines: Successfully started running 3 pipeline(s). |
There was a problem hiding this comment.
🟡 Changes recommended
The new branch-folding analysis lacks targeted tests exercising global assertion propagation across signed/unsigned comparisons, masked shifts, exception preservation, and PHI facts including loop backedges.
1 open issue
What changed in this PR
Adds LLVM-derived KnownBits analysis to RyuJIT, complementing range analysis with bit-pattern facts to fold integer comparisons during global assertion propagation. This advances #129276 without introducing DemandedBits or the separate long-comparison narrowing work.
Changes:
- Adds known-bit transfer functions and value-number analysis using bounds, assertions, and PHI inputs.
- Integrates comparison folding and assertion generation for long comparisons.
- Registers the new sources and documents LLVM attribution.
| File | Description |
|---|---|
| THIRD-PARTY-NOTICES.TXT | Adds the LLVM KnownBits license notice. |
| src/coreclr/jit/knownbitsanalysis.h | Declares the analysis interface and query limits. |
| src/coreclr/jit/knownbitsanalysis.cpp | Implements value-number, bounds, assertion, and PHI analysis. |
| src/coreclr/jit/knownbits.h | Adds single-word integer helpers and known-bit transfer functions. |
| src/coreclr/jit/compiler.h | Adds storage for the PHI analysis cache. |
| src/coreclr/jit/CMakeLists.txt | Registers the new source and headers. |
| src/coreclr/jit/assertionprop.cpp | Integrates folding, long-comparison assertions, and cache lifecycle. |
Review effort: Balanced
37e51da to
d6ab839
Compare
|
/azp run Fuzzlyn, runtime-coreclr outerloop, runtime-coreclr libraries-jitstress |
|
Azure Pipelines: Successfully started running 3 pipeline(s). |
|
PTAL @AndyAyersMS cc @jakobbotsch @dotnet/jit-contrib Diffs - my initial prototype had -1.2MB diffs, but I decided to simplify it & make some changes to keep the TP regression sane. I filed a separate PR to improve TP by 0.5% to mitigate regressions in this PR. The PR is big, but it isn't too complex. It consists of 3 things:
I plan a couple of follow-ups to benefit from known bits in more places, e.g., it's very profitable for removing checked contexts or redundant casts. |
AndyAyersMS
left a comment
There was a problem hiding this comment.
Overall this looks good to me.
Is there some way to stress test this? Hard to tell how much coverage we're getting since we're intentionally limiting calls to to control TP.
Maybe two dimensions of stress, one that relaxes depth, the other that just calls this more often to try an optimize things?
| if ((genActualType(vnStore->TypeOfVN(op1VN)) != TYP_INT) || (genActualType(vnStore->TypeOfVN(op2VN)) != TYP_INT)) | ||
| const var_types op1Type = genActualType(vnStore->TypeOfVN(op1VN)); | ||
| const var_types op2Type = genActualType(vnStore->TypeOfVN(op2VN)); | ||
| if ((op1Type == TYP_LONG) && (op2Type == TYP_LONG) && !isEqualityRelop) |
There was a problem hiding this comment.
Do these new assertions cause other assertions to get kicked out? I thought we had metrics here but I only see them for local AP.
There was a problem hiding this comment.
Yes, global AP caps the table at 512 so it can happen. Added GlobalAssertionCount/GlobalAssertionOverflow metrics so we can track it in SPMI.
| result.resetAll(); | ||
| } | ||
|
|
||
| cache->Set(vn, result, KnownBitsPhiCache::Overwrite); |
There was a problem hiding this comment.
We don't expect to actually overwrite here, do we?
There was a problem hiding this comment.
It overwrites the "being computed" marker we set before visiting the PHI args. Added a comment.
| KnownBits amount = Compute(funcApp.GetArg(1), 32, assertions); | ||
| #ifdef TARGET_64BIT | ||
| // The shift amount is masked. | ||
| amount = amount & KnownBits::makeConstant(APInt(32, width - 1)); |
There was a problem hiding this comment.
Not sure I understand this, shift amount is masked on 32 bit targets too
There was a problem hiding this comment.
Right, switched to TARGET_MASKS_SHIFTS, arm32 is the only one that does not mask.
| // Arguments: | ||
| // vn - the value number | ||
| // width - the bit width of the value (32 or 64) | ||
| // assertions - the assertions that hold for "vn", can be uninit |
There was a problem hiding this comment.
Since you apply assertions to "otherVN" below, seems like this is understating what these represent.
There was a problem hiding this comment.
Fair, reworded the comment.
| // Stand-in for the std::optional<bool> results of llvm::KnownBits's comparisons. | ||
| struct KnownBool | ||
| { | ||
| bool HasValue; | ||
| bool Value; | ||
|
|
||
| KnownBool() | ||
| : HasValue(false) | ||
| , Value(false) | ||
| { | ||
| } | ||
| KnownBool(bool Value) | ||
| : HasValue(true) | ||
| , Value(Value) | ||
| { | ||
| } | ||
| }; |
There was a problem hiding this comment.
This could probably be unified with RelopEvaluationResult
There was a problem hiding this comment.
Done, switched to RelopEvaluationResult.
| // The known bits of PHIs, see KnownBitsAnalysis::ComputeForPhi. | ||
| struct KnownBitsPhiCache : public JitHashTable<ValueNum, JitSmallPrimitiveKeyFuncs<ValueNum>, KnownBits> | ||
| { | ||
| KnownBitsPhiCache(CompAllocator alloc) | ||
| : JitHashTable(alloc) | ||
| { | ||
| } | ||
| }; |
There was a problem hiding this comment.
Any reason for this instead of a typedef? We use typedefs everywhere else.
There was a problem hiding this comment.
Done, it is a typedef now.
| case VNF_RSH: | ||
| case VNF_RSZ: | ||
| { | ||
| KnownBits amount = Compute(funcApp.GetArg(1), 32, assertions); |
There was a problem hiding this comment.
I don't think it's guaranteed that the shift amount is TYP_INT (CIL allows native int shift amount). Can that cause a problem here?
There was a problem hiding this comment.
Good catch. It now computes the amount at its own width and truncates it.
| BitVecOps::Iter iter(m_compiler->apTraits, assertions); | ||
| unsigned index = 0; | ||
| while (iter.NextElem(&index)) |
There was a problem hiding this comment.
BitVecOps::VisitBits is more efficient than BitVecOps::Iter.
There was a problem hiding this comment.
@jakobbotsch, do we want to sweep the rest of Iter usages? I can give it a try :)
There was a problem hiding this comment.
Sure, that sounds like a good change.
One thing we may want to experiment with for TP purposes is rewriting BitVecOps::VisitBits into a shape that calls the functor only once, to facilitate inlining even large-looking callbacks.
There was a problem hiding this comment.
I wanted to have for (const AssertionDsc& dsc : everywhere, but couldn't do that without a 0.2/0.3 TP regression :\
- Use RelopEvaluationResult instead of KnownBool - Use a typedef for the PHI cache - Handle native int shift amounts, use TARGET_MASKS_SHIFTS - Use BitVecOps::VisitBits in MergeAssertions - Add STRESS_KNOWNBITS_BUDGET and STRESS_KNOWNBITS_RELOPS stress modes - Add GlobalAssertionCount/GlobalAssertionOverflow metrics Co-authored-by: Copilot <[email protected]> Copilot-Session: acaabbe4-a3ed-45ef-838d-d23718212f6e
|
Added two stress modes: STRESS_KNOWNBITS_BUDGET (larger budget and PHI depth, also looks through 32-bit PHIs) and STRESS_KNOWNBITS_RELOPS (analyzes all EQ/NE relops). |
|
@jakobbotsch @AndyAyersMS I've addressed the feedback, anything else? I want to use known-bits in a few more places to unlock the full diffs 🙂 |
| // Not to be inlined (LTCG does that): this is called from a hot path of the assertion | ||
| // propagation where it rarely has anything to do, having it inlined there costs TP. | ||
| NOINLINE RelopEvaluationResult EvalRelop(GenTreeOp* relop, ASSERT_VALARG_TP assertions); |
There was a problem hiding this comment.
How big of a TP cost..? I would suggest we leave those decisions up to the native compiler unless it is really egregious. Since the TP runs don't have PGO it is also hard to make an informed decision.
There was a problem hiding this comment.
@jakobbotsch That is from my early attempts to keep the TP regression sane, it's currently quite small (~+0.25%) so I'm removing it.
Co-authored-by: Copilot <[email protected]> Copilot-Session: acaabbe4-a3ed-45ef-838d-d23718212f6e

Contributes to #129276.
Adds a port of LLVM's
KnownBitsto RyuJIT and uses it in global assertion prop to foldX relop CNS.What's in
knownbits.h: single-word (up to 64 bits) port of LLVM'sAPInt/KnownBitsand their transfer functions. LLVM's names and structure are kept to make comparing with the original easy (Apache-2.0 WITH LLVM-exception, THIRD-PARTY-NOTICES.TXT updated).KnownBitsAnalysis: computes the known bits (and signed/unsigned bounds) of a VN from:AND,OR,XOR,NOT,NEG,ADD,SUB,MUL,DIV,MOD,UDIV,UMOD,LSH,RSH,RSZ, including checkedADD/SUB/MUL(as nsw/nuw)X relop CNSandX relop YassertionsX relop CNS(EQ/NE/LT/LE/GT/GE, signed and unsigned,intandlong).X relop CNSassertions forlongcompares too.Examples
Not in this PR
x % 8->x & 7, alignment fixups, removing redundant masks/extensions,x & C->0, unsigned forms,abs.longcompares toint(JIT: narrow masked 64-bit equality comparisons when the comparand uses bit 31 #134380).Diffs - my initial prototype had -1.2MB diffs, but I decided to simplify it & make some changes to keep the TP regression sane. I filed a separate PR to improve TP by 0.5% to mitigate regressions in this PR.
The PR is big, but it isn't too complex. It consists of 3 things:
I plan a couple of follow-ups to benefit from known bits in more places, e.g., it's very profitable for removing checked contexts or redundant casts. https://gist.github.com/EgorBo/5d2ddcb93f0dc34cf69ba6ab5e57b7e2
libclrjit.dll size increase
+18,944 bytes(but it reduces a lot more from r2r)