This is an archive of the discontinued LLVM Phabricator instance.

[AVR][NFC] Fix incorrect register states in expanding pseudo instructions
ClosedPublic

Authored by benshi001 on Jan 27 2022, 5:32 AM.

Diff Detail

Event Timeline

benshi001 created this revision.Jan 27 2022, 5:32 AM
benshi001 requested review of this revision.Jan 27 2022, 5:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 27 2022, 5:32 AM

Five new tests are added

ASRBNRd.mir
LSLBNRd.mir
LSLWNRd.mir
LSRBNRd.mir
LSRWNRd.mir

Actually one extra ASRWNRd.mir should be added, I omitted it since it is added in D115618

benshi001 updated this revision to Diff 407754.Feb 10 2022, 8:40 PM
benshi001 added inline comments.
llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp
2088

The SrcReg should not be set killed when SrcReg==DstLoReg.

benshi001 added inline comments.Feb 11 2022, 3:41 AM
llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp
2088

For example,

$r19r18 = sext killed $r18, though the src r18 is killed, but r19r18 is defined and live, so r18 can not be set to killed when expanding the pseudo.

Without this patch, the following simple C code will crash.

int foo(char a, char b, ...) {
  return a + b;
}

The crash message is

# After AVR pseudo instruction expansion pass
# Machine code for function add: NoPHIs, TracksLiveness, NoVRegs, TiedOpsRewritten
Frame Objects:
  fi#-3: size=2, align=1, fixed, at location [SP+6]
  fi#-2: size=1, align=1, fixed, at location [SP+4]
  fi#-1: size=2, align=1, fixed, at location [SP+2]
  fi#0: dead
  fi#1: dead
  fi#2: size=1, align=1, at location [SP-1]
  fi#3: size=1, align=1, at location [SP-2]

bb.0 (%ir-block.2):
  liveins: $r28, $r29
  frame-setup PUSHRr killed $r28, implicit-def $sp, implicit $sp
  frame-setup PUSHRr killed $r29, implicit-def $sp, implicit $sp
  $r28 = frame-setup INRdA 61
  $r29 = frame-setup INRdA 62
  dead early-clobber $r24 = LDDRdPtrQ $r29r28, 7 :: (load (s8) from %fixed-stack.1)
  early-clobber $r24 = LDDRdPtrQ $r29r28, 5 :: (load (s16) from %fixed-stack.2, align 1)
  early-clobber $r25 = LDDRdPtrQ $r29r28, 6 :: (load (s16) from %fixed-stack.2, align 1)
  early-clobber $r24 = LDDRdPtrQ $r29r28, 5 :: (dereferenceable load (s16) from %ir.3, align 1)
  early-clobber $r25 = LDDRdPtrQ $r29r28, 6 :: (dereferenceable load (s16) from %ir.3, align 1)
  early-clobber $r18 = LDDRdPtrQ $r29r28, 7 :: (dereferenceable load (s8) from %ir.4)
  $r19 = MOVRdRr killed $r18
  $r19 = ADDRdRr $r19(tied-def 0), killed $r19, implicit-def $sreg
  $r19 = SBCRdRr killed $r19(tied-def 0), killed $r19, implicit-def dead $sreg, implicit killed $sreg
  $r24 = ADDRdRr $r24(tied-def 0), killed $r18, implicit-def $sreg
  $r25 = ADCRdRr $r25(tied-def 0), killed $r19, implicit-def dead $sreg, implicit killed $sreg
  $r29 = POPRd implicit-def $sp, implicit $sp
  $r28 = POPRd implicit-def $sp, implicit $sp
  RET implicit killed $r25r24

# End machine code for function add.

*** Bad machine code: Using an undefined physical register ***
- function:    add
- basic block: %bb.0  (0x55dbe136ed40)
- instruction: $r24 = ADDRdRr $r24(tied-def 0), killed $r18, implicit-def $sreg
- operand 2:   killed $r18
fatal error: error in backend: Found 1 machine code errors.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.      Program arguments: ../build-avr-1/bin/clang test.c --target=avr -mmcu=atmega328 -S
1.      <eof> parser at end of file
2.      Code generation
3.      Running pass 'Function Pass Manager' on module 'test.c'.
4.      Running pass 'Verify generated machine code' on function '@add'
 #0 0x000055dbdce2227f PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
 #1 0x000055dbdce1fdfc llvm::sys::CleanupOnSignal(unsigned long) (../build-avr-1/bin/clang+0x1fc0dfc)
 #2 0x000055dbdcd6d968 llvm::CrashRecoveryContext::HandleExit(int) (../build-avr-1/bin/clang+0x1f0e968)
 #3 0x000055dbdce181be llvm::sys::Process::Exit(int, bool) (../build-avr-1/bin/clang+0x1fb91be)
 #4 0x000055dbdbad90a6 (../build-avr-1/bin/clang+0xc7a0a6)
 #5 0x000055dbdcd7530f llvm::report_fatal_error(llvm::Twine const&, bool) (../build-avr-1/bin/clang+0x1f1630f)
 #6 0x000055dbdc247a3e (anonymous namespace)::MachineVerifierPass::runOnMachineFunction(llvm::MachineFunction&) (.part.480) MachineVerifier.cpp:0:0
 #7 0x000055dbdc247a71 (anonymous namespace)::MachineVerifierPass::runOnMachineFunction(llvm::MachineFunction&) MachineVerifier.cpp:0:0
 #8 0x000055dbdc1e1038 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (../build-avr-1/bin/clang+0x1382038)
 #9 0x000055dbdc6a345f llvm::FPPassManager::runOnFunction(llvm::Function&) (../build-avr-1/bin/clang+0x184445f)
