This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Teach RISCVMergeBaseOffset about cases where we use SHXADD to add some immediates.
ClosedPublic

Authored by craig.topper on Jun 8 2022, 8:11 PM.

Details

Summary

For an addition with simm14 and simm15 immediates with 2 or 3 trailing bits,
we can use a shXadd instruction and an addi to do the addition.

This patch teaches RISCVMergeBaseOffset to see through this pattern.
I don't think the sh1add case occurs because we use two addis for that,
but I implemented it for completeness.

Diff Detail

Event Timeline

craig.topper created this revision.Jun 8 2022, 8:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 8 2022, 8:11 PM
craig.topper requested review of this revision.Jun 8 2022, 8:11 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 8 2022, 8:11 PM
sunshaoce added inline comments.Jun 8 2022, 10:37 PM
llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll
245

12848 -> 6424

Fix comment in test.

Add assertion that the ADDI immediate is a simm12.

reames accepted this revision.Jun 9 2022, 3:02 PM

LGTM

This revision is now accepted and ready to land.Jun 9 2022, 3:02 PM
This revision was landed with ongoing or failed builds.Jun 9 2022, 4:07 PM
This revision was automatically updated to reflect the committed changes.
hctim added a subscriber: hctim.Jun 13 2022, 10:16 AM

Hi, looks like this patch broke the sanitizer buildbots (the fast builder, testing under ubsan). I can see it easily being missed as the bot was already red:

https://lab.llvm.org/buildbot/#/builders/5/builds/24595

Some instructions about helping reproduce sanitizer bots are here: https://github.com/google/sanitizers/wiki/SanitizerBotReproduceBuild

FAIL: LLVM :: CodeGen/RISCV/hoist-global-addr-base.ll (32134 of 66378)
******************** TEST 'LLVM :: CodeGen/RISCV/hoist-global-addr-base.ll' FAILED ********************
Script:
--
: 'RUN: at line 2';   /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/llc -mtriple=riscv32  < /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll | /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/FileCheck  /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll --check-prefixes=CHECK,RV32,RV32I
: 'RUN: at line 3';   /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/llc -mtriple=riscv64  < /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll | /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/FileCheck  /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll --check-prefixes=CHECK,RV64,RV64I
: 'RUN: at line 4';   /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/llc -mtriple=riscv32 -mattr=+zba < /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll | /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/FileCheck  /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll --check-prefixes=CHECK,RV32,RV32ZBA
: 'RUN: at line 5';   /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/llc -mtriple=riscv64 -mattr=+zba < /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll | /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/FileCheck  /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll --check-prefixes=CHECK,RV64,RV64ZBA
--
Exit Code: 2
Command Output (stderr):
--
/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp:239:30: runtime error: left shift of negative value -2048
    #0 0x5590c6f830fa in matchShiftedOffset /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp:239:30
    #1 0x5590c6f830fa in detectAndFoldOffset /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp:302:10
    #2 0x5590c6f830fa in (anonymous namespace)::RISCVMergeBaseOffsetOpt::runOnMachineFunction(llvm::MachineFunction&) /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp:373:21
    #3 0x5590c7c61f9a in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/CodeGen/MachineFunctionPass.cpp:73:13
    #4 0x5590c8210919 in llvm::FPPassManager::runOnFunction(llvm::Function&) /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1430:27
    #5 0x5590c821940d in llvm::FPPassManager::runOnModule(llvm::Module&) /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1476:16
    #6 0x5590c821130c in runOnModule /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1545:27
    #7 0x5590c821130c in llvm::legacy::PassManagerImpl::run(llvm::Module&) /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:535:44
    #8 0x5590c6055677 in compileModule(char**, llvm::LLVMContext&) /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/tools/llc/llc.cpp:730:8
    #9 0x5590c60530ab in main /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/tools/llc/llc.cpp:417:22
    #10 0x7f50f77ae09a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2409a) (BuildId: eb6a5dd378d22b1e695984462a799cd4c81cdc22)
    #11 0x5590c6027a59 in _start (/b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/llc+0x600ea59)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/lib/Target/RISCV/RISCVMergeBaseOffset.cpp:239:30 in 
FileCheck error: '<stdin>' is empty.
FileCheck command line:  /b/sanitizer-x86_64-linux-fast/build/llvm_build_ubsan/bin/FileCheck /b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/CodeGen/RISCV/hoist-global-addr-base.ll --check-prefixes=CHECK,RV32,RV32ZBA