Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions THIRD-PARTY-NOTICES.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -1803,3 +1803,24 @@ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

License notice for LLVM known bits (src/coreclr/jit/knownbits.h)
Comment thread
EgorBo marked this conversation as resolved.
Resolved
-------------------------------

Derived from https://github.com/llvm/llvm-project: llvm/include/llvm/ADT/APInt.h, llvm/lib/Support/APInt.cpp,
llvm/include/llvm/Support/KnownBits.h, llvm/lib/Support/KnownBits.cpp, llvm/lib/IR/ConstantRange.cpp and
llvm/lib/Analysis/ValueTracking.cpp

Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.

Licensed under the Apache License, Version 2.0 (the "License") with LLVM Exceptions;
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://llvm.org/LICENSE.txt

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
3 changes: 3 additions & 0 deletions src/coreclr/jit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ set( JIT_SOURCES
jiteh.cpp
jithashtable.cpp
jitmetadata.cpp
knownbitsanalysis.cpp
layout.cpp
lclmorph.cpp
lclvars.cpp
Expand Down Expand Up @@ -387,6 +388,8 @@ set( JIT_HEADERS
jitmetadatalist.h
jitpch.h
jitstd.h
knownbits.h
knownbitsanalysis.h
lir.h
loopcloning.h
loopcloningopts.h
Expand Down
50 changes: 48 additions & 2 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#include "jitpch.h"
#include "rangecheck.h"
#include "knownbitsanalysis.h"
#ifdef _MSC_VER
#pragma hdrstop
#endif
Expand Down Expand Up @@ -838,6 +839,7 @@ void Compiler::optAssertionTraitsInit(AssertionIndex assertionCount)
void Compiler::optAssertionInit(bool isLocalProp)
{
assert(NO_ASSERTION_INDEX == 0);
optKnownBitsPhiCache = nullptr;
const unsigned maxTrackedLocals = (unsigned)JitConfig.JitMaxLocalsToTrack();

// We initialize differently for local prop / global prop
Expand Down Expand Up @@ -1913,9 +1915,32 @@ AssertionInfo Compiler::optCreateJTrueBoundsAssertion(GenTree* tree)
ValueNum op1VN = relopFuncApp.GetArg(0);
ValueNum op2VN = relopFuncApp.GetArg(1);

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)
Comment thread
EgorBo marked this conversation as resolved.
Resolved
{
// For now, we don't have consumers for assertions derived from non-int32 comparisons
// 64-bit comparisons: we only create "X relop CNS" assertions for these, they are used
// to compute the known bits (and the ranges, if they fit into 32 bits) of 64-bit values.
ssize_t cns;
if (vnStore->IsVNIntegralConstant(op1VN, &cns))
{
std::swap(op1VN, op2VN);
relopFunc = ValueNumStore::SwapRelop(relopFunc);
}

if (vnStore->IsVNIntegralConstant(op2VN, &cns) && (!isUnsignedRelop || (cns > 0)))
{
AssertionDsc dsc = AssertionDsc::CreateConstantBound(this, relopFunc, op1VN, op2VN);
AssertionIndex idx = optAddAssertion(dsc);
optCreateComplementaryAssertion(idx);
return idx;
}
return NO_ASSERTION_INDEX;
}

if ((op1Type != TYP_INT) || (op2Type != TYP_INT))
{
// For now, we don't have consumers for assertions derived from other comparisons
return NO_ASSERTION_INDEX;
}

Expand Down Expand Up @@ -4836,6 +4861,21 @@ GenTree* Compiler::optAssertionPropGlobal_RelOp(ASSERT_VALARG_TP assertions,
}
}

// See if the known bits of "op1" tell us the result of comparing it with a constant.
// This is a hot path, so don't call into the analysis for the relops it can't handle
// anyway (e.g. checks for null or for a handle).
if (op2->IsIntegralConst() && !op2->IsIconHandle() && varTypeIsIntegral(op1))
{
const RelopEvaluationResult result = KnownBitsAnalysis(this).EvalRelop(tree->AsOp(), assertions);
if (result != RelopEvaluationResult::Unknown)
{
JITDUMP("Folding relop [%06u] based on known bits.\n", dspTreeID(tree));
newTree = gtWrapWithSideEffects((result == RelopEvaluationResult::True) ? gtNewTrue() : gtNewFalse(), tree,
GTF_ALL_EFFECT);
return optAssertionProp_Update(newTree, tree, stmt);
}
}

// The rest is for EQ/NE only.
if (!tree->OperIs(GT_EQ, GT_NE))
{
Expand Down Expand Up @@ -6848,6 +6888,9 @@ PhaseStatus Compiler::optAssertionPropMain()
// All assertions are created, size the bit vectors accordingly.
optAssertionTraitsInit(max(optAssertionCount, (AssertionIndex)1));

Metrics.GlobalAssertionCount = optAssertionCount;
Metrics.GlobalAssertionOverflow = optAssertionOverflow;

if (optAssertionCount == 0)
{
// Zero out the bbAssertionIn values, as these can be referenced in RangeCheck::MergeAssertion
Expand Down Expand Up @@ -7022,5 +7065,8 @@ PhaseStatus Compiler::optAssertionPropMain()
optAssertionPropagatedCurrentStmt = false; // clear it back as we are done with stmts.
}

// The cached known bits of PHIs depend on this phase's assertions, don't let them outlive it.
optKnownBitsPhiCache = nullptr;

return madeChanges ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING;
}
8 changes: 7 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ struct JumpThreadInfo; // defined in redundantbranchopts.cpp
class ProfileSynthesis; // defined in profilesynthesis.h
class PerLoopInfo; // defined in inductionvariableopts.cpp
class RangeCheck; // defined in rangecheck.h
struct KnownBits; // defined in knownbits.h
#ifdef TARGET_WASM
class WasmInterval; // defined in fgwasm.h
enum class WasmValueType : unsigned;
Expand Down Expand Up @@ -9398,7 +9399,10 @@ class Compiler
{
return optAssertionCount;
}
ASSERT_TP* bbJtrueAssertionOut;
// The known bits of PHIs, see KnownBitsAnalysis::ComputeForPhi.
typedef JitHashTable<ValueNum, JitSmallPrimitiveKeyFuncs<ValueNum>, KnownBits> KnownBitsPhiCache;
KnownBitsPhiCache* optKnownBitsPhiCache = nullptr;
ASSERT_TP* bbJtrueAssertionOut;

// Assertion prop helpers.
ASSERT_TP& GetAssertionDep(unsigned lclNum, bool mustExist = false);
Expand Down Expand Up @@ -11936,6 +11940,8 @@ class Compiler
STRESS_MODE(POISON_IMPLICIT_BYREFS) \
STRESS_MODE(THREE_OPT_LAYOUT) \
STRESS_MODE(GET_RANGE) /* Force slow SSA-based range walk in GetRange */ \
STRESS_MODE(KNOWNBITS_BUDGET) /* Use larger budgets in KnownBitsAnalysis */ \
STRESS_MODE(KNOWNBITS_RELOPS) /* Let KnownBitsAnalysis try to fold all EQ/NE relops */ \
STRESS_MODE(COUNT)

enum compStressArea
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/jitmetadatalist.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ JITMETADATAMETRIC(NewArrayHelperCalls, int, 0)
JITMETADATAMETRIC(StackAllocatedArrays, int, 0)
JITMETADATAMETRIC(LocalAssertionCount, int, 0)
JITMETADATAMETRIC(LocalAssertionOverflow, int, 0)
JITMETADATAMETRIC(GlobalAssertionCount, int, 0)
JITMETADATAMETRIC(GlobalAssertionOverflow, int, 0)
JITMETADATAMETRIC(MorphTrackedLocals, int, 0)
JITMETADATAMETRIC(MorphLocals, int, 0)
JITMETADATAMETRIC(EnumeratorGDVProvisionalNoEscape, int, 0)
Expand Down
Loading
Loading