llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Scheduler] Recent improvements lead to compile time bloat

Open bcardosolopes opened this issue 10 years ago • 16 comments

Bugzilla Link 26940
Resolution FIXED
Resolved on Apr 01, 2016 09:25
Version trunk
OS All
CC @atrick,@bcardosolopes,@chandlerc,@hfinkel,@MatzeB,@JonPsson,@Weverything

Extended Description

Commit r259673 (“[ScheduleDAGInstrs::buildSchedGraph()] Handling of memory dependecies rewritten”) introduced a huge slowdown in an internal benchmark:

=== Before r259673

$ clang -arch arm64 -Os -c t.mi real 1389.12s user 1314.71s => 22min sys 14.92s

$ clang -arch x86_64 -Os -c t.mi real 76.90s user 75.46s => 1min15s sys 0.92s

=== After r259673

real 10045.62s user 9131.03s => 3h20min sys 95.60s

real 2217.71s user 2174.12s => 36min23s sys 33.07s

ARM64 is a lot worse than x86, but compile time for both are pretty bad! Note that before r259673 arm64 time was already bad (22min) but got even worse after. Gonna try to reduce the testcase / provide a testcase with the same reproducible slowdown factor.

bcardosolopes avatar Mar 14 '16 20:03 bcardosolopes

Well, I am not sure if that is a "random knob", considering that the scheduler must have a cut-off mechanism somewhere since there is no theoretical limit to the sizes of regions.

But I think you have a point in that this value should be found after the data structures have been reworked, per your suggestions. Hopefully, the 1000 limit could be good enough then :-)

JonPsson avatar Apr 01 '16 16:04 JonPsson

Err, if we can solve this problem without tweaking random knobs in the compiler that nobody will ever quite tune (IE by fixing data structures), we should do that, rather than finding good values for those knobs. :)

llvmbot avatar Apr 01 '16 16:04 llvmbot

Nobody checked setting -dag-maps-huge-region to 100 / 200 as initial value? Per my previous compile times, the test case that went from 2.5 min to 7.7 sec was further reduced to 3.4 sec with a value of 200...

JonPsson avatar Apr 01 '16 09:04 JonPsson

Created attachment 16098 [details] Bugfix in buildSchedGraph() to make -dag-maps-huge-region work properly

I found that the reduction of the maps did in fact never happen in this test case. This was because all the stores / loads were made with addresses from arguments and they thus became "unknown" stores / loads. Fixed by removing continue statements and making sure that the test for reduction always takes place.

Before this fix compile time was 2m28s, which then became 7.7s. For smaller values of -dag-maps-huge-region (default is 1000): 200 3.4s 100 3.2s 50 3.1s 10 3.05s

I would like to propose also to set the default value to 100, or so.

Furthermore, it seems that the SUnit::Preds / Succs should be a set instead of a vector, to enable faster check for overlap and removal. Has anyone tried this?

I was just about to point these out. These should be sets or small dense maps.
->overlaps is just checking a few fields, and latency is being used for sort ordering.

This would be trivial to make into an ordered set or unordered_multimap.

llvmbot avatar Apr 01 '16 01:04 llvmbot

Fixed in r265063.

bcardosolopes avatar Apr 01 '16 01:04 bcardosolopes

For the results on our internal testcase, we got it down to ~5min, which is much better than what we had before your patch, thanks Jonas.

bcardosolopes avatar Apr 01 '16 01:04 bcardosolopes

We've hit this hard, I've mailed your patch out in http://reviews.llvm.org/D18673 Jonas, thanks for that!

chandlerc avatar Mar 31 '16 22:03 chandlerc

Thanks!

Andy, please review this small patch, and also let me know about the default value for the reduction threshold.

/Jonas

JonPsson avatar Mar 31 '16 09:03 JonPsson

Jonas,

I tested your patch. For the examples I've come across, this patch has brought back the pre-r259673 compile times.

Richard

Weverything avatar Mar 31 '16 02:03 Weverything

Bugfix in buildSchedGraph() to make -dag-maps-huge-region work properly I found that the reduction of the maps did in fact never happen in this test case. This was because all the stores / loads were made with addresses from arguments and they thus became "unknown" stores / loads. Fixed by removing continue statements and making sure that the test for reduction always takes place.

