avr-llvm / llvm

[MERGED UPSTREAM] AVR backend for the LLVM compiler library
220 stars 21 forks source link

Wrong MachineOperand accessor error #37

Closed dylanmckay closed 9 years ago

dylanmckay commented 9 years ago

Assertion error:

void llvm::MachineOperand::setIsEarlyClobber(bool): Assertion `isReg() && IsDef && "Wrong MachineOperand accessor"' failed.

Code to reproduce:

C (main.c):


struct ss
{
    int a;
    int b;
    int c;
};

void loop(struct ss *x, struct ss **y, int z)
{
    int i;
    for (i=0; i<z; ++i)
    {
        x->c += y[i]->b;
    }
}

Command line:

clang --target=avr -O1 main.c -o main.o

Or alternatively:

LLVM IR (main.ll):

; ModuleID = '/home/dylan/Desktop/tmp.c'
target datalayout = "e-p:16:8:8-i8:8:8-i16:8:8-i32:8:8-i64:8:8-f32:8:8-f64:8:8-n8"
target triple = "avr-none"

%struct.ss = type { i16, i16, i16 }

; Function Attrs: nounwind
define void @loop(%struct.ss* %x, %struct.ss** %y, i16 %z) #0 {
entry:
  %x.addr = alloca %struct.ss*, align 2
  %y.addr = alloca %struct.ss**, align 2
  %z.addr = alloca i16, align 2
  %i = alloca i16, align 2
  store %struct.ss* %x, %struct.ss** %x.addr, align 2
  store %struct.ss** %y, %struct.ss*** %y.addr, align 2
  store i16 %z, i16* %z.addr, align 2
  store i16 0, i16* %i, align 2
  br label %for.cond

for.cond:                                         ; preds = %for.inc, %entry
  %0 = load i16, i16* %i, align 2
  %1 = load i16, i16* %z.addr, align 2
  %cmp = icmp slt i16 %0, %1
  br i1 %cmp, label %for.body, label %for.end

for.body:                                         ; preds = %for.cond
  %2 = load i16, i16* %i, align 2
  %3 = load %struct.ss**, %struct.ss*** %y.addr, align 2
  %arrayidx = getelementptr inbounds %struct.ss*, %struct.ss** %3, i16 %2
  %4 = load %struct.ss*, %struct.ss** %arrayidx, align 2
  %b = getelementptr inbounds %struct.ss, %struct.ss* %4, i32 0, i32 1
  %5 = load i16, i16* %b, align 2
  %6 = load %struct.ss*, %struct.ss** %x.addr, align 2
  %c = getelementptr inbounds %struct.ss, %struct.ss* %6, i32 0, i32 2
  %7 = load i16, i16* %c, align 2
  %add = add nsw i16 %7, %5
  store i16 %add, i16* %c, align 2
  br label %for.inc

for.inc:                                          ; preds = %for.body
  %8 = load i16, i16* %i, align 2
  %inc = add nsw i16 %8, 1
  store i16 %inc, i16* %i, align 2
  br label %for.cond

for.end:                                          ; preds = %for.cond
  ret void
}

attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.ident = !{!0}

!0 = !{!"clang version 3.7.0 (https://github.com/llvm-mirror/clang.git 00a0b3863ac817790d9b344da4fc499b71140a7a) (https://github.com/llvm-mirror/llvm.git 7e8c7af2467dd33d7f41b797b771221ca08dfbf3)"}

Command line:

llc main.ll -o main.o -march=avr -mcpu=avrxmega1

Tested with dedb06d9539f46e4aeed3a4dc53880147493c4e1

4ntoine commented 9 years ago
MBA-Anton:bin asmirnov$ ./clang -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/variants/standard -I/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include --target=avr /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/avr-libc/realloc.c -o /tmp/arduino_test1/realloc.c.o
Assertion failed: (isReg() && IsDef && "Wrong MachineOperand accessor"), function setIsEarlyClobber, file /Users/asmirnov/Documents/dev/src/avr-llvm/llvm/include/llvm/CodeGen/MachineOperand.h, line 394.

0  clang-3.7                0x000000010be13b5e llvm::sys::PrintStackTrace(__sFILE*) + 46
1  clang-3.7                0x000000010be14f1b PrintStackTraceSignalHandler(void*) + 27
2  clang-3.7                0x000000010be15365 SignalHandler(int) + 565
3  libsystem_platform.dylib 0x00007fff9398a5aa _sigtramp + 26
4  libsystem_platform.dylib 0x00007fff54cf1830 _sigtramp + 3241570976
5  clang-3.7                0x000000010be14f4b raise + 27
6  clang-3.7                0x000000010be15002 abort + 18
7  clang-3.7                0x000000010be14fe1 __assert_rtn + 129
8  clang-3.7                0x000000010b361437 llvm::MachineOperand::setIsEarlyClobber(bool) + 151
9  clang-3.7                0x000000010b359a2a llvm::MachineInstr::addOperand(llvm::MachineFunction&, llvm::MachineOperand const&) + 1674
10 clang-3.7                0x000000010af3d37f llvm::MachineInstrBuilder::addReg(unsigned int, unsigned int, unsigned int) const + 479
11 clang-3.7                0x000000010c43c10b llvm::InstrEmitter::AddRegisterOperand(llvm::MachineInstrBuilder&, llvm::SDValue, unsigned int, llvm::MCInstrDesc const*, llvm::DenseMap<llvm::SDValue, unsigned int, llvm::DenseMapInfo<llvm::SDValue>, llvm::detail::DenseMapPair<llvm::SDValue, unsigned int> >&, bool, bool, bool) + 1611
12 clang-3.7                0x000000010c43cad9 llvm::InstrEmitter::AddOperand(llvm::MachineInstrBuilder&, llvm::SDValue, unsigned int, llvm::MCInstrDesc const*, llvm::DenseMap<llvm::SDValue, unsigned int, llvm::DenseMapInfo<llvm::SDValue>, llvm::detail::DenseMapPair<llvm::SDValue, unsigned int> >&, bool, bool, bool) + 2457
13 clang-3.7                0x000000010c43efab llvm::InstrEmitter::EmitMachineNode(llvm::SDNode*, bool, bool, llvm::DenseMap<llvm::SDValue, unsigned int, llvm::DenseMapInfo<llvm::SDValue>, llvm::detail::DenseMapPair<llvm::SDValue, unsigned int> >&) + 1787
14 clang-3.7                0x000000010c5253e6 llvm::InstrEmitter::EmitNode(llvm::SDNode*, bool, bool, llvm::DenseMap<llvm::SDValue, unsigned int, llvm::DenseMapInfo<llvm::SDValue>, llvm::detail::DenseMapPair<llvm::SDValue, unsigned int> >&) + 102
15 clang-3.7                0x000000010c5234dc llvm::ScheduleDAGSDNodes::EmitSchedule(llvm::MachineBasicBlock::bundle_iterator<llvm::MachineInstr, llvm::ilist_iterator<llvm::MachineInstr> >&) + 1340
16 clang-3.7                0x000000010c60a3dc llvm::SelectionDAGISel::CodeGenAndEmitDAG() + 8396
17 clang-3.7                0x000000010c6082fd llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::Instruction const>, llvm::ilist_iterator<llvm::Instruction const>, bool&) + 253
18 clang-3.7                0x000000010c608019 llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) + 5241
19 clang-3.7                0x000000010c605877 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 1143
20 clang-3.7                0x000000010b355c3e llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 110
21 clang-3.7                0x000000010b74d6db llvm::FPPassManager::runOnFunction(llvm::Function&) + 427
22 clang-3.7                0x000000010b74d9e8 llvm::FPPassManager::runOnModule(llvm::Module&) + 104
23 clang-3.7                0x000000010b74e3f4 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) + 1412
24 clang-3.7                0x000000010b74dc9e llvm::legacy::PassManagerImpl::run(llvm::Module&) + 302
25 clang-3.7                0x000000010b74eb71 llvm::legacy::PassManager::run(llvm::Module&) + 33
26 clang-3.7                0x000000010c666671 (anonymous namespace)::EmitAssemblyHelper::EmitAssembly(clang::BackendAction, llvm::raw_ostream*) + 1601
27 clang-3.7                0x000000010c665e32 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::StringRef, llvm::Module*, clang::BackendAction, llvm::raw_ostream*) + 114
28 clang-3.7                0x000000010c88bac5 clang::BackendConsumer::HandleTranslationUnit(clang::ASTContext&) + 1397
29 clang-3.7                0x000000010d3bc74b clang::ParseAST(clang::Sema&, bool, bool) + 1291
30 clang-3.7                0x000000010c2764fa clang::ASTFrontendAction::ExecuteAction() + 522
31 clang-3.7                0x000000010c889b7f clang::CodeGenAction::ExecuteAction() + 3999
32 clang-3.7                0x000000010c275a78 clang::FrontendAction::Execute() + 120
33 clang-3.7                0x000000010c205dbc clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) + 1020
34 clang-3.7                0x000000010c2e7e41 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) + 3201
35 clang-3.7                0x000000010af17570 cc1_main(llvm::ArrayRef<char const*>, char const*, void*) + 2496
36 clang-3.7                0x000000010af0a82b ExecuteCC1Tool(llvm::ArrayRef<char const*>, llvm::StringRef) + 171
37 clang-3.7                0x000000010af0969a main + 1274
38 libdyld.dylib            0x00007fff8fb515fd start + 1
39 libdyld.dylib            0x0000000000000041 start + 1883957829
Stack dump:
0.  Program arguments: /Users/asmirnov/Documents/dev/src/avr-llvm/llvm/build/bin/clang-3.7 -cc1 -triple avr -emit-obj -disable-free -main-file-name realloc.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -target-cpu atmega328p -target-linker-version 241.9 -g -dwarf-column-info -ffunction-sections -fdata-sections -coverage-file /tmp/arduino_test1/realloc.c.o -resource-dir /Users/asmirnov/Documents/dev/src/avr-llvm/llvm/build/bin/../lib/clang/3.7.0 -dependency-file /tmp/arduino_test1/realloc.c.d -MT /tmp/arduino_test1/realloc.c.o -D F_CPU=16000000L -D USB_VID=null -D USB_PID=null -D ARDUINO=105 -I /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino -I /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/variants/standard -I /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include -Os -Wall -fdebug-compilation-dir /Users/asmirnov/Documents/dev/src/avr-llvm/llvm/build/bin -ferror-limit 19 -fmessage-length 115 -mstackrealign -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -vectorize-slp -o /tmp/arduino_test1/realloc.c.o -x c /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/avr-libc/realloc.c 
1.  <eof> parser at end of file
2.  Code generation
3.  Running pass 'Function Pass Manager' on module '/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/cores/arduino/avr-libc/realloc.c'.
4.  Running pass 'AVR DAG->DAG Instruction Selection' on function '@realloc'
clang-3.7: error: unable to execute command: Illegal instruction: 4
clang-3.7: error: clang frontend command failed due to signal (use -v to see invocation)
clang version 3.7.0 (https://github.com/avr-llvm/clang.git 98cf874e96493bec6b96f3a8339f11150f4f198b) (llvm/llvm a411aba0af80de3090448dc3e56f39d8627a6647)
Target: avr
Thread model: posix
clang-3.7: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script.
4ntoine commented 9 years ago

i'm able to reproduce error and debug until that point.

2015-03-03 23 36 12

Unfortunately i'm not so familiar with LLVM internals to understand what's wrong here.. Any suggestions?

dylanmckay commented 9 years ago

Reopening because this same problem (although presumably with a different instruction with the wrong operand marked as earlyclobber).

See test/CodeGen/AVR/mul.ll

dylanmckay commented 9 years ago

Fixed in 26896ec4092450267a03a53bda79919037ccebba.

4ntoine commented 9 years ago

still happens:

MBA-Anton:bin asmirnov$ ./clang -c -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino -I/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/variants/standard -I/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/avr/include --target=avr /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp -o /tmp/arduino_test1/HardwareSerial.cpp.o
In file included from /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino/HardwareSerial.cpp:32:
/Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/avr/cores/arduino/HardwareSerial_private.h:119:5: warning: 
      expression result unused; assign into a variable to force a volatile load [-Wunused-volatile-lvalue]
    *_udr;
    ^~~~~
fatal error: error in backend: ran out of registers during register allocation
clang-3.7: error: clang frontend command failed with exit code 70 (use -v to see invocation)
clang version 3.7.0 (https://github.com/avr-llvm/clang.git aebd4f6d15e44635ecc7338d4454389c4573bc2b) (llvm/llvm 9c7c5212863ed8b06323ad07fa84bad29ff051f8)
Target: avr
Thread model: posix
clang-3.7: note: diagnostic msg: PLEASE submit a bug report to http://llvm.org/bugs/ and include the crash backtrace, preprocessed source, and associated run script.
clang-3.7: note: diagnostic msg: 
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
clang-3.7: note: diagnostic msg: /var/folders/64/fwfkm1k51zbd4_c5lwpsbljh0000gn/T/HardwareSerial-04d113.cpp
clang-3.7: note: diagnostic msg: /var/folders/64/fwfkm1k51zbd4_c5lwpsbljh0000gn/T/HardwareSerial-04d113.sh
clang-3.7: note: diagnostic msg: 

********************
dylanmckay commented 9 years ago

The bug you describe is #1.

A hack existed that fixed it (in most cases I think) but I removed it to see if it changed anything in 7f4cc26311e08d90c9296897beff116e96066bfd.

You could try reverting this commit in your own private branch and see if the error exists, otherwise I will investigate when I have time.