llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

IPSCCP produces wrong code when passing structs

Open dobbelaj-snps opened this issue 7 years ago • 4 comments

Bugzilla Link 42738
Resolution FIXED
Resolved on Aug 01, 2019 01:42
Version trunk
OS Windows NT
Attachments test_works.ll, test_fails.ll
CC @efriedma-quic,@fhahn

Extended Description

IPSCCP produces wrong code when passing structs (with constants).

(For the complete files, see the attachments; The only difference is the absence of 'i32 %dummy0, i32 %dummmy1' arguments for '@f')

test_works.ll: define internal i32 @​f((i32 %dummy0, i32 %dummy1, %struct.S %s.coerce) #​0 { .... ret i32 123 }

define dso_local i32 @​main() #​0 { %call = call i32 @​f(%struct.S { i64 100 }) call void @​do_report(i32 undef, i32 undef, i32 %call) ret i32 0 }

'opt -S -o - --ipsccp test_works.ll' results in: define dso_local i32 @​main() #​0 { %call = call i32 @​f(i32 undef, i32 undef, %struct.S { i64 100 }) call void @​do_report(i32 123) ret i32 0 }

define internal i32 @​f(i32 %dummy0, i32 %dummy1, %struct.S %s.coerce) #​0 { ... ret i32 undef }

test_fails.ll: define internal i32 @​f(%struct.S %s.coerce) #​0 { .... ret i32 123 }

define dso_local i32 @​main() #​0 { %call = call i32 @​f(%struct.S { i64 100 }) call void @​do_report(i32 %call) ret i32 0 }

'opt -S -o - --ipsccp test_fails.ll' results in: define dso_local i32 @​main() #​0 { %call = call i32 @​f(%struct.S { i64 100 }) call void @​do_report(i32 %call) ; NOTE THE DIFFERENCE HERE ret i32 0 }

define internal i32 @​f(i32 %dummy0, i32 %dummy1, %struct.S %s.coerce) #​0 { ... ret i32 undef }

dobbelaj-snps avatar Jul 24 '19 12:07 dobbelaj-snps

Yes, this fixes the issue. Thanks !

dobbelaj-snps avatar Aug 01 '19 08:08 dobbelaj-snps

Proposed fix: https://reviews.llvm.org/D65439

fhahn avatar Jul 30 '19 12:07 fhahn

Thanks for the detailed report!

Marking things as overdefined in ResolveUndefsIn should be the safe things to do. Unfortunately, for function calls, this is not quite true, as we zap all returns in functions that are not overdefined, assuming all call sites are also not overdefined. This is not the first such error and I think we should add at least an assert when zapping function returns.

As to address this issue, when we encounter any function call for tracked functions, we should solve that function first. I'll put up patches.

fhahn avatar Jul 24 '19 14:07 fhahn

Analysis: in llvm/lib/Transforms/Scalar/SCCP.cpp: SCCPSolver::ResolvedUndefsIn (around line 1477): It seems that arg0 and arg1 are checked here to see if they are 'struct' types. If so, the 'markOverdefined' path is taken.

For the call to '@f', this means that, in the working case (arg0 and arg1 are not a struct), the known constant is propagated. In the failing case (arg0 is a struct), '@f' is marked as overdefined, and the known return constant is not propagated.

BUT, around line 2178, @​f is modified to return a 'ret i32 undef' (in both cases). Based on the comments, it seems the algorithm assumes that the original known value was propagated correctly. (Maybe that should be added as a postcondition check ?)

I think that something is wrong with the upfront exclusion of a struct type for arg0/arg1, especially for a 'Call/Invoke/CallBr'; maybe also for a 'select'. But I am not familiar enough with this code to know for sure...

dobbelaj-snps avatar Jul 24 '19 13:07 dobbelaj-snps