#10 0x000055dbdc6a36f1 llvm::FPPassManager::runOnModule(llvm::Module&) (../build-avr-1/bin/clang+0x18446f1)
#11 0x000055dbdc6a49ef llvm::legacy::PassManagerImpl::run(llvm::Module&) (../build-avr-1/bin/clang+0x18459ef)
#12 0x000055dbdd138b4a clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_delete<llvm::raw_pwrite_stream> >) (../build-avr-1/bin/clang+0x22d9b4a)
#13 0x000055dbddd6a919 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) crtstuff.c:0:0
#14 0x000055dbde95d4a9 clang::ParseAST(clang::Sema&, bool, bool) (../build-avr-1/bin/clang+0x3afe4a9)
#15 0x000055dbddd6ab02 clang::CodeGenAction::ExecuteAction() (../build-avr-1/bin/clang+0x2f0bb02)
#16 0x000055dbdd7972e1 clang::FrontendAction::Execute() (../build-avr-1/bin/clang+0x29382e1)
#17 0x000055dbdd73119a clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (../build-avr-1/bin/clang+0x28d219a)
#18 0x000055dbdd8627b7 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (../build-avr-1/bin/clang+0x2a037b7)
#19 0x000055dbdbadaa84 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (../build-avr-1/bin/clang+0xc7ba84)
#20 0x000055dbdbad693d ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&) driver.cpp:0:0
#21 0x000055dbdd5caa75 void llvm::function_ref<void ()>::callback_fn<clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const::'lambda'()>(long) Job.cpp:0:0
#22 0x000055dbdcd6d813 llvm::CrashRecoveryContext::RunSafely(llvm::function_ref<void ()>) (../build-avr-1/bin/clang+0x1f0e813)
#23 0x000055dbdd5cce68 clang::driver::CC1Command::Execute(llvm::ArrayRef<llvm::Optional<llvm::StringRef> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, bool*) const (../build-avr-1/bin/clang+0x276de68)
#24 0x000055dbdd5a08ba clang::driver::Compilation::ExecuteCommand(clang::driver::Command const&, clang::driver::Command const*&) const (../build-avr-1/bin/clang+0x27418ba)
#25 0x000055dbdd5a148f clang::driver::Compilation::ExecuteJobs(clang::driver::JobList const&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) const (../build-avr-1/bin/clang+0x274248f)
#26 0x000055dbdd5a86e7 clang::driver::Driver::ExecuteCompilation(clang::driver::Compilation&, llvm::SmallVectorImpl<std::pair<int, clang::driver::Command const*> >&) (../build-avr-1/bin/clang+0x27496e7)
#27 0x000055dbdba4cd78 main (../build-avr-1/bin/clang+0xbedd78)
#28 0x00007f1836a46bf7 __libc_start_main /build/glibc-S9d2JN/glibc-2.27/csu/../csu/libc-start.c:344:0
#29 0x000055dbdbad64ea _start (../build-avr-1/bin/clang+0xc774ea)
clang-14: error: clang frontend command failed with exit code 70 (use -v to see invocation)
clang version 14.0.0 (git@github.com:llvm/llvm-project.git 0adf0fd5cb59211934e96bfcdaedb18bfbb6357d)
Target: avr
Thread model: posix
InstalledDir: /home/shiben/work/llvm-project/build-avr-4/../build-avr-1/bin
clang-14: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-14: note: diagnostic msg: /tmp/test-c9b549.c
clang-14: note: diagnostic msg: /tmp/test-c9b549.sh
clang-14: note: diagnostic msg:

********************
benshi001 updated this revision to Diff 411947.Feb 28 2022, 7:13 PM
benshi001 added inline comments.
llvm/lib/Target/AVR/AVRExpandPseudoInsts.cpp
2084

We can not set MOV's Operand(1) to killed when SrcReg==DstHiReg, since DstHiReg is the source operand of the following ADDRdRr.

benshi001 updated this revision to Diff 412299.Mar 1 2022, 6:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 1 2022, 6:29 PM
benshi001 updated this revision to Diff 412308.Mar 1 2022, 7:31 PM
benshi001 updated this revision to Diff 412326.Mar 1 2022, 10:50 PM
benshi001 updated this revision to Diff 414548.Mar 10 2022, 6:25 PM
aykevl accepted this revision.Mar 25 2022, 7:35 AM
This revision is now accepted and ready to land.Mar 25 2022, 7:35 AM
This revision was landed with ongoing or failed builds.Mar 25 2022, 9:02 AM
This revision was automatically updated to reflect the committed changes.