This is an archive of the discontinued LLVM Phabricator instance.

[SPARC][MC] Fix encoding of backwards BPr branches
ClosedPublic

Authored by koakuma on Feb 14 2023, 7:14 AM.

Details

Summary

Make sure that the upper bits of the offset is placed in bits 20-21 of the instruction word.
This fixes the encoding of backwards (negative offset) BPr branches.

(Previously, the upper two bits of the offset would overwrite parts of the rs1 field, causing it to branch on the wrong register, with the wrong offset)

Diff Detail

Event Timeline

koakuma created this revision.Feb 14 2023, 7:14 AM
koakuma requested review of this revision.Feb 14 2023, 7:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 14 2023, 7:14 AM
jrtc27 added inline comments.Mar 1 2023, 9:01 AM
llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
43–44

This doesn't make sense to me. Shouldn't it be (Value >> 16) & 0x3? The fixup tables are correctly specifying the 2 bits for BE and LE.

llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
67

Isn't this going to create two relocations rather than one? Really there should be a single fixup for the whole thing.

koakuma added inline comments.Mar 4 2023, 1:49 AM
llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
67

Yeah, I tried to merge the _14 and the _2 parts, but the MCFixupKindInfo table does not support split bits.
One thing that I can think of is to just emit the real ELF relocation with the _14 internal type and emit a R_SPARC_NONE instead with the _2 internal type but I'm not really sure if this is okay?

koakuma updated this revision to Diff 502362.Mar 4 2023, 2:14 AM
  • Fix calculation for fixup_sparc_br16_2.
  • Make sure to properly shift fixup values as described in the fixup tables.
  • Only emit one R_SPARC_WDISP16 relocation per one fixup_sparc_br16_14/2 pair.
koakuma updated this revision to Diff 502395.Mar 4 2023, 5:15 PM

Remove stray include statement.

jrtc27 added inline comments.Mar 11 2023, 4:17 PM
llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp
72

This is pretty gross, and wastes space in the result. Please just have a single fixup.

koakuma updated this revision to Diff 504445.Mar 12 2023, 8:52 AM

Merge fixup_sparc_br16_2/fixup_sparc_br16_14 into a single fixup.

jrtc27 added inline comments.Mar 18 2023, 4:02 PM
llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
50

This looks good now

209

Could you please commit this reformatting separately? Combining significant reformatting with functional changes isn't great.

353

This seems unrelated?

llvm/lib/Target/Sparc/MCTargetDesc/SparcFixupKinds.h
15–111

If you're going to reformat this, deindent the namespace too to match every other target, and commit it all separately from the functional change.

llvm/test/MC/Sparc/sparc64-bpr-offset.s
4

for should be from or to

koakuma added inline comments.Mar 18 2023, 4:36 PM
llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
209

Okay, will remove the formatting changes from this patch.

353

I put this because I noticed that the offset/size values from the table is not used at all, so the upper part of the fixup bits aren't placed properly.
(This happens on the old version of the patch with split fixup_sparc_br16_2/fixup_sparc_br16_14, at least)

It's basically just adding what other architectures already do (but for some reason hasn't been done here in the SPARC backend)... should I submit this separately?

koakuma updated this revision to Diff 506341.Mar 18 2023, 6:16 PM

Undo formatting changes and fix typo in test comment.

arsenm accepted this revision.Apr 26 2023, 10:27 AM

Don't know anything about sparc but code seems fine

This revision is now accepted and ready to land.Apr 26 2023, 10:27 AM
This revision was landed with ongoing or failed builds.Apr 26 2023, 3:56 PM
This revision was automatically updated to reflect the committed changes.
vitalybuka reopened this revision.Apr 26 2023, 7:42 PM
vitalybuka added a subscriber: vitalybuka.

https://lab.llvm.org/buildbot/#/builders/85/builds/15963

FAIL: LLVM :: DebugInfo/Sparc/gnu-window-save.ll (44906 of 74125)
******************** TEST 'LLVM :: DebugInfo/Sparc/gnu-window-save.ll' FAILED ********************
Script:
--
: 'RUN: at line 1';   /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/llc -filetype=obj -O0 < /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/test/DebugInfo/Sparc/gnu-window-save.ll -mtriple sparc64-unknown-linux-gnu | /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/llvm-dwarfdump -v - | /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/FileCheck /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/test/DebugInfo/Sparc/gnu-window-save.ll --check-prefix=SPARC64
: 'RUN: at line 2';   /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/llc -filetype=obj -O0 < /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/test/DebugInfo/Sparc/gnu-window-save.ll -mtriple sparc-unknown-linux-gnu   | /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/llvm-dwarfdump -v - | /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/FileCheck /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/test/DebugInfo/Sparc/gnu-window-save.ll --check-prefix=SPARC32
--
Exit Code: 2
Command Output (stderr):
--
/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp:357:15: runtime error: shift exponent 4294967264 is too large for 64-bit type 'uint64_t' (aka 'unsigned long')
    #0 0x562868ff62ff in (anonymous namespace)::ELFSparcAsmBackend::applyFixup(llvm::MCAssembler const&, llvm::MCFixup const&, llvm::MCValue const&, llvm::MutableArrayRef<char>, unsigned long, bool, llvm::MCSubtargetInfo const*) const /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp
    #1 0x562869ff521f in llvm::MCAssembler::layout(llvm::MCAsmLayout&) /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/MC/MCAssembler.cpp:928:22
    #2 0x562869ff5f2d in llvm::MCAssembler::Finish() /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/MC/MCAssembler.cpp:938:3
    #3 0x56286a02453e in llvm::MCELFStreamer::finishImpl() /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/MC/MCELFStreamer.cpp:718:27
    #4 0x5628694200a5 in llvm::AsmPrinter::doFinalization(llvm::Module&) /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:2397:16
    #5 0x562869efba12 in llvm::FPPassManager::doFinalization(llvm::Module&) /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1499:41
    #6 0x562869ef1c8a in runOnModule /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1586:41
    #7 0x562869ef1c8a in llvm::legacy::PassManagerImpl::run(llvm::Module&) /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:44
    #8 0x5628672cf06c in compileModule(char**, llvm::LLVMContext&) /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/tools/llc/llc.cpp:756:8
    #9 0x5628672cc89b in main /b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm-project/llvm/tools/llc/llc.cpp:420:22
    #10 0x7fafb5c2350f  (/lib/x86_64-linux-gnu/libc.so.6+0x2350f) (BuildId: d1704d25fbbb72fa95d517b883131828c0883fe9)
    #11 0x7fafb5c235c8 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x235c8) (BuildId: d1704d25fbbb72fa95d517b883131828c0883fe9)
    #12 0x5628672a02e4 in _start (/b/sanitizer-x86_64-linux-bootstrap-ubsan/build/llvm_build_ubsan/bin/llc+0x5d892e4)
This revision is now accepted and ready to land.Apr 26 2023, 7:42 PM
koakuma updated this revision to Diff 517557.Apr 27 2023, 8:03 AM

Remove the offset shift calculation. We already took care of proper placement of BPr offset value through other means, so this is not needed anymore.

This should also takes care of the sanitizer errors.

This revision was landed with ongoing or failed builds.May 4 2023, 8:19 PM
This revision was automatically updated to reflect the committed changes.