This is an archive of the discontinued LLVM Phabricator instance.

[SDAG] Preserve unpredictable metadata, teach X86CmovConversion to respect this metadata
ClosedPublic

Authored by xbolva00 on Jan 25 2022, 3:13 AM.

Details

Summary

Sometimes an developer would like to have more control over cmov vs branch. We have unpredictable metadata in LLVM IR, but currently it is ignored by X86 backend. Propagate this metadata and avoid cmov->branch conversion in X86CmovConversion for cmov with this metadata.

Example:

int MaxIndex(int n, int *a) {
    int t = 0;
    for (int i = 1; i < n; i++) {
        // cmov is converted to branch by X86CmovConversion
        if (a[i] > a[t]) t = i;
    }
    return t;
}

int MaxIndex2(int n, int *a) {
    int t = 0;
    for (int i = 1; i < n; i++) {
        // cmov is preserved
        if (__builtin_unpredictable(a[i] > a[t])) t = i;
    }
    return t;
}

Diff Detail

Event Timeline

xbolva00 created this revision.Jan 25 2022, 3:13 AM
xbolva00 requested review of this revision.Jan 25 2022, 3:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 25 2022, 3:13 AM
xbolva00 added a comment.EditedJan 25 2022, 3:15 AM

Not super familiar in this area, to be noted.

I also plan to update release notes if the patch is accepted.

xbolva00 edited the summary of this revision. (Show Details)Jan 25 2022, 3:15 AM
nikic added inline comments.Jan 25 2022, 3:41 AM
llvm/include/llvm/CodeGen/MachineInstr.h
124

This will increase MI size by 8 bytes.

xbolva00 added inline comments.Jan 25 2022, 3:54 AM
llvm/include/llvm/CodeGen/MachineInstr.h
124

Yes, but what we can do?

Is it a big problem in practise?

xbolva00 added inline comments.Jan 25 2022, 4:54 AM
llvm/include/llvm/CodeGen/MachineInstr.h
123–124

Do we need *uint32* for NumOperands?

@xbolva00 Are you specifically aiming to get this in for 14.x? TBH this close to the branch date, I'd prefer to delay this work until the branch is made and initial cherrypicks are out of the way, and we aim for 15.x

We've tried similar things to this in the past and its taken a long time to get it stable (usually by disabling it a lot of it).

@xbolva00 Are you specifically aiming to get this in for 14.x? TBH this close to the branch date, I'd prefer to delay this work until the branch is made and initial cherrypicks are out of the way, and we aim for 15.x

We've tried similar things to this in the past and its taken a long time to get it stable (usually by disabling it a lot of it).

No problem, not aiming for 14.x. We can delay it.

Matt added a subscriber: Matt.Jan 28 2022, 5:09 AM

@xbolva00 Are you still working on this atm or are you waiting to see what happens with https://discourse.llvm.org/t/rfc-cmov-vs-branch-optimization/59628 ?

llvm/include/llvm/CodeGen/MachineInstr.h
84

I don't suppose there's any existing MIFlag bits that we can get rid of?

@xbolva00 Are you still working on this atm or are you waiting to see what happens with https://discourse.llvm.org/t/rfc-cmov-vs-branch-optimization/59628 ?

Yes, I am still interested in this patch.

That RFC would benefit from this work as well, especially if they go for midend LLVM IR pass - but I think that pass could be super tricky to do it right and actually do not regress the performance for "important workloads" (I remember some huge regression around LLVM 9 related to cmov vs branch).

craig.topper added inline comments.Feb 6 2022, 11:59 AM
llvm/include/llvm/CodeGen/MachineInstr.h
84

I wondered if we could have separate X86 CMOV opcodes for unpredictable? Since we're only trying to convey information to this one X86 specific pass. But that might require too many updates to the CMOV related functions in X86InstrInfo.

xbolva00 added inline comments.Feb 6 2022, 12:04 PM
llvm/include/llvm/CodeGen/MachineInstr.h
123–124
RKSimon added inline comments.Feb 7 2022, 5:32 AM
llvm/include/llvm/CodeGen/MachineInstr.h
123–124

