This is an archive of the discontinued LLVM Phabricator instance.

[X86] Fix encoding for ATOMIC_LOGIC_OP
ClosedPublic

Authored by n-omer on Mar 13 2023, 6:05 AM.

Details

Summary

In D140939 the following issue was introduced:

multiclass ATOMIC_LOGIC_OP_RM<bits<8> Opc8, string s> {
  let Defs = [EFLAGS], mayLoad = 1, mayStore = 1, isCodeGenOnly = 1,
      SchedRW = [WriteBitTestSetRegRMW]  in {
    def 16rm : Ii8<Opc8, MRMDestMem, (outs), (ins i16mem:$src1, GR16:$src2),           -------> Type Ii8
                  !strconcat(s, "{w}\t{$src2, $src1|$src1, $src2}"),
                  [(set EFLAGS, (!cast<SDNode>("x86_rm_" # s) addr:$src1, GR16:$src2))]>,
               OpSize16, TB, LOCK;
    def 32rm : Ii8<Opc8, MRMDestMem, (outs), (ins i32mem:$src1, GR32:$src2),           -------> Type Ii8
                  !strconcat(s, "{l}\t{$src2, $src1|$src1, $src2}"),
                  [(set EFLAGS, (!cast<SDNode>("x86_rm_" # s) addr:$src1, GR32:$src2))]>,
               OpSize32, TB, LOCK;
    def 64rm : RIi8<Opc8, MRMDestMem, (outs), (ins i64mem:$src1, GR64:$src2),         -------> Type RIi8
                   !strconcat(s, "{q}\t{$src2, $src1|$src1, $src2}"),
                   [(set EFLAGS, (!cast<SDNode>("x86_rm_" # s) addr:$src1, GR64:$src2))]>,
               TB, LOCK;
  }
}

As can be seen above, the ATOMIC_LOGIC_OP_RM definitions use the Ii8/RIi8 encoding, which makes the code emitted by the MC layer incorrect.

The problem can be observed in the test case I've added by looking at the value of the relocation, at the moment the value is off by 1 because of https://github.com/llvm/llvm-project/blob/34de7da6246cdfa6ff6f3d3c514583cddc0a10ec/llvm/lib/Target/X86/MCTargetDesc/X86MCCodeEmitter.cpp#L581. After applying my patch the relocation is as expected.

https://github.com/llvm/llvm-project/issues/61384

This patch fixes the issue and adds a regression test.

Diff Detail

Event Timeline

n-omer created this revision.Mar 13 2023, 6:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 6:05 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
n-omer requested review of this revision.Mar 13 2023, 6:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 6:05 AM

We should probably get this merged into 16.x - please can you raise a bug for tracking?

llvm/lib/Target/X86/X86InstrCompiler.td
899

Copy + paste typo from the ATOMIC_LOGIC_OP above I suppose

llvm/test/CodeGen/X86/atomic-rm-bit-test-64.ll
1663 ↗(On Diff #504615)

Please can you raise a bug and reference it here to speed up triage

n-omer edited the summary of this revision. (Show Details)Mar 13 2023, 6:15 AM
RKSimon edited the summary of this revision. (Show Details)Mar 13 2023, 7:13 AM
n-omer updated this revision to Diff 504706.Mar 13 2023, 9:21 AM

Patch itself LGTM but the summary needs a cleanup - the defm section is irrelevant (and confusing) - just explain that ATOMIC_LOGIC_OP_RM shouldn't be using the *i8 instruction encodings

n-omer edited the summary of this revision. (Show Details)Mar 14 2023, 2:33 AM
RKSimon accepted this revision.Mar 14 2023, 4:20 AM
RKSimon edited the summary of this revision. (Show Details)

LGTM - cheers

This revision is now accepted and ready to land.Mar 14 2023, 4:20 AM
This revision was landed with ongoing or failed builds.Mar 14 2023, 4:41 AM
This revision was automatically updated to reflect the committed changes.