Skip to content

Commit

Permalink
Tweak bounds check elimination (#42692)
Browse files Browse the repository at this point in the history
* Add GVN and move IRCE to allow more functions to vectorize
* Canonicalize arraysize(Vector, 1) to arraylen(Vector)

Co-authored-by: Prem Chintalapudi <premc@csail.mit.edu>
Co-authored-by: Valentin Churavy <vchuravy@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 18, 2021
1 parent e68c047 commit f0c46b3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/aotcompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,6 @@ void addMachinePasses(legacy::PassManagerBase *PM, TargetMachine *TM, int optlev
PM->add(createGVNPass());
}



// this defines the set of optimization passes defined for Julia at various optimization levels.
// it assumes that the TLI and TTI wrapper passes have already been added.
void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
Expand Down Expand Up @@ -729,7 +727,7 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
PM->add(createLoopUnswitchPass());
PM->add(createLICMPass());
PM->add(createJuliaLICMPass());
PM->add(createInductiveRangeCheckEliminationPass());
PM->add(createInductiveRangeCheckEliminationPass()); // Must come before indvars
// Subsequent passes not stripping metadata from terminator
PM->add(createInstSimplifyLegacyPass());
PM->add(createLoopIdiomPass());
Expand All @@ -749,13 +747,21 @@ void addOptimizationPasses(legacy::PassManagerBase *PM, int opt_level,
PM->add(createMemCpyOptPass());
PM->add(createSCCPPass());

//These next two passes must come before IRCE to eliminate the bounds check in #43308
PM->add(createCorrelatedValuePropagationPass());
PM->add(createDeadCodeEliminationPass());

PM->add(createInductiveRangeCheckEliminationPass()); // Must come between the two GVN passes

// Run instcombine after redundancy elimination to exploit opportunities
// opened up by them.
// This needs to be InstCombine instead of InstSimplify to allow
// loops over Union-typed arrays to vectorize.
PM->add(createInstructionCombiningPass());
PM->add(createJumpThreadingPass());
PM->add(createCorrelatedValuePropagationPass());
if (opt_level >= 3) {
PM->add(createGVNPass()); // Must come after JumpThreading and before LoopVectorize
}
PM->add(createDeadStoreEliminationPass());

// More dead allocation (store) deletion before loop optimization
Expand Down
9 changes: 9 additions & 0 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2401,13 +2401,22 @@ static intptr_t arraytype_maxsize(jl_value_t *ty)
return INTPTR_MAX / elsz;
}

static Value *emit_arraylen(jl_codectx_t &ctx, const jl_cgval_t &tinfo);

static Value *emit_arraysize(jl_codectx_t &ctx, const jl_cgval_t &tinfo, Value *dim)
{
size_t ndim;
MDNode *tbaa = tbaa_arraysize;
if (arraytype_constdim(tinfo.typ, &ndim)) {
if (ndim == 0)
return ConstantInt::get(T_size, 1);
if (ndim == 1) {
if (auto d = dyn_cast<ConstantInt>(dim)) {
if (d->getZExtValue() == 1) {
return emit_arraylen(ctx, tinfo);
}
}
}
if (ndim > 1) {
if (tinfo.constant && isa<ConstantInt>(dim)) {
auto n = cast<ConstantInt>(dim)->getZExtValue() - 1;
Expand Down

4 comments on commit f0c46b3

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible new issues were detected. A full report can be found here.

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your package evaluation job has completed - possible issues were detected. A full report can be found here.

Please sign in to comment.