Before this fix compile time was 2m28s, which then became 7.7s. For smaller values of -dag-maps-huge-region (default is 1000): 200 3.4s 100 3.2s 50 3.1s 10 3.05s

I would like to propose also to set the default value to 100, or so.

Furthermore, it seems that the SUnit::Preds / Succs should be a set instead of a vector, to enable faster check for overlap and removal. Has anyone tried this?

Please try the patch and let me know what you think,

Jonas

JonPsson avatar Mar 26 '16 11:03 JonPsson

That's awesome, thanks for the reduced testcase!

bcardosolopes avatar Mar 15 '16 01:03 bcardosolopes

I have also ran into this issue and managed to get a reduced example (also attached).

array.cc: void ProcessArr1(const float *in, float *out) { out[0] = in[1] + in[2] + in[3]; ... repeat line 2000 times ... }

int main(int argc, char *argv[]) { float arr[10]; float out[10]; ProcessArr1(arr, out); return 0; }

clang array.cc -O2

Prior to r259673, this would compile in ~2 seconds. After, it would take ~40 seconds. I did not see any effect from modifying the -dag-maps-huge-region value.

Weverything avatar Mar 15 '16 01:03 Weverything

Take a look at the inverted call tree:

Running Time Self (ms) Symbol Name 1935910.0ms 100.0% 1935910.0 llvm::SUnit::addPred(llvm::SDep const&, bool) 1935601.0ms 99.9% 0.0 llvm::ScheduleDAGInstrs::addChainDependency(llvm::SUnit*, llvm::SUnit*, unsigned int) 1935601.0ms 99.9% 0.0 llvm::ScheduleDAGInstrs::buildSchedGraph(llvm::AAResults*, llvm::RegPressureTracker*, llvm::PressureDiffs*, llvm::LiveIntervals*, bool) 1935601.0ms 99.9% 0.0 llvm::ScheduleDAGMILive::buildDAGWithRegPressure() 1935601.0ms 99.9% 0.0 llvm::ScheduleDAGMILive::schedule() 1935601.0ms 99.9% 0.0 (anonymous namespace)::MachineSchedulerBase::scheduleRegions(llvm::ScheduleDAGInstrs&, bool) 1935601.0ms 99.9% 0.0 (anonymous namespace)::MachineScheduler::runOnMachineFunction(llvm::MachineFunction&) 1935601.0ms 99.9% 0.0 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) 1935601.0ms 99.9% 0.0 llvm::FPPassManager::runOnFunction(llvm::Function&) 1935601.0ms 99.9% 0.0 llvm::FPPassManager::runOnModule(llvm::Module&) 1935601.0ms 99.9% 0.0 llvm::legacy::PassManagerImpl::run(llvm::Module&) 1935601.0ms 99.9% 0.0 main

One can spot a quadratic behaviour in addPred when there's an overlap:

bool SUnit::addPred(const SDep &D, bool Required) { // If this node already has this dependence, don't add a redundant one. for (SmallVectorImpl::iterator I = Preds.begin(), E = Preds.end(); I != E; ++I) { // Zero-latency weak edges may be added purely for heuristic ordering. Don't // add them if another kind of edge already exists. if (!Required && I->getSUnit() == D.getSUnit()) return false; if (I->overlaps(D)) { // Extend the latency if needed. Equivalent to removePred(I) + addPred(D). if (I->getLatency() < D.getLatency()) { SUnit *PredSU = I->getSUnit(); // Find the corresponding successor in N. SDep ForwardD = *I; ForwardD.setSUnit(this); for (SmallVectorImpl::iterator II = PredSU->Succs.begin(), EE = PredSU->Succs.end(); II != EE; ++II) { if (*II == ForwardD) { II->setLatency(D.getLatency()); break; } } I->setLatency(D.getLatency()); } return false; } } ...

bcardosolopes avatar Mar 15 '16 01:03 bcardosolopes

Giving the -dag-maps-huge-region flag a try, did use -dag-maps-huge-region=10 but it didn't help much mitigating the issue, it only decreases compile time by 2min:

real 160m20.168s user 150m1.280s sys 1m30.513s

bcardosolopes avatar Mar 14 '16 20:03 bcardosolopes

assigned to @JonPsson

bcardosolopes avatar Mar 14 '16 20:03 bcardosolopes