This is an archive of the discontinued LLVM Phabricator instance.

[TableGen][SubtargetEmitter] Add the StartAtCycles field in the WriteRes class.
ClosedPublic

Authored by fpetrogalli on May 10 2023, 2:28 PM.

Details

Summary

Conditions that need to be met:

  1. count(StartAtCycles) == count(ReservedCycles);
  2. For each i: StartAtCycles[i] < ReservedCycles[i];
  3. If left unspecified, the elements are set to 0.

Diff Detail

Event Timeline

fpetrogalli created this revision.May 10 2023, 2:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2023, 2:28 PM
fpetrogalli requested review of this revision.May 10 2023, 2:28 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2023, 2:28 PM

Please refer to the documentation in https://reviews.llvm.org/D150312.

andreadb accepted this revision.May 12 2023, 4:14 AM

Hi Francesco,

Thanks for contributing this patch!

In the past, I always wanted to add the ability to model "delayed resource consumption". However, I never got the time to actually do it.
On x86, we could use StartAtCycle to fix scheduling information for horizontal operations. We could also use it for improving instructions with folded loads (for which, we use read-advance entries).

In the past, the lack of a proper mechanism for describing delayed resource consumption was identified as the main reason why sometimes llvm-mca produced poor quality perf reports.
I remember I discussed this issue with Andrew Trick a couple of times.
Eventually, he suggested that we implemented something very similar to your StartAtCycle (see https://lists.llvm.org/pipermail/llvm-dev/2020-May/141487.html).
I believe that this (and the rest of your patch set) will fix issue: https://github.com/llvm/llvm-project/issues/45218

In future, more work will be needed to teach llvm-mca and exegesis how to correctly handle StartAtCycle information (if available).
However, that would be a separate task :).

Long story short: your approach looks good to me and it similar to what was suggested in the past by others.

Patch looks good to me (modulo a few minor nits - see below).

Cheers,
-Andrea

llvm/include/llvm/MC/MCSchedule.h
62

This code comment should describe (in a sentence or two) what is StartAtCycle used for.

llvm/tools/llvm-exegesis/lib/SchedClassResolution.cpp
86

Is there a ticket for this?

llvm/utils/TableGen/SubtargetEmitter.cpp
1196–1197

Should we also check for negative values? Not that I expect people to use negative delay cycles.

This revision is now accepted and ready to land.May 12 2023, 4:14 AM
RKSimon added inline comments.May 12 2023, 5:07 AM
llvm/utils/TableGen/SubtargetEmitter.cpp
1327–1328

Updated generated comment: {ProcResourceIdx, Cycles, StartCycles}

RKSimon added inline comments.May 12 2023, 5:17 AM
llvm/tools/llvm-exegesis/lib/SchedClassResolution.cpp
86

I've raised these to discuss future support for llvm-exegesis and llvm-mca
https://github.com/llvm/llvm-project/issues/62680
https://github.com/llvm/llvm-project/issues/62681

RKSimon added inline comments.May 12 2023, 7:26 AM
llvm/utils/TableGen/SubtargetEmitter.cpp
1221

Comment indentation looks wrong?

fpetrogalli marked an inline comment as done.

Address first round of review. Thank you @RKSimon and @andreadb.

fpetrogalli marked 5 inline comments as done.May 12 2023, 9:10 AM
RKSimon added inline comments.May 12 2023, 9:50 AM
llvm/utils/TableGen/SubtargetEmitter.cpp
1166

!size ?

Fix typos. [NFC]

fpetrogalli marked an inline comment as done.May 15 2023, 1:32 AM
This revision was landed with ongoing or failed builds.May 15 2023, 1:54 AM
This revision was automatically updated to reflect the committed changes.
fhahn added a subscriber: fhahn.May 15 2023, 5:34 AM

This patch is causing multiple warnings when building with assertions.

[3147/3173] Building CXX object unittests/tools/llvm-exegesis/CMakeFiles/LLVMExegesisTests.dir/X86/SchedClassResolutionTest.cpp.o
...//llvm-project/llvm/unittests/tools/llvm-exegesis/X86/SchedClassResolutionTest.cpp:68:70: warning: missing field 'StartAtCycle' initializer [-Wmissing-field-initializers]
      computeIdealizedProcResPressure(STI.getSchedModel(), {{P0Idx, 2}});

It would be great if you could take a look. If it takes longer to fix, please revert the commit in the meantime.

This patch is causing multiple warnings when building with assertions.

[3147/3173] Building CXX object unittests/tools/llvm-exegesis/CMakeFiles/LLVMExegesisTests.dir/X86/SchedClassResolutionTest.cpp.o
...//llvm-project/llvm/unittests/tools/llvm-exegesis/X86/SchedClassResolutionTest.cpp:68:70: warning: missing field 'StartAtCycle' initializer [-Wmissing-field-initializers]
      computeIdealizedProcResPressure(STI.getSchedModel(), {{P0Idx, 2}});

It would be great if you could take a look. If it takes longer to fix, please revert the commit in the meantime.

Thank you for the heads up @fhahn - I have created a fix at https://reviews.llvm.org/D150569

Francesco

fhahn added inline comments.May 15 2023, 6:42 AM
llvm/include/llvm/MC/MCSchedule.h
66

This should use /// for doxygen comment?

michaelmaitland added inline comments.
llvm/utils/TableGen/SubtargetEmitter.cpp
1198

Do we need this check?

I am trying to model the following case:

I have two resources, ResA and ResB. I'd like it to be the case that ResA models the first of a two stage resource where each stage takes one cycle. When the first stage is not being used, another instruction can enter into the first stage. We can model this by letting ResA represent the first stage, and setting ResourceCycles=1 for ResA and Latency=2+LatencyFromResB (The 2 represents 1 cycle from the first stage and 1 cycle from the second stage).

I'd like to model that ResB is used for one cycle, only after both stages of the resource have finished. In other words, this is two cycles from the start of issuing the instruction. Using StartAtCycles I write something like this:

let ResourceCycles = [1, 1], StartAtCycles =[0, 2] in
  WriteRes<ResourceName, [ResA, ResB]>;

Here, I run into this FatalError. Am I misunderstanding the semantics of StartAtCycle? Is there a better way to accomplish what I'm after?

llvm/utils/TableGen/SubtargetEmitter.cpp
1198

When I read the comment I now understand:

/// TODO: consider renaming the field `StartAtCycle` and `Cycles` to
/// `AcquireAtCycle` and `ReleaseAtCycle` respectively, to stress the
/// fact that resource allocation is now represented as an interval,
/// relatively to the issue cycle of the instruction.

Maybe it would be a good idea to make this change as it is much easier to understand whats going on when we use this naming. I will prepare a patch.

fpetrogalli marked 2 inline comments as done.Aug 23 2023, 6:16 AM
fpetrogalli added inline comments.
llvm/utils/TableGen/SubtargetEmitter.cpp
1198

@michaelmaitland - thank you for addressing the TODO in https://reviews.llvm.org/D158568