That might be possible - maybe investigate with a suitable check inside MachineInstr::addOperand?

RKSimon added inline comments.Feb 13 2022, 3:10 AM
llvm/include/llvm/CodeGen/MachineInstr.h
1893

Have you been able to ensure none of the callers of these functions implicitly truncate back to u16?

llvm/include/llvm/CodeGen/SelectionDAGNodes.h
398

Missing comment description?

This comment was removed by xbolva00.
Herald added a project: Restricted Project. · View Herald TranscriptMar 9 2022, 11:36 AM
xbolva00 added inline comments.May 18 2023, 12:51 AM
llvm/include/llvm/CodeGen/MachineInstr.h
123–124

Now done.

124

Last time I tried I was not able to observe ani significant memory usage regressions.
I reduced MI size by switching NumOperands to 16bits so I can get use these saved bits for Flags.

xbolva00 updated this revision to Diff 523363.May 18 2023, 7:00 AM

Rebased and updated callsites.

khei4 added a subscriber: khei4.May 18 2023, 11:28 PM
nikic added inline comments.May 20 2023, 9:50 AM
llvm/lib/Target/X86/X86CmovConversion.cpp
312

Is it safe to just skip it entirely, or do we need to treat it as a non-cmov instruction?

llvm/lib/Target/X86/X86ISelLowering.cpp
26486

Is it possible to directly pass the flags here instead? Seems odd to use FlagInserter for a single instruction.

xbolva00 added inline comments.May 22 2023, 7:20 AM
llvm/lib/Target/X86/X86CmovConversion.cpp
312

Good point, I am not sure, probably we should threat it that way.

@RKSimon / @craig.topper any suggestions?

llvm/lib/Target/X86/X86ISelLowering.cpp
26486

Right, I will fix it.

craig.topper added inline comments.May 22 2023, 11:02 AM
llvm/include/llvm/CodeGen/MachineInstr.h
115

I kind of think this description is overly short. Unpredictable for an instruction could mean many things.

124

Use uint32_t since we've very carefully counted bits in all of the fields.

craig.topper added inline comments.May 22 2023, 11:08 AM
llvm/lib/Target/X86/X86CmovConversion.cpp
309

unpredictable*

312

What instructions can this flag appear on? There's a check later for instructions that define EFLAGS we can't skip that check.

xbolva00 updated this revision to Diff 527036.May 31 2023, 7:33 AM

Addressed review feedback

xbolva00 marked an inline comment as done.May 31 2023, 7:35 AM
xbolva00 added inline comments.
llvm/include/llvm/CodeGen/MachineInstr.h
115

Changed, feel free to suggest better description.

llvm/lib/Target/X86/X86CmovConversion.cpp
312

Select -> CMOV.

Okay, fixed and EFLAGS check is not skipped.

craig.topper added inline comments.May 31 2023, 10:21 AM
llvm/lib/CodeGen/MIRPrinter.cpp
805

This needs to be added to MIRParser.cpp, MIRLexer.h and MIRLexer.cpp

xbolva00 updated this revision to Diff 527165.May 31 2023, 12:44 PM

Addressed review comments.

xbolva00 marked an inline comment as done.May 31 2023, 12:45 PM
xbolva00 added inline comments.
llvm/lib/CodeGen/MIRPrinter.cpp
805

Thanks

xbolva00 marked an inline comment as done.May 31 2023, 12:46 PM
nikic accepted this revision.Jun 1 2023, 6:03 AM

LGTM

llvm/include/llvm/CodeGen/SelectionDAGNodes.h
398

Not done.

This revision is now accepted and ready to land.Jun 1 2023, 6:03 AM
xbolva00 updated this revision to Diff 527547.Jun 1 2023, 11:54 AM

Added missing comment.

xbolva00 marked 2 inline comments as done.Jun 1 2023, 11:54 AM
This revision was landed with ongoing or failed builds.Jun 1 2023, 11:56 AM
This revision was automatically updated to reflect the committed changes.