Skip to content

SROA pass and llvm.fake.use #137572

Open
Open
@hongjia

Description

@hongjia

I run into a problem with the MSP430 backend.

With the following program:

short gs = 1234;
int gi = 12345;

float foo(void)
{
  double vd = -876.543;
  int a = gi + gs;
  return a;
}

And compile it with clang (the main branch):

 ~/build/bin/clang -S -emit-llvm -Og -target msp430 t.c

The generated IR:

; ModuleID = 't.c'
source_filename = "t.c"
target datalayout = "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16"
target triple = "msp430"

@gs = dso_local local_unnamed_addr global i16 1234, align 2
@gi = dso_local local_unnamed_addr global i16 12345, align 2

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: readwrite)
define dso_local float @foo() local_unnamed_addr #0 {
entry:
  %0 = load i16, ptr @gi, align 2, !tbaa !2
  %1 = load i16, ptr @gs, align 2, !tbaa !6
  %add = add nsw i16 %1, %0
  %conv = sitofp i16 %add to float
  notail call void (...) @llvm.fake.use(i16 %add) #2
  notail call void (...) @llvm.fake.use(double -8.765430e+02) #2
  ret float %conv
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: readwrite)
declare void @llvm.fake.use(...) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: readwrite) "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: readwrite) }
attributes #2 = { nounwind }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"wchar_size", i32 2}
!1 = !{!"clang version 21.0.0git (https://github.com/llvm/llvm-project.git 7baa09297603adef3183364d8990b97e9be23d31)"}
!2 = !{!3, !3, i64 0}
!3 = !{!"int", !4, i64 0}
!4 = !{!"omnipotent char", !5, i64 0}
!5 = !{!"Simple C/C++ TBAA"}
!6 = !{!7, !7, i64 0}
!7 = !{!"short", !4, i64 0}

And llc cannot compile the IR:

 ~/build/bin/llc -filetype asm -o - -O1 --debug --mtriple msp430 t.ll
...
Legalizing node: t12: ch = fake_use t10, ConstantFP:f64<-8.765430e+02>
Analyzing result type: ch
Legal result type
Analyzing operand: t10: ch = fake_use t9, t7
Legal operand
Analyzing operand: t11: f64 = ConstantFP<-8.765430e+02>
Soften float operand 1: t12: ch = fake_use t10, ConstantFP:f64<-8.765430e+02>
SoftenFloatOperand Op #1: t12: ch = fake_use t10, ConstantFP:f64<-8.765430e+02>

LLVM ERROR: Do not know how to soften this operator's operand!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/hjcao/build/bin/llc -filetype asm -o - -O1 --debug --mtriple msp430 t.ll
1.	Running pass 'Function Pass Manager' on module 't.ll'.
2.	Running pass 'MSP430 DAG->DAG Pattern Instruction Selection' on function '@foo'
 #0 0x000055fca09602ca llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/hjcao/data/temp/llvm-project/llvm/lib/Support/Unix/Signals.inc:804:22
 #1 0x000055fca0960708 PrintStackTraceSignalHandler(void*) /home/hjcao/data/temp/llvm-project/llvm/lib/Support/Unix/Signals.inc:880:1
 #2 0x000055fca095dec0 llvm::sys::RunSignalHandlers() /home/hjcao/data/temp/llvm-project/llvm/lib/Support/Signals.cpp:105:20
 #3 0x000055fca095fc74 SignalHandler(int, siginfo_t*, void*) /home/hjcao/data/temp/llvm-project/llvm/lib/Support/Unix/Signals.inc:418:13
 #4 0x00007f1c69f2cdf0 (/lib/x86_64-linux-gnu/libc.so.6+0x3fdf0)
 #5 0x00007f1c69f8195c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x00007f1c69f2ccc2 raise ./signal/../sysdeps/posix/raise.c:27:6
 #7 0x00007f1c69f154ac abort ./stdlib/abort.c:81:3
 #8 0x000055fca088853e llvm::report_fatal_error(llvm::Twine const&, bool) /home/hjcao/data/temp/llvm-project/llvm/lib/Support/ErrorHandling.cpp:126:9
 #9 0x000055fca0888357 llvm::report_fatal_error(llvm::StringRef, bool) /home/hjcao/data/temp/llvm-project/llvm/lib/Support/ErrorHandling.cpp:87:68
#10 0x000055fca077d160 llvm::DAGTypeLegalizer::SoftenFloatOperand(llvm::SDNode*, unsigned int) /home/hjcao/data/temp/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeFloatTypes.cpp:1127:53
#11 0x000055fca06e8011 llvm::DAGTypeLegalizer::run() /home/hjcao/data/temp/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:335:46
#12 0x000055fca06ebf0f llvm::SelectionDAG::LegalizeTypes() /home/hjcao/data/temp/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeTypes.cpp:1060:37
#13 0x000055fca06c5142 llvm::SelectionDAGISel::CodeGenAndEmitDAG() /home/hjcao/data/temp/llvm-project/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:979:36
...

I find that after the SROA pass, the operand of llvm.fake.use is left a FP64 constant:

...
*** IR Dump After SROAPass on foo ***
; Function Attrs: nounwind
define dso_local float @foo() #0 {
entry:
  %0 = load i16, ptr @gi, align 2, !tbaa !2
  %1 = load i16, ptr @gs, align 2, !tbaa !6
  %add = add nsw i16 %0, %1
  %conv = sitofp i16 %add to float
  notail call void (...) @llvm.fake.use(i16 %add) #3
  notail call void (...) @llvm.fake.use(double -8.765430e+02) #3
  ret float %conv
}
...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions