for (HBasicBlock* block : loop_info->GetBlocks()) { // Check whether one of the successor is loop exit. for (HBasicBlock* successor : block->GetSuccessors()) { if (!loop_info->Contains(*successor)) {
analysis_results->exits_num_++;
// We track number of invariant loop exits which correspond to HIf instruction and // can be eliminated by loop peeling; other control flow instruction are ignored and will // not cause loop peeling to happen as they either cannot be inside a loop, or by // definition cannot be loop exits (unconditional instructions), or are not beneficial for // the optimization.
HIf* hif = block->GetLastInstruction()->AsIfOrNull(); if (hif != nullptr && !loop_info->Contains(*hif->InputAt(0)->GetBlock())) {
analysis_results->invariant_exits_num_++;
}
}
}
bool LoopAnalysis::MakesScalarPeelingUnrollingNonBeneficial(HInstruction* instruction, const CodeGenerator& codegen) { return instruction->IsNewArray() || instruction->IsNewInstance() ||
instruction->IsUnresolvedInstanceFieldGet() ||
instruction->IsUnresolvedInstanceFieldSet() || instruction->IsUnresolvedStaticFieldGet() ||
instruction->IsUnresolvedStaticFieldSet() || // Support loops with intrinsified invokes. Treat non-intrinsics and unimplemented // intrinsics as non-beneficial.
(instruction->IsInvoke() && !(instruction->AsInvoke()->IsIntrinsic() &&
codegen.IsIntrinsicCallFree(instruction->AsInvoke())));
}
// Default implementation of loop helper; used for all targets unless a custom implementation // is provided. Enables scalar loop peeling and unrolling with the most conservative heuristics. class ArchDefaultLoopHelper : public ArchNoOptsLoopHelper { public: explicit ArchDefaultLoopHelper(const CodeGenerator& codegen) : ArchNoOptsLoopHelper(codegen) {} // Scalar loop unrolling parameters and heuristics. // // Maximum possible unrolling factor. static constexpr uint32_t kScalarMaxUnrollFactor = 2; // Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled. static constexpr uint32_t kScalarHeuristicMaxBodySizeInstr = 17; // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled. static constexpr uint32_t kScalarHeuristicMaxBodySizeBlocks = 6; // Maximum number of instructions to be created as a result of full unrolling. static constexpr uint32_t kScalarHeuristicFullyUnrolledMaxInstrThreshold = 35;
// Custom implementation of loop helper for arm64 target. Enables heuristics for scalar loop // peeling and unrolling and supports SIMD loop unrolling. class Arm64LoopHelper : public ArchDefaultLoopHelper { public: explicit Arm64LoopHelper(const CodeGenerator& codegen) : ArchDefaultLoopHelper(codegen) {} // SIMD loop unrolling parameters and heuristics. // // Maximum possible unrolling factor. static constexpr uint32_t kArm64SimdMaxUnrollFactor = 8; // Loop's maximum instruction count. Loops with higher count will not be unrolled. static constexpr uint32_t kArm64SimdHeuristicMaxBodySizeInstr = 50;
// Loop's maximum instruction count. Loops with higher count will not be peeled/unrolled. static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeInstr = 40; // Loop's maximum basic block count. Loops with higher count will not be peeled/unrolled. static constexpr uint32_t kArm64ScalarHeuristicMaxBodySizeBlocks = 8;
uint32_t GetSIMDUnrollingFactor(HBasicBlock* block,
int64_t trip_count,
uint32_t max_peel,
uint32_t vector_length) const override { // Don't unroll with insufficient iterations. // TODO: Unroll loops with unknown trip count.
DCHECK_NE(vector_length, 0u); // TODO: Unroll loops in predicated vectorization mode. if (codegen_.SupportsPredicatedSIMD()) { return LoopAnalysisInfo::kNoUnrollingFactor;
} if (trip_count < (2 * vector_length + max_peel)) { return LoopAnalysisInfo::kNoUnrollingFactor;
} // Don't unroll for large loop body size.
uint32_t instruction_count = block->GetInstructions().CountSize(); if (instruction_count >= kArm64SimdHeuristicMaxBodySizeInstr) { return LoopAnalysisInfo::kNoUnrollingFactor;
} // Find a beneficial unroll factor with the following restrictions: // - At least one iteration of the transformed loop should be executed. // - The loop body shouldn't be "too big" (heuristic).
uint32_t uf1 = kArm64SimdHeuristicMaxBodySizeInstr / instruction_count; // Do the calculation in int64_t to get the right result, and bring it down to `UINT32_MAX` if // it's bigger than that. Note that we are going to do a std::min with `unroll_cnt` below // anyway (which is uint_32t).
int64_t unroll_factor_64 = (trip_count - max_peel) / vector_length;
uint32_t uf2 = (unroll_factor_64 > UINT32_MAX) ? UINT32_MAX : unroll_factor_64;
uint32_t unroll_factor =
TruncToPowerOfTwo(std::min({uf1, uf2, kArm64SimdMaxUnrollFactor}));
DCHECK_GE(unroll_factor, 1u); return unroll_factor;
}
};
// Custom implementation of loop helper for X86_64 target. Enables heuristics for scalar loop // peeling and unrolling and supports SIMD loop unrolling. class X86_64LoopHelper : public ArchDefaultLoopHelper { // mapping of machine instruction count for most used IR instructions // Few IRs generate different number of instructions based on input and result type. // We checked top java apps, benchmarks and used the most generated instruction count.
uint32_t GetMachineInstructionCount(HInstruction* inst) const { switch (inst->GetKind()) { case HInstruction::InstructionKind::kAbs: return3; case HInstruction::InstructionKind::kAdd: return1; case HInstruction::InstructionKind::kAnd: return1; case HInstruction::InstructionKind::kArrayLength: return1; case HInstruction::InstructionKind::kArrayGet: return1; case HInstruction::InstructionKind::kArraySet: return1; case HInstruction::InstructionKind::kBoundsCheck: return2; case HInstruction::InstructionKind::kCheckCast: return9; case HInstruction::InstructionKind::kDiv: return8; case HInstruction::InstructionKind::kDivZeroCheck: return2; case HInstruction::InstructionKind::kEqual: return3; case HInstruction::InstructionKind::kGreaterThan: return3; case HInstruction::InstructionKind::kGreaterThanOrEqual: return3; case HInstruction::InstructionKind::kIf: return2; case HInstruction::InstructionKind::kInstanceFieldGet: return2; case HInstruction::InstructionKind::kInstanceFieldSet: return1; case HInstruction::InstructionKind::kLessThan: return3; case HInstruction::InstructionKind::kLessThanOrEqual: return3; case HInstruction::InstructionKind::kMax: return2; case HInstruction::InstructionKind::kMin: return2; case HInstruction::InstructionKind::kMul: return1; case HInstruction::InstructionKind::kNotEqual: return3; case HInstruction::InstructionKind::kOr: return1; case HInstruction::InstructionKind::kRem: return11; case HInstruction::InstructionKind::kSelect: return2; case HInstruction::InstructionKind::kShl: return1; case HInstruction::InstructionKind::kShr: return1; case HInstruction::InstructionKind::kSub: return1; case HInstruction::InstructionKind::kTypeConversion: return1; case HInstruction::InstructionKind::kUShr: return1; case HInstruction::InstructionKind::kVecReplicateScalar: return2; case HInstruction::InstructionKind::kVecExtractScalar: return1; case HInstruction::InstructionKind::kVecReduce: return4; case HInstruction::InstructionKind::kVecNeg: return2; case HInstruction::InstructionKind::kVecAbs: return4; case HInstruction::InstructionKind::kVecNot: return3; case HInstruction::InstructionKind::kVecAdd: return1; case HInstruction::InstructionKind::kVecSub: return1; case HInstruction::InstructionKind::kVecMul: return1; case HInstruction::InstructionKind::kVecDiv: return1; case HInstruction::InstructionKind::kVecMax: return1; case HInstruction::InstructionKind::kVecMin: return1; case HInstruction::InstructionKind::kVecOr: return1; case HInstruction::InstructionKind::kVecXor: return1; case HInstruction::InstructionKind::kVecShl: return1; case HInstruction::InstructionKind::kVecShr: return1; case HInstruction::InstructionKind::kVecLoad: return1; case HInstruction::InstructionKind::kVecStore: return1; case HInstruction::InstructionKind::kXor: return1; default: return1;
}
}
// Maximum possible unrolling factor. static constexpr uint32_t kX86_64MaxUnrollFactor = 2; // pow(2,2) = 4
// According to Intel® 64 and IA-32 Architectures Optimization Reference Manual, // avoid excessive loop unrolling to ensure LSD (loop stream decoder) is operating efficiently. // This variable takes care that unrolled loop instructions should not exceed LSD size. // For Intel Atom processors (silvermont & goldmont), LSD size is 28 // TODO - identify architecture and LSD size at runtime static constexpr uint32_t kX86_64UnrolledMaxBodySizeInstr = 28;
// Loop's maximum basic block count. Loops with higher count will not be partial // unrolled (unknown iterations). static constexpr uint32_t kX86_64UnknownIterMaxBodySizeBlocks = 2;
if ((trip_count == 0) || (trip_count == LoopAnalysisInfo::kUnknownTripCount)) { // Don't unroll for large loop body size.
unroll_factor = GetUnrollingFactor(loop_info, header); if (unroll_factor <= 1) { return LoopAnalysisInfo::kNoUnrollingFactor;
}
} else { // Don't unroll with insufficient iterations. if (trip_count < (2 * vector_length + max_peel)) { return LoopAnalysisInfo::kNoUnrollingFactor;
}
// Don't unroll for large loop body size.
uint32_t unroll_cnt = GetUnrollingFactor(loop_info, header); if (unroll_cnt <= 1) { return LoopAnalysisInfo::kNoUnrollingFactor;
}
// Find a beneficial unroll factor with the following restrictions: // - At least one iteration of the transformed loop should be executed. // - The loop body shouldn't be "too big" (heuristic). // Do the calculation in int64_t to get the right result, and bring it down to `UINT32_MAX` if // it's bigger than that. Note that we are going to do a std::min with `unroll_cnt` below // anyway (which is uint_32t).
// SuspendCheck inside loop is handled with Goto. // Ignoring SuspendCheck & Goto as partially unrolled loop body will have only one Goto. // Instruction count for Goto is being handled during unroll factor calculation below. if (inst->IsSuspendCheck() || inst->IsGoto()) { continue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.